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

时间:2015-08-15 02:50:35

标签: c++ directx directx-11 depth-buffer render-to-texture

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

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

不幸的是,渲染后我没有从深度缓冲区(下图)获得任何信息:

enter image description here

我猜这不对。奇怪的是,深度缓冲似乎有效(我得到正确的面部顺序等)。

深度缓冲区代码:

//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);取消绑定深度缓冲区(所以我可以将它用作纹理)。

然后,我使用简单的后处理着色器渲染我的纹理(从第一遍和深度缓冲区渲染目标),仅用于调试目的(第二遍):

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);
    }
...

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

1 个答案:

答案 0 :(得分:1)

根据评论:

纹理被正确渲染和采样,但由于数据介于0.999和1.0f之间,数据看起来是均匀的红色。

您可以采取一些措施来提高可用的深度精度,但最简单的方法是简单地确保您的近距离和远距离剪辑距离对于您正在绘制的场景而言不会过小/过大。 / p>

假设仪表是你的单位,0.1(10cm)的近夹和200(米)的远夹比1cm和20km更合理。

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