1

I am unable to read depth buffer from compute shader.

I am using this in my hlsl code.

Texture2D<float4> gDepthTextures : register(t3);
// tried this.
//Texture2D<float> gDepthTextures : register(t3);
// and this.
//Texture2D<uint> gDepthTextures : register(t3);
// and this.
//Texture2D<uint4> gDepthTextures : register(t3);

And doing this to read the buffer.

outputTexture[dispatchThreadId.xy]=gDepthTextures.Load(int3(dispatchThreadId.xy,0));

And I am detaching depth buffer from render target.

ID3D11RenderTargetView *nullView[3]={NULL,NULL,NULL};
        g_pImmediateContext->OMSetRenderTargets( 3, nullView, NULL );

Still I am getting this error in output.

*D3D11 ERROR: ID3D11DeviceContext::Dispatch: The Shader Resource View dimension declared in the shader code (TEXTURE2D) does not match the view type bound to slot 3 of the Compute Shader unit (BUFFER).  This mismatch is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #354: DEVICE_DRAW_VIEW_DIMENSION_MISMATCH]*

This is how I am creating shader resource view.

// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory( &descDepth, sizeof(descDepth) );
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = g_pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil );
if( FAILED( hr ) )
    return hr;

// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory( &descDSV, sizeof(descDSV) );
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV,     &g_pDepthStencilView );
if( FAILED( hr ) )
    return hr;

//  Create depth shader resource view.
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc,sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
srvDesc.Format=DXGI_FORMAT_R32_UINT;
srvDesc.ViewDimension=D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip=0;
srvDesc.Texture2D.MipLevels=1;


hr=g_pd3dDevice->CreateShaderResourceView(g_pDepthStencil,&srvDesc,&g_pDepthSRV);
if(FAILED(hr))
    return hr;

I have tried all the formats mentioned here in combination with the hlsl texture formats float, float4, uint, uint4 with no success. Any idea?

1 Answer 1

0

Replace DXGI_FORMAT_R32_UINT by DXGI_FORMAT_R32_FLOAT for your shader resource view, since you use R32_Typeless, you have a floating point buffer.

Texture2D gDepthTextures will be the one you need to load or sample the depth later.

Also it looks like that your texture is not bound properly to your compute shader (since runtime tells you you have a buffer bound in there).

Make sure you have:

immediateContext->CSSetShaderResources(3,1,g_pDepthSRV);

Called before your dispatch.

As a side note, to debug those type of issues, you can also call CSGetShaderResources (and other equivalent), in order to check what's bound in your pipeline before your call.

7
  • still getting "D3D11 ERROR: ID3D11DeviceContext::Dispatch: The Shader Resource View dimension declared in the shader code (TEXTURE2D) does not match the view type bound to slot 3 of the Compute Shader unit (BUFFER). This mismatch is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #354: DEVICE_DRAW_VIEW_DIMENSION_MISMATCH]" Mar 2, 2014 at 7:33
  • Edited answer, seems your depth is not bound prior to dispatch (the float format is still needed for your sampling by the way).
    – mrvux
    Mar 2, 2014 at 10:05
  • I already am doing this just before dispatch. "g_pImmediateContext->CSSetShaderResources(3,1,&g_pDepthSRV);" Mar 2, 2014 at 12:47
  • I used CSGetShaderResource, returned pointer is different than what i passed initially. don't know how to compare two SRVs. tried this too "ID3D11ShaderResourceView *tmp[]={g_pDepthSRV}; g_pImmediateContext->CSSetShaderResources(3,1,tmp);" still returning different pointer. Mar 2, 2014 at 13:05
  • MM maybe g_pDepthSRV is overwritten somewhere in your code?
    – mrvux
    Mar 2, 2014 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.