gpt4 book ai didi

directx - 深度模板缓冲区不工作 directx11

转载 作者:行者123 更新时间:2023-12-02 14:49:41 25 4
gpt4 key购买 nike

好吧,我现在尝试了一切,但我真的迷失了......

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

有什么帮助吗?

最佳答案

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

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

D3D11 ERROR: ID3D11Device::CreateTexture2D: A D3D11_USAGE_DEFAULT Resource cannot have any CPUAccessFlags set. The following CPUAccessFlags bits cannot be set in this case: D3D11_CPU_ACCESS_READ (1), D3D11_CPU_ACCESS_WRITE (1). [ STATE_CREATION ERROR #98: CREATETEXTURE2D_INVALIDCPUACCESSFLAGS]

关于directx - 深度模板缓冲区不工作 directx11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19199189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com