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

Hlsl 无法从计算着色器读取深度缓冲区,hlsl,directx-11,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

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

我在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);
我正在从渲染目标分离深度缓冲区

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;

我尝试了所有与hlsl纹理格式float、float4、uint、uint4结合使用的格式,但没有成功。有什么想法吗?

对于着色器资源视图,将DXGI\u格式\u R32\u UINT替换为DXGI\u格式\u R32\u浮点,因为您使用的是R32\u无类型,所以您有一个浮点缓冲区

Texture2D gDepthTextures将是您稍后需要加载或采样深度的材质

另外,看起来纹理没有正确绑定到计算着色器(因为运行时告诉您在其中绑定了缓冲区)

确保您有:

immediateContext->CSSetShaderResources(3,1,g_pDepthSRV);
在你出动前打过电话


作为补充说明,要调试这些类型的问题,您还可以调用(和其他等效程序),以便在调用之前检查管道中绑定的内容。

仍然获得“D3D11错误:ID3D11DeviceContext::Dispatch:着色器代码(TEXTURE2D)中声明的着色器资源视图维度”与绑定到计算着色器单元(缓冲区)插槽3的视图类型不匹配。如果着色器实际使用该视图,则此不匹配无效(例如,由于着色器代码分支而未跳过该视图)。[执行错误354:设备(绘制)视图(尺寸)不匹配]“编辑的答案,在分派之前似乎未绑定深度(顺便说一句,您的采样仍然需要浮点格式)。我在发送之前就已经这样做了。“g_pImmediateContext->CSSetShaderResources(3,1,&g_pDepthSRV);”我使用了CSGetShaderResource,返回的指针与我最初传递的指针不同。不知道如何比较两个SRV。也尝试过这个“ID3D11ShaderResourceView*tmp[]={g_pDepthSRV};g_pimediatecontext->CSSetShaderResources(3,1,tmp);“仍然返回不同的指针。MM可能g_pDepthSRV在代码中的某个地方被覆盖了?
immediateContext->CSSetShaderResources(3,1,g_pDepthSRV);