【问题标题】:DirectX11 Depth values only 0 and 1DirectX11 深度值只有 0 和 1
【发布时间】:2015-09-13 21:20:32
【问题描述】:

我正在将我的 OpenGL 渲染器移植到 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);

【问题讨论】:

  • 您的视口使用什么值?
  • 最大值为 1.0f,最小值为 0.0f。如果我更改这些值,则没有任何渲染,并且 DirectX 在输出窗口中显示错误。
  • 看起来不错。您确定使用正确的深度值进行渲染吗?尝试从像素着色器中输出特定的深度。
  • 我将深度计算为 position.z / position.w,并在 PS 中绑定为颜色。一切都是黑色的,所以深度始终为 0。也许这是我的顶点着色器的问题?在顶点着色器中,我将矩阵乘以 float4x4 MVP = mul(MV, P);并将顶点位置乘以计算矩阵与 mul(float4(pos.xyz, 1.0f), MVP);矩阵来自OpenGL,它们通过转置和变化符号进行转换。一切看起来都像 OpenGL,但没有深度。

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


【解决方案1】:

我找到了解决方案。问题是矩阵乘法。我将它从 C++ 移到着色器,现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多