diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 8cd389db..c10c6ad3 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; #endif using System.Text; using ComputeSharp.D2D1.Extensions; +using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop.Effects; using ComputeSharp.D2D1.Shaders.Dispatching; using ComputeSharp.D2D1.Shaders.Interop.Buffers; @@ -26,6 +27,7 @@ public static unsafe class D2D1PixelShaderEffect /// The type of D2D1 pixel shader to register. /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader @@ -40,12 +42,13 @@ public static unsafe class D2D1PixelShaderEffect /// The type of implementation to register. /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { - RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); + RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); } /// @@ -55,6 +58,7 @@ public static unsafe class D2D1PixelShaderEffect /// A pointer to the ID2D1Factory1 instance to use. /// An optional factory of instances to use for the transform. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader @@ -131,6 +135,8 @@ public static unsafe class D2D1PixelShaderEffect /// /// The type of D2D1 pixel shader to register. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// A blob containing serialized information that can be used to register an effect. + /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader @@ -144,6 +150,8 @@ public static unsafe class D2D1PixelShaderEffect /// 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. + /// A blob containing serialized information that can be used to register an effect. + /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader @@ -158,6 +166,8 @@ public static unsafe class D2D1PixelShaderEffect /// The type of D2D1 pixel shader to get the registration blob for. /// An optional factory of instances to use for the transform. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// A blob containing serialized information that can be used to register an effect. + /// Thrown if an effect is registered multiple times with different properties. /// /// /// This blob can be useful to marshal registration data across an app domain boundary without having assembly references cross this boundary as well. @@ -267,14 +277,22 @@ public static unsafe class D2D1PixelShaderEffect /// The type of D2D1 pixel shader to create an effect for. /// A pointer to the ID2D1DeviceContext instance to use. /// A pointer to the resulting ID2D1Effect* pointer to produce. + /// + /// Thrown if this method is called before initializing a shader effect for the shader type . + /// To do so, make sure to call either (or an overload), or + /// to use (or an overload), and use that blob to register an effect. + /// /// For more info, see . public static unsafe void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { - Guid shaderId = typeof(T).GUID; + if (!PixelShaderEffect.For.TryGetId(out Guid id)) + { + ThrowHelper.ThrowInvalidOperationException("The effect for the input shader type has not been initialized yet."); + } ((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect( - effectId: &shaderId, + effectId: &id, effect: (ID2D1Effect**)d2D1Effect).Assert(); } @@ -285,7 +303,7 @@ public static unsafe class D2D1PixelShaderEffect /// 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 SetConstantBuffer(in T shader, void* d2D1Effect) + public static unsafe void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) where T : unmanaged, ID2D1PixelShader { D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs index 8c209e8d..cc8fbf5a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.For{T}.cs @@ -131,6 +131,28 @@ internal unsafe partial struct PixelShaderEffect /// public static int NumberOfInputs => numberOfInputs; + /// + /// Tries to get the effect id, if it has been initialized. + /// + /// The resulting effect id. + /// Whether or not the effect had been initialized. + public static bool TryGetId(out Guid id) + { + lock (typeof(For)) + { + if (isInitialized) + { + id = Id; + + return true; + } + } + + id = default; + + return false; + } + /// private static int CreateEffect(IUnknown** effectImpl) { diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index ee9791f0..65de58db 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -35,7 +35,7 @@ internal static class D2D1TestRunner D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - D2D1PixelShaderEffect.SetConstantBuffer(in shader, d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(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()); @@ -76,7 +76,7 @@ internal static class D2D1TestRunner D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); - D2D1PixelShaderEffect.SetConstantBuffer(in shader, d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);