Friday, March 4, 2011

GLSL Engine Uniforms Revisited

Game Engine Gems 2
A few months back, I mentioned my two articles in Game Engine Gems 2. Now that the book is available, I want to revisit a piece of advice in one article, A Framework for GLSL Engine Uniforms.

This article is about implementing a framework for GLSL uniforms that are automatically set by the engine. A shader author simply declares the uniform to use it. Since the engine has a list of names for all engine uniforms, it can identify what engine uniforms a shader uses, and track them accordingly. A shader author is ensured that the engine will update a uniform to the appropriate value before the shader is invoked. In addition to standard stuff like transformation matrices, these uniforms are also used for more engine-specific values, e.g., time, sun position, etc.

At the end of the article I suggest:

If you are up for parsing GLSL code, you could also eliminate the need for
shader authors to declare engine uniforms by carefully searching the shader's
source for them. This task in nontrivial considering preprocessor transformations,
multiline comments, strings, and compiler optimizations that could eliminate
uniforms altogether

Well, I should have thought more about this because you do not need to parse GLSL for uniform names to eliminate the need to declare engine uniforms. Foruntently, it is much easier; since glShaderSource takes an array of strings, just have one string contain the declarations for all the engine uniforms and let the compiler throw out the unused ones.

I've used this approach for a few months now and am quite happy with it. At first, I was not sure about how useful it would be, but I've found it makes copying and pasting code among shaders much easier because I don't have to remember to declare engine uniforms.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.