Skip to main content
GameDev.net gamedev.net
🔒 Locked

Speedier depth buffer?

Started by Puzzler183 Jan 27, 2004 at 7:18 PM 9 replies 650+ views
Original Post
Puzzler183
Puzzler183
Uhm, just curious about something: if I switch the near and far of the depth buffer every frame along with the depth test function, would that be faster than clearing it every frame, or does it not matter on todays hardware?
Puzzler183
Puzzler183
Provided you switch the depth testing function every frame with it, I think it should work, but I can''t test for lack of a compiler...
Sneftel
Sneftel
Let''s say that in frame 1 you drew an object in the center of the screen that had a Z-value of 0.5. Then in the second frame, that first object was gone, and instead you had a different object in the center that had a Z value, with the new planes, of 0.4. It wouldn''t be drawn, even though the first object was gone.

Besides, clearing buffers is really really fast.


"Sneftel is correct, if rather vulgar." --Flarelocke
bzroom
bzroom
glReadPixels
Puzzler183
Puzzler183
Meh, I''m going to try it because I don''t think my idea quite got accross (not only that, but it was a little flawed, and so that is fixable)... Instead of just switching the planes, you have to switch the sign along with the depth function in both cases. So I''ll test it with FPS showing and see what I get, and if it''s faster, post the code and results.
Sneftel
Sneftel
Dude, it won''t work. Trust me on this. Without clearing the Z-buffer to the backmost value each frame, you can''t be guaranteed that stuff that should be drawn, will be drawn.

If you''re still insistent on trying it, do a spinning cube. That''ll show the problem pretty clearly.


"Sneftel is correct, if rather vulgar." --Flarelocke
theNestruo
theNestruo
I think this is what you're looking for...
quote:

SIGGRAPH '97
Course 24: OpenGL and Windows System Integration
OpenGL Performance Optimization


Eliminate depth buffer clearing
If the scene you're drawing covers the entire window there is a trick which let's you omit the depth buffer clear. The idea is to only use half the depth buffer range for each frame and alternate between using GL_LESS and GL_GREATER as the depth test function.
Example:

int EvenFlag;

/* Call this once during initialization and whenever the window
* is resized.
*/

void init_depth_buffer( void )
{
glClearDepth( 1.0 );
glClear( GL_DEPTH_BUFFER_BIT );
glDepthRange( 0.0, 0.5 );
glDepthFunc( GL_LESS );
EvenFlag = 1;
}

/* Your drawing function */
void display_func( void )
{
if (EvenFlag) {
glDepthFunc( GL_LESS );
glDepthRange( 0.0, 0.5 );
}
else {
glDepthFunc( GL_GREATER );
glDepthRange( 1.0, 0.5 );
}
EvenFlag = !EvenFlag;

/* draw your scene */
}



Copied and pasted from here

[EDIT: horrible format and link not working fixed]
theNestruo

Syntax error in 2410
Ok

[edited by - theNestruo on January 28, 2004 7:46:07 PM]
theNestruoSyntax error in 2410Ok
_the_phantom_
_the_phantom_
note that was from 97 and since then things have moved on a bit hardware wise, infact both ATI and nVidia recomend clearing the z-buffer over trying to do clever tricks
theNestruo
theNestruo
_the_phantom_: Yeah, the article is really dated (and I don''t like that tricky things at all ). I just posted it (trying) to enlight the original poster about his algorithm.
theNestruoSyntax error in 2410Ok

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.