# Matrix in constant buffer on the GPU contains NaNs but not on the CPU

I've got a shader with the following constant buffer:

cbuffer ShaderParameters
{
matrix inverseViewProjectionMatrix;
float4 cameraPosition;
};


When using Visual Studio 2012's graphic debugger I see that the last column of this matrix contains only NaNs, the other values are correct. So somehow my program is passing wrong values to the GPU.

However when I debugged my C++ code I see no NaNs in the matrix that I upload to the GPU. So what is going on? Below are the relevant parts of code

Struct for the constant buffer on the CPU:

struct ShaderParameters
{
XMMATRIX inverseViewProjectionMatrix;
XMFLOAT4 cameraPosition;
};


Buffer creation:

D3D11_BUFFER_DESC shaderParametersDesc;


Buffer setting (every frame)

D3D11_MAPPED_SUBRESOURCE mappedResource;

XMMATRIX view = camera->GetViewMatrix();
XMMATRIX proj = camera->GetProjectionMatrix();
XMMATRIX mult = XMMatrixMultiply(view, proj);
XMMATRIX invp = XMMatrixInverse(nullptr, mult);

if(XMMatrixIsNaN(invp))
throw std::exception("invp is NAN");



Note that the exception is never thrown and if I inspect the matrix manually there are sane values in there (see image)

EDIT: some asked (via twitter) to include some more shader source code as unused values might be seen by the debugger as NaN. So here is the complete vertex shader:

cbuffer ShaderParameters
{
matrix inverseViewProjectionMatrix;
float4 cameraPosition;
};

struct VertexInputType
{
float4 position : POSITION;
float4 uv       : TEXCOORD0;
};

struct PixelInputType
{
float4 position         : SV_POSITION;
float4 direction        : TEXCOORD0;
float4 cameraPosition   : TEXCOORD1;
};

PixelInputType main(VertexInputType input)
{
PixelInputType output;

input.position.w = 1.0f;
output.position = input.position;

output.direction = mul(input.position, inverseViewProjectionMatrix) - cameraPosition;
output.direction.xyz = normalize(output.direction.xyz);

output.direction.w = 1.0f;

output.cameraPosition = cameraPosition;

return output;
}