diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs index b8849221..9b7bc03d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs @@ -116,13 +116,20 @@ partial struct ResourceTextureManager // If the method has already been called, just forward the call if (@this->d2D1EffectContext is not null) { - return @this->d2D1EffectContext->CreateResourceTexture( + @this->d2D1Multithread->Enter(); + + // Create the resource after taking a D2D lock + int hresult = @this->d2D1EffectContext->CreateResourceTexture( resourceId: resourceId, resourceTextureProperties: resourceTextureProperties, data: data, strides: strides, dataSize: dataSize, resourceTexture: &@this->d2D1ResourceTexture); + + @this->d2D1Multithread->Leave(); + + return hresult; } // Initialize into a staging buffer @@ -165,13 +172,20 @@ partial struct ResourceTextureManager // If a resource texture already exists, just forward the call if (@this->d2D1ResourceTexture is not null) { - return @this->d2D1ResourceTexture->Update( + @this->d2D1Multithread->Enter(); + + // Take a D2D lock here too to ensure thread safety + int hresult = @this->d2D1ResourceTexture->Update( minimumExtents: minimumExtents, maximimumExtents: maximimumExtents, strides: strides, dimensions: dimensions, data: data, dataCount: dataCount); + + @this->d2D1Multithread->Leave(); + + return hresult; } // Otherwise update the staging buffer diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs index 0354601a..34023129 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs @@ -1,5 +1,7 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Shaders.Interop.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER @@ -91,14 +93,25 @@ partial struct ResourceTextureManager lock (@this->lockHandle.Target!) { - // If this is the first time the method is called, just store the context + // If this is the first time the method is called, just store the context. + // Before doing so, get an ID2D1Multithread instance, to ensure thread safety. + // For additional info, see docs on ID2D1EffectFactoryExtensions.GetD2D1Multithread. if (@this->d2D1EffectContext is null) { - effectContext->AddRef(); + using ComPtr d2D1Multithread = default; - @this->d2D1EffectContext = effectContext; + int hresult = effectContext->GetD2D1Multithread(d2D1Multithread.GetAddressOf()); - return S.S_OK; + // If an ID2D1Multithread object is available, we can safely store the context + if (Windows.SUCCEEDED(hresult)) + { + _ = effectContext->AddRef(); + + @this->d2D1EffectContext = effectContext; + @this->d2D1Multithread = d2D1Multithread.Detach(); + } + + return hresult; } // Otherwise, just do nothing and return S_FALSE. This allows an existing resource texture diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs index 989024a3..e8dbbd22 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs @@ -100,6 +100,11 @@ internal unsafe partial struct ResourceTextureManager /// private ID2D1EffectContext* d2D1EffectContext; + /// + /// The instance used to lock accesses to and . + /// + private ID2D1Multithread* d2D1Multithread; + /// /// The object, if one was created already. /// @@ -155,6 +160,7 @@ internal unsafe partial struct ResourceTextureManager @this->lpVtblForID2D1ResourceTextureManagerInternal = VtblForID2D1ResourceTextureManagerInternal; @this->referenceCount = 1; @this->d2D1EffectContext = null; + @this->d2D1Multithread = null; @this->d2D1ResourceTexture = null; @this->resourceId = null; @this->resourceTextureProperties = default; @@ -215,14 +221,21 @@ internal unsafe partial struct ResourceTextureManager { lock (this.lockHandle.Target!) { - if (this.d2D1EffectContext is not null) + // Also synchronize on the ID2D1Multithread instance before touching D2D objects + if (this.d2D1Multithread is not null) { + // Enter the lock, and then free the effect context. That is guaranteed to + // not be null here, as it is only ever set if ID2D1Multithread is retrieved. + this.d2D1Multithread->Enter(); this.d2D1EffectContext->Release(); - } - if (this.d2D1ResourceTexture is not null) - { - this.d2D1ResourceTexture->Release(); + // Release the resource too if it has been created + if (this.d2D1ResourceTexture is not null) + { + this.d2D1ResourceTexture->Release(); + } + + this.d2D1Multithread->Leave(); } if (this.resourceId is not null)