diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs index ab3ccb30..ea1f07a6 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Interop.Effects; @@ -35,7 +36,7 @@ unsafe partial class PixelShaderEffect /// /// Indicates whether an effect factory for has been registered. /// - private bool isEffectFactoryRegistered; + private volatile bool isEffectFactoryRegistered; /// /// Creates a new instance. @@ -53,6 +54,20 @@ unsafe partial class PixelShaderEffect /// Ensures an effect factory is registered in Win2D for the current effect type, or registers one otherwise. /// public void EnsureEffectFactoryIsRegistered() + { + if (this.isEffectFactoryRegistered) + { + return; + } + + EnsureEffectFactoryIsRegisteredWithLock(); + } + + /// + /// Same as , but with a slower path using a lock. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private void EnsureEffectFactoryIsRegisteredWithLock() { lock (this.lockObject) {