pouët.net

Go to bottom

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?
added on the 2021-04-15 12:36:33 by arm1n arm1n
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.
added on the 2021-04-15 12:45:11 by Gargaj Gargaj
So your "float Param" is stored as "float4 Param", so (16 * 4 + 8 * 4) * sizeof(float) = 384
added on the 2021-04-15 12:46:09 by Gargaj Gargaj
thanks gargaj, much appreciated! Seems I was reading that page not closely enough :)
added on the 2021-04-15 12:49:24 by arm1n arm1n
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.
added on the 2021-04-15 12:55:10 by Gargaj Gargaj
Yup, changed it to "float4 Param[4];" and everything works as it should. Thanks again for your help.
added on the 2021-04-15 16:20:17 by arm1n arm1n

login

Go to top