RESOLVED : For the newbie's purpose, the problem was simply that i thought padding could occur anywhere (in this case at the end of the struct). But the packing has to be done every 16 bytes involving padding in the middle of the struct.
Hi,
I'm new to d3d11 so i apologies for this newbie question.
I'm trying to map data to several cbuffer but it seems that only the first (register(cb0)) cbuffer is actually getting correct data. I have no control what so ever on what is written in the next ones.
The problem appears only in the pixel shader. I've tried to add an extra cbuffer in the vertex shader and there's i no problem.
I think i have correctly aligned data to 16 bytes packing.
I use the same MatrixBuffer for both vertex shader and pixel shader.
Map() call sequence order is irrelevant and doesn't change anything.
EDIT : I've managed to reduce code to the following snippet. I'm using same buffer for cbuffer TestBuffer. And it works in the vertex shader but not in the pixel shader.
EDIT 2 : If i set TestBuffer : register(cb0) and MatrixBuffer : register(cb1) in the pixel shader i now get this strange compile error :
+ compile_error 0x001b80e8 "error X4567: maximum cbuffer exceeded. target has 14 slots, manual bind to slot 4294967295 failed\n"
VertexShader :
// Typedefs
//------------------------------------------------
struct VertexInputType
{
float4 position : POSITION;
float3 normal : NORMAL;
};
struct PixelInputType
{
float4 v_position : SV_POSITION;
float3 w_normal : NORMAL;
float3 w_position : TEXCOORD0;
};
// Globals
//------------------------------------------------
cbuffer MatrixBuffer
{
float4x4 world;
float4x4 view;
float4x4 projection;
};
cbuffer TestBuffer
{
float4 test;
}
// Shader
//------------------------------------------------
PixelInputType SPMLVShader(VertexInputType input)
{
PixelInputType output;
output.v_position = input.position;
output.w_normal = input.normal;
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
// Calculate the position of the vertex in world coordinates.
output.w_position = mul(input.position, world);
// Calculate the position of the vertex against the world, view, and projection matrices.
output.v_position.xyz = output.w_position.xyz;
output.v_position = mul(output.v_position, view);
output.v_position = mul(output.v_position, projection);
// Calculate the normal vector against the world matrix only.
output.w_normal = mul(input.normal, (float3x3)world);
// Normalize the normal vector.
output.w_normal = normalize(output.w_normal);
// CORRECT OUTPUT
if(test.x == 222 && test.y == 223 && test.z == 224 && test.w == 225)
{
return output;
}
// DO NOT DISPLAY ANYTHING
output.v_position = float4(0, 0, 0, 0);
return output;
}
PixelShader :
// Typedefs
//------------------------------------------------
struct PixelInputType
{
float4 v_position : SV_POSITION;
float3 w_normal : NORMAL;
float3 w_position : TEXCOORD;
};
// Globals
//--------------------------------------
cbuffer MatrixBuffer
{
float4x4 world;
float4x4 view;
float4x4 projection;
};
cbuffer TestBuffer
{
float4 test;
};
float4 SPMLPShader(PixelInputType input) : SV_TARGET
{
if(test.x == 222 && test.y == 223 && test.z == 224 && test.w == 225)
{
return float4(0.0f, 1.0f, 0.0f, 1.0f);
}
else
{
return float4(1.0f, 0.0f, 0.0f, 1.0f);
}
}
I've been searching a lot, but i have no clue yet on where it comes from.
Best regards.