Shader defines and comments

Playground:
https://www.babylonjs-playground.com/#6UBVLF#3

I’ve noticed that in version 3.3.0, one can add comments after #ifdef in the shader code, and it won’t affect anything. This is consistent with what I would expect in C, though I’m not an expert on the standard by any means.

In the latest version of Babylon (4.2), though, I’ve noticed that the comment actually changes whether the #ifdef is pasted in. Is this a feature or a bug? I’m not great at shaders, and I don’t know the exact standard; if this is expected behavior, perhaps a note in the changelog would be helpful, as I’ve spent hours tracking down a bug related to this.
const trivialFragmentShader = String.raw`
void main()
{

            gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);

            #ifdef RED // Try removing this comment
            gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
            #endif

            #ifdef RED
            gl_FragColor.z = 0.0;
            #endif

}
 `;

We could say it’s “by design”, because a pre-processor parser has been implemented that evaluates and replaces the #if ... / #else ... / #endif constructs, but it does not try to support all features of C/GLSL for that matter.

However, as it was not very difficult to support the single line comment // there, I have created a PR:

1 Like