当点具有相同深度时,opengl深度缓冲变慢 [英] opengl depth buffer slow when points have same depth

查看:110
本文介绍了当点具有相同深度时,opengl深度缓冲变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作2D游戏,涉及在屏幕上绘制大量重叠的四边形.在什么都不重要之前发生了什么.

I'm making a 2d game involving drawing huge numbers of overlapping quads to the screen. What goes in front of what doesn't really matter.

如果我用从0到z的z值绘制每个四边形,并设置了glDepthFunc(GL_LESS),我将会得到相当不错的速度提升. 这是为了避免必须绘制完全隐藏或部分隐藏在其他四边形后面的四边形. 因此,我使用类似以下内容绘制四边形:

If I draw each of my quads with z values from 0 upwards and have glDepthFunc(GL_LESS) set I get quite a nice speed boost as you would expect. This is to avoid having to draw quads which are either totally hidden or partially hidden behind other quads. So I draw the quads using something like:

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

但是,由于我不使用z值作为实际深度,所以我似乎应该能够使用:

However as I don't use the z value for actual depth it seems like I should be able to just go:

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

或仅将0.0f的z值编码到着色器中. (第三个参数是z值,并最终在着色器中不变地设置为gl_position.)

Or just code the z value of 0.0f into the shader. (the 3rd argument is the z value and ends up being set to gl_position in the shader unchanged.)

奇怪的是,第二种方法(每次我都将z值设置为0.0f)最终得到的帧率几乎不到前一种的帧率.

The strange thing is that the second method (where I set the z value to 0.0f everytime), ends up getting almost less than half the framerate of the former.

这是为什么?他们都使用glDepthFunc(GL_LESS)和

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));

一样.我认为,如果每次都将z设置为0.0f会更快.为什么不呢?

Just the same. I would think that if any setting the z to 0.0f each time would be faster. Why is it not?

推荐答案

我不是很肯定,但是我的猜测是,基元之间z值的较小增量可以使zcull硬件正常工作.这将在片段到达片段着色器之前将其剔除.除了避免片段着色器工作之外,当片段进入深度缓冲区测试时,这种剔除的发生速度可能比普通z测试更快.

I'm not positive, but my speculation is that the small delta in z values between primitives allows the zcull hardware to work. This will cull out the fragments before they get to the fragment shader. Besides avoiding the fragment shader work, this culling can happen at a faster rate than normal z-testing when the fragment makes it to the depth buffer test.

这篇关于当点具有相同深度时,opengl深度缓冲变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆