How to disable auto-optimization for GLSL

If I define a uniform variable in GLSL and do not use it in the main function, then at runtime there will be an error message saying “no such uniform named XXX”. How to disable this kind of auto-optimization.

Instead, use glGetUniformLocation(program, “uniform_name”).
This function returns -1 if uniform_name does not correspond to an active uniform variable in [i]program

[/i]i.e. If the function returns >=0, then it is okay to set the uniform

Use a uniform block with the shared or std140 qualifiers. These have a fixed layout so that they can be shared between different shaders.

Alternatively, force the variable to be used in some way, e.g. multiply it by a very small value (so that the effect isn’t discernible) then add it to an output.