This is a simple Python 3.x GLSL Preprocessor script with an accompanying CMake function.
The script finds lines like this in a source GLSL file:
#include SomeLibrary.glsl
And substitutes them for that file's contents in the output, recursively processing any includes
inside of that file, and inserting GLSL #line
directives to ensure line numbers appear correctly.
Add the included CMake function declaration to your CMakeLists.txt or an included file, and add
glslprepro.py
in your project directory. (You could move them both to a subdirectory and adjust
the CMake function appropriately, too!)
The function add_shader(TARGET, SHADER, ...DEPENDENCIES)
will be available for use.
The argument TARGET
is the executable that will depend on these shaders (usually your main
Game executable, or an Assets target.) SHADER
is your source file name. ...DEPENDENCIES
should list
any other GLSL files your main file includes, to ensure that the output is generated properly whenever an
included GLSL file is changed.
For example, if you have some project with main.cpp
and a shader file test.glsl
that depends on MyUtils.glsl
:
add_executable(Game main.cpp)
add_shader(Game test.glsl MyUtils.glsl)
To use it manually, just invoke it with the source and output file paths:
python3 glslprepro.py SourceDirectory/MyShader.glsl OutputDirectory/MyShader.glsl
You may also omit the output file path in order to view output on the command line.