简体   繁体   中英

DirectX11 Depth values only 0 and 1

I'm porting my OpenGL Renderer to DirectX. Almost everything work fine, but I have a problem with depth. Everything in 3D is rendering as flat. I've checked Visual Studio Graphics Analyzer and Depth Buffer and State is bind. 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). OpenGL renderer checked with gDEBugger shows values from 0.f to 1.f and OpenGL is working. I've also checked error codes. Everything returns S_OK. What could I do wrong?

Here is my DepthStencilState creation:

                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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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