Here's what I'd like to achieve:

  1. Rendering a first pass of objects in my scene, using standard depth comparison
  2. Rendering another pass of objects in the same scene, but with the following rules:
    • A Pixel of the 2nd pass always override the first pass (no depth compare between them)
    • Use Depth comparison between pixels written from the second pass.

In English I want depth comparison made inside each pass but I always want the second pass pixels to override the first pass ones.

Some things I've thought:

  • I tried to think about using stencil to solve this, but I couldn't find a way.
  • I know I could render into a separate target the second pass then composite the result into the first, but I'd like to avoid that.
  • I could use two separate Depth Buffer, one dedicated to each pass. (I never tried, but I figure it's possible to switch the depth buffer in a Render Target "on the fly")

Any idea of the best solution?


2 Answers 2


You could draw the first pass, then clear the depth buffer and draw the second pass. (Assuming you don't need depth values from the first pass at any point later in your pipeline.)

  • \$\begingroup\$ Ok, this is not totally what I wanted to do, but it makes sense to go that way. I may have to shuffle some rendering, but clearing the depth buffer is the best way. I could also go for two Depth Buffers I tested it and it works great! Anyway, thank you for your answer! \$\endgroup\$
    – Nock
    Jun 11, 2012 at 18:26

Another way of doing this is to partition your depth range using two viewports - the first viewport has depth range 0 to 0.5 and the second has depth range 0.5 to 1, but otherwise they are identical. Your "first pass" would therefore go into the second viewport, and your "second pass" into the first.

That would have the advantage of enabling you to draw your objects in a more-or-less order-independent manner, but at the tradeoff of reduced depth buffer precision. Depending on the rest of your program's requirements this may or may not be a big deal.

  • \$\begingroup\$ Yes, I thought about this one, but I'm not ready to give up precious depth range for this feature! :) \$\endgroup\$
    – Nock
    Jun 11, 2012 at 16:28
  • \$\begingroup\$ Nathan Reed's answer is probably the best way then. \$\endgroup\$ Jun 11, 2012 at 16:50

You must log in to answer this question.

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