Specialization Constants
Specialization Constants are a new feature in Vulkan that allow code to be re-optimized based on integer constant values, without doing a full recompilation of the shader code. These are useful to set the value for rarely changing values such as 'modes' in shader, or selection of particular code paths that are doing via a switch() or if() statement. In the past this may have been done with a #define
statement, or a uniform
.
A specialized version of a shader will be cached and re-used, but takes up GPU resources. So they should not be used for constantly changing values, but instead for values that are only changed sometimes, within a limited range of values.
To define a specialization constant, declare a constant with an extra layout() qualifier.
layout(constant_id = 0) const int SomeMode = 0;
Then you can use SomeMode
just as you would any other variable. If you don't want it to be = 0, you can assign a different value on the 'Constants' page of the GLSL TOP, GLSL MAT etc.
You can declare multiple specialization constants, you just need to give each one it's own unique constant_id
value (0, 1, 2, etc.).