# XNA 4.0 HLSL - strange depth map

I want to draw my pre-rendered depth map to the scene.

I get my depth value in the following way:

// Depth is stored as distance from camera / far plane distance, 1-d for more precision
output.Depth = float4(1-(input.Depth.x / input.Depth.y),0,0,1);


output.Position = mul(input.Position, worldViewProjection);
output.Depth.xy = output.Position.zw;


(input = output of Vertex Shader)

and store it in a RenderTarget2D

depthTarg = new RenderTarget2D(GraphicsDevice, viewWidth,
viewHeight, false, SurfaceFormat.Single, DepthFormat.Depth24, samples, RenderTargetUsage.DiscardContents);


But somehow, when I draw that (Single), I get this result: (displaying rgb)

channel r:

(all black)

channel g:

channel b:

channel a:

Why is there data in every color channel althrough i only defined a value on the red parameter (and 1 on a, but you can somehow see the outlines of the model, so it cant be all 1)

And surprisingly, the b channel looks almost like what I am looking for, but has strange lines in it which stay at the same distance to the camera if I move it.

• What happens when you use Surfaceformat.Color? (I know Single is the way to go, just curious) Also try to enhance the constrast of your images, maybe using photoshop, its often hard to see small differences in depth/color using just your eyes :). May 22, 2014 at 12:29
• Using Photoshop proved that the r channel really is 0/0/0 (totally black) on every single pixel. Setting the Surfaceformat to Surfacevormat.Color unfortunately doesn't change anything at all :c May 22, 2014 at 13:50
• This does not seem all that surprising to me. The lowest-order bits are green, followed by blue and then alpha. What surprises me is that you thought swizzling a depth texture would produce color. If you were using 32-bit depth, then the "red" channel would not be constant. But as it stands right now, you should be treating the texture as a 24-bit fixed-point number, which means dividing each of the channels and summing them. May 22, 2014 at 14:25
• (e.g. green / (255.0f * 255.0f * 255.0f) + blue / (255.0f * 255.0f) + alpha / 255.0f) May 22, 2014 at 14:36
• By the way, since the alpha channel is nowhere near white, your range between near and far clip planes can be tweaked significantly. You are not getting an optimal distribution of depth precision in this scene, and would benefit from moving the far plane closer. This may not always be the case, but that is how you should interpret the image in the alpha channel - if it does not range from white to black in a given scene, then your depth buffer is not as precise as it could be. May 22, 2014 at 14:57