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

3

我需要渲染一张从外部获取的带深度的图片。我可以构建两个纹理,然后将它们传递到一个着色器中(我可以验证像素着色器中的采样值是正确的)。

以下是我的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;
}

我使用这些数据(具有PrimitiveTopology.TriangleStrip),构建了顶点缓冲区。其中第一个参数 Vector4 是位置,第二个参数 Vector2 是纹理坐标:

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(我使用SharpDX和C#):
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);

设置后备缓冲区和深度/模板缓冲区:

// 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);

这是我构建发送到着色器的颜色/深度纹理的方式:
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/ 然而我自己无法解决它。
提前感谢!
1个回答

4
要写入深度缓冲区,您需要针对 SV_Depth 系统值语义。因此,您的像素着色器输出结构将更像以下内容:
struct PS_OUT
{
    float4 color : SV_Target;
    float depth : SV_Depth;
};

在您的示例中,着色器不需要指定SV_Target(输出变量已在结构体内定义)。因此,它应该是这样的:

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 优化,因此通常最好尽可能晚地执行深度写入操作。

我非常清楚性能惩罚的问题,但是因为API将其流式传输给我,所以没有其他办法。非常感谢! - chainerlt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接