KhronosGroup / glslang

Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Other
2.9k stars 815 forks source link

Wrong vector sizes in push constants #3637

Closed KoT3isGood closed 1 day ago

KoT3isGood commented 5 days ago

Each uint and float are 4 bytes

layout(push_constant) uniform constants
{
   uint posX;
   uint posY;
   uint fontSize;
   uint textOffset;
   uint posZ;
   float r;
   float g;
   float b;
} PushConstants;

And so this push constant size is 32

Each vec3 is made from 3 floating points, so its size should be 12 bytes But when I replace r,g,b with vec3

layout(push_constant) uniform constants
{
   uint posX;
   uint posY;
   uint fontSize;
   uint textOffset;
   uint posZ;
   vec3 color;
} PushConstants;

Push constant size becomes 44 (according to Vulkan)

arcady-lunarg commented 4 days ago

This is becuase the vec3 is being aligned to a multiple of 16 bytes, which is the next higher power of 2 from its size. If you want to align the vec3 with the same alignment as a scalar float, you can use the GL_EXT_scalar_block_layout extension and declare your block with layout(push_constant, scalar) uniform constants, which will result in color being assigned an offset of 20.

ravi688 commented 3 days ago

By default "Vulkan" GLSL uses std430 alignment rules for push constant buffers.