gpt4 book ai didi

c++ - Direct3D 11 深度模板/alpha 混合问题

转载 作者:行者123 更新时间:2023-11-28 01:48:46 25 4
gpt4 key购买 nike

我一直在为一款游戏开发 3D 渲染器,直到现在它首先渲染所有无纹理网格,然后使用 DrawIndexed 渲染所有纹理网格。为了提高性能,我切换到 DrawIndexedInstanced 并使其首先渲染纹理网格,这揭示了我的 alpha 混合和/或深度检查设置方式的问题。以下图片应说明问题所在:

View through the top of the front-most textures (textured meshes rendered first)

The same view, slightly different angle (textureless meshes rendered first)

前景和背景是一排排带纹理的矩形网格,前景是部分透明的网格。中间一行是无纹理网格,其透明度设置为 0.3f。当首先渲染带纹理的网格时,无纹理的网格会被前景中的透明网格遮挡。然而,当首先渲染无纹理网格时,它们会完全遮住后面的纹理网格,即使它们的透明度为 0.3f。当无纹理网格遮挡其他无纹理网格时,不会发生这种情况,alpha 混合在这种情况下可以正常工作。

这是我设置光栅器状态、深度模板状态和深度模板 View 的地方:

ID3D11Texture2D *pBackBuffer;
D3D11_TEXTURE2D_DESC backBufferDesc;
m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
pBackBuffer->GetDesc(&backBufferDesc);
RELEASE_RESOURCE(pBackBuffer);

// creating a buffer for the depth stencil
D3D11_TEXTURE2D_DESC depthStencilBufferDesc;
ZeroMemory(&depthStencilBufferDesc, sizeof(D3D11_TEXTURE2D_DESC));

depthStencilBufferDesc.ArraySize = 1;
depthStencilBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilBufferDesc.CPUAccessFlags = 0; // No CPU access required.
depthStencilBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilBufferDesc.Width = backBufferDesc.Width;
depthStencilBufferDesc.Height = backBufferDesc.Height;
depthStencilBufferDesc.MipLevels = 1;
depthStencilBufferDesc.SampleDesc.Count = 4;
depthStencilBufferDesc.SampleDesc.Quality = 0;
depthStencilBufferDesc.Usage = D3D11_USAGE_DEFAULT;
m_device->CreateTexture2D(&depthStencilBufferDesc, NULL, &m_depthStencilBuffer);

// creating a depth stencil view
HRESULT hr = m_device->CreateDepthStencilView( m_depthStencilBuffer,
NULL,
&m_depthStencilView);


// setup 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;

hr = m_device->CreateDepthStencilState(&depthStencilStateDesc, &m_depthStencilState);

// setup rasterizer state.
D3D11_RASTERIZER_DESC rasterizerDesc;
ZeroMemory(&rasterizerDesc, sizeof(D3D11_RASTERIZER_DESC));

rasterizerDesc.AntialiasedLineEnable = FALSE;
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 = FALSE;
rasterizerDesc.ScissorEnable = FALSE;
rasterizerDesc.SlopeScaledDepthBias = 0.0f;

// create the rasterizer state
hr = m_device->CreateRasterizerState(&rasterizerDesc, &m_RasterizerState);

m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView);
m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1);
m_deviceContext->RSSetState(m_RasterizerState);

这是我启用 alpha 混合的地方:

D3D11_BLEND_DESC blendDescription;
ZeroMemory(&blendDescription, sizeof(D3D11_BLEND_DESC));
blendDescription.RenderTarget[0].BlendEnable = TRUE;
blendDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
m_device->CreateBlendState(&blendDescription, &m_blendState);
m_deviceContext->OMSetBlendState(m_blendState, 0, 0xffffffff);

我知道给无纹理网格一个纯白色完全不透明的纹理会在某种程度上解决这个问题,但我怀疑深度测试有问题。

当我使用 D3D11_CREATE_DEVICE_DEBUG 标志创 build 备时,它没有给我任何错误或警告。

Create 函数返回的所有 HRESULT 都是 S_OK。

提前致谢。

最佳答案

要使混合起作用,您必须先渲染所有完全不透明的对象,然后按从后到前的顺序渲染所有具有透明度的对象。这意味着您的透明物体会根据与相机的距离进行排序,较远的物体在前。

理想情况下,不透明对象按相反方向(从前到后)排序,以便深度测试丢弃被遮挡的像素。

这通常是通过将所有绘制请求放入队列中来完成的。一旦场景中的所有内容都在队列中,您可以根据各种因素(包括透明度、距离、 Material 等)对其进行排序。然后您可以遍历队列并以正确的顺序发出所有绘制请求。

但对于简单的情况,只需确保首先绘制不透明对象,然后按照一般从后到前的顺序绘制透明对象。

关于c++ - Direct3D 11 深度模板/alpha 混合问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43815438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com