PDA

View Full Version : skipping the depth buffer clear



coder1
02-19-2004, 09:08 AM
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

mikael_aronsson
02-19-2004, 09:44 AM
Hi !

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

Mikael

Sektor
02-19-2004, 09:44 AM
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

coder1
02-19-2004, 09:53 AM
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
2.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

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

nexusone
02-19-2004, 10:41 AM
Is there really that much overhead in clearing the depth buffer to really make a diffrence and a need to skip it?

coder1
02-19-2004, 10:57 AM
Originally posted by nexusone:
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! http://www.opengl.org/discussion_boards/ubb/smile.gif

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.

chowe6685
02-19-2004, 11:03 AM
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?

coder1
02-19-2004, 11:25 AM
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

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).]

endash
02-19-2004, 04:08 PM
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.

chowe6685
02-19-2004, 05:41 PM
I stand by my earlier statement but you could do this
glDepthFunc(GL_ALWAYS);
and then draw a quad as far away as possible, that would effectively clear the buffer

Relic
02-20-2004, 02:48 AM
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.

Dr Dogg
02-20-2004, 05:03 AM
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.

coder1
02-21-2004, 08:33 AM
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! http://www.opengl.org/discussion_boards/ubb/smile.gif

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

update:
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. http://www.opengl.org/discussion_boards/ubb/wink.gif

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