I started making a basic D3D11 application for Win8 without the Effects framework, everything was ok until I tried to pass constant buffers data to the shader, the data I'm passing to the shader doesn't match the actuall data the shader is reading.
I have a self made Matrix structure, and a struct containing a float value and the Matrix structure:
struct Matrix
{
float m00, m01, m02, m03;
float m10, m11, m12, m13;
float m20, m21, m22, m23;
float m30, m31, m32, m33;
};
struct ConstantBuffer
{
float value1;
Matrix value2;
} g_CBData;
.
After creating the constant buffer object, y update the data in my shader using:
//---------- update constant buffer ----------//
Matrix m =
{
1.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
g_CBData.value1 = 1.0f;
g_CBData.value2 = m;
D3D11_MAPPED_SUBRESOURCE mappedResource;
g_pDeviceContext->Map(g_pConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
CopyMemory(mappedResource.pData, &g_CBData, sizeof(ConstantBuffer) );
g_pDeviceContext->Unmap(g_pConstantBuffer, 0);
g_pDeviceContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer );
.
However, if I try to read the data I'm sending to the vertex shader using something like this:
buffer cbPerFrame : register (b0)
{
float value1;
float4x4 value2;
};
struct VSInput
{
float3 Pos : POSITION;
float4 Col : COLOR;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
float4 Col : COLOR;
};
VSOutput main( VSInput vs_in )
{
VSOutput vs_out;
vs_out.Pos = float4( vs_in.Pos, 1 );
vs_out.Col = vs_in.Col * value2._m11; // <- checking "value1" and the values in "value2"
return vs_out;
}
.
the data layout:simply does not match to what I'm sending to it!
The weird thing is that if I use in the constant buffer ONLY the "float value1" or "Matrix value2", the shader reads the corresponding data properly (the Matrix gets transposed, as I read it is expected), but if I include in the constant buffer structure both values, it looks like the binary layout simply gets messed up inside the vertex shader.. with apparently no logical order.
so.... what am I doing wrong?... how am I supposed to pass the data to the shader so I can read it properly?
Thanks!