3

I want to reconstruct the position in view space from depth buffer texture. I've managed to set the depth buffer shader resource view into shader and I believe there's no problem with it.

I used this formula to calculate the pixel position in view space for each pixel on the screen:

Texture2D textures[4]; //color, normal (in view space), depth buffer (as texture), random
SamplerState ObjSamplerState;

cbuffer cbPerObject : register(b0) {
    float4 notImportant;
    float4 notImportant2;
    float2 notImportant3;
    float4x4 projectionInverted;
};

float3 getPosition(float2 textureCoordinates) {
    //textures[2] stores the depth buffer
    float depth = textures[2].Sample(ObjSamplerState, textureCoordinates).r;
    float3 screenPos = float3(textureCoordinates.xy* float2(2, -2) - float2(1, -1), 1 - depth);
    float4 wpos = mul(float4(screenPos, 1.0f), projectionInverted);
    wpos.xyz /= wpos.w;
    return wpos.xyz;
}

, but it gives me wrong result:

enter image description here

I calculate the inverted projection matrix this way on CPU and pass it to pixel shader:

ConstantBuffer2DStructure cbPerObj;
DirectX::XMFLOAT4X4 projection = camera->getProjection();
DirectX::XMMATRIX camProjection = XMLoadFloat4x4(&projection);
camProjection = XMMatrixTranspose(camProjection);
DirectX::XMVECTOR det; DirectX::XMMATRIX projectionInverted = XMMatrixInverse(&det, camProjection);
cbPerObj.projectionInverted = projectionInverted;
...
context->UpdateSubresource(constantBuffer, 0, NULL, &cbPerObj, 0, 0);
context->PSSetConstantBuffers(0, 1, &constantBuffer);

I know that this calculations for vertex shader was ok (so I guess that myCamera->getProjection() returns good result):

DirectX::XMFLOAT4X4 view = myCamera->getView();
DirectX::XMMATRIX camView = XMLoadFloat4x4(&view);
DirectX::XMFLOAT4X4 projection = myCamera->getProjection();
DirectX::XMMATRIX camProjection = XMLoadFloat4x4(&projection);
DirectX::XMMATRIX worldViewProjectionMatrix = objectWorldMatrix * camView * camProjection;

constantsPerObject.worldViewProjection = XMMatrixTranspose(worldViewProjectionMatrix);
constantsPerObject.world = XMMatrixTranspose(objectWorldMatrix);
constantsPerObject.view = XMMatrixTranspose(camView);

But maybe I've calculated the inverted projection matrix in a wrong way? Or did I make another mistake?

edit

As @NicoSchertler spotted, the 1 - depth part in shader was an mistake. I've changed it to depth - 1 and made some minor changes in textures' format etc. I have such an result for now:

enter image description here

Note that this is for different camera's angle (as I don't have the earlier one anymore). Here's the reference - normals in view space:

enter image description here

It looks somehow better, but is it all right? It looks strange and not very smooth. It's a precision problem?

edit 2

As @NicoSchertler suggested the depth buffer in DirectX should use [0...1] range. So I've changed depth - 1 to depth to have:

float depth = textures[2].Sample(ObjSamplerState, textureCoordinates).r;
float3 screenPos = float3(textureCoordinates.xy* float2(2, -2) - float2(1, -1), depth);// -1); //<-- the change
float4 wpos = mul(float4(screenPos, 1.0f), projectionInverted);
wpos.xyz /= wpos.w;
return wpos.xyz;

But I got that result:

enter image description here

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • @NicoSchertler Indeed! That should be `depth - 1`. I've edited the question. Can you confirm that position in view space looks ok now? Or should I change something else too (I'm not sure)? If everything is good, you can post your comment as an answer and I will accept it. But I guess there's something more that need to be fixed here. – PolGraphic Aug 21 '15 at 13:07
  • But why `depth - 1`? Aren't your depth values in the [0, 1] range? – Nico Schertler Aug 22 '15 at 07:34
  • @NicoSchertler I think that DirectX depth buffer is indeed in `[0...1]` range. But look at my edit of the question - the output position without `- 1` part looks wrong. I'm confused. – PolGraphic Aug 22 '15 at 10:55
  • @PolGraphic did you resolve your problem? I have got very similar problem. – yzz Aug 17 '17 at 19:27
  • @yzz Unfortunatelly not, but I had some progress. You can check https://gamedev.stackexchange.com/questions/105835/strange-ssao-effect-wrong-position-normal-textures-in-view-space for further details. Note me if you find a solution. – PolGraphic Aug 17 '17 at 19:53
  • @PolGraphic I think that your normal texture is invalid, because the monitor should be blue (normals goes directly at camera - so they are parallel to camera z-axis - eventually it should be black, if you have inverted z-axis. Of course if it is in view space. – yzz Aug 17 '17 at 21:37

0 Answers0