GLSL functions may be also defined in the form of C++-style templates using the template keyword. Its syntax and meaning is similar to C++ with one important difference – the template arguments are untyped and passed as text, similar to macros. For this reason, make sure to wrap template arguments in parentheses where necessary, and do not use variables that won't be available inside the function.

Currently, only GLSL functions can be defined as templates. The template arguments must always be specified explicitly.


Example of template function definition:

template <IMAGE_NAME>
glsl vec4 sampleImage(vec2 pos) {
    return texture(IMAGE_NAME, pos);

Example of usage in a GLSL function:

glsl vec4 sampleMyImage(vec2 pos) {
    return sampleImage<MyImage>(pos);

Example of referencing the function in a Shadron object initializer:

image MyImageCopy = glsl(sampleImage<MyImage>, sizeof(MyImage));