When is a specialization constant updated?

In Vulkan we get to declare the specialization constants when creating a shader stage.
Looking at the GL_KHR_vulkan_glsl documentation it says :

    this specific specialization constant.  The API or an intermediate tool can
    then change its value to another constant integer before it is fully
    lowered to executable code.  If it is never changed before final lowering,
    it will retain the value of 12.

the SPIRV documentation says:

Specialization is intended for constant objects that will not have known constant values until after initial generation of a SPIR-V module. Such objects are called specialization constants.

In this blog (Improving shader performance with Vulkan’s specialization constants – Developer Log)
Specialization Constants are described as:

The concept behind specialization constants is very simple: they allow applications to set the value of a shader constant at run-time. At first sight, this might not look like much, but it can have very important implications for certain shaders.

I’ve talked to some people who are dabbling with Vulkan for some time now, and they say that a Specialization Constant isn’t updated at runtime, but instead when the pipeline is built. The people I asked also said they haven’t used them at all. So I wonder when are Specialization Constants updated. Before the shader stage being executed, or when the pipeline is created.

By definition, the pipeline is created before the shader stage can be executed. So they are both correct.

All of these things you’ve cited are saying the same thing, just with different words.

Pipelines are built at runtime. But that requires a particular definition of “runtime”: when the application is running. This is as opposed to SPIR-V shader modules, which are compiled into binaries that programs usually load. And therefore, compilation does not happen at “runtime”.

What this person probably meant by “runtime” was “while recording command buffers”. Which is true. Specialization constants are provided at pipeline creation time, and you shouldn’t be creating pipelines in the middle of recording CBs.

1 Like

I see, so the person who told me that they get updated/provided when we create the pipeline was correct.

Thanks for the confirmation. :+1:t2: