Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Depth Buffer Read-Only

Tags:

opengl

What is the usage of making the depth buffer read-only? I've read on redbook that it is the case where you need to draw opaque objects first then the transparent ones. I've tested that and also on two opaque objects over each other, the result is hard to explain and i've found nothing usefull about making it read-only. Can some one explain about it?

like image 764
pooya Avatar asked Aug 08 '10 07:08

pooya


1 Answers

That absolutely makes sense in some scenarios. Here are two:

  1. You may use very expensive pixel-shaders for your graphics. If you do so you want to minimize the amount of pixels that need to get shaded. To do so you can first render your scene into the depth-buffer only (disable writes to the color buffer). Then you set your depth-buffer to read-only and render all your geometry with color-writes enabled and depth-compare-mode set to equal. This makes sure that every pixel on the screen gets painted exactly once, and you can use very expensive pixel shaders while still maintaining a nice frame-rate.

  2. The transparency-problem: If you render transparent (alpha-blended) triangles the order of drawing makes a significant difference to the final image. To get good looking result a tried and trusted way is to first render all opaque geometry. Then disable writes to the depth-buffer and render all transparent stuff sorted from back to front. This will make sure that your transparent polygons are always drawn in order and they don't interfere with the opaque stuff.

Just imagine this case:

  • The camera is in a street-scene and you are looking at a shop with a window and some nice stuff inside the shop.

  • You draw the transparent window first - with depth writes enabled.

  • Then you draw the interior of the shop.. Due to the z-buffer all pixels will get discarded because the graphic card thinks that the pixels aren't visible.

  • You'll end up with a completely wrong image: A window in front of ... well ... whatever was in your graphic memory before you started rendering.

As said: To get around this you first render all your opaque things (the shop interior) with depth-test and depth-writes on. Then you render the window with depth-test on and depth-write off. This will solve this issue.

Here is an example of how ugly such things can be. It shows yet another way of handling transparency wrong (take a closer look at the carpet).

alt text

like image 196
Nils Pipenbrinck Avatar answered Oct 19 '22 23:10

Nils Pipenbrinck