Hi there!
Was having some trouble lately. Check out the HLSL code below:
Code:
cbuffer cbPerObject : register(b0)
{
float3 light;
float4x4 gWorldViewProj;
};
When compiling this shader offline, where you get a nice readout of how it's been assembled you see this:
Code:
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//
// Buffer Definitions:
//
// cbuffer cbPerObject
// {
//
// float3 light; // Offset: 0 Size: 12 [unused]
// float4x4 gWorldViewProj; // Offset: 16 Size: 64
//
// }
Note the offset for the float3. Clearly states it reads 12 bytes but sets the offset to 16. Why though? When I ran this code the render screen got all messed up as the first element of the matrix was read (presumably) as the extra padding byte for the float3.
I fixed it by just making the variable that gets loaded into the constant buffer a vector4. The last element of the vector4 just isn't read by the shader it's discarded in the padding.
Was just wondering why it does that?
Thanks