skipping the depth buffer clear

Hi all,

Is there a way to skip/avoid clearing the depth buffer? Just like the “cheat” of avoiding clearing the color buffer?
(i.e. cheating the color buffer: that by drawing a quad or something right in front of your view (or camera) BEFORE drawing the rest of your scene so that the geometry blocks everything then you no longer have to clear the color buffer. Because the geometry just “erased” every pixel’s value from the last frame.)
Anyway, again my question is, is there a similar way that clearing the depth buffer may be avoided as well?

Thanks in advance.

Hi !

Yes you can draw a quad and disable writing to the color buffer and only update the depth buffer.


when you draw the scene, you probably use something like glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
this clears the depth buffer and the color buffer
removing GL_DEPTH_BUFFER_BIT from glClear is what you need to do

yes exactly, that means that the depth buffer wont be cleared. So I get all this garbage from the previous frame because I didn’t include the GL_DEPTH_BUFFER_BIT in glClear().
So what I want to do is for the depth buffer to be cleared even if not calling glClear(GL_DEPTH_BUFFER_BIT).
Like when the color buffer dont get cleared by not calling glClear(GL_COLOR_BUFFER_BIT), but the screen gets “cleared” by drawing a quad RIGHT IN FRONT of your view at the start of the frame and BEFORE drawing your scene.

Let’s see:

normal color buffer clearing:
1.start renderscene
3.draw scene and objects
4.repeat renderscene

cheating the color buffer clearing:
1.start renderscene
2.glClear(GL_DEPTH_BUFFER_BIT); // note: GL_COLOR_BUFFER_BIT not included
3.draw a quad right in front of your view. make sure quad occupies the entire screen. this somehow simulates clearing the color buffer because it drew over the entire screen; OR make sure everything on the screen is redrawn.
4.draw scene and objects.
5.repeat renderscene.

now this is what i want to do without having artifacts in the depth buffer from the last frame:
1.start renderscene
2.//glClear(GL_DEPTH_BUFFER_BIT); // note: glClear() is no longer called.
4.draw scene and objects.
5.repeat renderscene.
Is there anyway I can cheat step 2 and still have the effect of clearing the depth buffer?

I want to do this just to avoid the overhead of clearing the depth buffer. Just like the overhead of the clearing the color buffer was avoided. Thanks for the patience.

[This message has been edited by coder1 (edited 02-19-2004).]

Is there really that much overhead in clearing the depth buffer to really make a diffrence and a need to skip it?

Originally posted by nexusone:
[b]Is there really that much overhead in clearing the depth buffer to really make a diffrence and a need to skip it?


This is the exact reply I was expecting to get. Yeah there seems to be very little overhead in clearing the depth buffer. I was just trying to squeeze performance out of the usual gl routines, before going into the engine-specific details (octrees, LoD, etc.).
Anyway I agree with you fully, that’s why I posted this topic so that if ever there’s a trick to skip clearing the depth buffer then he/she can bring it up. cheers!

But there was this one moment, just one part in my older projects (months older) that when I disabled clearing the depth buffer the scene got rendered pretty fast. at least normally fast.

I think that most drivers can clear faster than they can render a quad, especially if you are clearing lots of bits simultaniously. Besides isn’t the bottleneck likely to be somewhere other than the clearing of the screen?

Ok Im really convinced now. I just wondered if glClear(GL_DEPTH_BUFFER_BIT) can be skipped the way glClear(GL_COLOR_BUFFER_BIT) was skipped, and still get the depth buffer “cleared.”
Anyway, I guess the answer is “no,” that there’s no substitute in clearing the depth buffer. ok thanks guys!
I have other question (about tricks on speeding up texturing) but i will put it in a new post.later.

But i’m still very much interested in new replies about this topic though. later.

[This message has been edited by coder1 (edited 02-19-2004).]

I agree with chowe6685. I am under the impression that graphics hardware can clear these buffers very fast. I think at least some have circuitry to clear all of those buffer bytes (several megabytes) in one step. Poof.

I stand by my earlier statement but you could do this
and then draw a quad as far away as possible, that would effectively clear the buffer

Again, drawing any OpenGL primitive to fill the screen can be slower than a pure glClear.
There are several ways to omit the depth buffer clear.
Here’s one: For most games you are insided something. If you need to draw any pixel on the screen anyway and can draw the back e.g. a skybox first, use that to fill the depth buffer.
On the other hand, if the skybox has some complicated effect running, it might be better to clear the screen and render in grossly front to back order to fail more depth tests and decrease overdraw (always good). YMMV.

I dunno about your engine but I usuaully don’t clear either the colour or depth buffers as the sky box is drawn without depth testing so clears the screen and removes any old depth fragments.

The key point here being that although drawing the skybox is slower than clearing buffers, I have to draw the skybox one way or the other so I may aswell do something usfull with it.

chowe6685, i’ll try your glDepthFunc(GL_ALWAYS).
Relic, yeah drawing from front to back reduces overdraw. Anyway i ended up clearing my depth buffer.
DrDogg, thanks to your reply i realized how trivial my questions is! of course, draw something with depth testing disabled. so this geometry literaly erases graphics fragments from the last frame! duh! i cant believed i missed that.
Thanks again for all of your replies!

[This message has been edited by coder1 (edited 02-21-2004).]

The glDepthFunc(GL_ALWAYS) worked! cool.

Note to anyone reading this post. Dont get mislead that you should skip clearing the depth buffer. yes there is little overhead in clearing the depth buffer. it just happened that i wanted to avoid clearing those bits. but who knows, another coder may also find this useful.

[This message has been edited by coder1 (edited 02-21-2004).]