I am trying to use depth buffer as an texture for second pass in my shader.
According to official documentation ("Reading the Depth-Stencil Buffer as a Texture" paragraph), I've set D3D11_TEXTURE2D_DESC.Format
to DXGI_FORMAT_R24G8_TYPELESS
(as it was DXGI_FORMAT_D24_UNORM_S8_UINT
earlier, when I was not using depth buffer as 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_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);
The results
succeeded. Then I tried to create shader resource view for my buffer:
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);
Unfortunately, that generates an error:
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]
I was also trying with DXGI_FORMAT_R24G8_TYPELESS
, DXGI_FORMAT_D24_UNORM_S8_UINT
and DXGI_FORMAT_R8G8B8A8_UNORM
as shaderResourceViewDesc.Format
.
Where is the problem? I was following the documentation and I do not see it.