无法从Compute着色器读取深度缓冲区

时间:2014-03-01 17:38:18

标签: hlsl directx-11

我无法从计算着色器读取深度缓冲区。

我在我的hlsl代码中使用它。

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

这样做是为了阅读缓冲区。

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

我正从渲染目标中分离深度缓冲区。

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

我仍然在输出中收到此错误。

*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]*

这就是我创建着色器资源视图的方法。

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

我已经尝试了所有提到的格式here和hlsl纹理格式float,float4,uint,uint4但没有成功。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将DXGI_FORMAT_R32_UINT替换为DXGI_FORMAT_R32_FLOAT用于着色器资源视图,因为您使用R32_Typeless,您有一个浮点缓冲区。

Texture2D gDepthTextures将是您稍后加载或采样深度所需的那个。

此外,您的纹理看起来没有正确绑定到您的计算着色器(因为运行时会告诉您在那里绑定了缓冲区)。

确保您拥有:

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

在你的派遣之前打电话。

作为旁注,要调试这些类型的问题,您还可以调用CSGetShaderResources(和其他等效的),以便在通话之前检查管道中的内容。