c++ - DirectX11 深度值只有 0 和 1

标签 c++ directx directx-11 depth-buffer

我正在将我的 OpenGL 渲染器移植到 DirectX。几乎一切正常,但我对深度有疑问。 3D 中的所有内容都呈现为平面。我已经检查了 Visual Studio 图形分析器和深度缓冲区,并且状态已绑定(bind)。如果我检查 Depth/Stencil Texture Resource,只有值 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 绑定(bind)状态。

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++ 移至着色器,现在它可以正常工作。

关于c++ - DirectX11 深度值只有 0 和 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554665/

相关文章:

c++ - 如何使用 range-v3 将 vector 转换为新 vector ?

c++ - C/C++ 帮助在 C 中处理多个文件

c++ - EasyPHP MySQL 无法启动

c++ - fbx 网格负载几何变换

windows - D3D11_USAGE_STAGING,使用什么样的GPU/CPU显存?

c++ - 尝试使用 DirectXTK 创建 GamePad 时出现错误 LNK2019

c++ - shader.hlsl 文件导致错误?

c++ - 如何直接有效地访问非常大的文本文件?

directx - 为 Windows 8 应用程序调试 HLSL

c++ - 如何将世界坐标转换为屏幕坐标