Jump to content
  • Advertisement
Sign in to follow this  
Anthom

[D3D12] Shader scrambles Constantbuffer values

This topic is 1038 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!