Sign in to follow this  

[D3D12] Shader scrambles Constantbuffer values

This topic is 824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

my shader scrambles the values of my constbuffer.

 

i have a struct like:

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];
};

Displaying the constbuffer in the gpu debugger looks fine. 2 matrices and the 2 vectors at the end.

 

[attachment=29125:Unbenannt.PNG]

 

Now if i visualize the viewDir vector in the shader like:


// desc heap cbv
cbuffer CameraConstBuffer : register(b0)
{
float4x4 projectionMatrix;
float4x4 viewMatrix;
float3 viewPos;
float3 viewDir;
}
// in pixel shader
float3 vpos = (viewDir); float value = vpos.x; // uses the passed y value 
//float value = vpos.y; // uses the passed z value 
color = float4(value, value, value, 1.0);

viewDir.x is the y value of the view direction
viewDir.y is the z value of the view direction
and viewDir.z is 0.0

 

looks like it all got shifted back one position. and its reading buffer index 36-38 instead of 35 - 37!

 

if i use float viewPos[2] to shift back one offset, it still doesnt work. so its just reading the cbv values false and not indexing them at the wrong offset.

 

if i switch viewPos with viewDir, viewPos is affected. always the last member of the constbuffer struct.

Edited by Anthom

Share this post


Link to post
Share on other sites

indeed, you were right, Thanks!

 

this fixed it:


// in the c++ code
struct CameraConstData
{
urd::Matrix projection; // 64 ( 16 floats)
urd::Matrix view; // 64 ( 16 floats)
urd::Vec3 viewPosition; // 12 ( 3 floats)
float a;
urd::Vec3 viewDir; // 12 ( 3 floats)
float b;


// 104 bytes (26 * 4)
float offset[26];
};
// in the shader
// desc heap cbv 
cbuffer CameraConstBuffer : register(b0) { 
float4x4 projectionMatrix; 
float4x4 viewMatrix; 
float3 viewPos; float a; 
float3 viewDir; float b; 
}

so hlsl got its 16 byte boundry.

Edited by Anthom

Share this post


Link to post
Share on other sites

This topic is 824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this