From 265de8d1ea4e70f19d2027972319d0d314cdfca9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Nov 2023 12:10:57 +0100 Subject: [PATCH 1/3] Use ComPtr for all fields of native objects --- ...lShaderEffect.ID2D1DrawTransformMethods.cs | 16 +++++----- ...ixelShaderEffect.ID2D1EffectImplMethods.cs | 6 ++-- .../Effects/PixelShaderEffect.Properties.cs | 18 +++--------- .../Interop/Effects/PixelShaderEffect.cs | 18 ++++++------ ...ManagerImpl.ID2D1ResourceTextureManager.cs | 22 +++++++------- ...mpl.ID2D1ResourceTextureManagerInternal.cs | 17 ++++++----- .../D2D1ResourceTextureManagerImpl.cs | 25 ++++++++-------- ...teContextImpl.ID2D1DrawInfoUpdateContex.cs | 8 ++--- ...tImpl.ID2D1DrawInfoUpdateContexInternal.cs | 3 +- .../D2D1DrawInfoUpdateContextImpl.cs | 10 +++++-- .../Interop/ID3D12AllocationImpl.cs | 27 +++++++---------- .../Interop/ID3D12MemoryAllocatorImpl.cs | 29 +++++-------------- .../Helpers/DeviceHelper.IDXGIFactory6.cs | 14 ++++----- 13 files changed, 96 insertions(+), 117 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 331f4cca..670ff504 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -69,10 +69,10 @@ partial struct PixelShaderEffect { default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectsCount, @this->GetGlobals().InputCount, nameof(inputRectsCount)); - if (@this->d2D1TransformMapper is not null) + if (@this->d2D1TransformMapper.Get() is not null) { // Forward to the current ID2D1TransformMapper instance - @this->d2D1TransformMapper->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount).Assert(); + @this->d2D1TransformMapper.Get()->MapOutputRectToInputRects(outputRect, inputRects, inputRectsCount).Assert(); } else { @@ -117,7 +117,7 @@ partial struct PixelShaderEffect { default(ArgumentOutOfRangeException).ThrowIfNotEqual((int)inputRectCount, @this->GetGlobals().InputCount, nameof(inputRectCount)); - if (@this->d2D1TransformMapper is not null) + if (@this->d2D1TransformMapper.Get() is not null) { using ComPtr d2D1DrawInfoUpdateContext = default; @@ -126,10 +126,10 @@ partial struct PixelShaderEffect drawInfoUpdateContext: d2D1DrawInfoUpdateContext.GetAddressOf(), constantBuffer: @this->constantBuffer, constantBufferSize: @this->GetGlobals().ConstantBufferSize, - d2D1DrawInfo: @this->d2D1DrawInfo).Assert(); + d2D1DrawInfo: @this->d2D1DrawInfo.Get()).Assert(); // Forward the call to the input ID2D1TransformMapper instance - HRESULT hresult = @this->d2D1TransformMapper->MapInputRectsToOutputRect( + HRESULT hresult = @this->d2D1TransformMapper.Get()->MapInputRectsToOutputRect( updateContext: (ID2D1DrawInfoUpdateContext*)d2D1DrawInfoUpdateContext.Get(), inputRects: inputRects, inputOpaqueSubRects: inputOpaqueSubRects, @@ -209,10 +209,10 @@ partial struct PixelShaderEffect { default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual((int)inputIndex, @this->GetGlobals().InputCount, nameof(inputIndex)); - if (@this->d2D1TransformMapper is not null) + if (@this->d2D1TransformMapper.Get() is not null) { // Forward to the current ID2D1TransformMapper instance - @this->d2D1TransformMapper->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect).Assert(); + @this->d2D1TransformMapper.Get()->MapInvalidRect(inputIndex, invalidInputRect, invalidOutputRect).Assert(); } else { @@ -248,7 +248,7 @@ partial struct PixelShaderEffect try { // Store the new ID2D1DrawInfo object - ComPtr.CopyTo(drawInfo, ref @this->d2D1DrawInfo); + @this->d2D1DrawInfo.Attach(new ComPtr(drawInfo).Get()); 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 d60fca99..eee5e906 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -70,7 +70,7 @@ internal unsafe partial struct PixelShaderEffect transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform).Assert(); // Store the new ID2D1EffectContext object - ComPtr.CopyTo(effectContext, ref @this->d2D1EffectContext); + @this->d2D1EffectContext.Attach(new ComPtr(effectContext).Get()); return S.S_OK; } @@ -99,7 +99,7 @@ internal unsafe partial struct PixelShaderEffect // First, set the constant buffer, if available if (@this->constantBuffer is not null) { - @this->d2D1DrawInfo->SetPixelShaderConstantBuffer( + @this->d2D1DrawInfo.Get()->SetPixelShaderConstantBuffer( buffer: @this->constantBuffer, bufferCount: (uint)@this->GetGlobals().ConstantBufferSize).Assert(); } @@ -131,7 +131,7 @@ internal unsafe partial struct PixelShaderEffect ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref resourceTextureDescriptions[i]; // Set the ID2D1ResourceTexture object to the current index in the ID2D1DrawInfo object in use - @this->d2D1DrawInfo->SetResourceTexture( + @this->d2D1DrawInfo.Get()->SetResourceTexture( textureIndex: (uint)resourceTextureDescription.Index, resourceTexture: d2D1ResourceTexture.Get()).Assert(); } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 733d9f57..404b0be1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -107,14 +107,7 @@ unsafe partial struct PixelShaderEffect default(ArgumentNullException).ThrowIfNull(data); default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize)); - if (@this->d2D1TransformMapper is null) - { - *(void**)data = null; - } - else - { - ((IUnknown*)@this->d2D1TransformMapper)->QueryInterface(Windows.__uuidof(), (void**)data).Assert(); - } + @this->d2D1TransformMapper.CopyTo((ID2D1TransformMapper**)data).Assert(); if (actualSize is not null) { @@ -150,11 +143,8 @@ unsafe partial struct PixelShaderEffect // Check that the input object implements ID2D1TransformMapper unknown.CopyTo(transformMapper.GetAddressOf()).Assert(); - // If there's already an existing manager, release it - ComPtr.Dispose(@this->d2D1TransformMapper); - // Store the transform mapper manager into the effect - @this->d2D1TransformMapper = transformMapper.Detach(); + @this->d2D1TransformMapper.Attach(transformMapper.Detach()); return S.S_OK; } @@ -226,7 +216,7 @@ unsafe partial struct PixelShaderEffect unknown.CopyTo(resourceTextureManagerInternal.GetAddressOf()).Assert(); // Initialize the resource texture manager, if an effect context is available - if (this.d2D1EffectContext is not null) + if (this.d2D1EffectContext.Get() is not null) { uint dimensions = (uint)GetGlobals().ResourceTextureDescriptions.Span[index].Dimensions; @@ -235,7 +225,7 @@ unsafe partial struct PixelShaderEffect // enable sharing resource texture managers across different source textures and effects), or // E_INVALIDARG if the manager has already been initialized through the public interface, // and the stored dimensions for that don't match the ones for this resource texture index. - resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions).Assert(); + resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext.Get(), &dimensions).Assert(); } // Store the resource texture manager into the buffer diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 5cbc4ca6..8d5e847f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -80,17 +80,17 @@ internal unsafe partial struct PixelShaderEffect /// /// The instance to use, if any. /// - private ID2D1TransformMapper* d2D1TransformMapper; + private ComPtr d2D1TransformMapper; /// /// The instance currently in use. /// - private ID2D1DrawInfo* d2D1DrawInfo; + private ComPtr d2D1DrawInfo; /// /// The instance currently in use. /// - private ID2D1EffectContext* d2D1EffectContext; + private ComPtr d2D1EffectContext; /// /// The resource texture managers for the current instance. @@ -118,9 +118,9 @@ internal unsafe partial struct PixelShaderEffect @this->referenceCount = 1; @this->globalsHandle = globalsHandle; @this->constantBuffer = null; - @this->d2D1TransformMapper = null; - @this->d2D1DrawInfo = null; - @this->d2D1EffectContext = null; + @this->d2D1TransformMapper = default; + @this->d2D1DrawInfo = default; + @this->d2D1EffectContext = default; @this->resourceTextureManagerBuffer = default; *effectImpl = (IUnknown*)@this; @@ -197,9 +197,9 @@ internal unsafe partial struct PixelShaderEffect NativeMemory.Free(this.constantBuffer); - ComPtr.Dispose(this.d2D1TransformMapper); - ComPtr.Dispose(this.d2D1DrawInfo); - ComPtr.Dispose(this.d2D1EffectContext); + this.d2D1TransformMapper.Dispose(); + this.d2D1DrawInfo.Dispose(); + this.d2D1EffectContext.Dispose(); // Retrieve all possible resource texture managers in use and release the ones that had been // assigned (from one of the property bindings). We just try to dispose all of them here and diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs index b71c856a..076bb4ac 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs @@ -19,7 +19,7 @@ unsafe partial struct D2D1ResourceTextureManagerImpl lock (@this->lockHandle.Target!) { // If the method has already been called, fail - if (@this->d2D1ResourceTexture is not null || + if (@this->d2D1ResourceTexture.Get() is not null || @this->data is not null) { return E.E_NOT_VALID_STATE; @@ -35,20 +35,20 @@ unsafe partial struct D2D1ResourceTextureManagerImpl } // If the method has already been called, just forward the call - if (@this->d2D1EffectContext is not null) + if (@this->d2D1EffectContext.Get() is not null) { - @this->d2D1Multithread->Enter(); + @this->d2D1Multithread.Get()->Enter(); // Create the resource after taking a D2D lock - int hresult = @this->d2D1EffectContext->CreateResourceTexture( + int hresult = @this->d2D1EffectContext.Get()->CreateResourceTexture( resourceId: resourceId, resourceTextureProperties: resourceTextureProperties, data: data, strides: strides, dataSize: dataSize, - resourceTexture: &@this->d2D1ResourceTexture); + resourceTexture: @this->d2D1ResourceTexture.GetAddressOf()); - @this->d2D1Multithread->Leave(); + @this->d2D1Multithread.Get()->Leave(); return hresult; } @@ -76,14 +76,14 @@ unsafe partial struct D2D1ResourceTextureManagerImpl { lock (@this->lockHandle.Target!) { - if (@this->d2D1ResourceTexture is null && + if (@this->d2D1ResourceTexture.Get() is null && @this->data is null) { return E.E_NOT_VALID_STATE; } // If no resource texture already exists, update the staging buffer - if (@this->d2D1ResourceTexture is null) + if (@this->d2D1ResourceTexture.Get() is null) { return ID2D1ResourceTextureManagerMethods.UpdateWithStagingBuffer( @this: @this, @@ -103,10 +103,10 @@ unsafe partial struct D2D1ResourceTextureManagerImpl // checked for this while taking the instance lock). So we can now take the D2D // lock and update the texture directly. We need to take this when we're not also // taking the instance lock, or we might trigger an AB/BA deadlock across threads. - @this->d2D1Multithread->Enter(); + @this->d2D1Multithread.Get()->Enter(); // Take a D2D lock here too to ensure thread safety - int hresult = @this->d2D1ResourceTexture->Update( + int hresult = @this->d2D1ResourceTexture.Get()->Update( minimumExtents: minimumExtents, maximimumExtents: maximimumExtents, strides: strides, @@ -114,7 +114,7 @@ unsafe partial struct D2D1ResourceTextureManagerImpl data: data, dataCount: dataCount); - @this->d2D1Multithread->Leave(); + @this->d2D1Multithread.Get()->Leave(); return hresult; } 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 4e31ccc5..a9492d0a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Shaders.Interop.Extensions; using ComputeSharp.Win32; @@ -70,7 +71,7 @@ partial struct D2D1ResourceTextureManagerImpl // 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) + if (@this->d2D1EffectContext.Get() is null) { using ComPtr d2D1Multithread = default; @@ -88,9 +89,9 @@ 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); - - @this->d2D1Multithread = d2D1Multithread.Detach(); + // Also store the actual multithread object after storing the effect context. + @this->d2D1EffectContext.Attach(new ComPtr(effectContext).Get()); + @this->d2D1Multithread.Attach(d2D1Multithread.Detach()); } return hresult; @@ -117,15 +118,15 @@ partial struct D2D1ResourceTextureManagerImpl lock (@this->lockHandle.Target!) { // If the effect context is null, it means Initialize has not been called yet - if (@this->d2D1EffectContext is null) + if (@this->d2D1EffectContext.Get() is null) { return E.E_NOT_VALID_STATE; } // If the texture has already been created, just return it - if (@this->d2D1ResourceTexture is not null) + if (@this->d2D1ResourceTexture.Get() is not null) { - ComPtr.WriteTo(@this->d2D1ResourceTexture, out *resourceTexture); + @this->d2D1ResourceTexture.CopyTo(resourceTexture).Assert(); return S.S_OK; } @@ -139,7 +140,7 @@ partial struct D2D1ResourceTextureManagerImpl using ComPtr d2D1ResourceTexture = default; // Create the resource now, as it hasn't been created yet - int result = @this->d2D1EffectContext->CreateResourceTexture( + int result = @this->d2D1EffectContext.Get()->CreateResourceTexture( resourceId: @this->resourceId, resourceTextureProperties: &@this->resourceTextureProperties, data: @this->data, diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index 83482269..3700fd5a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -74,17 +74,17 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl /// /// The object currently wrapped by the current instance. /// - private ID2D1EffectContext* d2D1EffectContext; + private ComPtr d2D1EffectContext; /// /// The instance used to lock accesses to and . /// - private ID2D1Multithread* d2D1Multithread; + private ComPtr d2D1Multithread; /// /// The object, if one was created already. /// - private ID2D1ResourceTexture* d2D1ResourceTexture; + private ComPtr d2D1ResourceTexture; /// /// The to identify the resource, if available. @@ -142,9 +142,9 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl @this->lpVtblForID2D1ResourceTextureManager = VtblForID2D1ResourceTextureManager; @this->lpVtblForID2D1ResourceTextureManagerInternal = VtblForID2D1ResourceTextureManagerInternal; @this->referenceCount = 1; - @this->d2D1EffectContext = null; - @this->d2D1Multithread = null; - @this->d2D1ResourceTexture = null; + @this->d2D1EffectContext = default; + @this->d2D1Multithread = default; + @this->d2D1ResourceTexture = default; @this->requiresMultithread = requiresMultithread ? 1 : 0; @this->resourceId = null; @this->resourceTextureProperties = default; @@ -211,24 +211,25 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl this.lockHandle.Free(); // Synchronize on the ID2D1Multithread instance before touching D2D objects - if (this.d2D1Multithread is not null) + if (this.d2D1Multithread.Get() 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.d2D1Multithread.Get()->Enter(); // Release the resource too if it has been created - ComPtr.Dispose(this.d2D1ResourceTexture); + this.d2D1ResourceTexture.Dispose(); // 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 // been released. Releasing an effect context first, in case it was the last reference // to it, will otherwise cause the Release() call on the resource texture to explode. - _ = this.d2D1EffectContext->Release(); + this.d2D1EffectContext.Dispose(); - this.d2D1Multithread->Leave(); + this.d2D1Multithread.Get()->Leave(); - _ = this.d2D1Multithread->Release(); + // Finally release the multithread object after exiting the lock and releasing all other D2D objects + this.d2D1Multithread.Dispose(); } NativeMemory.Free(this.resourceId); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs index afceb039..9672e376 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs @@ -42,7 +42,7 @@ partial struct D2D1DrawInfoUpdateContextImpl return E.E_POINTER; } - if (@this->d2D1DrawInfo is null) + if (@this->d2D1DrawInfo.Get() is null) { return RO.RO_E_CLOSED; } @@ -71,7 +71,7 @@ partial struct D2D1DrawInfoUpdateContextImpl return E.E_NOT_VALID_STATE; } - if (@this->d2D1DrawInfo is null) + if (@this->d2D1DrawInfo.Get() is null) { return RO.RO_E_CLOSED; } @@ -98,7 +98,7 @@ partial struct D2D1DrawInfoUpdateContextImpl return E.E_INVALIDARG; } - if (@this->d2D1DrawInfo is null) + if (@this->d2D1DrawInfo.Get() is null) { return RO.RO_E_CLOSED; } @@ -113,7 +113,7 @@ partial struct D2D1DrawInfoUpdateContextImpl Buffer.MemoryCopy(buffer, @this->constantBuffer, bufferCount, bufferCount); // Propagate it to the ID2D1DrawInfo object in use as well - return @this->d2D1DrawInfo->SetPixelShaderConstantBuffer(buffer, bufferCount); + return @this->d2D1DrawInfo.Get()->SetPixelShaderConstantBuffer(buffer, bufferCount); } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs index 9063310e..2507c776 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs @@ -10,7 +10,8 @@ unsafe partial struct D2D1DrawInfoUpdateContextImpl /// public int Close() { - this.d2D1DrawInfo = null; + // No need to call Release on the ID2D1DrawInfo object (see notes in the Factory method) + this.d2D1DrawInfo = default; return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs index b9a7272a..7d4c4f3f 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs @@ -75,7 +75,7 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl /// /// The instance currently in use. /// - private ID2D1DrawInfo* d2D1DrawInfo; + private ComPtr d2D1DrawInfo; /// /// The factory method for instances. @@ -109,7 +109,13 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl @this->referenceCount = 1; @this->constantBuffer = constantBuffer; @this->constantBufferSize = constantBufferSize; - @this->d2D1DrawInfo = d2D1DrawInfo; + + // This ID2D1DrawInfo manager instance is short lived, and the contract guarantees that it will not remain + // alive after the underlying ID2D1DrawInfo instance has been invalidated. As such, we can optimize the + // initialization by skipping the AddRef/Release calls on the ID2D1DrawInfo object, and simply storing the + // pointer to the object. When this instance is closed, that will just be set to null again. + @this->d2D1DrawInfo = default; + @this->d2D1DrawInfo.Attach(d2D1DrawInfo); *drawInfoUpdateContext = @this; diff --git a/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12AllocationImpl.cs b/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12AllocationImpl.cs index 65018e87..685dba11 100644 --- a/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12AllocationImpl.cs +++ b/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12AllocationImpl.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using ComputeSharp.Core.Extensions; using ComputeSharp.Interop.Allocation; using ComputeSharp.Win32; using D3D12MA_Allocation = TerraFX.Interop.DirectX.D3D12MA_Allocation; @@ -47,17 +48,17 @@ internal unsafe struct ID3D12AllocationImpl /// /// The parent instance. /// - private ID3D12MemoryAllocatorImpl* allocatorImpl; + private ComPtr allocatorImpl; /// /// The wrapped instance. /// - private D3D12MA_Allocation* allocation; + private ComPtr allocation; /// /// The underlying instance. /// - private ID3D12Resource* d3D12Resource; + private ComPtr d3D12Resource; /// /// The factory method for instances. @@ -86,15 +87,11 @@ internal unsafe struct ID3D12AllocationImpl return E.E_OUTOFMEMORY; } - _ = allocatorImpl->AddRef(); - _ = allocation->AddRef(); - _ = d3D12Resource->AddRef(); - @this->lpVtbl = Vtbl; @this->referenceCount = 1; - @this->allocatorImpl = allocatorImpl; - @this->allocation = allocation; - @this->d3D12Resource = d3D12Resource; + @this->allocatorImpl = new ComPtr(allocatorImpl); + @this->allocation = new ComPtr(allocation); + @this->d3D12Resource = new ComPtr(d3D12Resource); *allocationImpl = @this; @@ -140,15 +137,15 @@ internal unsafe struct ID3D12AllocationImpl if (referenceCount == 0) { - _ = @this->allocation->Release(); - _ = @this->d3D12Resource->Release(); + @this->allocation.Dispose(); + @this->d3D12Resource.Dispose(); // Release the parent allocator as well as last thing. This is needed because the D3D12MA allocator // will fail if an allocator is released while there are still outstanding allocations. So here we // increment the reference count of the allocator when creating a new allocation object, and then we // decrement it after the underlying allocation has been released. This means that if this allocation // is the last thing keeping the allocator alive, it'll be disposed correctly after this last object. - _ = @this->allocatorImpl->Release(); + @this->allocatorImpl.Dispose(); NativeMemory.Free(@this); } @@ -165,9 +162,7 @@ internal unsafe struct ID3D12AllocationImpl return E.E_POINTER; } - _ = @this->d3D12Resource->AddRef(); - - *resource = @this->d3D12Resource; + @this->d3D12Resource.CopyTo(resource).Assert(); return S.S_OK; } diff --git a/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorImpl.cs b/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorImpl.cs index ea7c3d0a..b8a379f2 100644 --- a/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorImpl.cs +++ b/src/ComputeSharp.D3D12MemoryAllocator/Interop/ID3D12MemoryAllocatorImpl.cs @@ -51,12 +51,12 @@ internal unsafe struct ID3D12MemoryAllocatorImpl /// /// The in use associated to the current device. /// - private D3D12MA_Allocator* allocator; + private ComPtr allocator; /// /// The instance in use, if the device is using cache coherent UMA. /// - private D3D12MA_Pool* pool; + private ComPtr pool; /// /// The factory method for instances. @@ -83,19 +83,10 @@ internal unsafe struct ID3D12MemoryAllocatorImpl return E.E_OUTOFMEMORY; } - _ = allocator->AddRef(); - - // The pool is not necessarily available (it's only used for UMA devices), as it allows creating - // resources in a CPU readable memory region. With non UMA devices, we use the default behavior. - if (pool is not null) - { - _ = pool->AddRef(); - } - @this->lpVtbl = Vtbl; @this->referenceCount = 1; - @this->allocator = allocator; - @this->pool = pool; + @this->allocator = new ComPtr(allocator); + @this->pool = new ComPtr(pool); *allocatorImpl = @this; @@ -122,12 +113,8 @@ internal unsafe struct ID3D12MemoryAllocatorImpl // must have been disposed already before the allocator is released. This is done by // having the allocator also increment the reference count of their associated allocator. // So here we just have to release first the pool, if present, and then the allocator. - if (this.pool is not null) - { - _ = this.pool->Release(); - } - - _ = this.allocator->Release(); + this.pool.Dispose(); + this.allocator.Dispose(); NativeMemory.Free(Unsafe.AsPointer(ref this)); } @@ -190,13 +177,13 @@ internal unsafe struct ID3D12MemoryAllocatorImpl D3D12MA_ALLOCATION_DESC allocationDesc = default; allocationDesc.HeapType = (TerraFX.Interop.DirectX.D3D12_HEAP_TYPE)heapType; allocationDesc.Flags = allocationFlags; - allocationDesc.CustomPool = @this->pool; + allocationDesc.CustomPool = @this->pool.Get(); using ComPtr d3D12MA_allocation = default; using ComPtr d3D12Resource = default; // Invoke D3D12MA and create an allocation and resource - TerraFX.Interop.Windows.HRESULT hresult = @this->allocator->CreateResource( + TerraFX.Interop.Windows.HRESULT hresult = @this->allocator.Get()->CreateResource( pAllocDesc: &allocationDesc, pResourceDesc: (TerraFX.Interop.DirectX.D3D12_RESOURCE_DESC*)resourceDescription, InitialResourceState: (TerraFX.Interop.DirectX.D3D12_RESOURCE_STATES)resourceStates, diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs index 9fa7cb8e..cd4b9c15 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs @@ -132,7 +132,7 @@ partial class DeviceHelper /// /// The wrapped instance. /// - private IDXGIFactory4* dxgiFactory4; + private ComPtr dxgiFactory4; /// /// Creates and initializes a new instance. @@ -144,9 +144,7 @@ partial class DeviceHelper IDXGIFactory4As6Backcompat* @this = (IDXGIFactory4As6Backcompat*)NativeMemory.Alloc((nuint)sizeof(IDXGIFactory4As6Backcompat)); @this->lpVtbl = Vtbl; - @this->dxgiFactory4 = dxgiFactory4; - - _ = dxgiFactory4->AddRef(); + @this->dxgiFactory4 = new ComPtr(dxgiFactory4); *dxgiFactory6 = (IDXGIFactory6*)@this; } @@ -155,7 +153,7 @@ partial class DeviceHelper [UnmanagedCallersOnly] private static uint Release(IDXGIFactory4As6Backcompat* @this) { - _ = @this->dxgiFactory4->Release(); + @this->dxgiFactory4.Dispose(); NativeMemory.Free(@this); @@ -166,21 +164,21 @@ partial class DeviceHelper [UnmanagedCallersOnly] private static int EnumAdapters(IDXGIFactory4As6Backcompat* @this, uint Adapter, IDXGIAdapter** ppAdapter) { - return @this->dxgiFactory4->EnumAdapters(Adapter, ppAdapter); + return @this->dxgiFactory4.Get()->EnumAdapters(Adapter, ppAdapter); } /// [UnmanagedCallersOnly] private static int EnumWarpAdapter(IDXGIFactory4As6Backcompat* @this, Guid* riid, void** ppvAdapter) { - return @this->dxgiFactory4->EnumWarpAdapter(riid, ppvAdapter); + return @this->dxgiFactory4.Get()->EnumWarpAdapter(riid, ppvAdapter); } /// [UnmanagedCallersOnly] private static int EnumAdapterByGpuPreference(IDXGIFactory4As6Backcompat* @this, uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, Guid* riid, void** ppvAdapter) { - return @this->dxgiFactory4->EnumAdapters1(Adapter, (IDXGIAdapter1**)ppvAdapter); + return @this->dxgiFactory4.Get()->EnumAdapters1(Adapter, (IDXGIAdapter1**)ppvAdapter); } } } \ No newline at end of file From c7664916ad9913359ac38f552995df5ac566c6e3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Nov 2023 13:57:07 +0100 Subject: [PATCH 2/3] Remove leftover ComPtr extensions --- .../Extensions/ICanvasDeviceExtensions.cs | 2 +- src/ComputeSharp.Win32.D3D12/ComPtr`1.cs | 68 ------------------- 2 files changed, 1 insertion(+), 69 deletions(-) diff --git a/src/ComputeSharp.D2D1.WinUI/Extensions/ICanvasDeviceExtensions.cs b/src/ComputeSharp.D2D1.WinUI/Extensions/ICanvasDeviceExtensions.cs index d4d6d476..c44ccee0 100644 --- a/src/ComputeSharp.D2D1.WinUI/Extensions/ICanvasDeviceExtensions.cs +++ b/src/ComputeSharp.D2D1.WinUI/Extensions/ICanvasDeviceExtensions.cs @@ -71,7 +71,7 @@ internal static unsafe class ICanvasDeviceExtensions else { // Otherwise, just use the input device context - ComPtr.WriteTo(d2D1DeviceContext, out *d2D1DeviceContextEffective); + *d2D1DeviceContextEffective = new ComPtr(d2D1DeviceContext).Get(); } } } \ No newline at end of file diff --git a/src/ComputeSharp.Win32.D3D12/ComPtr`1.cs b/src/ComputeSharp.Win32.D3D12/ComPtr`1.cs index 58d6a1be..319b21e0 100644 --- a/src/ComputeSharp.Win32.D3D12/ComPtr`1.cs +++ b/src/ComputeSharp.Win32.D3D12/ComPtr`1.cs @@ -13,74 +13,6 @@ using static ComputeSharp.Win32.S; namespace ComputeSharp.Win32; -/// -/// Extensions for to operate on raw pointers as well. -/// -internal static unsafe class ComPtr -{ - /// - /// 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 Dispose(T* other) - where T : unmanaged - { - if (other is not null) - { - _ = ((IUnknown*)other)->Release(); - } - } - - /// - /// 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) - 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 - // (which can be the case even if the two pointers are not identical). To double check, a full check - // through an IUnknown* cast would be needed, but to avoid the extra QueryInterface call on both objects, - // we can just always do one AddRef call which avoids issues in all cases as well. The situation we need - // to avoid is that without a temporary copy, releasing the target object might actually destroy the - // object, causing the following AddRef call to AV. - if (obj is not null) - { - _ = ((IUnknown*)obj)->AddRef(); - } - - T* pointer = other; - - if (pointer is not null) - { - _ = ((IUnknown*)pointer)->Release(); - } - - other = obj; - } - - /// - /// 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) - 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. From 3840dce77819c11d4c7c2615702d23311a52462e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 21 Nov 2023 13:05:19 +0100 Subject: [PATCH 3/3] Temporarily disable failing Win2D test See https://github.com/microsoft/Win2D/issues/935 --- .github/workflows/dotnet.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 62652467..fd9a1775 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -270,8 +270,9 @@ jobs: msbuild samples\ComputeSharp.SwapChain.D2D1.Cli\ComputeSharp.SwapChain.D2D1.Cli.csproj /restore -t:publish /p:Configuration=Release /p:Platform=${{matrix.platform}} /p:RuntimeIdentifier=win-${{matrix.platform}} - # Just like for the DX12 sample, run it on x64 to validate it works correctly - - if: matrix.platform == 'x64' + # Just like for the DX12 sample, run it on x64 to validate it works correctly. + # Temporarily disabled due to https://github.com/microsoft/Win2D/issues/935. + - if: false # matrix.platform == 'x64' name: Run ComputeSharp.SwapChain.D2D1.Cli run: > $process = (Start-Process samples\ComputeSharp.SwapChain.D2D1.Cli\bin\x64\Release\net8.0-windows10.0.22621\win-x64\publish\computesharp.d2d1.cli.exe -PassThru);