简体   繁体   English

DirectX11深度值仅0和1

[英]DirectX11 Depth values only 0 and 1

I'm porting my OpenGL Renderer to DirectX. 我正在将OpenGL渲染器移植到DirectX。 Almost everything work fine, but I have a problem with depth. 几乎一切都可以正常工作,但是我对深度有疑问。 Everything in 3D is rendering as flat. 3D中的所有内容都呈现为平面。 I've checked Visual Studio Graphics Analyzer and Depth Buffer and State is bind. 我已经检查了Visual Studio图形分析器和深度缓冲区和状态绑定。 If I check Depth/Stencil Texture Resource, there are only values 0.f (where something is on screen) and 1.f (where nothing is rendered). 如果我检查“深度/模板纹理资源”,则只有值0.f(屏幕上显示某些内容)和1.f(未渲​​染任何内容)。 OpenGL renderer checked with gDEBugger shows values from 0.f to 1.f and OpenGL is working. 用gDEBugger检查的OpenGL渲染器显示的值从0.f到1.f,并且OpenGL正在工作。 I've also checked error codes. 我还检查了错误代码。 Everything returns S_OK. 一切都返回S_OK。 What could I do wrong? 我该怎么办?

Here is my DepthStencilState creation: 这是我的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);

I'm binding State via OMSetDepthStencilState. 我通过OMSetDepthStencilState绑定状态。

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);

I've found a solution. 我找到了解决方案。 The problem was matrices multiplication. 问题是矩阵乘法。 I moved it to shader from C++ and now it's working without problems. 我将其从C ++移到了着色器,现在它可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM