Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check the content of the depth/stencil buffer

Tags:

directx-11

Sometimes, i want to output the content of depth/stencil buffer for debugging purpose. I did some search on google, but without any lucky. How to achieve that?

like image 351
user2914677 Avatar asked Nov 28 '13 02:11

user2914677


1 Answers

You can create a shader resource view for your depth buffer, here is some code using it (explanations following):

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;
}

The following is to convert formats, since they are not compatible between resources. So you need to specify a format for resource, one different one for depth view and one for shader view

Now here is the code to create you resource:

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;
    }
}

From there you can use the ShaderResourceView like any other texture input once you rendered to your depth.

like image 133
mrvux Avatar answered Sep 28 '22 16:09

mrvux