From aa9ed772bac6f0b36e28c79232e2a4b0478148e6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 17 Jun 2022 11:52:50 +0200 Subject: [PATCH] Add D2D1PixelShader.GetConstantBufferSize() API --- .../Shaders/Interop/D2D1PixelShader.cs | 11 ++++ .../D2D1BufferSizeDispatchDataLoader.cs | 63 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 8dbb4461..3679a1f7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -263,6 +263,17 @@ 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. /// 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(); + } + } +}