Directx 如何在深度测试中考虑 alpha 混合

Directx how to account for alpha blending in depth test

我正在尝试实现一种简单的方法来在我的玩具应用程序中渲染粒子。我正在使用 alpha 混合渲染广告牌四边形,但这会导致深度模板问题,其中完全透明的四边形部分仍然遮挡它们后面的粒子,因为它们未能通过深度测试。结果如下所示:

这是我设置管道的方式:

// rasterizer
D3D11_RASTERIZER_DESC rasterizerDesc;
rasterizerDesc.AntialiasedLineEnable = true;
rasterizerDesc.CullMode = D3D11_CULL_BACK;
rasterizerDesc.DepthBias = 0;
rasterizerDesc.DepthBiasClamp = 0.0f;
rasterizerDesc.DepthClipEnable = true;
rasterizerDesc.FillMode = D3D11_FILL_SOLID;
rasterizerDesc.FrontCounterClockwise = false;
rasterizerDesc.MultisampleEnable = true;
rasterizerDesc.ScissorEnable = false;
rasterizerDesc.SlopeScaledDepthBias = 0.0f;

// depth stencil state
D3D11_DEPTH_STENCIL_DESC depthStencilStateDesc;
ZeroMemory(&depthStencilStateDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
depthStencilStateDesc.DepthEnable = TRUE;
depthStencilStateDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilStateDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilStateDesc.StencilEnable = FALSE;

// blend state
D3D11_BLEND_DESC blendStateDescription;
ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC));

blendStateDescription.AlphaToCoverageEnable = FALSE;
blendStateDescription.IndependentBlendEnable = FALSE;
blendStateDescription.RenderTarget[0].BlendEnable = TRUE;
blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

我已经搜索了一段时间,但不确定如何设置深度模板以在此处正确使用 alpha 混合,或者这是否是正确的方法。查看 render doc capture 它并不关心透明度:

不确定这里还有哪些其他设置可能很重要,所以请在评论中告诉我,我将包括与此案例相关的任何其他设置。

我终于明白了。简而言之,没有自动的方法来处理这个问题。一个简单的实现是先绘制不透明对象,同时写入深度缓冲区,然后仅通过深度测试绘制透明对象,而不写入深度模板。透明物应按深度排序,并从距视点最远到最近呈现。

跟进之前的图片: