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

I get my depth value in the following way:

basically (Pixel Shader)

// 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);

where (Vertex Shader)

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)

enter image description here

channel r:

(all black)

channel g:

enter image description here

channel b:

enter image description here

channel a:

enter image description here

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.

enter image description here

  • \$\begingroup\$ 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 :). \$\endgroup\$
    – Roy T.
    May 22, 2014 at 12:29
  • \$\begingroup\$ 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 \$\endgroup\$ May 22, 2014 at 13:50
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ May 22, 2014 at 14:25
  • 2
    \$\begingroup\$ (e.g. green / (255.0f * 255.0f * 255.0f) + blue / (255.0f * 255.0f) + alpha / 255.0f) \$\endgroup\$ May 22, 2014 at 14:36
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ May 22, 2014 at 14:57

1 Answer 1


As Andon M. Coleman pointed out: The problem was that I treated the Depth Map as float4 and tried to get the r channel to retrieve my depth value.

I now simply get a float value from the map and (for presentation purposes) multiply it with a scalar of 500, creating a new Texture with its r,g,b channel holding the data calculated before.


enter image description here


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .