gpt4 book ai didi

directx-11 - 如何检查深度/模板缓冲区的内容

转载 作者:行者123 更新时间:2023-12-04 22:31:38 24 4
gpt4 key购买 nike

有时,我想输出深度/模板缓冲区的内容以进行调试。我在谷歌上做了一些搜索,但没有任何运气。如何做到这一点?

最佳答案

你可以为你的深度缓冲区创建一个着色器资源 View ,这里有一些使用它的代码(解释如下):

DXGI_FORMAT GetDepthResourceFormat(DXGI_FORMAT depthformat)
{
DXGI_FORMAT resformat;
switch (depthformat)
{
case DXGI_FORMAT::DXGI_FORMAT_D16_UNORM:
resformat = DXGI_FORMAT::DXGI_FORMAT_R16_TYPELESS;
break;
case DXGI_FORMAT::DXGI_FORMAT_D24_UNORM_S8_UINT:
resformat = DXGI_FORMAT::DXGI_FORMAT_R24G8_TYPELESS;
break;
case DXGI_FORMAT::DXGI_FORMAT_D32_FLOAT:
resformat = DXGI_FORMAT::DXGI_FORMAT_R32_TYPELESS;
break;
case DXGI_FORMAT::DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
resformat = DXGI_FORMAT::DXGI_FORMAT_R32G8X24_TYPELESS;
break;
}

return resformat;
}

DXGI_FORMAT GetDepthSRVFormat(DXGI_FORMAT depthformat)
{
DXGI_FORMAT srvformat;
switch (depthformat)
{
case DXGI_FORMAT::DXGI_FORMAT_D16_UNORM:
srvformat = DXGI_FORMAT::DXGI_FORMAT_R16_FLOAT;
break;
case DXGI_FORMAT::DXGI_FORMAT_D24_UNORM_S8_UINT:
srvformat = DXGI_FORMAT::DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
break;
case DXGI_FORMAT::DXGI_FORMAT_D32_FLOAT:
srvformat = DXGI_FORMAT::DXGI_FORMAT_R32_FLOAT;
break;
case DXGI_FORMAT::DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
srvformat = DXGI_FORMAT::DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS;
break;
}
return srvformat;
}

以下是转换格式,因为它们在资源之间不兼容。
所以你需要为资源指定一种格式,一种不同的格式用于深度 View ,一种用于着色器 View

现在这里是创建资源的代码:
void DepthStencil::Initialize(DxDevice* device, int w, int h,DXGI_FORMAT format, int     samplecount)
{
HRESULT hr;

DXGI_FORMAT resformat = GetDepthResourceFormat(format);
DXGI_FORMAT srvformat = GetDepthSRVFormat(format);

D3D11_TEXTURE2D_DESC desc;
desc.ArraySize = 1;
desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.Format = resformat;
desc.Height = h;
desc.MipLevels = 1;
desc.MiscFlags = 0;
desc.SampleDesc.Count = samplecount;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE::D3D11_USAGE_DEFAULT;
desc.Width = w;

hr = mDevice->GetDevice()->CreateTexture2D(&desc,NULL,&mObj->mResource);

if (FAILED(hr))
{
DxException exc("Failed to create DepthStencil resource");
throw exc;
}

D3D11_DEPTH_STENCIL_VIEW_DESC ddesc;
ZeroMemory(&ddesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
ddesc.Format = format;
ddesc.ViewDimension = samplecount > 1 ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
ddesc.Texture2D.MipSlice = 0;

hr = mDevice->GetDevice()->CreateDepthStencilView(mObj->mResource,&ddesc,&mObj->mDSV);

if (FAILED(hr))
{
DxException exc("Failed to create DepthStencil View");
throw exc;
}

D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
ZeroMemory(&srvd,sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
srvd.Format = srvformat;
srvd.ViewDimension = samplecount > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
srvd.Texture2D.MipLevels = 1;

hr = mDevice->GetDevice()->CreateShaderResourceView(mObj->mResource,&srvd,&mObj->mSRV);

if (FAILED(hr))
{
DxException exc("Failed to create DepthStencil Shader View");
throw exc;
}
}

从那里你可以像使用任何其他纹理输入一样使用 ShaderResourceView,一旦你渲染到你的深度。

关于directx-11 - 如何检查深度/模板缓冲区的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256815/

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