Using #ifdef in OpenGL* ES 2.0 Shaders

Published:03/26/2012   Last Updated:03/26/2012

It’s nice to use #ifdef’s in an OpenGL* shader. This allows a shader file do things like contain a vertex shaders and pixel shaders in the same file. Or have different render paths selected at run time. Here is an example of a simple shader file:



To use a shader in OpenGL ES, the program will typically:
1. Load a file
2. Compile the shader
3. Link the shader

Before compiling the shader, the program calls glShaderSource to set the shader source. The real trick is with glShaderSource. This function can take multiple strings and combines them for compilation.

Here is a code sample that compiles a shader given a string (ShaderData) and the size of the string (Size). The function is passed a type (Type) which is defined as GL_VERTEX_SHADER or GL_FRAGMENT_SHADER. This function uses an #ifdef to compile the source as a vertex or pixel shader:



The documentation for glShaderSource says that the function can be called with string lengths of 0 for NULL terminated strings. However, for my implementation of OGLES 2 this did not work, so I had to specify the individual string lengths (as defined in ShaderStringLengths for this example).

With #ifdef’s, an application can modify a shader at runtime. It’s possible to enable/disable features like post processing effects, dynamic lighting, and more based on #defines. Best of luck!

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804