Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/307935947146809345
added 1031 characters in body
Source Link
Roy T.
  • 10.2k
  • 38
  • 58

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; } 

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; } 
Source Link
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