Vireo  0.0
Vireo 3D Rendering Hardware Interface
Push constants

A Push constant is a tiny block of GPU-visible memory (with a limit to 128 or 256 bytes, depending on the graphic API and the drivers) embedded directly in command lists. It's ideal for frequently-changing, small pieces of data (e.g. material index, small transform matrices, flags).

Declaring a push constant

Push constant are not objects, they don't need to be created like other Vireo objects. They are just small structs, matrix or small arrays. Just define your data like any CPU-side data then create the description of the data by declaring a vireo::PushConstantsDesc.

Example of a push constant struct used to specify which model and which material to use in a shader :

struct PushConstants {
uint32_t modelIndex;
uint32_t materialIndex;
};
static constexpr auto pushConstantsDesc = vireo::PushConstantsDesc {
.size = sizeof(PushConstants),
};

Binding a push constant

During the initialization phase store the push constant description in the pipeline resources object :

pipelineConfig.resources = vireo->createPipelineResources(
{ descriptorLayout, samplers.getDescriptorLayout() },
pushConstantsDesc);

The push constant will be bound to the first free set/space number : if you have two descriptor layouts in the pipeline resource the push constant will be bound to the set 2 (and the binding 0 since Vireo currently only support one push constant per pipeline resources).

Using a push constant

During the render pass update the data then copy the data in the command list using vireo::CommandList::pushConstants :

pushConstants.modelIndex = Scene::MODEL_OPAQUE;
pushConstants.materialIndex = Scene::MATERIAL_ROCKS;
cmdList->pushConstants(pipelineConfig.resources, pushConstantsDesc, &pushConstants);

In the shader, if you use the Slang shader language use the [[push_constant]] attribute for Vulkan and the register syntax for DirectX :

struct PushConstants {
uint modelIndex;
uint materialIndex;
};
[[push_constant]]
PushConstants pushConstants : register(b0, space2);