From f37e4446f6c4275d195ed63ed050c084346bfaea Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 Oct 2023 17:18:06 +0200 Subject: [PATCH 1/5] Move ComPtr static helpers to ComPtr type --- ...lShaderEffect.ID2D1DrawTransformMethods.cs | 2 +- ...ixelShaderEffect.ID2D1EffectImplMethods.cs | 2 +- .../Effects/PixelShaderEffect.Properties.cs | 4 +- .../Interop/Effects/PixelShaderEffect.cs | 9 ++- ...mpl.ID2D1ResourceTextureManagerInternal.cs | 4 +- .../D2D1ResourceTextureManagerImpl.cs | 2 +- src/TerraFX.Interop.Windows/ComPtr`1.cs | 66 +++++++++++-------- 7 files changed, 50 insertions(+), 39 deletions(-) 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. From 62d81ad03f5b3bd3a2cf28ff4af3d06b670ec75f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 Oct 2023 20:41:16 +0200 Subject: [PATCH 2/5] Remove implicit ComPtr operators --- .../Extensions/ICanvasDeviceExtensions.cs | 2 +- ...ixelShaderEffect.ID2D1EffectImplMethods.cs | 2 +- .../Effects/PixelShaderEffect.Properties.cs | 6 +++--- .../Shaders/Translation/ShaderCompiler.cs | 2 +- .../Helpers/SwapChainManager.cs | 2 +- .../Graphics/Commands/CommandList.cs | 4 ++-- .../ID3D12DescriptorHandleAllocator.cs | 2 +- .../Graphics/GraphicsDevice.Execute.cs | 8 ++++---- .../Graphics/GraphicsDevice.Resources.cs | 8 ++++---- .../Graphics/Resources/Abstract/Buffer{T}.cs | 2 +- .../Resources/Abstract/Texture1D{T}.cs | 2 +- .../Resources/Abstract/Texture2D{T}.cs | 2 +- .../Resources/Abstract/Texture3D{T}.cs | 2 +- .../Resources/Abstract/TransferBuffer{T}.cs | 2 +- .../Abstract/TransferTexture1D{T}.cs | 2 +- .../Abstract/TransferTexture2D{T}.cs | 2 +- .../Abstract/TransferTexture3D{T}.cs | 2 +- .../Shaders/Models/PipelineData.cs | 4 ++-- src/TerraFX.Interop.Windows/ComPtr`1.cs | 20 ------------------- 19 files changed, 28 insertions(+), 48 deletions(-) diff --git a/src/ComputeSharp.D2D1.UI/Extensions/ICanvasDeviceExtensions.cs b/src/ComputeSharp.D2D1.UI/Extensions/ICanvasDeviceExtensions.cs index ed84a11b..ab5a75e4 100644 --- a/src/ComputeSharp.D2D1.UI/Extensions/ICanvasDeviceExtensions.cs +++ b/src/ComputeSharp.D2D1.UI/Extensions/ICanvasDeviceExtensions.cs @@ -77,7 +77,7 @@ internal static unsafe class ICanvasDeviceExtensions else { // Otherwise, just use the input device context - *d2D1DeviceContextEffective = new ComPtr(d2D1DeviceContext); + ComPtr.WriteTo(d2D1DeviceContext, out *d2D1DeviceContextEffective); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index befe939c..016291a9 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -153,7 +153,7 @@ internal unsafe partial struct PixelShaderEffect for (int i = 0; i < resourceTextureDescriptions.Length; i++) { - using ComPtr resourceTextureManager = @this->resourceTextureManagerBuffer[i]; + using ComPtr resourceTextureManager = new(@this->resourceTextureManagerBuffer[i]); // If the current resource texture manager is not set, we cannot render, as there's an unbound resource texture if (resourceTextureManager.Get() is null) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index a9aabc4c..33aed1bf 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -244,7 +244,7 @@ unsafe partial struct PixelShaderEffect default(ArgumentNullException).ThrowIfNull(value); - using ComPtr unknown = (IUnknown*)value; + using ComPtr unknown = new((IUnknown*)value); using ComPtr transformMapper = default; // Check that the input object implements ID2D1TransformMapper @@ -280,7 +280,7 @@ unsafe partial struct PixelShaderEffect default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize)); default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual(index, GetGlobals().ResourceTextureCount); - using ComPtr resourceTextureManager = this.resourceTextureManagerBuffer[index]; + using ComPtr resourceTextureManager = new(this.resourceTextureManagerBuffer[index]); resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data).Assert(); @@ -316,7 +316,7 @@ unsafe partial struct PixelShaderEffect default(ArgumentNullException).ThrowIfNull(value); - using ComPtr unknown = (IUnknown*)value; + using ComPtr unknown = new((IUnknown*)value); using ComPtr resourceTextureManager = default; // Check that the input object implements ID2D1ResourceTextureManager diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index 146c9e4f..4451830d 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -134,7 +134,7 @@ internal sealed unsafe partial class ShaderCompiler return dxcBlobBytecode.Move(); } - return ThrowHslsCompilationException(dxcOperationResult); + return ThrowHslsCompilationException(dxcOperationResult.Get()); } /// diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index a1894e3c..d2829172 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -337,7 +337,7 @@ internal sealed unsafe partial class SwapChainManager : ReferenceTracked this.d3D12Device.Get()->CreateCommandList( 0, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, - this.d3D12CommandAllocator, + this.d3D12CommandAllocator.Get(), null, Win32.__uuidof(), (void**)d3D12GraphicsCommandList).Assert(); diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs index 2544d0c1..c7d3c6d7 100644 --- a/src/ComputeSharp/Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs @@ -55,7 +55,7 @@ internal unsafe struct CommandList : IDisposable // Set the heap descriptor if the command list is not for copy operations if (d3D12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY) { - device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList); + device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList.Get()); } } @@ -78,7 +78,7 @@ internal unsafe struct CommandList : IDisposable out this.d3D12CommandAllocator.GetPinnableReference()); // Set the heap descriptor for the command list - device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList); + device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList.Get()); } /// diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index 00c94b3f..6a245080 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -83,7 +83,7 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// /// Gets the in use for the current instance. /// - public readonly ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap; + public readonly ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap.Get(); /// /// Rents a new bundle of CPU and GPU handles to use in a resource. diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs index 897a7ce3..e51e42c6 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs @@ -49,14 +49,14 @@ unsafe partial class GraphicsDevice { case D3D12_COMMAND_LIST_TYPE_COMPUTE: commandListPool = ref this.computeCommandListPool; - d3D12CommandQueue = this.d3D12ComputeCommandQueue; - d3D12Fence = this.d3D12ComputeFence; + d3D12CommandQueue = this.d3D12ComputeCommandQueue.Get(); + d3D12Fence = this.d3D12ComputeFence.Get(); d3D12FenceValue = ref this.nextD3D12ComputeFenceValue; break; case D3D12_COMMAND_LIST_TYPE_COPY: commandListPool = ref this.copyCommandListPool; - d3D12CommandQueue = this.d3D12CopyCommandQueue; - d3D12Fence = this.d3D12CopyFence; + d3D12CommandQueue = this.d3D12CopyCommandQueue.Get(); + d3D12Fence = this.d3D12CopyFence.Get(); d3D12FenceValue = ref this.nextD3D12CopyFenceValue; break; default: default(ArgumentException).Throw(nameof(commandList)); return; diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs index 24cfa1a5..3a0f89a7 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.Resources.cs @@ -31,7 +31,7 @@ unsafe partial class GraphicsDevice } else { - allocation = null; + allocation = default; d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource(resourceType, sizeInBytes, IsCacheCoherentUMA); } } @@ -68,7 +68,7 @@ unsafe partial class GraphicsDevice } else { - allocation = null; + allocation = default; d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource( resourceType, @@ -114,7 +114,7 @@ unsafe partial class GraphicsDevice } else { - allocation = null; + allocation = default; d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource( resourceType, @@ -164,7 +164,7 @@ unsafe partial class GraphicsDevice } else { - allocation = null; + allocation = default; d3D12Resource = this.d3D12Device.Get()->CreateCommittedResource( resourceType, diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 55006d31..4cf4dfee 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -150,7 +150,7 @@ public abstract unsafe partial class Buffer : IReferenceTrackedObject, IGraph /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the instance for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index ac6372e2..e9f025da 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -135,7 +135,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the instance for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 5aa0b7ac..e95ff83f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -144,7 +144,7 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the instance for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 03692f34..18077312 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -153,7 +153,7 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the instance for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs index fb1f8d02..bfe72475 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -95,7 +95,7 @@ public abstract unsafe partial class TransferBuffer : IReferenceTrackedObject /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the pointer to the start of the mapped buffer data. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs index 1a41b43a..f2195d9c 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs @@ -100,7 +100,7 @@ public abstract unsafe partial class TransferTexture1D : IReferenceTrackedObj /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the value for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 790b4515..f0c2d780 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -107,7 +107,7 @@ public abstract unsafe partial class TransferTexture2D : IReferenceTrackedObj /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the value for the current resource. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 26caf240..e1df8a3d 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -115,7 +115,7 @@ public abstract unsafe partial class TransferTexture3D : IReferenceTrackedObj /// /// Gets the instance currently mapped. /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + internal ID3D12Resource* D3D12Resource => this.d3D12Resource.Get(); /// /// Gets the value for the current resource. diff --git a/src/ComputeSharp/Shaders/Models/PipelineData.cs b/src/ComputeSharp/Shaders/Models/PipelineData.cs index b00f6762..617fac35 100644 --- a/src/ComputeSharp/Shaders/Models/PipelineData.cs +++ b/src/ComputeSharp/Shaders/Models/PipelineData.cs @@ -35,12 +35,12 @@ internal sealed unsafe class PipelineData : ReferenceTrackedObject /// /// Gets the instance for the current object. /// - public ID3D12RootSignature* D3D12RootSignature => this.d3D12RootSignature; + public ID3D12RootSignature* D3D12RootSignature => this.d3D12RootSignature.Get(); /// /// Gets the instance for the current object. /// - public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState; + public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState.Get(); /// protected override void DangerousOnDispose() diff --git a/src/TerraFX.Interop.Windows/ComPtr`1.cs b/src/TerraFX.Interop.Windows/ComPtr`1.cs index e06aae5a..092aa947 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -98,26 +98,6 @@ internal unsafe struct ComPtr : IDisposable 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. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator ComPtr(T* other) - => new ComPtr(other); - - /// Unwraps a instance and returns the internal raw pointer. - /// The instance to unwrap. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator T*(ComPtr other) - => other.Get(); - /// Converts the current object reference to type and assigns that to a target value. /// The interface type to use to try casting the current COM object. /// A raw pointer to the target value to write to. From 7ed580404ab4d91adba3d4337f9582d3ee69af3d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 Oct 2023 20:55:34 +0200 Subject: [PATCH 3/5] Remove unnecessary ComPtr methods --- .../Helpers/SwapChainManager.cs | 2 +- src/TerraFX.Interop.Windows/ComPtr`1.cs | 76 +------------------ 2 files changed, 3 insertions(+), 75 deletions(-) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index d2829172..efcd2acb 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -348,7 +348,7 @@ internal sealed unsafe partial class SwapChainManager : ReferenceTracked using (ComPtr idxgiSwapChain = default) { - this.dxgiSwapChain3.CopyTo(&idxgiSwapChain).Assert(); + this.dxgiSwapChain3.CopyTo(idxgiSwapChain.GetAddressOf()).Assert(); this.swapChainPanelNative.Get()->SetSwapChain(idxgiSwapChain.Get()).Assert(); } diff --git a/src/TerraFX.Interop.Windows/ComPtr`1.cs b/src/TerraFX.Interop.Windows/ComPtr`1.cs index 092aa947..48d5ab37 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -98,56 +98,6 @@ internal unsafe struct ComPtr : IDisposable InternalAddRef(); } - /// Converts the current object reference to type and assigns that to a target value. - /// The interface type to use to try casting the current COM object. - /// A raw pointer to the target value to write to. - /// The result of for the target type . - /// This method will automatically release the target COM object pointed to by , if any. - public readonly HRESULT As(ComPtr* p) - where U : unmanaged - { - return ((IUnknown*)ptr_)->QueryInterface(__uuidof(), (void**)p->ReleaseAndGetAddressOf()); - } - - /// Converts the current object reference to type and assigns that to a target value. - /// The interface type to use to try casting the current COM object. - /// A reference to the target value to write to. - /// The result of for the target type . - /// This method will automatically release the target COM object pointed to by , if any. - public readonly HRESULT As(ref ComPtr other) - where U : unmanaged - { - U* ptr; - HRESULT result = ((IUnknown*)ptr_)->QueryInterface(__uuidof(), (void**)&ptr); - - other.Attach(ptr); - return result; - } - - /// Converts the current object reference to a type indicated by the given IID and assigns that to a target value. - /// The IID indicating the interface type to convert the COM object reference to. - /// A raw pointer to the target value to write to. - /// The result of for the target IID. - /// This method will automatically release the target COM object pointed to by , if any. - public readonly HRESULT AsIID(Guid* riid, ComPtr* other) - { - return ((IUnknown*)ptr_)->QueryInterface(riid, (void**)other->ReleaseAndGetAddressOf()); - } - - /// Converts the current object reference to a type indicated by the given IID and assigns that to a target value. - /// The IID indicating the interface type to convert the COM object reference to. - /// A reference to the target value to write to. - /// The result of for the target IID. - /// This method will automatically release the target COM object pointed to by , if any. - public readonly HRESULT AsIID(Guid* riid, ref ComPtr other) - { - IUnknown* ptr; - HRESULT result = ((IUnknown*)ptr_)->QueryInterface(riid, (void**)&ptr); - - other.Attach(ptr); - return result; - } - /// Releases the current COM object, if any, and replaces the internal pointer with an input raw pointer. /// The input raw pointer to wrap. /// This method will release the current raw pointer, if any, but it will not increment the references for . @@ -308,16 +258,9 @@ internal unsafe struct ComPtr : IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] public T** ReleaseAndGetAddressOf() { - _ = InternalRelease(); - return GetAddressOf(); - } + Dispose(); - /// Resets the current instance by decrementing the reference count for the target COM object and setting the internal raw pointer to . - /// The updated reference count for the COM object that was in use, if any. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint Reset() - { - return InternalRelease(); + return GetAddressOf(); } /// Swaps the current COM object reference with that of a given instance. @@ -350,19 +293,4 @@ internal unsafe struct ComPtr : IDisposable _ = ((IUnknown*)temp)->AddRef(); } } - - // Decrements the reference count for the current COM object, if any - private uint InternalRelease() - { - uint @ref = 0; - T* temp = ptr_; - - if (temp != null) - { - ptr_ = null; - @ref = ((IUnknown*)temp)->Release(); - } - - return @ref; - } } \ No newline at end of file From 352d878f5dcfa0e92771a5606eb70468860e899e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 Oct 2023 21:24:07 +0200 Subject: [PATCH 4/5] Rename ComPtr.Release for consistency --- .../Interop/Effects/PixelShaderEffect.Properties.cs | 4 ++-- .../Shaders/Interop/Effects/PixelShaderEffect.cs | 8 ++++---- .../ResourceManagers/D2D1ResourceTextureManagerImpl.cs | 2 +- src/TerraFX.Interop.Windows/ComPtr`1.cs | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 33aed1bf..e8862adf 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.Dispose(@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.Dispose(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 68782436..59e2d5ae 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -231,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.Dispose(this.d2D1TransformMapper); + ComPtr.Dispose(this.d2D1DrawInfo); + ComPtr.Dispose(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.Dispose(this.resourceTextureManagerBuffer[i]); } NativeMemory.Free(Unsafe.AsPointer(ref this)); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index 36309e44..cb269fd0 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.Dispose(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 48d5ab37..ca4edb48 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -6,10 +6,10 @@ using System; using System.ComponentModel; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using static TerraFX.Interop.Windows.Windows; using static TerraFX.Interop.Windows.S; -using System.Diagnostics.CodeAnalysis; namespace TerraFX.Interop.Windows; @@ -19,12 +19,12 @@ namespace TerraFX.Interop.Windows; internal static unsafe class ComPtr { /// - /// Releases a target COM object, if it's not . + /// Disposes 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 Dispose(T* other) where T : unmanaged { if (other is not null) From c7e70c8383cfd729bf868108909c193d10175d98 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 Oct 2023 21:24:35 +0200 Subject: [PATCH 5/5] Fix wrong IID in IUnknownExtensions.IsSameInstance --- src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs b/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs index 18ced739..e8e06034 100644 --- a/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs +++ b/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs @@ -39,8 +39,8 @@ internal static unsafe class IUnknownExtensions using ComPtr leftUnknown = default; using ComPtr rightUnknown = default; - ((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof(), leftUnknown.GetVoidAddressOf()).Assert(); - ((IUnknown*)right)->QueryInterface(Win32.__uuidof(), rightUnknown.GetVoidAddressOf()).Assert(); + ((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof(), leftUnknown.GetVoidAddressOf()).Assert(); + ((IUnknown*)right)->QueryInterface(Win32.__uuidof(), rightUnknown.GetVoidAddressOf()).Assert(); return leftUnknown.Get() == rightUnknown.Get(); }