有时,我想输出深度/模板缓冲区的内容以进行调试。我在google上做了一些搜索,但没有任何幸运。如何实现?
答案 0 :(得分:10)
您可以为深度缓冲区创建着色器资源视图,以下是使用它的一些代码(以下说明):
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;
}
以下是转换格式,因为它们在资源之间不兼容。 因此,您需要指定资源的格式,一个用于深度视图,另一个用于着色器视图
现在这里是创建资源的代码:
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。