Jump to content
  • Advertisement
Sign in to follow this  
GavinC

Pixel depth to world-space position woes (deferred renderer).

This topic is 3943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm pretty sure this is something that would have come up at some point in the past... and I actually found a few "solutions" to this issue, but somehow (inexplicably to myself :P) I'm falling-down a lot with the implementation, and I'm not sure where it's going wrong... so I thought a fresh set of eyes (or a hundred) would be useful. The situation: -------------- I have a deferred renderer, everything works lovely except I don't like the fact that I'm using 3 16-bit channels to store the position data for each pixel, so I would like to replace that with a single 32-bit "depth" value, and use it to back-project the original world-position from that instead when-required (this would actually free up THREE 8 bit channels for use elsewhere, which would be very nice indeed :P) My implementation: ------------------ 1. First of all, I create a D3DFMT_R32F render-target to store the depth in. 2. Then, when rendering the geometry pass, I do the following to get the depth (in the vertex shader, interpolated and sent to the pixelshader). Out.Pos = mul(In.Pos, matWorldViewProjection); Out.Depth = Out.Pos.z / Out.Pos.w; //This should be correct. 3. Then in the pixelshader I simply dump the depth value out to the render-target, something like. Out.DepthTarget = in.Depth; //Passed from the vertex shader. Now so far... so good as far as I can see. I have a whole full-resolution float filled with the depth-value of each rendered pixel. I suspect my mistake is somewhere in the "using it" part. During the lighting pass, I render spheres for the point light geometry. I generate projected texture coordinates to sample the G-Buffer thusly: //In the vertex-shader. Out.ProjectedTexCoords = mul(mul(Pos, matWorld), matProjectiveTextureMatrix); //Where matProjectiveTextureMatrix is view * projection * remap //Where remap is [0.5f, 0.0f, 0.0f, 0.0f] [0.0f, -0.5f, 0.0f, 0.0f] [0.0f, 0.0f, 0.5f, 0.0f] [0.5f, 0.5f, 0.5f, 1.0f] //In the pixel-shader. float2 ProjectedTexCoords = i.ProjectedTexCoords.xy / i.ProjectedTexCoords.w; //Then add a small offset here to map directly to texels. Again, by this point, all is still fine. The projected texture coordinates are correct, and I can have everything working fine in the deferred renderer so long as I'm still storing the position manually rather than using depth. --------------------------------- 5. Trying to use the depth value. (This is the bit that's broken... right? :P) --------------------------------- //Retrieve the depth from the render-target. float Depth = tex2D(DepthSampler, ProjectedTexCoords).r; //Use it, along with our UV coords, to compute the original world-space position. float4 ViewPos = float4(ProjectedTexCoords.xy, Depth, 1); float4 WorldPosition = mul(ViewPos, InverseViewProjectionMatrix); .... and I'm guessing THAT's where it all goes horribly wrong. Of course, the real problem is that the only bit of code I can't verify as working is part (5) (the rest all works fine providing I'm storing the position manually rather than the depth).... but the solution in (5) is one I found as a solution online that people seem to agree works for them (though it was XNA). Completely stumped on this one guys. Any help would be greatly appreciated. I'm away for a few days, but if you can spot the glaring mistake in my implementation then feel free to ridicule me and then provide an answer :P Also, if it makes any difference (which I doubt it would considering all the matrices are provided....) I'm using a right-handed coordinate system. Thanks in advance for any help you can throw my way.

Share this post


Link to post
Share on other sites
Advertisement
Fortunately for you, there was a thread not too long ago that discussed this very topic. You should find some helpful answers there (specifically in the second post). Good luck! :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!