   DX9 Rendering Scene Depth to Texture
  Ok, so I've done quite a bit of looking around for something to answer my questions about rendering and then using a texture for scene depth. My goal is to figure out how to correctly render the depth buffer to a texture in DX9 for use in shadow mapping and other full screen effects.What I have right now is a shader pass that renders my scene to a render target texture that is D3DFMT_R32F as some of the examples I have found have suggested.For my shaders I have the following:float4x4 gWorldViewProjectionMatrix;struct VS_INPUT {   float4 Position : POSITION0;   };struct VS_OUTPUT {   float4 Position : POSITION0;   float  Depth    : TEXCOORD0;};VS_OUTPUT main( VS_INPUT IN ){   VS_OUTPUT OUT; OUT.Position = mul(IN.Position, gWorldViewProjectionMatrix);   //Documents on a Microsoft page said to do 1-(z/w) for better precision   OUT.Depth=  1-(OUT.Position.z/OUT.Position.w); return( OUT );}struct PS_INPUT {   float Depth : TEXCOORD0;};float4 main( PS_INPUT IN ) : COLOR0{  return float4(IN.Depth, IN.Depth, IN.Depth, 1);}So what I end up is with all the objects BLACK, or WHITE if do not do the (1-(z/w)). However if I get reall close to the geometry then I start to see a bit of a gradient depth shading.Now here is the part that I'm looking for some guidance. Ideally I'd like to have depth information that I can visualize (I'm guessing the data is there for use even though it is all black, but I always see a nice gradient depth shading in most demos I see).Now I am assuming that if I want a gradient look that I'd have to some how scale the depth value between the near and far planes so that I get a value between 0 and 1? Then the other part in question is if I should even be trying to change what has been written with the black because went I go to use the values, I would have to do something special to them when I go to do different effects and calculations off the depth buffer such as DoF, SSAO, or Deferred shading?Thanks for taking the time!   
  Have you tried just outputting the z?   ;  Yeah, I was just working on it right now. I just took the z value as is and passed  it into the pixel shader. I then did depthScaled = IN.Depth/(far-near); and wrote the depthScaled to the render target. I had my far clip plane set really high so even when it was scaled from 0 to 1 it seemed dark close up. I lowered the far and was able to get the debug image that I was expecting to see.So now I have something debug-able but I guess I'd like to know if keeping it in the 0-1 range is ideal or should I just write the z value as is?   ;  You can't divide by w in the vertex shader, since post-projection z doesn't interpolate linearly after you've performed homogeneous division.  You have to do it in the pixel shader.  Just pass z and w on to the pixel shader then do your calculation.And yes, you won't be able to use directly use this depth value in any other shaders without doing some conversion.  You'll have subtract from 1 again, and you'll need to scale your value to the distance between your near and far clip planes in order to get linear view-space depth.  There's been a good amount of discussion on this topic in graphics programming & theory, if you want to check that out.   