   Output depth values to render target
  I have a "position buffer".  I want to render a screen quadrilateral, fetch the current texel from the "position buffer", and write to the depth buffer based on the "position buffer" (to simulate geometry that can clip objects drawn later).  Given the current pixel position in view-space, how can I calculate the depth I should write out from the pixel shader?  (I'm using Direct3D9)   
  You could use the z component of the view position.  However, you need to normalize the value to the [0,1] range before writing it to the output buffer.   ;Quote:Original post by Jason ZYou could use the z component of the view position.  However, you need to normalize the value to the [0,1] range before writing it to the output buffer.Thanks for the reply.  However, that would only work if I was calculating the other object's depth values manually, as well.  What I'm trying to do, though, is recreate a depth buffer from this "position buffer" that would be usable with the fixed function pipeline.1) "position buffer"2) recreate depth buffer3) render other objects which can be tested against the above depth buffer by the hardware (otherwise I would implement the culling inside the shader itself, which would be much easier, but not what I desire)Appreciate the help!   ;  This is pretty easy.  From what I understand, you have view-space position stored in a buffer.  In your pixel shader, just sample that buffer and then transform that position with your projection matrix.  Then take the z component of the resulting coordinate, and divide it the w component.  This gives you the z-value that would be written to the depth buffer for that particular pixel position.float3 positionVS = tex2D(positionSampler, texCoord).xyz;float4 positionCS = mul(float4(positionVS, 1.0f), projectionMatrix);float depth = positionCS.z / positionCS.w;Then to fill the depth buffer you can just write that value out to DEPTH.Also just so you're aware...it's generally not necessary to store an entire position vector in a buffer since you can reconstruct it from a single depth value. I wrote a blog about that if you're interested.   ;Quote:Original post by MJPThis is pretty easy.  From what I understand, you have view-space position stored in a buffer.  In your pixel shader, just sample that buffer and then transform that position with your projection matrix.  Then take the z component of the resulting coordinate, and divide it the w component.  This gives you the z-value that would be written to the depth buffer for that particular pixel position.float3 positionVS = tex2D(positionSampler, texCoord).xyz;float4 positionCS = mul(float4(positionVS, 1.0f), projectionMatrix);float depth = positionCS.z / positionCS.w;Then to fill the depth buffer you can just write that value out to DEPTH.Aha, thank you very much.  So I don't need to bother with the near/far clip plane equations (as shown on wikipedia)? (before someone else comes in with the elitist "Try it." reply, yes, I will try it soon enough)On second thought: it seems you've suggested a second method.  There's the wikipedia method which seems to use the near/far clip planes and viewSpacePos.z, and there's your method which simply uses post-projection Z divided by W.  Do they achieve the same result?Quote:Also just so you're aware...it's generally not necessary to store an entire position vector in a buffer since you can reconstruct it from a single depth value. I wrote a blog about that if you're interested.Thanks for the link.  However, I'm aware of that and indeed I'm actually encoding a 32-bit view-space depth in a byte RGBA texture.  I didn't mention that in my first post because people would suggest to use that depth - which I cannot, because it is not viewSpacePos.z but rather length(viewSpacePos.xyz) [wink].   