深度模具缓冲区不工作directx11

深度模具缓冲区不工作directx11,directx,buffer,direct3d,depth,Directx,Buffer,Direct3d,Depth,好的,我试了所有的方法,但我真的迷路了 ID3D11Texture2D* depthStencilTexture; D3D11_TEXTURE2D_DESC depthTexDesc; ZeroMemory (&depthTexDesc, sizeof(D3D11_TEXTURE2D_DESC)); depthTexDesc.Width = set->mapSettings["SCREEN_WIDTH"]; depthTexDesc.Height = set->mapSet

好的,我试了所有的方法,但我真的迷路了

ID3D11Texture2D* depthStencilTexture;

D3D11_TEXTURE2D_DESC depthTexDesc;
ZeroMemory (&depthTexDesc, sizeof(D3D11_TEXTURE2D_DESC));
depthTexDesc.Width = set->mapSettings["SCREEN_WIDTH"];
depthTexDesc.Height = set->mapSettings["SCREEN_HEIGHT"];
depthTexDesc.MipLevels = 1;
depthTexDesc.ArraySize = 1;
depthTexDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthTexDesc.SampleDesc.Count = 1;
depthTexDesc.SampleDesc.Quality = 0;
depthTexDesc.Usage = D3D11_USAGE_DEFAULT;
depthTexDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthTexDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
depthTexDesc.MiscFlags = 0;

mDevice->CreateTexture2D(&depthTexDesc, NULL, &depthStencilTexture);

D3D11_DEPTH_STENCIL_DESC dsDesc;
// Depth test parameters
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;//LESS

// Stencil test parameters
dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing
dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; //KEEP
dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; //INCR
dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; //KEEP
dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing
dsDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; //KEEP
dsDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; //DECR
dsDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; //KEEP
dsDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Create depth stencil state
mDevice->CreateDepthStencilState(&dsDesc, &mDepthStencilState);

D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory (&depthStencilViewDesc, sizeof(depthStencilViewDesc));
depthStencilViewDesc.Format = depthTexDesc.Format;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;

mDevice->CreateDepthStencilView(depthStencilTexture, &depthStencilViewDesc, &mDepthStencilView);

mDeviceContext->OMSetDepthStencilState(mDepthStencilState, 1);
然后我打电话

mDeviceContext->OMSetRenderTargets(1, &mTargetView, mDepthStencilView);
显然,我在每一帧之前都会清洗

mDeviceContext->ClearRenderTargetView(mTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f));
mDeviceContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );
但它仍然只保留最后一个像素,不需要测试

PS我已经检查了光栅化器,它只正确地绘制了正面


有任何帮助吗?

检查您的HRESULT-调用CreateTexture2D几乎肯定会失败,因为您在默认纹理上指定了CPU\U访问标志。由于您从不检查任何错误或指针,这只会将NULL传播到所有深度对象,从而有效地禁用深度测试

您还可以通过启用D3D调试层、将D3D11_创建_设备_调试添加到D3D11CreateDevice上的标志来捕获类似的错误。如果执行了此操作,您将看到以下调试喷出:

D3D11错误:ID3D11Device::CreateTexture2D:D3D11的默认用法 资源不能设置任何CPUAccessFlags。以下 在这种情况下,无法设置CPUAccessFlags位:D3D11\u CPU\u访问\u读取 (1) ,D3D11_CPU_访问_写入(1)。[状态#创建错误#98: CREATETEXTURE2D_INVALIDCPUACCESSFLAGS]


你到底想做什么?您已将模具缓冲区设置为始终通过。。。