Taking advantage of specialization constants
Specialization constants are a Vulkan feature that allows developers to define constant values when creating a pipeline. This is particularly useful when the same shader is needed for multiple use cases that differ only for some constant values, for example, materials. This is a more elegant solution compared to pre-processor definitions as they can be dynamically controlled at runtime without having to recompile the shaders.
In our case, we want to be able to control the workgroup size of compute shaders based on the hardware we are running to obtain the best performance:
- The first step in the implementation is to determine whether a shader uses specialization constants. We now identify any variables that have been decorated with the following type when parsing the shader SPIR-V:
case ( SpvDecorationSpecId ):
{
id.binding = data[ word_index + 3 ];
break;
}
- When parsing all the variables...