diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs
index 1ca9843d..3679a1f7 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs
@@ -263,6 +263,57 @@ public static class D2D1PixelShader
return dataLoader.GetResultingDispatchData();
}
+ ///
+ /// Gets the size of the constant buffer for a D2D1 pixel shader of a specified type.
+ ///
+ /// The type of D2D1 pixel shader to retrieve info for.
+ /// The size of the constant buffer for a D2D1 pixel shader of type .
+ public static int GetConstantBufferSize()
+ where T : unmanaged, ID2D1PixelShader
+ {
+ return D2D1BufferSizeDispatchDataLoader.For.ConstantBufferSize;
+ }
+
+ ///
+ /// 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/D2D1BufferSizeDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs
new file mode 100644
index 00000000..f0ec9dec
--- /dev/null
+++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs
@@ -0,0 +1,63 @@
+using System;
+using System.Runtime.CompilerServices;
+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 , to just retrieve the constant buffer size.
+///
+internal unsafe struct D2D1BufferSizeDispatchDataLoader : ID2D1DispatchDataLoader
+{
+ ///
+ /// The size of the constant buffer.
+ ///
+ private int size;
+
+ ///
+ /// Gets the size of the constant buffer.
+ ///
+ /// The size of the constant buffer.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public int GetConstantBufferSize()
+ {
+ return this.size;
+ }
+
+ ///
+ void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data)
+ {
+ this.size = data.Length * sizeof(uint);
+ }
+
+ ///
+ /// A helper class caching computed constant buffer sizes for generic shaders.
+ ///
+ /// The type of D2D1 pixel shader to retrieve info for.
+ public static class For
+ where T : unmanaged, ID2D1PixelShader
+ {
+ ///
+ /// Gets the constant buffer size for a shader of type .
+ ///
+ public static int ConstantBufferSize { get; } = GetConstantBufferSize();
+
+ ///
+ /// Calculates the constant buffer size for a shader of type .
+ ///
+ /// The constant buffer size for a shader of type .
+ private static int GetConstantBufferSize()
+ {
+ D2D1BufferSizeDispatchDataLoader dataLoader = default;
+
+ Unsafe.SkipInit(out T shader);
+
+ shader.LoadDispatchData(ref dataLoader);
+
+ return dataLoader.GetConstantBufferSize();
+ }
+ }
+}
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;
+ }
+ }
+}
diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs
index 909edd46..437e235d 100644
--- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs
+++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs
@@ -275,4 +275,162 @@ public partial class D2D1PixelShaderTests
return result;
}
}
+
+ [TestMethod]
+ public unsafe void GetConstantBufferSize()
+ {
+ int size = D2D1PixelShader.GetConstantBufferSize();
+
+ Assert.AreEqual(size, 124);
+
+ // Repeated calls always return the same result
+ size = D2D1PixelShader.GetConstantBufferSize();
+
+ Assert.AreEqual(size, 124);
+ }
+
+ [TestMethod]
+ public unsafe void GetConstantBuffer_ReadOnlyMemory()
+ {
+ GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
+
+ ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader);
+
+ ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(constantBuffer.Span);
+ }
+
+ [TestMethod]
+ public unsafe void GetConstantBuffer_Span()
+ {
+ GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
+
+ byte[] buffer = new byte[1024];
+
+ int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer);
+
+ Assert.IsTrue(bytesWritten > 0);
+ Assert.IsTrue(bytesWritten <= buffer.Length);
+
+ ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten));
+ }
+
+ [TestMethod]
+ [DataRow(0)]
+ [DataRow(4)]
+ [DataRow(27)]
+ [ExpectedException(typeof(ArgumentException))]
+ public unsafe void GetConstantBuffer_Span_DestinationTooShort(int size)
+ {
+ GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
+
+ byte[] buffer = new byte[size];
+
+ _ = D2D1PixelShader.GetConstantBuffer(in shader, buffer);
+
+ Assert.Fail();
+ }
+
+ [TestMethod]
+ public unsafe void GetConstantBuffer_TryGetSpan()
+ {
+ GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
+
+ byte[] buffer = new byte[1024];
+
+ bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten);
+
+ Assert.IsTrue(success);
+ Assert.IsTrue(bytesWritten > 0);
+ Assert.IsTrue(bytesWritten <= buffer.Length);
+
+ ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten));
+ }
+
+ [TestMethod]
+ [DataRow(0)]
+ [DataRow(4)]
+ [DataRow(27)]
+ public unsafe void GetConstantBuffer_TryGetSpan_DestinationTooShort(int size)
+ {
+ GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
+
+ byte[] buffer = new byte[size];
+
+ bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten);
+
+ Assert.IsFalse(success);
+ Assert.AreEqual(bytesWritten, 0);
+ }
+
+ // Gets a ShaderWithScalarVectorAndMatrixTypes instance with test values
+ private void GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader)
+ {
+ shader = new ShaderWithScalarVectorAndMatrixTypes(
+ x: 111,
+ y: 222,
+ z: 333,
+ f2x3: new(55, 44, 888, 111, 222, 333),
+ a: 22,
+ i1x3: new(1, 2, 3),
+ d2: new(3.14, 6.28),
+ c: 42,
+ i1x2: new(111, 222),
+ i2x2: new(11, 22, 33, 44),
+ d: 9999);
+ }
+
+ // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypes instance
+ private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(ReadOnlySpan span)
+ {
+ Assert.AreEqual(31 * sizeof(uint), span.Length);
+
+ fixed (byte* buffer = span)
+ {
+ Assert.AreEqual(111, *(int*)&buffer[0]);
+ Assert.AreEqual(222, *(int*)&buffer[4]);
+ Assert.AreEqual(333, *(int*)&buffer[8]);
+ Assert.AreEqual(55, *(float*)&buffer[16]);
+ Assert.AreEqual(44, *(float*)&buffer[20]);
+ Assert.AreEqual(888, *(float*)&buffer[24]);
+ Assert.AreEqual(111, *(float*)&buffer[32]);
+ Assert.AreEqual(222, *(float*)&buffer[36]);
+ Assert.AreEqual(333, *(float*)&buffer[40]);
+ Assert.AreEqual(22, *(int*)&buffer[44]);
+ Assert.AreEqual(1, *(int*)&buffer[48]);
+ Assert.AreEqual(2, *(int*)&buffer[52]);
+ Assert.AreEqual(3, *(int*)&buffer[56]);
+ Assert.AreEqual(3.14, *(double*)&buffer[64]);
+ Assert.AreEqual(6.28, *(double*)&buffer[72]);
+ Assert.AreEqual(42, *(int*)&buffer[80]);
+ Assert.AreEqual(111, *(int*)&buffer[84]);
+ Assert.AreEqual(222, *(int*)&buffer[88]);
+ Assert.AreEqual(11, *(int*)&buffer[96]);
+ Assert.AreEqual(22, *(int*)&buffer[100]);
+ Assert.AreEqual(33, *(int*)&buffer[112]);
+ Assert.AreEqual(44, *(int*)&buffer[116]);
+ Assert.AreEqual(9999, *(int*)&buffer[120]);
+ }
+ }
+
+ [D2DInputCount(0)]
+ [AutoConstructor]
+ private readonly partial struct ShaderWithScalarVectorAndMatrixTypes : ID2D1PixelShader
+ {
+ public readonly int x;
+ public readonly int y;
+ public readonly int z;
+ public readonly float2x3 f2x3;
+ public readonly int a;
+ public readonly Int1x3 i1x3;
+ public readonly double2 d2;
+ public readonly int c;
+ public readonly Int1x2 i1x2;
+ public readonly int2x2 i2x2;
+ public readonly int d;
+
+ public float4 Execute()
+ {
+ return 0;
+ }
+ }
}
\ No newline at end of file