3

好的,我此时尝试了一切,我真的迷路了....

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我已经检查了光栅化器,它只正确地绘制了正面

有什么帮助吗?

4

1 回答 1

4

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

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

D3D11 错误:ID3D11Device::CreateTexture2D:D3D11_USAGE_DEFAULT 资源不能设置任何 CPUAccessFlags。在这种情况下无法设置以下 CPUAccessFlags 位:D3D11_CPU_ACCESS_READ (1)、D3D11_CPU_ACCESS_WRITE (1)。[状态创建错误#98:CREATETEXTURE2D_INVALIDCPUACCESSFLAGS]

于 2013-10-06T16:10:29.343 回答