HLSL 在像素着色器中修改深度

HLSL modify depth in pixel shader

我需要渲染从外部获得的图像(具有深度)。我可以构造两个纹理并将它们毫无问题地传递到着色器中(我可以验证在像素着色器中采样的值是否正确)。

这是我的 HLSL 的样子:

// image texture
Texture2D m_TextureColor : register(t0);

// depth texture with values [0..1]
Texture2D<float> m_TextureDepth : register(t1);

// sampler to forbid linear filtering since we're dealing with pixels
SamplerState m_TextureSampler { Filter = MIN_MAG_MIP_POINT; };

struct VS_IN
{
    float4 position : POSITION;
    float2 texcoord : TEXCOORD;
};

struct VS_OUT
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

struct PS_OUT
{
    float4 color : COLOR0;
    float depth : DEPTH0;
};

VS_OUT VS(VS_IN input)
{
    VS_OUT output = (VS_OUT)0;

    output.position = input.position;
    output.texcoord = input.texcoord;

    return output;
}

PS_OUT PS(VS_OUT input) : SV_Target
{
    PS_OUT output = (PS_OUT)0;

    output.color = m_TextureColor.SampleLevel(m_TextureSampler, input.texcoord, 0);

    // I want to modify depth of the pixel,
    // but it looks like it has no effect on depth no matter what I set here
    output.depth = m_TextureDepth.SampleLevel(m_TextureSampler, input.texcoord, 0);

    return output;
}

我从第一个参数 Vector4 是位置,第二个参数 Vector2 是纹理坐标的那些(使用 PrimitiveTopology.TriangleStrip)构造顶点缓冲区:

new[]
{
    new Vertex(new Vector4(-1, -1, 0.5f, 1), new Vector2(0, 1)),
    new Vertex(new Vector4(-1, 1, 0.5f, 1), new Vector2(0, 0)),
    new Vertex(new Vector4(1, -1, 0.5f, 1), new Vector2(1, 1)),
    new Vertex(new Vector4(1, 1, 0.5f, 1), new Vector2(1, 0)),
}

一切正常:我看到了我的图像,我可以从深度纹理中采样深度并从中构建一些视觉效果(这就是我可以验证的方式 我采样的深度值是正确的)。但是我不知道如何修改像素的深度,以便在进行深度测试时可以正确地吃掉它。因为目前这一切都取决于我设置什么样的z值作为我的顶点位置。

这就是我设置 DirectX11 的方式(我正在使用 SharpDXC#):

var swapChainDescription = new SwapChainDescription
{
    BufferCount = 1,
    ModeDescription = new ModeDescription(bufferSize.Width, bufferSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
    IsWindowed = true,
    OutputHandle = HostHandle,
    SampleDescription = new SampleDescription(1, 0),
    SwapEffect = SwapEffect.Discard,
    Usage = Usage.RenderTargetOutput,
};
var swapChainFlags = DeviceCreationFlags.None | DeviceCreationFlags.BgraSupport;
SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, swapChainFlags, swapChainDescription, out var device, out var swapchain);

设置后台缓冲区和depth/stencil缓冲区:

// color buffer
using (var textureColor = SwapChain.GetBackBuffer<Texture2D>(0))
{
    TextureColorResourceView = new RenderTargetView(Device, textureColor);
}

// depth buffer
using (var textureDepth = new Texture2D(Device, new Texture2DDescription
{
    Format = Format.D32_Float,
    ArraySize = 1,
    MipLevels = 1,
    Width = BufferSize.Width,
    Height = BufferSize.Height,
    SampleDescription = new SampleDescription(1, 0),
    Usage = ResourceUsage.Default,
    BindFlags = BindFlags.DepthStencil,
    CpuAccessFlags = CpuAccessFlags.None,
    OptionFlags = ResourceOptionFlags.None
}))
{
    TextureDepthResourceView = new DepthStencilView(Device, textureDepth);
}

DeviceContext.OutputMerger.SetTargets(TextureDepthResourceView, TextureColorResourceView);

正在准备深度模板状态:

var description = DepthStencilStateDescription.Default();
description.DepthComparison = Comparison.LessEqual;
description.IsDepthEnabled = true;
description.DepthWriteMask = DepthWriteMask.All;
DepthState = new DepthStencilState(Device, description);

并使用它:

DeviceContext.OutputMerger.SetDepthStencilState(DepthState);

这就是我构建 color/depth 发送到着色器的纹理的方式:

public static (ShaderResourceView resource, Texture2D texture) CreateTextureDynamic(this Device device, System.Drawing.Size size, Format format)
{
    var textureDesc = new Texture2DDescription
    {
        MipLevels = 1,
        Format = format,
        Width = size.Width,
        Height = size.Height,
        ArraySize = 1,
        BindFlags = BindFlags.ShaderResource,
        Usage = ResourceUsage.Dynamic,
        SampleDescription = new SampleDescription(1, 0),
        CpuAccessFlags = CpuAccessFlags.Write,
    };

    var texture = new Texture2D(device, textureDesc);
    return (new ShaderResourceView(device, texture), texture);
}

另外因为我需要经常更新它们:

public static void UpdateResource(this Texture2D texture, int[] buffer, System.Drawing.Size size)
{
    var dataBox = texture.Device.ImmediateContext.MapSubresource(texture, 0, MapMode.WriteDiscard, MapFlags.None, out var dataStream);
    Parallel.For(0, size.Height, rowIndex => Marshal.Copy(buffer, size.Width * rowIndex, dataBox.DataPointer + dataBox.RowPitch * rowIndex, size.Width));
    dataStream.Dispose();
    texture.Device.ImmediateContext.UnmapSubresource(texture, 0);
}

public static void UpdateResource(this Texture2D texture, float[] buffer, System.Drawing.Size size)
{
    var dataBox = texture.Device.ImmediateContext.MapSubresource(texture, 0, MapMode.WriteDiscard, MapFlags.None, out var dataStream);
    Parallel.For(0, size.Height, rowIndex => Marshal.Copy(buffer, size.Width * rowIndex, dataBox.DataPointer + dataBox.RowPitch * rowIndex, size.Width));
    dataStream.Dispose();
    texture.Device.ImmediateContext.UnmapSubresource(texture, 0);
}

我也用谷歌搜索了很多关于这个的内容,发现了类似的帖子:https://www.gamedev.net/forums/topic/573961-how-to-set-depth-value-in-pixel-shader/但是我无法设法解决它。

提前致谢!

要写入深度缓冲区,您需要以 SV_Depth 系统值语义为目标。所以你的像素着色器输出结构看起来更像下面这样:

struct PS_OUT
{
    float4 color : SV_Target;
    float depth : SV_Depth;
};

并且着色器不会像您的示例中那样指定 SV_Target(SV_ 输出在结构中定义)。所以它看起来像:

PS_OUT PS(VS_OUT input)
{
    PS_OUT output = (PS_OUT)0;

    output.color = m_TextureColor.SampleLevel(m_TextureSampler, input.texcoord, 0);

    // Now that output.depth is defined with SV_Depth, and you have depth-write enabled,
    // this should write to the depth buffer.
    output.depth = m_TextureDepth.SampleLevel(m_TextureSampler, input.texcoord, 0);

    return output;
}

请注意,显式写入深度(特别是在 AMD 硬件上)可能会导致一些性能损失,因为这会强制绕过他们的早期深度硬件优化。使用该深度缓冲区的所有未来绘制调用都将禁用早期 Z 优化,因此通常最好尽可能晚地执行深度写入操作。