Using C++11’s raw string literals to better organize stock shaders

As part of the OpenGL renderer unification refactor, I was looking for a way to extract stock shader’s code into separated files in order to improve the way they’re implemented and make them more readable. Basically, I wanted to avoid this kind of code.

It turns out C++11 provides a alternative way to define string resources through raw string literals. Originally targeted at writing regular expressions, this mechanism allows to write strings without worrying about escape characters and other formatting tools, making it specially useful for big chunks of text (as in shader code).

After extracting the shader code into separated files I can now easily #include them in the shader class implementation, resulting in a much more pleasant code organization. I agree that having to encapsulate GLSL code between R”( and )” tokens is a bit intrusive and prevents such code to be tested on external tools, but it’s a small price to pay for a much better organization.

At the moment, several shaders have been refactored to use this mechanism and I’m hoping all of them will be updated before v4.0 comes out.

Advertisements