godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.15k stars 97 forks source link

Add hint_enum to int shader uniforms #7847

Open adamscoble opened 1 year ago

adamscoble commented 1 year ago

Describe the project you are working on

I am building a number of custom shaders.

Describe the problem or limitation you are having in your project

(I tried to find this as I was sure I'd seen someone suggest it before, but couldn't.)

I would like to have a number of behaviour options in a shader, and it is most convenient to use an integer. However without #7846 it's impossible to make this intuitive. Adding an enum hint to int uniforms would make this much more pleasant.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Being able to add a hint_enum to integer uniforms would allow a straightforward dropdown to select a value in the inspector, which would be very intuitive for the user, and self-documenting.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

This is the more difficult part, I'm not sure how the hint system works exactly so forgive my ignorance, but I was thinking something like:

uniform int my_enum : hint_enum("FirstOption", "SecondOption", "ThirdOption") = 0;

If this enhancement will not be used often, can it be worked around with a few lines of script?

Not really. You can of course add a comment to the shader, but then the user has to open the shader to see. Alternatively #7846 would help, as they could just be listed in the documentation.

Is there a reason why this should be core and not an add-on in the asset library?

It's pretty useful behaviour, I feel. Especially if you're making addon shaders where users will be tweaking parameters.

TokisanGames commented 11 months ago

It was requested here, and closed when they moved to the proposal repo. The leading statement there was that enums would encourage branching, which is said to be discouraged.

However in my experience with modern cards, this is not necessarily an issue, and maybe never was. TLDR, simple branching or static conditions such as an enum uniform are perfectly fine as long as large groups of cores can execute the same code. Nested branches, or dynamic conditions start to slow down the GPU.

Enums would be a great addition to manage uniforms.

Calinou commented 11 months ago

Shaders don't have a string type in the first place[^1], so supporting a syntax that accepts strings may be challenging.

[^1]: This is also the case for GLSL, although the Vulkan debug print extension has finally brought along a string type with it.

TokisanGames commented 11 months ago

Glsl also doesn't have hints. The hint is for the inspector, which will translate right into ints for glsl.

geekley commented 2 months ago

I would expect this to work on uint, as well as e.g. lowp int, etc. Not sure about ivec_ and uvec_ (theoretically should be possible I think, but not sure if it would be useful).

geekley commented 2 months ago

It would be useful to also have a hint_flags working in a similar way (but allowing multiple choices).