D3D cbuffer packing/alignment rules
category: code [glöplog]
Anybody has a clue why the following cbuffer layout causes RenderDoc to complain I should provide 384 bytes instead of 192?
cbuffer CBufferParamData : register(b2)
{
float Param[16];
float4 Weights[8];
};
I thought this is properly 16-byte aligned. What do I miss?
cbuffer CBufferParamData : register(b2)
{
float Param[16];
float4 Weights[8];
};
I thought this is properly 16-byte aligned. What do I miss?
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules
Quote:
Arrays are not packed in HLSL by default. To avoid forcing the shader to take on ALU overhead for offset computations, every element in an array is stored in a four-component vector. Note that you can achieve packing for arrays (and incur the addressing calculations) by using casting.
So your "float Param" is stored as "float4 Param", so (16 * 4 + 8 * 4) * sizeof(float) = 384
thanks gargaj, much appreciated! Seems I was reading that page not closely enough :)
There's also a hack at the bottom of the page to work around it, but it makes it clear that you're going to lose performance if you do so.
Yup, changed it to "float4 Param[4];" and everything works as it should. Thanks again for your help.