# How can I reduce the amount of GLSL code written?

I am writing a game engine for Linux. It has a sufficiently large number of effects that can be turned on / off. Effects are defined before compiling with #ifdef. But the problem is that I literally have to “wrap” each variable in #ifdef. Another problem is that when adding new effects, I have to rewrite the conditions, since one variable can refer to several effects at once. For example:

#if defined ALGINE_LIGHTING_MODE_ENABLED || defined ALGINE_SHADOW_MAPPING_MODE_ENABLED
uniform vec3 viewPos;                       // Camera position
#endif

#ifdef ALGINE_DOF_MODE_ENABLED
uniform float focalDepth;
uniform float focalRange;
#endif


How could I reduce the amount of re-write needed when adding new effects?

• I don't think you need to conditionally disable uniforms. Any half-decent shader compiler will optimize them away if they're not used. And you don't have to conditionally disable writes to them: When using glGetUniformLocation with glUniform*, non-existing uniforms (including those that were optimized away) are silently ignored. – HolyBlackCat Oct 27 '18 at 19:40

If you are using macros for defining the variables, then there is really no way around it. You could, however, do something like this:

// constants.h
#if defined ALGINE_LIGHTING_MODE_ENABLED || defined
uniform vec3 viewPos;                       // Camera position
#endif

#ifdef ALGINE_DOF_MODE_ENABLED
uniform float focalDepth;
uniform float focalRange;
#endif


// vertex-shader.glsl

• Alright. If you do decide to use this though (or someone else does), then please refer to this answer for a description of how this can be achieved. Since glsl has no understanding of your file system, you will have to invent a small hack to manually load all the #include'd files together, and then do some string concatenation. But it is definitely possible. – ワイきんぐ Nov 1 '18 at 17:03