Use ComPtr<T> for all fields of native objects

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-21 12:07:14 +01:00
1 親 423ee790ca
コミット 265de8d1ea
13ファイル変更
+96 -117

検索条件に一致するファイルはありません

+11 -16
ファイルの表示
@@ -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
/// <summary>
/// The parent <see cref="ID3D12MemoryAllocatorImpl"/> instance.
/// </summary>
private ID3D12MemoryAllocatorImpl* allocatorImpl;
private ComPtr<ID3D12MemoryAllocatorImpl> allocatorImpl;
/// <summary>
/// The wrapped <see cref="D3D12MA_Allocation"/> instance.
/// </summary>
private D3D12MA_Allocation* allocation;
private ComPtr<D3D12MA_Allocation> allocation;
/// <summary>
/// The underlying <see cref="ID3D12Resource"/> instance.
/// </summary>
private ID3D12Resource* d3D12Resource;
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The factory method for <see cref="ID3D12AllocationImpl"/> 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<ID3D12MemoryAllocatorImpl>(allocatorImpl);
@this->allocation = new ComPtr<D3D12MA_Allocation>(allocation);
@this->d3D12Resource = new ComPtr<ID3D12Resource>(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;
}
+8 -21
ファイルの表示
@@ -51,12 +51,12 @@ internal unsafe struct ID3D12MemoryAllocatorImpl
/// <summary>
/// The <see cref="D3D12MA_Allocator"/> in use associated to the current device.
/// </summary>
private D3D12MA_Allocator* allocator;
private ComPtr<D3D12MA_Allocator> allocator;
/// <summary>
/// The <see cref="D3D12MA_Pool"/> instance in use, if the device is using cache coherent UMA.
/// </summary>
private D3D12MA_Pool* pool;
private ComPtr<D3D12MA_Pool> pool;
/// <summary>
/// The factory method for <see cref="ID3D12MemoryAllocatorImpl"/> 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<D3D12MA_Allocator>(allocator);
@this->pool = new ComPtr<D3D12MA_Pool>(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> d3D12MA_allocation = default;
using ComPtr<ID3D12Resource> 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,