2014-03-01 56 views
1

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

我在我的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纹理格式浮动,float4变量,UINT,uint4没有成功。任何想法?

回答

0

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

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

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

请确保您有:

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

您的调度之前调用。

作为一个方面说明,为了调试这些类型的问题,您也可以拨打CSGetShaderResources(和其他等价物),以便在您的调用之前检查您的管道中的什么。

+0

仍然得到“D3D11 ERROR:ID3D11DeviceContext :: Dispatch:着色器代码(TEXTURE2D)中声明的着色器资源视图尺寸与计算着色器单元(BUFFER)的插槽3绑定的视图类型不匹配,这种不匹配是无效的如果着色器实际使用视图(例如,由于着色器代码分支,它不会被跳过)[执行错误#354:DEVICE_DRAW_VIEW_DIMENSION_MISMATCH]“ –

+0

编辑答案,似乎在调度之前您的深度没有被绑定(仍然需要float格式你的采样方式)。 – catflier

+0

我已经在发送之前这样做了。 “g_pImmediateContext-> CSSetShaderResources(3,1,&g_pDepthSRV);” –