DirectX11深度值仅为0和1

DirectX11 Depth values only 0 and 1

本文关键字:深度 DirectX11      更新时间:2023-10-16

我正在将我的OpenGL Renderer移植到DirectX。几乎一切都很好,但我有深度的问题。3D中的所有内容都呈现为平面。我已经检查了Visual Studio图形分析器和深度缓冲,状态是绑定的。如果我检查深度/模板纹理资源,只有值0。F(屏幕上的东西)和1。F(没有渲染)。使用gDEBugger检查OpenGL渲染器显示从0开始的值。从F到1。f和OpenGL工作。我还检查了错误代码。一切都返回S_OK。我能做错什么呢?

这是我的DepthStencilState创建:

                D3D11_DEPTH_STENCIL_DESC dsDescEn;
                ZeroMemory(&dsDescEn, sizeof(D3D11_DEPTH_STENCIL_DESC));
                dsDescEn.DepthEnable = TRUE;
                dsDescEn.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
                dsDescEn.DepthFunc = D3D11_COMPARISON_LESS;
                dsDescEn.StencilEnable = TRUE;
                dsDescEn.StencilReadMask = 0xFF;
                dsDescEn.StencilWriteMask = 0xFF;
                dsDescEn.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
                dsDescEn.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
                dsDescEn.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
                dsDescEn.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
                dsDescEn.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
                dsDescEn.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
                dsDescEn.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
                dsDescEn.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
                DirectX11Device::getDevice().getDevicePtr()->CreateDepthStencilState(&dsDescEn, &m_dSStateEn);

我通过OMSetDepthStencilState绑定State。

D3D11_TEXTURE2D_DESC depthStencilDesc;
                    depthStencilDesc.Width = width;
                    depthStencilDesc.Height = height;
                    depthStencilDesc.MipLevels = 1;
                    depthStencilDesc.ArraySize = 1;
                    depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
                    depthStencilDesc.SampleDesc.Count = 1;
                    depthStencilDesc.SampleDesc.Quality = 0;
                    depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
                    depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
                    depthStencilDesc.CPUAccessFlags = 0;
                    depthStencilDesc.MiscFlags = 0;
                    D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
                    ZeroMemory(&depthStencilViewDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
                    depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
                    depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
                    depthStencilViewDesc.Texture2D.MipSlice = 0;
                    DirectX11Device::getDevice().getDevicePtr()->CreateTexture2D(&depthStencilDesc, NULL, &m_stencilDepthBuff);
                    DirectX11Device::getDevice().getDevicePtr()->CreateDepthStencilView(m_stencilDepthBuff, &depthStencilViewDesc, &m_stencilDepthView);

我找到了解决办法。问题是矩阵乘法。我把它从c++移到shader中,现在它可以正常工作了。