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.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(); } 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..016291a9 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; } @@ -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 6e124bd3..e8862adf 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -244,14 +244,14 @@ 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 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(); @@ -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 @@ -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 9ed9f6ac..59e2d5ae 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.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.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..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/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..efcd2acb 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(); @@ -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/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 4f1a3273..ca4edb48 100644 --- a/src/TerraFX.Interop.Windows/ComPtr`1.cs +++ b/src/TerraFX.Interop.Windows/ComPtr`1.cs @@ -6,44 +6,26 @@ 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; -/// 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 . + /// 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) { @@ -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,76 +68,34 @@ 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; } +} - /// Converts a raw pointer to a new instance and increments the ref count. +/// 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. - [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. - /// 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 + public ComPtr(T* other) { - 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; + ptr_ = other; + InternalAddRef(); } /// Releases the current COM object, if any, and replaces the internal pointer with an input raw pointer. @@ -316,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. @@ -358,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