Depth buffer as texture - "D3D11 ERROR: The Format is invalid when creating a View"

Depth buffer as texture - "D3D11 ERROR: The Format is invalid when creating a View"

我正在尝试使用深度缓冲区作为我的着色器中第二次传递的纹理。

根据 official documentation"Reading the Depth-Stencil Buffer as a Texture" 段落),我已将 D3D11_TEXTURE2D_DESC.Format 设置为 DXGI_FORMAT_R24G8_TYPELESS(原样DXGI_FORMAT_D24_UNORM_S8_UINT 早些时候,当我没有使用深度缓冲区作为纹理时):

D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R24G8_TYPELESS; //normally it was DXGI_FORMAT_D24_UNORM_S8_UINT
descDepth.SampleDesc.Count = antiAliasing.getCount();
descDepth.SampleDesc.Quality = antiAliasing.getQuality();
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

ID3D11Texture2D* depthStencil = NULL;
result = device->CreateTexture2D(&descDepth, NULL, &depthStencil);

results 成功。然后我尝试为我的缓冲区创建着色器资源视图:

D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
//setup the description of the shader resource view
shaderResourceViewDesc.Format = DXGI_FORMAT_R32_FLOAT;
shaderResourceViewDesc.ViewDimension = antiAliasing.isOn() ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;

//create the shader resource view.
device->CreateShaderResourceView(depthStencil, &shaderResourceViewDesc, &depthStencilShaderResourceView);

不幸的是,这会产生一个错误:

D3D11 ERROR: ID3D11Device::CreateShaderResourceView: The Format (0x29, R32_FLOAT) is invalid, when creating a View; it is not a fully qualified Format castable from the Format of the Resource (0x2c, R24G8_TYPELESS). [ STATE_CREATION ERROR #127: CREATESHADERRESOURCEVIEW_INVALIDFORMAT]

D3D11 ERROR: ID3D11Device::CreateShaderResourceView: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #131: CREATESHADERRESOURCEVIEW_INVALIDARG_RETURN]

我还尝试将 DXGI_FORMAT_R24G8_TYPELESSDXGI_FORMAT_D24_UNORM_S8_UINTDXGI_FORMAT_R8G8B8A8_UNORM 作为 shaderResourceViewDesc.Format

问题出在哪里?我一直在看文档,但我没有看到。

格式 DXGI_FORMAT_R24G8_TYPELESSDXGI_FORMAT_R32_FLOAT 不是 'cast compatible'。该无类型格式仅与 DXGI_FORMAT_D24_UNORM_S8_UINTDXGI_FORMAT_R24_UNORM_X8_TYPELESSDXGI_FORMAT_X24_TYPELESS_G8_UINT

兼容

DXGI_FORMAT_R32_TYPELESS 兼容 DXGI_FORMAT_D32_FLOATDXGI_FORMAT_R32_FLOAT