3
\$\begingroup\$

I am attempting to add an SSAO post processing shader to my deferred rendering engine. I am running into problems with reading from the depth buffer. In the images linked below you can see the depth buffer, and the values I'm reading out of it (the values are scaled - see 'range' value at top of image)

Depth Buffer

What I read from the depth buffer

It looks to me like the format must be wrong somehow but I'm not sure in what way.

The depth buffer is being created as follows:

// Texture
D3D11_TEXTURE2D_DESC textureDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = m_Desc.Width;
textureDesc.Height = m_Desc.Height;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
textureDesc.MiscFlags = 0;
HRESULT hr = m_pDevice->CreateTexture2D(&textureDesc, nullptr, &m_pDepth);

// DS VIEW
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));
descDSV.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = m_pDevice->CreateDepthStencilView(m_pDepth, &descDSV, &m_pDepthStencilView);

// SR View
D3D11_SHADER_RESOURCE_VIEW_DESC depthSrvDesc;
ZeroMemory(&depthSrvDesc, sizeof(depthSrvDesc));
depthSrvDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
depthSrvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
depthSrvDesc.Texture2D.MipLevels = 1;
depthSrvDesc.Texture2D.MostDetailedMip = 0;
hr = m_pDevice->CreateShaderResourceView(m_pDepth, &depthSrvDesc, &m_pDepthShaderResourceView);

And the important parts of my SSAO shader:

Texture2D gTextureDepth;

struct VS_INPUT
{
    float3 Position : POSITION;
    float2 TexCoord : TEXCOORD0;
};

struct PS_INPUT
{
    float4 Position : SV_POSITION;
    float2 TexCoord : TEXCOORD0;
};

PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT)0;

    output.Position = float4(input.Position, 1.0f);
    output.TexCoord = input.TexCoord;

    return output;
}

float4 PS(PS_INPUT input) : SV_Target
{
    int3 loadCoord = int3(input.Position.xy, 0);

    float depth = gTextureDepth.Load(loadCoord).r;

    return float4(depth, depth, depth, 1.0);
}

Any ideas?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.