OpenGL 模板缓冲区 OR 操作?

发布于 2024-09-24 22:18:47 字数 725 浏览 13 评论 0原文

我不确定这是否可行,但值得一试。我使用模板缓冲区来减少使用此算法的延迟渲染器中光体积的过度绘制(当相机位于体积之外时):

  1. 使用便宜的着色器,将深度测试设置为 LEQUAL 绘制背面,将它们标记在模板中缓冲。
  2. 使用昂贵的照明着色器,绘制正面并将深度测试设置为 GEQUAL。

这将导致仅光体积内的像素被着色。当绘制多个灯光时就会出现这个问题。首先,由于状态更改的成本很高,因此为每个灯光在廉价和昂贵的着色器之间重复切换可能不是最好的主意。理想情况下,我希望通过使用廉价着色器渲染 8 个光照体积,然后使用昂贵的着色器渲染 8 个光照体积,来充分利用模板缓冲区的所有 8 位。然而,当灯光重叠时就会出现问题,因为无法区分哪些像素属于哪些灯光。

我想到的解决方案是在每个灯光的模板缓冲区中使用 1 位。因此,对于光n,在廉价通道中标记模板缓冲区中的第n位,然后在昂贵通道期间仅渲染该位打开的像素。

我以前没有使用过模板缓冲区,但从我读到的内容来看,这似乎不可能。为此,我必须使用按位“或”设置模板缓冲区,并且模板函数必须按位“与”。然而,我能看到的模板缓冲区上的唯一操作是:KEEP、ZERO、REPLACE、INCR、DECR 和 INVERT,唯一的函数是:NEVER、ALWAYS、LESS、EQUAL、LEQUAL、GEQUAL、GREATER 和 NOTEQUAL。

有什么方法可以使用模板缓冲区以某种方式获得这种“或”和“与”行为吗?如果没有,是否有其他方法可以有效渲染光体积?

I'm not sure if this is possible to do, but it's worth a shot. I'm using the stencil buffer to reduce overdraw for light volumes in a deferred renderer using this algorithm (when the camera is outside the volume):

  1. Using a cheap shader, draw back faces with depth testing set to LEQUAL, marking them in the stencil buffer.
  2. Using the expensive lighting shader, draw front faces with depth testing set to GEQUAL.

This will cause only pixels within the light volume to be shaded. The problem with this comes when drawing multiple lights. First, since state changes are expensive, it's probably not the best idea to repeatedly switch between the cheap and expensive shader for each light. Ideally I'd like to take advantage of all 8 bits of the stencil buffer by rendering 8 light volumes with the cheap shader followed by 8 light volumes with the expensive shader. However, issues arise when lights overlap, as there's no way to tell which pixels belong to which lights.

The solution that comes to mind would be to use 1 bit in the stencil buffer per light. So, for light n, mark the nth bit in the stencil buffer in the cheap pass, then only render pixels with that bit on during the expensive pass.

I haven't used the stencil buffer before, but from what I'm reading this doesn't seem possible. For this to work I'd have to set the stencil buffer using bitwise OR and the stencil function would have to be bitwise AND. However, the only operations on the stencil buffer I can see are: KEEP, ZERO, REPLACE, INCR, DECR, and INVERT, and the only functions are: NEVER, ALWAYS, LESS, EQUAL, LEQUAL, GEQUAL, GREATER, and NOTEQUAL.

Is there any way to somehow get this OR and ANDing behavior using the stencil buffer? And if not, is there an alternative approach to efficiently rendering light volumes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-10-01 22:18:47

要获得 ORing 更新行为,请结合使用 glStencilMaskGL_REPLACE。要获得 ANDing 测试行为,请结合使用 glStencilFunc mask 参数和 GL_EQUAL。在这两种情况下,您都希望 glStencilFuncref 参数为 0xff

To get ORing update behavior, use glStencilMask combined with GL_REPLACE. To get ANDing test behavior, use the glStencilFunc mask parameter combined with GL_EQUAL. In both cases, you'll want the ref parameter to glStencilFunc to be 0xff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文