diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index cc0035b3..94367676 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -310,13 +310,8 @@ partial struct PixelShaderEffect try { - // Free the previous ID2D1DrawInfo object, if present - ComPtr.Release(@this->d2D1DrawInfo); - // Store the new ID2D1DrawInfo object - _ = drawInfo->AddRef(); - - @this->d2D1DrawInfo = drawInfo; + ComPtr.CopyTo(drawInfo, ref @this->d2D1DrawInfo); Guid shaderId = @this->GetGlobals().EffectId; D2D1PixelOptions pixelOptions = @this->GetGlobals().PixelOptions; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index f13cdbb2..18c454be 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -114,13 +114,8 @@ internal unsafe partial struct PixelShaderEffect // If loading the bytecode succeeded, set the transform node transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform).Assert(); - // Free the previous ID2D1EffectContext object, if present - ComPtr.Release(@this->d2D1EffectContext); - // Store the new ID2D1EffectContext object - _ = effectContext->AddRef(); - - @this->d2D1EffectContext = effectContext; + ComPtr.CopyTo(effectContext, ref @this->d2D1EffectContext); return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs index 1b8a84ed..99aa3985 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs @@ -124,9 +124,8 @@ partial struct D2D1ResourceTextureManagerImpl // Now, the effect context can actually be stored safely while holding the lock. // This is guaranteed to be the case here, as this method is only called (as per // contact of the COM interface) from ID2D1EffectImpl, which holds the D2D lock. - _ = effectContext->AddRef(); + ComPtr.WriteTo(effectContext, out @this->d2D1EffectContext); - @this->d2D1EffectContext = effectContext; @this->d2D1Multithread = d2D1Multithread.Detach(); } @@ -162,9 +161,7 @@ partial struct D2D1ResourceTextureManagerImpl // If the texture has already been created, just return it if (@this->d2D1ResourceTexture is not null) { - _ = @this->d2D1ResourceTexture->AddRef(); - - *resourceTexture = @this->d2D1ResourceTexture; + ComPtr.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture); return S.S_OK; } diff --git a/src/TerraFX.Interop.Windows/ComPtr`1.cs b/src/TerraFX.Interop.Windows/ComPtr`1.cs index abb74bcc..58582622 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -51,6 +51,39 @@ internal unsafe struct ComPtr : IDisposable } } + /// + /// Copies a given COM object into a target destination. + /// + /// The input COM object to copy. + /// The target destination for the COM object. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CopyTo(T* obj, ref T* other) + { + T* pointer = other; + + if (pointer is not null) + { + _ = ((IUnknown*)pointer)->Release(); + } + + _ = ((IUnknown*)obj)->AddRef(); + + other = obj; + } + + /// + /// Writes a given COM object into a target (assumed uninitialized) destination. + /// + /// The input COM object to copy. + /// The target destination for the COM object. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteTo(T* obj, out T* other) + { + _ = ((IUnknown*)obj)->AddRef(); + + other = obj; + } + /// Converts a raw pointer to a new instance and increments the ref count. /// The raw pointer to wrap. [MethodImpl(MethodImplOptions.AggressiveInlining)]