在 OMSetRenderTarget 中使用深度模板视图后,DirectX 3d 11 根本没有渲染任何内容

问题描述 投票:0回答:1

我已经尝试这个好几个小时了。我不知道为什么三角形没有在屏幕上渲染。

    D3D11_DEPTH_STENCIL_DESC dsDesc = {};
    dsDesc.DepthEnable = true;
    dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    dsDesc.DepthFunc = D3D11_COMPARISON_LESS;

    if (FAILED(pDevice->CreateDepthStencilState(&dsDesc, &pDSState)))
    {
        this->Release();
        return false;
    }

    pDeviceContext->OMSetDepthStencilState(pDSState, 1u);
    

    D3D11_TEXTURE2D_DESC descDepth = {};
    descDepth.Width = 800u;
    descDepth.Height = 600u;
    descDepth.MipLevels = 1u;
    descDepth.ArraySize = 1u;
    descDepth.Format = DXGI_FORMAT_D32_FLOAT;
    descDepth.SampleDesc.Count = 1u;
    descDepth.SampleDesc.Quality = 0u;
    descDepth.Usage = D3D11_USAGE_DEFAULT;
    descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags = 0;
    descDepth.MiscFlags = 0;

    if (FAILED(pDevice->CreateTexture2D(&descDepth,NULL,&pDepthStencil)))
    {
        this->Release();
        return false;
    }

    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV = {};
    descDSV.Format = DXGI_FORMAT_D32_FLOAT;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0u;
    if (FAILED(pDevice->CreateDepthStencilView(pDepthStencil, &descDSV,&pDSV)))
    {
        this->Release();
        return false;
    }
    

    pDeviceContext->OMSetRenderTargets(1u, &pTarget,pDSV);

如果我不放置 pDSV 值并将 NULL 放置在其位置,则在上述代码片段的末尾我确实会看到一个三角形。

而且我肯定会通过清除深度模板来清除缓冲区,如下所示

    const float color[] = { r,g,b,1 };
    this->pDeviceContext->ClearRenderTargetView(this->pTarget, color);
    this->pDeviceContext->ClearDepthStencilView(this->pDSV, D3D11_CLEAR_DEPTH, 1.0f, 0u);

你可能会说,你没有设置视口,但我也在设置它。

    D3D11_VIEWPORT vp;
    vp.Width = 800;
    vp.Height = 600;
    vp.MinDepth = 0.0f;
    vp.MaxDepth = 1.0f;
    vp.TopLeftX = 0;
    vp.TopLeftY = 0;
    graphics->pDeviceContext->RSSetViewports(1u, &vp);

现在看在上帝的份上,谁能告诉我为什么我的眼睛看不到屏幕上的三角形。我哪里做错了?是硬件要求还是我不知道的某个设置。 不,我没有使用多个样本,我对此也不知道。

我也想启用深度缓冲区。我尝试阅读文档,也尝试查看视频,还搜索了调试图形的方法。在 Visual Studio 2019 中通过帧捕获。但我没有明白我哪里出错了。

这是我的 github 存储库的链接,其中包含所有代码文件

directx directx-11 depth-buffer direct3d11
1个回答
0
投票

好的我发现了问题。事实是我忽略了调试器它所说的内容。但这是一个奇怪的错误。调试器说

插槽 0 处的 RenderTargetView 与 DepthStencilView 不兼容。如果视图的有效尺寸以及资源类型、多重采样计数和多重采样质量相等,则 DepthStencilView 只能与 RenderTargetView 一起使用。插槽 0 处的 RenderTargetView 具有 (w:804,h:604,as:1),而资源是具有 (mc:1,mq:0) 的 Texture2D。 DepthStencilView 具有(w:800,h:600,as:1),而资源是具有(mc:1,mq:0)的Texture2D。 [ STATE_SETTING 错误#388:OMSETRENDERTARGETS_INVALIDVIEW]

我不知道为什么它创建的 RenderTargetView 是 804 / 604 。我只是通过编辑交换链描述符强制它具有 800 / 600 尺寸。调试器还抛出了一个异常,我认为 IDE(Visual Studio 2019)有点奇怪。我可能想知道为什么它首先创建尺寸完全不同的 renderTarget 。确实与窗户样式有关,或者我认为是的,但我想知道为什么,如果有人能告诉天气,那是因为窗户样式,或者我不知道。

© www.soinside.com 2019 - 2024. All rights reserved.