c++ - 将 OpenGL 模板功能移植到 DirectX 11

标签 c++ directx directx-11 stencil-buffer

我有一些用 OpenGL 编写的渲染代码。我使用模板缓冲区来实现裁剪:


    //Let's assume this is done in render loop.

    if(!already_created())
    {
      create_stencil_attachment_and_bind_to_FB_as_depth_stencil_attachment();
    }

    glEnable(GL_STENCIL_TEST);
    glColorMask(0,0,0,0);
    glDepthMask(0);
    glClearStencil(0);
    glClear(GL_STENCIL_BUFFER_BIT);
    glStencilFunc(GL_ALWAYS,1,1);
    glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);

    render_to_stencil();

    glColorMask(1,1,1,1);
    glDepthMask(1);
    glStencilFunc(GL_EQUAL,1,1);
    glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);

    render_with_clipping();

    glDisable(GL_STENCIL_TEST);

现在的问题是,我需要将此代码移植到 DX11。我在 MSDN 上看到了示例和一些不错的教程。我最终得到了这个逻辑:

1. Create ID3D11Texture2D with format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT.
2. Create ID3D11DepthStencilState for rendering to stencil: //Let's call it DS_RENDER
   - For both front and back faces:
     - op = D3D11_STENCIL_OP_REPLACE for all 3 cases
     - func = D3D11_COMPARISON_ALWAYS
   - DepthEnable = FALSE
   - StencilEnable = TRUE
   - StencilReadMask = 0xFF
   - StencilWriteMask = 0xFF
3. Create ID3D11DepthStencilView for state and texture created before. //Let's call it DSV
4. Create ID3D11DepthStencilState for using stencil as 'input': //Let's call it DS_CLIP
   - For both front and back faces:
     - op = D3D11_STENCIL_OP_KEEP for all 3 cases
     - func = D3D11_COMPARISON_EQUAL
   - DepthEnable = FALSE
   - StencilEnable = TRUE
   - StencilReadMask = 0xFF
   - StencilWriteMask = 0xFF

Now, I'm not sure how do I set stencil as target or input.

MSDN says:

`pDevice->OMSetDepthStencilState(pDSState, 1);`

`pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, pDSV);`

如果我正确理解这些调用,第一个设置模板状态,而第二个将 pDSV 作为附加“附件”绑定(bind)到渲染目标。对吗?

如果是这样,这会像我预期的那样工作吗?



    pDevice->OMSetDepthStencilState(DS_RENDER, 1);
    pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, DSV);

    render_geometry_to_stencil_buffer();

    pDevice->OMSetDepthStencilState(DS_CLIP, 1);

    render_geometry_with_clipping();

    pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, NULL); //Does this disable stencil testing?

预先感谢您提供的每一个帮助或有用的提示。

最佳答案

如果你只想渲染到模板,使用(设置你的写作状态):

pd3dDeviceContext->OMSetRenderTargets(0, NULL, DSV);

你不需要渲染到颜色缓冲区,所以不需要绑定(bind)它。

然后渲染到你的目标并启用模板测试,使用:

pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, DSV);

当你使用stencil作为输入时,一个很简单的事情也是设置StencilWriteMask = 0; 所以它永远不会写入它(这就是你想要呈现剪裁几何体的内容)。

如果您使用:

pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, NULL); 

您确实会禁用任何形式的深度/模板测试(不再有深度限制,因此 DepthStencilState 将完全无效)。

此外,我会为您的深度格式使用 DXGI_FORMAT_D24_UNORM_S8_UINT(个人偏好),它将完全适合您的用例并且消耗更少的内存。

希望对您有所帮助。

关于c++ - 将 OpenGL 模板功能移植到 DirectX 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19320308/

相关文章:

c++ - 标准是否定义了 `a[i]` 的类型,其中 `a` 是 `T [M][N]`?

c++ - Qt - 查找鼠标屏幕 x,y - 即使应用程序已关闭

c++ - 否定一个概念 (C++20)

c++ - 使用并行处理在 C++ 中制作游戏

c++ - 向 Texture2D 复制和从 Texture2D 复制的最快方式

c++ - DirectX::SpriteFont/SpriteBatch 阻止绘制 3D 场景

c++ - 使用计算着色器处理渲染目标

c++ - TwoDimensionalShape 类应该包含什么?

windows - 如何在 Windows 游戏上叠加图形?

C++ 网格在不应该的时候变得动画化