From 3a68b5b38a03ed0ab8a7f28bc6b88525121601ce Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 11 Jun 2022 00:27:04 +0200 Subject: [PATCH] Add Span GetConstantBuffer APIs --- .../Shaders/Interop/D2D1PixelShader.cs | 40 ++++++++++++ .../D2D1ByteArrayDispatchDataLoader.cs | 2 +- .../D2D1ByteArrayInputDescriptionsLoader.cs | 2 +- .../D2D1ByteBufferDispatchDataLoader.cs | 65 +++++++++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 1ca9843d..8dbb4461 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -263,6 +263,46 @@ public static class D2D1PixelShader return dataLoader.GetResultingDispatchData(); } + /// + /// Gets the constant buffer from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to retrieve info for. + /// The input D2D1 pixel shader to retrieve info for. + /// The target to write the constant buffer to. + /// The number of bytes written into . + /// Thrown if is not large enough to contain the constant buffer. + public static int GetConstantBuffer(in T shader, Span span) + where T : unmanaged, ID2D1PixelShader + { + if (!TryGetConstantBuffer(in shader, span, out int writtenBytes)) + { + ThrowHelper.ThrowArgumentException(nameof(span), "The destination span is too short."); + } + + return writtenBytes; + } + + /// + /// Tries to get the constant buffer from an input D2D1 pixel shader. + /// + /// The type of D2D1 pixel shader to retrieve info for. + /// The input D2D1 pixel shader to retrieve info for. + /// The target to write the constant buffer to. + /// The number of bytes written into . + /// Whether or not the constant buffer was retrieved successfully. + public static unsafe bool TryGetConstantBuffer(in T shader, Span span, out int bytesWritten) + where T : unmanaged, ID2D1PixelShader + { + fixed (byte* buffer = span) + { + D2D1ByteBufferDispatchDataLoader dataLoader = new(buffer, span.Length); + + Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); + + return dataLoader.TryGetWrittenBytes(out bytesWritten); + } + } + /// /// Sets the constant buffer from an input D2D1 pixel shader, by calling ID2D1DrawInfo::SetPixelShaderConstantBuffer. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs index 9e2d13b7..5486fec6 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs @@ -21,7 +21,7 @@ internal struct D2D1ByteArrayDispatchDataLoader : ID2D1DispatchDataLoader /// /// Gets the resulting pixel shader constant buffer. /// - /// A array with the constant buffer for the current shader. + /// A array with the constant buffer for the current shader. [MethodImpl(MethodImplOptions.AggressiveInlining)] public byte[] GetResultingDispatchData() { diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs index f3a65c2f..7c871f1b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs @@ -22,7 +22,7 @@ internal struct D2D1ByteArrayInputDescriptionsLoader : ID2D1InputDescriptionsLoa /// /// Gets the resulting input descriptions. /// - /// A array with the available input descriptions. + /// A array with the available input descriptions. [MethodImpl(MethodImplOptions.AggressiveInlining)] public D2D1InputDescription[] GetResultingInputDescriptions() { diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs new file mode 100644 index 00000000..1471514a --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs @@ -0,0 +1,65 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.D2D1.__Internals; +using TerraFX.Interop.DirectX; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.Shaders.Loaders; + +/// +/// A data loader for D2D1 pixel shaders dispatched via . +/// +internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoader +{ + /// + /// The target buffer to write data to. + /// + private readonly byte* buffer; + + /// + /// The length of the buffer pointed to by . + /// + private readonly int length; + + /// + /// The number of written bytes. + /// + private int writtenBytes; + + /// + /// Creates a new instance. + /// + /// The target buffer to write data to. + /// The length of the buffer pointed to by . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D2D1ByteBufferDispatchDataLoader(byte* buffer, int length) + { + this.buffer = buffer; + this.length = length; + this.writtenBytes = 0; + } + + /// + /// Gets the number of written bytes, or 0 in case of failure. + /// + /// The number of written bytes, of 0 in case of failure. + /// Whether or not the constant buffer was retrieved successfully. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool TryGetWrittenBytes(out int writtenBytes) + { + return (writtenBytes = this.writtenBytes) != 0; + } + + /// + void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data) + { + ReadOnlySpan constantBuffer = MemoryMarshal.Cast(data); + + if (constantBuffer.TryCopyTo(new Span(this.buffer, this.length))) + { + this.writtenBytes = constantBuffer.Length; + } + } +}