From cab4986cfc02246348b14200b6000193a8e9de5c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 21 Apr 2022 02:18:46 +0200 Subject: [PATCH] Move pixel shader effect APIs to new D2D1PixelShaderEffect type --- .../Interop/D2D1EffectRegistrationData.cs | 8 ++--- .../Shaders/Interop/D2D1InteropServices.cs | 4 +-- ...es.Effects.cs => D2D1PixelShaderEffect.cs} | 34 ++++++++++--------- .../Helpers/D2D1TestRunner.cs | 16 ++++----- 4 files changed, 30 insertions(+), 32 deletions(-) rename src/ComputeSharp.D2D1/Shaders/Interop/{D2D1InteropServices.Effects.cs => D2D1PixelShaderEffect.cs} (89%) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs index a68a3073..402b9a70 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs @@ -8,8 +8,8 @@ using ComputeSharp.D2D1.NetStandard.System.Text; namespace ComputeSharp.D2D1.Interop; /// -/// A helper type used to read and validate effect registration blobs produced by -/// . +/// A helper type used to read and validate effect registration blobs produced +/// by . /// /// /// @@ -76,8 +76,8 @@ public unsafe readonly struct D2D1EffectRegistrationData /// The resulting instance, if successful. /// Whether or not the input blob has been correctly deserialized (if it was not malformed). /// - /// The input blob should have been created by a call to any of the overloads of - /// . + /// The input blob should have been created by a call to any of the overloads + /// of . /// public static unsafe bool TryLoad(ReadOnlyMemory blob, out D2D1EffectRegistrationData data) { diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs index 0c2c2ae5..db3709d1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.cs @@ -10,9 +10,9 @@ using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Interop; /// -/// Provides methods to extract reflection info on D2D1 shaders generated using this library. +/// Provides methods to interop with D2D1 APIs and compile shaders or extract their constant buffer data. /// -public static unsafe partial class D2D1InteropServices +public static unsafe class D2D1InteropServices { /// /// Loads the bytecode from an input D2D1 pixel shader. diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs similarity index 89% rename from src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs rename to src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 15f0b0b7..c94e6d0f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -15,8 +15,10 @@ using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Interop; -/// -unsafe partial class D2D1InteropServices +/// +/// An implementation of a D2D1 pixel shader effect that can be used to instantiate ID2D1Effect objects. +/// +public static unsafe class D2D1PixelShaderEffect { /// /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. @@ -25,10 +27,10 @@ unsafe partial class D2D1InteropServices /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// For more info, see . - public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, out Guid effectId) + public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader { - RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory1, null, out effectId); + RegisterForD2D1Factory1(d2D1Factory1, null, out effectId); } /// @@ -39,11 +41,11 @@ unsafe partial class D2D1InteropServices /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// For more info, see . - public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, out Guid effectId) + public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { - RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); + RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); } /// @@ -54,7 +56,7 @@ unsafe partial class D2D1InteropServices /// An optional factory of instances to use for the transform. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// For more info, see . - public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) + public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; @@ -129,11 +131,11 @@ unsafe partial class D2D1InteropServices /// /// The type of D2D1 pixel shader to register. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. - /// For more info and for details on the binary format, see . - public static unsafe ReadOnlyMemory GetPixelShaderEffectRegistrationBlob(out Guid effectId) + /// For more info and for details on the binary format, see . + public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader { - return GetPixelShaderEffectRegistrationBlob(null, out effectId); + return GetRegistrationBlob(null, out effectId); } /// @@ -142,12 +144,12 @@ unsafe partial class D2D1InteropServices /// The type of D2D1 pixel shader to register. /// The type of implementation to register. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. - /// For more info and for details on the binary format, see . - public static unsafe ReadOnlyMemory GetPixelShaderEffectRegistrationBlob(out Guid effectId) + /// For more info and for details on the binary format, see . + public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { - return GetPixelShaderEffectRegistrationBlob(static () => new TMapper(), out effectId); + return GetRegistrationBlob(static () => new TMapper(), out effectId); } /// @@ -186,7 +188,7 @@ unsafe partial class D2D1InteropServices /// For more info, see . /// /// - public static unsafe ReadOnlyMemory GetPixelShaderEffectRegistrationBlob(Func>? mapperFactory, out Guid effectId) + public static unsafe ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; @@ -261,7 +263,7 @@ unsafe partial class D2D1InteropServices /// A pointer to the ID2D1DeviceContext instance to use. /// A pointer to the resulting ID2D1Effect* pointer to produce. /// For more info, see . - public static unsafe void CreatePixelShaderEffectFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) + public static unsafe void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { Guid shaderId = typeof(T).GUID; @@ -278,7 +280,7 @@ unsafe partial class D2D1InteropServices /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1Effect instance to use. /// For more info, see . - public static unsafe void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) + public static unsafe void SetConstantBuffer(in T shader, void* d2D1Effect) where T : unmanaged, ID2D1PixelShader { D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index 373fc4f6..ee9791f0 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -29,15 +29,13 @@ internal static class D2D1TestRunner using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); using ComPtr d2D1Effect = default; - D2D1InteropServices.CreatePixelShaderEffectFromD2D1DeviceContext( - d2D1DeviceContext.Get(), - (void**)d2D1Effect.GetAddressOf()); + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - D2D1InteropServices.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBuffer(in shader, d2D1Effect.Get()); using ComPtr wicBitmap = WICHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height); using ComPtr d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), wicBitmap.Get(), d2D1Effect.Get()); @@ -72,15 +70,13 @@ internal static class D2D1TestRunner using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); - D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _); using ComPtr d2D1Effect = default; - D2D1InteropServices.CreatePixelShaderEffectFromD2D1DeviceContext( - d2D1DeviceContext.Get(), - (void**)d2D1Effect.GetAddressOf()); + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - D2D1InteropServices.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBuffer(in shader, d2D1Effect.Get()); using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);