diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 94367676..61bb031b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -311,7 +311,7 @@ partial struct PixelShaderEffect try { // Store the new ID2D1DrawInfo object - ComPtr.CopyTo(drawInfo, ref @this->d2D1DrawInfo); + 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 18c454be..befe939c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -115,7 +115,7 @@ internal unsafe partial struct PixelShaderEffect transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform).Assert(); // Store the new ID2D1EffectContext object - ComPtr.CopyTo(effectContext, ref @this->d2D1EffectContext); + ComPtr.CopyTo(effectContext, ref @this->d2D1EffectContext); return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 6e124bd3..a9aabc4c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -251,7 +251,7 @@ unsafe partial struct PixelShaderEffect unknown.CopyTo(transformMapper.GetAddressOf()).Assert(); // If there's already an existing manager, release it - ComPtr.Release(@this->d2D1TransformMapper); + ComPtr.Release(@this->d2D1TransformMapper); // Store the transform mapper manager into the effect @this->d2D1TransformMapper = transformMapper.Detach(); @@ -343,7 +343,7 @@ unsafe partial struct PixelShaderEffect ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index]; // If there's already an existing manager at this index, release it - ComPtr.Release(currentResourceTextureManager); + ComPtr.Release(currentResourceTextureManager); // Store the resource texture manager into the buffer currentResourceTextureManager = resourceTextureManager.Detach(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 9ed9f6ac..68782436 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -2,7 +2,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; -using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -232,16 +231,16 @@ internal unsafe partial struct PixelShaderEffect NativeMemory.Free(this.constantBuffer); - ComPtr.Release(this.d2D1TransformMapper); - ComPtr.Release(this.d2D1DrawInfo); - ComPtr.Release(this.d2D1EffectContext); + ComPtr.Release(this.d2D1TransformMapper); + ComPtr.Release(this.d2D1DrawInfo); + ComPtr.Release(this.d2D1EffectContext); // Retrieve all possible resource texture managers in use and release the ones that had been // assigned (from one of the property bindings). We just hardcode 16 here and dont access // the globals, as technically invoking APIs on it might throw an exception. for (int i = 0; i < 16; i++) { - ComPtr.Release(this.resourceTextureManagerBuffer[i]); + ComPtr.Release(this.resourceTextureManagerBuffer[i]); } NativeMemory.Free(Unsafe.AsPointer(ref this)); 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 99aa3985..f9e8105a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs @@ -124,7 +124,7 @@ 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. - ComPtr.WriteTo(effectContext, out @this->d2D1EffectContext); + ComPtr.WriteTo(effectContext, out @this->d2D1EffectContext); @this->d2D1Multithread = d2D1Multithread.Detach(); } @@ -161,7 +161,7 @@ partial struct D2D1ResourceTextureManagerImpl // If the texture has already been created, just return it if (@this->d2D1ResourceTexture is not null) { - ComPtr.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture); + ComPtr.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture); return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index 22887754..36309e44 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -249,7 +249,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl this.d2D1Multithread->Enter(); // Release the resource too if it has been created - ComPtr.Release(this.d2D1ResourceTexture); + ComPtr.Release(this.d2D1ResourceTexture); // Now that the resource, if any, has been released, the effect context can also // be released. This must be done only after any associated resource textures have diff --git a/src/TerraFX.Interop.Windows/ComPtr`1.cs b/src/TerraFX.Interop.Windows/ComPtr`1.cs index 4f1a3273..e06aae5a 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -13,37 +13,19 @@ using System.Diagnostics.CodeAnalysis; namespace TerraFX.Interop.Windows; -/// A type that allows working with pointers to COM objects more securely. -/// The type to wrap in the current instance. -/// While this type is not marked as so that it can also be used in fields, make sure to keep the reference counts properly tracked if you do store instances on the heap. -internal unsafe struct ComPtr : IDisposable - where T : unmanaged +/// +/// Extensions for to operate on raw pointers as well. +/// +internal static unsafe class ComPtr { - /// The raw pointer to a COM object, if existing. - private T* ptr_; - - /// Creates a new instance from a raw pointer and increments the ref count. - /// The raw pointer to wrap. - public ComPtr(T* other) - { - ptr_ = other; - InternalAddRef(); - } - - /// Creates a new instance from a second one and increments the ref count. - /// The other instance to copy. - public ComPtr(ComPtr other) - { - ptr_ = other.ptr_; - InternalAddRef(); - } - /// /// Releases a target COM object, if it's not . /// + /// The type of COM objects to work with. /// The COM object to potentially release. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Release(T* other) + public static void Release(T* other) + where T : unmanaged { if (other is not null) { @@ -54,10 +36,12 @@ internal unsafe struct ComPtr : IDisposable /// /// Copies a given COM object into a target destination, incrementing reference counts where needed. /// + /// The type of COM objects to work with. /// 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) + public static void CopyTo(T* obj, ref T* other) + where T : unmanaged { // We specifically need to increment the reference count on the source object before releasing the // target object, to avoid issues in case source and destination were pointers to the same object @@ -84,15 +68,43 @@ internal unsafe struct ComPtr : IDisposable /// /// Writes a given COM object into a target (assumed uninitialized) destination, incrementing reference counts where needed. /// + /// The type of COM objects to work with. /// 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) + public static void WriteTo(T* obj, out T* other) + where T : unmanaged { _ = ((IUnknown*)obj)->AddRef(); other = obj; } +} + +/// A type that allows working with pointers to COM objects more securely. +/// The type to wrap in the current instance. +/// While this type is not marked as so that it can also be used in fields, make sure to keep the reference counts properly tracked if you do store instances on the heap. +internal unsafe struct ComPtr : IDisposable + where T : unmanaged +{ + /// The raw pointer to a COM object, if existing. + private T* ptr_; + + /// Creates a new instance from a raw pointer and increments the ref count. + /// The raw pointer to wrap. + public ComPtr(T* other) + { + ptr_ = other; + InternalAddRef(); + } + + /// Creates a new instance from a second one and increments the ref count. + /// The other instance to copy. + public ComPtr(ComPtr other) + { + ptr_ = other.ptr_; + InternalAddRef(); + } /// Converts a raw pointer to a new instance and increments the ref count. /// The raw pointer to wrap.