0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – HolyBlackCat Oct 27 '18 at 19:40
2
\$\begingroup\$

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 
ALGINE_SHADOW_MAPPING_MODE_ENABLED
uniform vec3 viewPos;                       // Camera position
#endif

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

And then include this file in your shader code:

// vertex-shader.glsl
#include "constants.h"
// [...]
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you for the advice, but I decided to entrust the optimization of this code fragment to the compiler (as @HolyBlackCat advised), since I have a lot of these variables \$\endgroup\$ – congard Oct 29 '18 at 18:21
  • \$\begingroup\$ 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. \$\endgroup\$ – ワイきんぐ Nov 1 '18 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.