0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Maybe something to do with alignment perhaps? Have you tried replacing Vec3's with Vec4? \$\endgroup\$ Commented Sep 16, 2015 at 11:34
  • \$\begingroup\$ yes you were right, adding a float after each vec3/float3, fixed the problem. thanks. post as answer. msdn.microsoft.com/en-us/library/windows/desktop/… \$\endgroup\$ Commented Sep 16, 2015 at 11:45

1 Answer 1

0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\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
    Commented Sep 23, 2015 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .