   Reconstructing the pixel position from depth?
  How can I reconstruct the position of my pixels, when I only have the depth of the scene? Many put only the depth into the texture, and having one big R32G32B32A32 texture to hold all positions properly is a very slow way. Is it faster to save only the depth? And if so,how do I do that? :)   
  Firstly, depending on the hardware you may well be able to read the z-buffer directly. That should be quickest. However storing clip space z into an R32F texture will be more compatible.To reconstruct X and Y from that clip space z value, what you need to do is interpolate along a ray running from the near clip plane to the far clip plane using that z value (which is in the 0-1 range in clip space).A quick search turned up this which has some code.   ;  You definitely don't want to store clip-space z if you're just going to write depth into a floating-point texture. It has precision issues, and it's slower to reconstruct position with than a linear depth value.  If you're going to sample a hardware depth buffer though you of course don't have a choice, but it's easy to get linear z value back from perspective z/w if you look at the equations here and do a little algebra. Then with your linear z value you can do some variant of the frustum ray technique.   ;  I'm using DirectX10. It supports reading the depth buffer directly, doesn't it? I need it for my Deferred renderer.. Thanks for your links!   ;  It does. One issue though is that you can't have the depth buffer bound simultaneously as both a DepthStencilView and a ShaderResourceView. This means you can't sample from the depth buffer in your shader, and also use that buffer for depth-testing or stencil-testing your light volumes.   ;  Okay.. But I can use it? One more question: What is the stencil buffer? I know how it looks like, but I don't know what I can do with it..That I can't use it for depth testing while it is bound as SRView is not a problem. Who needs depth testing for a fullscreen quad? :P   ;Quote:Original post by mind in a boxOkay.. But I can use it? One more question: What is the stencil buffer? I know how it looks like, but I don't know what I can do with it..The stencil buffer just contains some number of integer bits (usually 8). When rendering you can write some integer value to the stencil buffer, and you can also use basic comparison operators (greater-than, less-than, equals) to perform a stencil test for culling pixels. Doing this you can create masks with arbitrary geometry. So for instance let's say you drew a circle right in the middle of screen, and you set the stencil states to write a stencil value of 1 for all pixels drawn. Then after that, you draw some other geometry with the reference value to 1 and you set the stencil test to EQUAL. This would mean that the second batch of geometry would only pass the stencil test where the stencil buffer has a value of 1, which means that the circle would effectively act as a mask for other geometry.Quote:Original post by mind in a boxThat I can't use it for depth testing while it is bound as SRView is not a problem. Who needs depth testing for a fullscreen quad? :PWell a very common optimization with deferred rendering is render light volumes for point and spot lights, rather than a full screen quad. If you do this you restrict the number of pixels that need to be shaded, and you can also use the depth test to cull pixels that don't need to be lit because they're blocked by other geometry, or because the light volume is "floating in air".;Quote:Well a very common optimization with deferred rendering is render light volumes for point and spot lights, rather than a full screen quad. If you do this you restrict the number of pixels that need to be shaded, and you can also use the depth test to cull pixels that don't need to be lit because they're blocked by other geometry, or because the light volume is "floating in air".I know, I was refereeing to drawing the two polygons. :) So, what is better then? Using the depth buffer and *not* cull them, or is it to write the depth to a R32F Texture and use it then?When using the stencil buffer to cull something, could I just write a sphere (via Geometry Shaders or a precomputed one) which is at the same size as the range of my Point Light to the stencil buffer, and then, everything other gets culled? Do I have to use scissoring then, too?   ;  To cull floating in the air lights with the stencil buffer, what you need to do is render the back faces of the light sphere with a reversed depth test into the stencil buffer (with colour writes disabled). That is you want the stencil buffer to be 1 where the backfaces of the light are buried inside the geometry. If the backfaces don't hit geometry then the light is floating in the air.After doing that you swap over the depth test, enable the stencil test, and render the front faces of the light. The depth test will reject pixels where the front faces are buried behind geometry.Note that this all assumes the camera isn't inside the light. If it is you never need to render the front faces of the light, which means you don't need the stencil buffer - just the z test.To render backfaces instead of front faces, swap the cull mode.Note that you either need to clear the stencil buffer between lights, or use a different stencil value for each light.For very small lights all that messing about with render states and the stencil buffer may not be worthwhile.   