im having trouble with one constantbuffer:

struct CameraConstData
    urd::Matrix projection; // 64 ( 16 floats)
    urd::Matrix view;       // 64 ( 16 floats)
    urd::Vec3 viewPosition; // 12 ( 3 floats)
    urd::Vec3 viewDir;      // 12 ( 3 floats)

    // 104 bytes (26 * 4)
    float offset[26];

inside the shader its defined like:

// desc heap cbv
cbuffer CameraConstBuffer : register(b0)
    float4x4 projectionMatrix;
    float4x4 viewMatrix;
    float3 viewPos;
    float3 viewDir;


now viewdir is always filled with wrong buffer indices.

viewDir.x is the passed y value viewDir.y is the passed z value viewDir.z is 0.0

checked it in the shader like:

float3 vpos = normalize(float4(viewDir, 0.0)).xyz;
//float value = vpos.x; // uses the passed y value
//float value = vpos.y; // uses the passed z value
color = float4(vpos, 1.0);

GPU Debugging shows that the constant buffer looks fine:

gpu constbuffer

i checked the vectors against the cpu side and they match.

So why is the shader reading index 36-38 instead of 35-37?

Its always affecting the 4th member of the struct. If i switch viewPosition with viewDir, viewPosition is false.


Aligning members should help. Either replace Vec3 and float3 with Vec4 and float4 or add a float after them.

| improve this answer | |
  • \$\begingroup\$ @anthom This solves the problem but is more of a workaround than a solution. The root of the problem is that HLSL default packing is not the same as C++ default packing. Most C++ compilers provide a mechanism to change packing, such as #pragma pack(16). See also this question. \$\endgroup\$ – MooseBoys Sep 23 '15 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.