2014-03-01 59 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);」 –