Hi,
I want to sample the depth buffer in a screen space effect in a shader that draws a full screen quad, using the back buffer and depth buffer as input. Basically I want to adjust the alpha for every pixel based on its distance from the camera, so nearby objects get almost masked out while far away objects have alphas closer to 1.0. I create my depth-stencil buffer and view like this:
D3D11_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = mRenderWindow->getClientAreaWidth();
depthStencilDesc.Height = mRenderWindow->getClientAreaHeight();
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
VERIFY(mD3DDevice->CreateTexture2D(&depthStencilDesc, 0, &mDepthStencilBuffer));
VERIFY(mD3DDevice->CreateDepthStencilView(mDepthStencilBuffer, 0, &mDepthStencilView));
I have a few questions.
1. Since I don't need to write to the depth buffer as I read from it in a shader, do I need to make any changes to the creation code above, or is it enough for me to unbind the depth buffer before binding it as a shader resource view?
2. Can I bind the mDepthStencilView as a shader resource view directly? Up till this point I have not sampled the depth buffer directly, only let the API use it in the depth test, so I have never thought of using it as an explicit shader input until now.
3. Since the format is DXGI_FORMAT_D24_UNORM_S8_UINT, that means that there are 24bits for the depth, right? How do I turn those into the floating point depth value between 0 and 1 in the pixel shader? Is there a special sampler I need to create or do I sample the xyz values and somehow combine those into a single value using bitwise operations in the shader?
Thanks for the help!