Using different push constants at different stages of the shader

I have a vertex shader with a push constant block containing one float:

layout(push_constant) uniform pushConstants { float test1; } u_pushConstants; 

And a fragment shader with another push-constant block with a different float value:

 layout(push_constant) uniform pushConstants { float test2; } u_pushConstants; 

test1 and test2 must be different.

The push-constant ranges for the pipeline layout are defined as follows:

 std::array<vk::PushConstantRange,2> ranges = { vk::PushConstantRange{ vk::ShaderStageFlagBits::eVertex, 0, sizeof(float) }, vk::PushConstantRange{ vk::ShaderStageFlagBits::eFragment, sizeof(float), // Push-constant range offset (Start after vertex push constants) sizeof(float) } }; 

The actual constants are then pushed during rendering as follows:

 std::array<float,1> constants = {123.f}; commandBufferDraw.pushConstants( pipelineLayout, vk::ShaderStageFlagBits::eVertex, 0, sizeof(float), constants.data() ); std::array<float,1> constants = {456.f}; commandBufferDraw.pushConstants( pipelineLayout, vk::ShaderStageFlagBits::eFragment, sizeof(float), // Offset in bytes sizeof(float), constants.data() ); 

However, when checking the values ​​inside the shaders, both values ​​have the value 123. It seems that the offsets are completely ignored. Am I using them incorrectly?

+6
source share
1 answer

In the layout of your pipeline, you indicated that your vertex shader will access a data range of [0, 4) bytes in a constant push range. You stated that your fragment shader will access a range of data from [4, 8] in a constant range of push.

But your shaders tell a different story.

 layout(push_constant) uniform pushConstants { float test2; } u_pushConstants; 

This definition says very clearly that the trigger constant range is starting to use [0, 4]. But you told Vulcan that he uses [4, 8]. Which should Vulcan believe: your shader or your pipeline layout?

The general rule to remember is this: your shader means what it says. The parameters set to create the pipeline cannot change the value of your code.

If you intend to use the fragment shader in fact [4, 8], then the fragment shader should really use it:

 layout(push_constant) uniform fragmentPushConstants { layout(offset = 4) float test2; } u_pushConstants; 

Since it has a different definition from the VS version, it must have a different block name. The offset layout defines the offset of the variable in question. This standard GLSL material and compilation for Vulkan does not change this.

+8
source

All Articles