在OpenGL中,如何在两个深度缓冲区之间进行深度测试? [英] In OpenGL, (how) can I depth test between two depth buffers?

查看:178
本文介绍了在OpenGL中,如何在两个深度缓冲区之间进行深度测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个深度缓冲区("DA"和"DB").我想将DA附加到帧缓冲区,并以正常/传统"方式对其进行渲染(针对GL_LESS的每个片段深度测试,如果通过,则将其绘制,并将其深度写入深度缓冲区). /p>

然后,我想附加数据库-在这里我想以传统的方式引用/使用数据库,而且还要使用GL_GREATER针对DA进行深度测试.如果该片段通过了这两个测试,那么我仍然只想将深度写入DB.

此操作将完成第二遍中绘制的所有内容,只有在第一遍中内容 之后,而中其他任何内容的前面,第二遍.

OpenGL 2.1(或OpenGL ES 2)是否提供任何此功能?我该怎么做才能解决它没有正式提供的问题?

如果它不提供它,这是我要怎么做的一个主意,那就是我想进行健全性检查...

您将附加一个充当深度缓冲区的渲染缓冲区,然后手动填充深度.并手动执行如果深度大于DA处的纹理像素"测试(失败时,丢弃片段).我很好奇我会遇到什么问题?有没有一种简单的方法可以模拟正式指定的深度缓冲区的深度精度?演出会很糟糕吗?还有其他问题吗?

解决方案

我不相信有一种方法可以拥有多个深度缓冲区.我以为可能至少有特定于供应商的扩展,但是即使如此,通过扩展注册表进行的搜索还是空的.

您在上一段中已经开始考虑的内容似乎是想到的唯一相当简单的选项.您将深度纹理用作第二个深度缓冲区,并根据片段与片段着色器中深度纹理的值的比较来丢弃片段.

您不必手动填充深度纹理.如果深度值是通过较早的渲染过程生成的,则可以使用glCopyTexImage2D()将其从主深度缓冲区复制到深度纹理. OpenGL 2.1中提供了此功能.在较新的版本中,您也可以使用glBlitFramebuffer()执行复制.

关于您的担忧:

  • 精度是一个非常有效的问题.当我过去玩类似的游戏时,我遇到了一些相当严重的精度问题.它们可能可以解决,但是您绝对需要提防它,并仔细考虑您的精度要求是什么,以及在必要时如何弄乱这些值.

  • 我当然希望性能会有所下降.固定功能深度缓冲区具有大量优化的优势,例如早期深度测试,这是自制"深度缓冲区无法提供的.不过,我不一定希望它会太可怕.如果需要的话,值得尝试.

我认为这至少在ES 2.0中是不可行的,至少不依赖扩展.您可能会考虑将深度值写入颜色纹理中,但我认为这样做会非常难看且效率低下. ES 2.0非常简单.但是对于您计划要做的事情,您已经超出了预期的功能集. ES 3.0添加了深度纹理以及其他可能发挥作用的功能.

Say I have two depth buffers ("DA" and "DB"). I'd like to attach DA to a framebuffer, and render to it in the normal/"traditional" way (every fragment depth tests against GL_LESS, if it passes, it is drawn and it's depth is written to the depth buffer).

Then, I'd like to attach DB- here I'd want to reference/use DB in the traditional way, but also depth test against DA with GL_GREATER. If the fragment passes both tests, I'd still just like to write depth to DB.

What this would accomplish is anything drawn in the second pass would only draw if it is behind the contents of the first pass, but in front of any other contents in the second pass.

Does OpenGL 2.1 (or OpenGL ES 2) offer any of this functionality? What could I do to work around it not formally offering it?

If it doesn't offer it, here's an idea for how I'd do it, that I would like to be sanity checked...

You attach a render buffer that acts as a depth buffer, then manually populate it with depths. And manually perform the "if depth greater than texel at DA" test (when failing, discard the fragment). I'm curious what problems I'd run into? Is there an easy way to emulate the depth precision of formally specified depth buffers? Would the performance be awful? Any other concerns?

解决方案

I don't believe there is a way to have multiple depth buffers. I thought there might at least be vendor specific extensions, but a search through the extension registry came up empty even for that.

What you already started considering in the last paragraph looks like the only reasonably straightforward option that comes to mind. You use a depth texture as your second depth buffer, and discard fragments based on their comparison with the value from the depth texture in your fragment shader.

You don't have to manually populate the depth texture. If the depth values are generated as the result of an earlier rendering pass, you can copy them from the primary depth buffer to a depth texture using glCopyTexImage2D(). This function is available in OpenGL 2.1. In newer versions, you can also use glBlitFramebuffer() to perform the copy.

On your concerns:

  • Precision is a very valid concern. I had some fairly severe precision issues when I played with something similar in the past. They can probably be solved, but you definitely need to watch out for it, and carefully think about what your precision requirements are, and how you can fudge the values if necessary.

  • I would certainly expect performance to suffer somewhat. The fixed function depth buffer has the advantage of heavy optimizations, like early depth testing, that you won't get with your "home made" depth buffer. I wouldn't necessarily expect it to be horrible, though. And if this is what you need, it's worth trying.

I think this is pretty much a no go in ES 2.0, at least without relying on extensions. You could potentially consider writing depth values into color textures, but I think it would get very ugly and inefficient. ES 2.0 is great for its simplicity. But for what you're planning to do, you're exceeding its intended feature set. ES 3.0 adds depth textures, and some other features that could come into play.

这篇关于在OpenGL中,如何在两个深度缓冲区之间进行深度测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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