错误的深度缓冲区(到纹理)输出?

Wrong depth buffer (to texture) output?

对于 SSAO 效果,我必须生成两个纹理:法线(在视图 space 中)和深度。

根据Microsoft tutorial将深度模板缓冲区作为纹理一章),我决定使用深度缓冲区作为纹理。

不幸的是,渲染后我从深度缓冲区(下图)得到了 none 信息:

估计不对。奇怪的是,深度缓冲区似乎可以工作(我得到正确的面部顺序等)。

深度缓冲代码:

//create depth stencil texture (depth buffer)
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;
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);
ERROR_HANDLE(SUCCEEDED(result), L"Could not create depth stencil texture.", MOD_GRAPHIC);

D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
//setup the description of the shader resource view
shaderResourceViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
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.
ERROR_HANDLE(SUCCEEDED(device->CreateShaderResourceView(depthStencil, &shaderResourceViewDesc, &depthStencilShaderResourceView)),
    L"Could not create shader resource view for depth buffer.", MOD_GRAPHIC);

createDepthStencilStates();
//set the depth stencil state.
context->OMSetDepthStencilState(depthStencilState3D, 1);

D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
// Initialize the depth stencil view.
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

// Set up the depth stencil view description.
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = antiAliasing.isOn() ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
//depthStencilViewDesc.Flags = D3D11_DSV_READ_ONLY_DEPTH;

// Create the depth stencil view.
result = device->CreateDepthStencilView(depthStencil, &depthStencilViewDesc, &depthStencilView);
ERROR_HANDLE(SUCCEEDED(result), L"Could not create depth stencil view.", MOD_GRAPHIC);

第一遍渲染后,我将深度模板与其他渲染目标(颜色、法线)一起设置为纹理资源,并将其附加到数组:

ID3D11ShaderResourceView ** textures = new ID3D11ShaderResourceView *[targets.size()+1];
for (unsigned i = 0; i < targets.size(); i++) {
    textures[i] = targets[i]->getShaderResourceView();
}
textures[targets.size()] = depthStencilShaderResourceView;
context->PSSetShaderResources(0, targets.size()+1, textures);

在第二遍之前,我调用 context->OMSetRenderTargets(1, &myRenderTargetView, NULL); 取消绑定深度缓冲区(因此我可以将其用作纹理)。

然后,我使用简单的 post-process 着色器渲染我的纹理(渲染目标来自第一遍 + 深度缓冲区),仅用于调试目的(第二遍):

Texture2D ColorTexture[3];
SamplerState ObjSamplerState;

float4 main(VS_OUTPUT input) : SV_TARGET0{
    float4 Color;
    Color = float4(0, 1, 1, 1);
    float2 textureCoordinates = input.textureCoordinates.xy * 2;
    if (input.textureCoordinates.x < 0.5f && input.textureCoordinates.y < 0.5f) {
        Color = ColorTexture[0].Sample(ObjSamplerState, textureCoordinates);
    }
    if (input.textureCoordinates.x > 0.5f && input.textureCoordinates.y < 0.5f) {
        textureCoordinates.x -= 0.5f;
        Color = ColorTexture[1].Sample(ObjSamplerState, textureCoordinates);
    }
    if (input.textureCoordinates.x < 0.5f && input.textureCoordinates.y > 0.5f) { //depth texture
        textureCoordinates.y -= 0.5f;
        Color = ColorTexture[2].Sample(ObjSamplerState, textureCoordinates);
    }
...

它适用于法线纹理。为什么它不适用于深度缓冲区(作为着色器资源视图)?

根据评论:

纹理被正确渲染和采样,但由于数据位于 0.999 和 1.0f 之间,数据看起来一致为红色。

您可以采取一些措施来提高可用的深度精度,但最简单的方法是确保您的近距和远距裁剪距离对于您正在绘制的场景不会过大 small/large .

假设您的单位是米,那么近距离剪辑 0.1(10 厘米)和远距离剪辑 200(米)比 1 厘米和 20 公里更合理。

即便如此,不要指望看到太多 black/dark 区域,z 缓冲区的非线性特性仍然意味着您的大部分深度值都被分流到 1。如果可视化深度缓冲区很重要,然后只需在显示之前将数据重新缩放到标准化的 0-1 范围即可。