C++ 将OpenGL模具功能移植到DirectX 11
我有一些用OpenGL编写的渲染代码。我使用模具缓冲区实现剪裁:C++ 将OpenGL模具功能移植到DirectX 11,c++,directx,directx-11,stencil-buffer,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,
//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
及
如果我正确理解这些调用,第一个将设置模具状态,而第二个将pDSV作为附加“附件”绑定到渲染目标。对吗
如果是的话,这会像我预期的那样起作用吗
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(1, &pRTV, DSV);
当您使用stencil作为输入时,一件非常简单的事情就是将StencilWriteMask设置为0;
因此,它永远不会写入(这就是您要渲染剪裁几何体的内容)
如果您使用:
pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, NULL);
实际上,您将禁用任何形式的深度/模具测试(没有更多的深度限制,因此DepthStencilState将完全无效)
此外,我会使用DXGI_格式_D24_UNORM_S8_UINT for your depth FORMAT(个人偏好tho),它将完全适合您的用例,并消耗更少的内存
希望这能有所帮助。我没有机会测试这个解决方案,但是它看起来是一致的,并且与我记得的关于DX中模具的这些片段相匹配:)我会尽快提供反馈。
pd3dDeviceContext->OMSetRenderTargets(0, NULL, DSV);
pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, DSV);
pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, NULL);