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

Chainerlt

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

我的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(我使用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);

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

// 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/但是无法设法解决了这一点。

提前致谢!

瓦拉克

要写入深度缓冲区,您需要定位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硬件上)时,可能会导致性能下降,因为这会绕过其早期的深度硬件优化。以后所有使用该深度缓冲区的绘制调用都将禁用Early-Z优化,因此,通常最好尽可能晚地执行深度写入操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档