Opengl depth buffer is slow when points have the same depth

I am making a 2d game with huge numbers of overlapping squares on the screen. What happens before that doesn't matter.

If I draw each of my quads with z values ​​from 0 upwards and set glDepthFunc (GL_LESS) I get pretty good speed boost as you'd expect. This is to avoid having to draw squares that are either completely hidden or partially hidden behind other quads. So I am drawing quads using something like:

float small = (float(1)/1000000);
for (int iii = 0; iii < 100000; iii++) {
    freeSpace = bullets[iii]->draw(opengl, freeSpace, iii*small);
}

      

However, since I am not using the z value for the actual depth, it seems that I should just go:

for (int iii = 0; iii < 100000; iii++) {
    freeSpace = bullets[iii]->draw(opengl, freeSpace, 0.0f);
}

      

Or just copy the z value 0.0f into the shader. (The 3rd argument is the z value and this sets the gl_position value in the shader unchanged.)

The weird thing is that the second method (where I set the z value to 0.0f each time) ends up getting almost half the frame rate of the first.

Why is this? They both use glDepthFunc (GL_LESS) and

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 100000*(2*3));

      

Same. I would have thought that if any setting z to 0.0f would be faster every time. Why is it wrong?

+3


source to share


1 answer


I'm not sure, but my guess is that a small delta in z values ​​between primitives allows the zcull apparatus to work. This will discard the fragments before they get into the fragment shader. In addition, to avoid the fragment shader working, this culling can be faster than regular z-testing when the fragment does it on a depth test buffer.



+3


source







All Articles