-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add FOR macro to GLSL preprocessor and J3MD #1758
Conversation
Does this come up often? My only hesitation is that this is not compatible with "real" GLSL code. Similar to our "#import" hack... but that has unarguably been a huge boon to the engine. For reference, even the C/C++ preprocessor does not have anything like this which is telling in that in 40 years or whatever of C development, they decided cutting and pasting code like this was the better alternative. It's cool code, though. I also wonder if shader nodes already had an answer for something like this (I doubt it). |
It comes up often especially if you are trying to build generic shaders. Another example, here, applying a filter to up to 6 textures at the same time + depth #for i=0..6 ( #ifdef SCENE_$i $0 #endif )
uniform sampler2D Input$i;
#ifdef MRT
layout(location=$i)
#endif
out vec4 outScene$i;
#endfor
#ifdef DEPTH
uniform sampler2D InputDepth;
#endif
void main(){
#for i=0..6 ( #ifdef SCENE_$i $0 #endif )
outScene$i = ...;
#endfor
#ifdef DEPTH
gl_FragDepth = ...;
#endif
} EDIT: this is used also in the j3mds to declared the inputs for the shader
|
This would've definitely been a helpful feature when I was making pbr terrain shaders, I recall trying to figure out if I could define textures in a for loop like this before realizing it was not possible at the time. If it doesn't break any existent shaders and if users can still code their shaders the normal way, then I'd say it looks like a useful feature to have. And since this PR isn't actually changing any existent shaders to use the feature yet, it seems like the risk of this breaking things is pretty low. I guess the only question is whether or not there's any significant downside to this like Paul mentioned. It doesn't seem like there would be, but I don't have enough experience to speculate that much, so I suppose the best way to know would be to write 2 versions of the same shader, one with the for loop macro and one the standard way, then compare to make sure there's no significant performance hits or other unexpected issues. |
Sounds like an useful feature to have, it would've definitely come handy in my shaders a couple times by now. |
That is a very cool idea. While I have never had the need for it, it looks like it could be useful. My only hesitation is that I have never seen anything like it before in any macro based C/C++ precompiler. On the other hand, most glsl precompilers have custom macros. For example the engine I am currently working in has |
I think this is mergeable |
Thanks for your contribution, Riccardo! |
This can be used in shaders that need to repeat some code where loops cannot be used (eg. uniforms declaration).
See for example
jmonkeyengine/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/TerrainLighting.frag
Lines 20 to 56 in ceb3564
This code can be rewritten like so
Note: the first diffuse map (m_DiffuseMap) will be renamed in m_DiffuseMap0
As you can see, it makes code much more readable and maintainable.