Skip to main content
1 of 2
Roy T.
  • 10.2k
  • 38
  • 58

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; shaderParametersDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; shaderParametersDesc.ByteWidth = sizeof(ShaderParameters); shaderParametersDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; shaderParametersDesc.MiscFlags = 0; shaderParametersDesc.StructureByteStride = 0; device->CreateBuffer(&shaderParametersDesc, NULL, &m_shaderParameters); 

Buffer setting (every frame)

D3D11_MAPPED_SUBRESOURCE mappedResource; ShaderParameters* shaderParameters; deviceContext->Map(m_shaderParameters, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); shaderParameters = (ShaderParameters*)mappedResource.pData; 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"); shaderParameters->inverseViewProjectionMatrix = XMMatrixTranspose(invp); shaderParameters->cameraPosition = camera->GetPosition(); deviceContext->Unmap(m_shaderParameters, 0); deviceContext->VSSetConstantBuffers(0, 1, &m_shaderParameters); 

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

Roy T.
  • 10.2k
  • 38
  • 58