I'm having trouble with passing 3x3 matrix through a constant buffer to my shader in DirectX. This is how I define my constant buffer:
In .cpp:
struct PostProcessConvolutionCB {
float screenWidth;
float screenHeight;
float sum;
XMFLOAT3X3 kernel;
};
In .hlsl:
struct PostProcessConvolutionCB {
float screenWidth;
float screenHeight;
float sum;
float3x3 kernel;
};
ConstantBuffer<PostProcessConvolutionCB> cb : register(b0);
struct PixelShaderInput {
float4 Position : SV_Position;
};
float4 main(PixelShaderInput IN) : SV_Target {
return float4(cb.kernel[1][1], 0.f, 0.f, 1.f);
}
It seems like access to some element is all messed up. To test this I initialized the matrix in the constant buffer like this: XMFLOAT3X3(0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f);
and tried displaying the value of each element by hardcoding the matrix indices in the shader like in hlsl snippet above (cb.kernel[1][1]
). After 9 runs I got the following results:
kernel[0][0] = 0.1
kernel[1][0] = 0.2
kernel[2][0] = 0.3
kernel[0][1] = 0.5
kernel[1][1] = 0.6
kernel[2][1] = 0.7
kernel[0][2] = 0.9
kernel[1][2] = 1.0
kernel[2][2] = 1.0
Seems like every row is aligned to 4 floats. Changing the matrix to 4x4 helps but I guess there has to be a way to use float3x3 type.
How to handle this properly?