Skip to main content
GameDev.net gamedev.net
🔒 Locked

Deferred depth problem

Started by keym Apr 30, 2013 at 3:47 PM 1 replies 1.5k views
Original Post
keym
keym

Hi guys,

I'm struggling again with my deferred renderer. This time I'm rewriting it from OpenGL/Cg to DX11/HLSL. I have almost everything working but (I think) my reconstruction from depth is now working correctly (I compared normals and they look the same as in OGL renderer, so I guess they are fine). The result I get is somewhat correct but bad things start to happen when I move away from the lit object - It becomes darker and darker and eventually gets all black. The thing is that I use almost the same code as when dealing with Cg/OGL and perhaps I've overlooked something with conversion. Fun fact - walls that are closer to the camera gets darker quicker than walls that are further away - maybe this will give some hint.

Posting pics


    //compute view space position
    #define ZNEAR zNearZFar.x
    #define ZFAR zNearZFar.y
    #define A (ZNEAR + ZFAR)
    #define B (ZNEAR - ZFAR)
    #define C (2.0 * ZNEAR * ZFAR)
    #define D (ndcPos.z * B)
    #define ZEYE (-C / (A + D))
    float depth2 = shaderTexture2.Sample(SampleType, texCoord);
    float3 ndcPos = (float3(texCoord.x, 1 - texCoord.y, depth2) - 0.5) * 2.0; //<<<Here I had to flip Y coord when compared to the OGL/Cg shader
    float4 clipPos;
    clipPos.w = -ZEYE;
    clipPos.xyz = ndcPos * clipPos.w;
    float3 vsPos = mul(invProj, clipPos).xyz;
unbird
unbird
Direct3D (any version) uses a different convention for the clip space normalized device coordinates: z goes from 0 to 1, not -1 to 1. (Off the top of my head: C = ZNEAR * ZFAR, but please check the formula yourself)

Best thing to do: Output position to a debug g-buffer (full float) and compare it with your reconstructed one.
keym
keym

So I guees I have to also do this:

from:


//From [0,1] to [-1,-1] in xyz
float3 ndcPos = (float3(texCoord.x, 1 - texCoord.y, depth2) - 0.5) * 2.0;

to:


//From [0,1] to [-1,1] only in xy, z stays in [0,1]
float3 ndcPos = float3(texCoord.x, 1 - texCoord.y, depth2);
ndcPos.xy = (ndcPos.xy - 0.5) * 2;

But this ^ and changing C to zNear*zFar sadly doesn't produce correct results. : /

Edit: For generating projection matrix I use D3DXMatrixPerspectiveFovRH (right handed coordinate system).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.