深度模板缓冲区不能正常工作x11

时间:2013-10-05 15:07:51

标签: 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->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 );

仍然只是保留最后一个没有测试的像素......

screenshot

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

任何人都有帮助吗?

1 个答案:

答案 0 :(得分:3)

检查你的HRESULTs - 对CreateTexture2D的调用几乎肯定会失败,因为你在DEFAULT纹理上指定了CPU_ACCESS标志。由于您从不检查任何错误或指针,因此只会将NULL传播到所有深度对象,从而有效地禁用深度测试。

您还可以通过将D3D11_CREATE_DEVICE_DEBUG添加到D3D11CreateDevice上的标志来启用D3D调试层来捕获此类错误。如果你这样做了,你会看到以下调试spew:

  

D3D11错误:ID3D11Device :: CreateTexture2D:D3D11_USAGE_DEFAULT   资源不能设置任何CPUAccessFlags。下列   在这种情况下,不能设置CPUAccessFlags位:D3D11_CPU_ACCESS_READ   (1),D3D11_CPU_ACCESS_WRITE(1)。 [STATE_CREATION错误#98:   CREATETEXTURE2D_INVALIDCPUACCESSFLAGS]