在 Direct3D 中使用模板缓冲区

发布于 2024-11-10 14:07:19 字数 144 浏览 0 评论 0原文

我正在尝试使用 ID3D11Texture2D 上的模板缓冲区将一个纹理的一部分覆盖在另一个纹理上。我计划通过在模板缓冲区上绘制所需的形状然后复制纹理来实现此目的。请建议一些好的教程,其中包含 2D 纹理的示例代码,以学习模板缓冲区的使用。 (或者一些替代方法来做到这一点)

I'm trying to use stencil buffers on ID3D11Texture2D to overlay a portion of one texture on another texture. I plan to do this by drawing the desired shape on the stencil buffer and then copying the texture. Please suggest some good tutorial with sample code for 2D Textures for learning the usage of stencil buffer. (or some alternate way of doing this)

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

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

发布评论

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

评论(1

空气里的味道 2024-11-17 14:07:19

Stencil mask 101:

  • 所有主流GPU都使用8位模板缓冲区,并且它们与深度缓冲区绑定在一起,因此必须选择包含模板的缓冲区格式(主要是D24S8),
  • 以避免在每帧的开头都清除深度和模板帧间性能问题 *(参见下面的注释)
  • 除非您知道自己在做什么,否则请始终使用 0xFF 作为读写掩码。
  • 出于性能原因,在进行模板读取测试时不要启用深度写入
  • 出于性能原因,如果可能的话,请将模板读取测试限制为 ==0 和 !=0,并清除为零
  • 如果您使用的是 D3D9,您可以使用剪刀矩形与清除模板缓冲区相结合,这比手动渲染四边形以将缓冲区设置回零要快得多。
  • 无法访问着色器中的模板信息,因此如果蒙版不在普通渲染目标中并采样为纹理,则后处理效果无法确定相邻像素的模板状态。
  • 可以根据以下条件修改模板缓冲区值:
    1. 模板测试失败
    2. 深度测试失败
    3. 模板和深度测试均通过
  • 先到者为准决定动作,可以是
    1. 保留(保留模板值)
    2. 替换(用“stencilref”值替换模板值)
    3. 增量(向当前缓冲区加 1)
    4. 递减(从模板缓冲区减 1)
    5. 零(将值直接设置为零,当其他条件之一需要使用当前 stencilref 值时很有用)

*不遵循这些规则中的大多数通常会导致最坏情况的模板性能:您的像素着色器为每个执行像素,并且不对模板掩模像素执行早期拒绝。此外,在较旧的 Geforce 卡上,这还可能导致所有后续绘制调用的早期深度拒绝被破坏,直到下一帧或深度缓冲区和深度缓冲区才会修复。模板被清除。

模板蒙版实际上很容易生成:

  • 清除模板缓冲区
  • 启用模板写入的模板状态(用 stencilref 1 替换或用 stencilref 1 替换或增量而不是保留)
  • 绘制多边形。请注意,在着色器中使用“丢弃”将阻止模板更新,这就是将纹理加载到模板缓冲区中的方式(但仅作为二进制掩码)

  • 禁用模板写入,启用模板测试

  • (理想情况下)还禁用深度写入
  • 绘制需要模板遮罩的效果

Stencil mask 101:

  • All mainstream GPUs use an 8 bit stencil buffer, and they are tied to the depth buffer, so you must choose a buffer format that includes stencil (D24S8 primarily)
  • Clear depth and stencil both for the beginning of each frame to avoid inter-frame performance problems *(see note below)
  • Unless you know what you are doing, use 0xFF for the read and write mask at all times.
  • For performance reasons, not have depth-writing enabled when doing stencil-read tests
  • For performance reasons, if at all possible, keep the stencil read tests restricted to ==0 and !=0, and clear to zero
  • If you are using D3D9, you can use scissor rects combined with clearing the stencil buffer, this is quite a bit faster than rendering a quad manually to set the buffer back to zero.
  • There is no way to access stencil information in a shader, so postprocess effects can't determine the stencil state of adjacent pixels without the mask also being in a normal rendertarget and sampled as texture.
  • Stencil buffer values can be modified from the following conditions:
    1. The stencil test failed
    2. The depth test failed
    3. The stencil and depth tests both passed
  • Whichever condition is first determines the action, which can be
    1. keep (leave the stencil value alone)
    2. replace (replace the stencil value with the 'stencilref' value)
    3. increment (add 1 to the current buffer)
    4. decrement (sub 1 from the stencil buffer)
    5. zero (set the value to directly zero, useful when one of the other conditions needs to use the current stencilref value)

*Failure to follow most of these rules usually results in worst case stencil performance: Your pixel shader executing for every pixel, and not performing an early reject for the stencil masked pixels. In addition on older Geforce cards this can also result in the breaking of the early depth reject for all subsequent draw calls, which won't be fixed until the next frame or depth buffer & stencil is cleared.

Stencil masks are actually pretty easy to generate:

  • Clear the stencil buffer
  • Enable the stencil state for stencil writing ('replace' with stencilref 1 or 'increment' instead of 'keep')
  • Draw a polygon. Note that using 'discard' in the shader will prevent the stencil from being updated, which is how you load a texture into the stencil buffer (but only as a binary mask)

  • Disable stencil writes, enable stencil test

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