diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index a8b8fc47..3a3d2cdc 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -4,6 +4,7 @@ using System.Threading; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Interop.Effects; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; @@ -66,6 +67,20 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// public T Value; + /// + /// Configures the to use for effects of type . + /// + /// The factory of instances to use for each created effect. + /// Thrown if initialization is attempted with a mismatched transform factory. + /// + /// Initialization can only be done once, and is shared across all effects of type . + /// Initializing an effect is not necessary before using it: if no transform has been set, the default one will be used. + /// + public static void ConfigureD2D1TransformMapperFactory(ID2D1TransformMapperFactory? d2D1DrawTransformMapperFactory) + { + PixelShaderEffect.For.Initialize(d2D1DrawTransformMapperFactory); + } + /// /// Gets or sets a value indicating whether to enable caching the output from drawing this effect. /// @@ -350,13 +365,41 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan // D2D1PixelShaderEffect APIs specifically need an ID2D1Factory1 (as ID2D1Factory1::RegisterEffectFromString is used) d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); - // Register the effect with the factory (TODO: only do this if needed) - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); + bool isPossiblyRegistered = true; + + // If the effect hasn't been initialized explicitly, initialize it now + if (!PixelShaderEffect.For.TryGetId(out Guid effectId)) + { + PixelShaderEffect.For.Initialize(null); + + effectId = PixelShaderEffect.For.Id; + isPossiblyRegistered = false; + } fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect) { - // Create an instance of the effect in use and store it in the current object - D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(deviceContext, (void**)d2D1Effect); + HRESULT hresult = D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED; + + // Try to create an instance of the effect in use and store it in the current object + if (isPossiblyRegistered) + { + hresult = deviceContext->CreateEffect(effectId: &effectId, effect: d2D1Effect); + } + + // Check if creation failed due to the effect not being registered. In that case, register + // it and then try again. This is much faster than check whether the effect is registered + // manually every time, by enumerating all registered effects for the target device context. + if (hresult == D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED) + { + // Register the effect with the factory (pass the same D2D1 draw transform mapper factory that was used before) + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), PixelShaderEffect.For.D2D1DrawTransformMapperFactory, out _); + + // Try to create the effect again + hresult = deviceContext->CreateEffect(effectId: &effectId, effect: d2D1Effect); + } + + // Rethrow any other errors + hresult.Assert(); } // If cache input has been set, forward that to the new effect diff --git a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/D2DERR.cs b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/D2DERR.cs index d11c5f59..542a9408 100644 --- a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/D2DERR.cs +++ b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/D2DERR.cs @@ -12,4 +12,7 @@ internal static partial class D2DERR [NativeTypeName("#define D2DERR_CYCLIC_GRAPH _HRESULT_TYPEDEF_(0x88990020L)")] public const int D2DERR_CYCLIC_GRAPH = unchecked((int)(0x88990020)); + + [NativeTypeName("#define D2DERR_EFFECT_IS_NOT_REGISTERED _HRESULT_TYPEDEF_(0x88990028L)")] + public const int D2DERR_EFFECT_IS_NOT_REGISTERED = unchecked((int)(0x88990028)); } \ No newline at end of file