DirectX-启用深度缓冲后无任何显示

时间:2019-06-30 15:42:42

标签: c++ directx rendering directx-11

我试图在DirectX 11.0的渲染器中实现深度缓冲,但是遇到了特殊问题。我是DirectX的新手,所以这可能是个愚蠢的问题,但我自己无法解决。我检查了许多关于该主题的教程,每一篇都展示了如何或多或少地做到这一点。

我在现场有两个三角形。当启用深度功能时,一切都将消失,而我只有蓝屏(背景色)。

要启用深度缓冲区,我首先创建了“深度模板纹理描述”,并使用“深度模板视图”创建了“深度模板缓冲区”。然后,作为功能OMSetRenderTargets的最后一个参数,我设置了DepthStencilView。之后,我创建了“深度模具状态”。

D3D11_TEXTURE2D_DESC depthStencilTextureDesc;
depthStencilTextureDesc.Width = width;
depthStencilTextureDesc.Height = height;
depthStencilTextureDesc.MipLevels = 1;
depthStencilTextureDesc.ArraySize = 1;
depthStencilTextureDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilTextureDesc.SampleDesc.Count = 1;
depthStencilTextureDesc.SampleDesc.Quality = 0;
depthStencilTextureDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilTextureDesc.CPUAccessFlags = 0;
depthStencilTextureDesc.MiscFlags = 0;

hr = Device->CreateTexture2D(&depthStencilTextureDesc, nullptr, DepthStencilBuffer.GetAddressOf());
if (FAILED(hr))
{
    Logger::Error("Error creating depth stencil buffer!");
    return false;
}

hr = Device->CreateDepthStencilView(DepthStencilBuffer.Get(), nullptr, DepthStencilView.GetAddressOf());
if (FAILED(hr))
{
    Logger::Error("Error creating depth stencil view!");
    return false;
}

Logger::Debug("Successfully created depth stencil buffer and view.");

DeviceContext->OMSetRenderTargets(1, RenderTargetView.GetAddressOf(), DepthStencilView.Get());
Logger::Debug("Binding render target output merge successfully.");

D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));

depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;

hr = Device->CreateDepthStencilState(&depthStencilDesc, DepthStencilState.GetAddressOf());
if (FAILED(hr))
{
    Logger::Error("Error creating depth stencil state!");
    return false;
}

然后我用以下代码设置视口深度​​:

viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;

然后,我移至“渲染”功能并添加了清除深度模具和设置状态,如下所示:

...

DeviceContext->ClearDepthStencilView(DepthStencilView.Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

...

DeviceContext->OMSetDepthStencilState(DepthStencilState.Get(), 0);

而且...不起作用。如果将OMSetRenderTargets的最后一个参数从DepthStencilView.Get()更改为nullptr,它将起作用。所以好像我在深度模板上做错了什么,但我不确定是什么。我为此Renderer.cpp HERE创建了要点。请帮助我解决此问题,以防万一我被卡在其中而又不知道该怎么办。

2 个答案:

答案 0 :(得分:1)

创建深度/模板视图时,请确保“渲染目标”视图和“深度模板”视图的“采样”和“计数”的MSAA设置相同。

为MSAA目标创建DSV时可能需要其他信息。这是创建DSV的示例(请注意,我没有使用模板缓冲区,而是选择了提高深度缓冲区的精度):

//Describe our Depth/Stencil Buffer
D3D11_TEXTURE2D_DESC depthStencilDesc;

depthStencilDesc.Width = activeDisplayMode.Width;
depthStencilDesc.Height = activeDisplayMode.Height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_R32_TYPELESS;
depthStencilDesc.SampleDesc.Count = sampleLevel;
depthStencilDesc.SampleDesc.Quality = qualityLevel;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;

if (MSAAEnabled == true)
{
    //Need a DSVDesc to let it know to use MSAA
    D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
    ZeroMemory(&depthStencilViewDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
    depthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
    depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
    depthStencilViewDesc.Texture2D.MipSlice = 0;

    dev->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
    dev->CreateDepthStencilView(depthStencilBuffer, &depthStencilViewDesc, &depthStencilView);
}
else
{
    //Don't need a DSVDesc
    dev->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
    dev->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
}

答案 1 :(得分:0)

我将总结在GaleRazorwind帮助下发现的内容。我通过将depthStencilTextureDesc多重采样值设置为BufferDesc中的相同值来解决我的问题。