diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs
index e0dfc511..076f8b75 100644
--- a/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Buffer2{T}.cs
@@ -67,7 +67,7 @@ namespace ComputeSharp.Graphics.Buffers
if (bufferType is BufferType.Constant or BufferType.ReadOnly or BufferType.ReadWrite)
{
- GraphicsDevice.ShaderResourceViewDescriptorAllocator.Allocate(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
+ GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);
switch (bufferType)
{
diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs
index a8c1c802..31a412bf 100644
--- a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs
+++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool2.cs
@@ -1,4 +1,5 @@
using ComputeSharp.Graphics.Extensions;
+using ComputeSharp.Graphics.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
@@ -36,7 +37,7 @@ namespace ComputeSharp.Graphics.Commands
///
/// The input to enqueue.
/// The fence value for the input .
- public void Enqueue(ID3D12CommandAllocator* commandAllocator, ulong fenceValue)
+ public void Enqueue(ComPtr commandAllocator, ulong fenceValue)
{
lock (this.d3d12CommandAllocatorQueue)
{
@@ -47,9 +48,11 @@ namespace ComputeSharp.Graphics.Commands
///
/// Gets a new or reused item to use to issue GPU commands.
///
- [Pure]
- public ID3D12CommandAllocator* GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence)
+ /// The rented object to use.
+ public ComPtr GetCommandAllocator(ID3D12Device* d3D12Device, ID3D12Fence* d3D12Fence)
{
+ using ComPtr d3D12CommandAllocator = default;
+
lock (this.d3d12CommandAllocatorQueue)
{
// See if there is a reusable command allocator
@@ -57,14 +60,18 @@ namespace ComputeSharp.Graphics.Commands
{
ID3D12CommandAllocatorEntry d3D12CommandAllocatorEntry = this.d3d12CommandAllocatorQueue.Peek();
+ d3D12CommandAllocatorEntry.D3D12CommandAllocator.Swap(ref *&d3D12CommandAllocator);
+
// We found one that is no longer in use, reset it and return it
if (d3D12CommandAllocatorEntry.FenceValue <= d3D12Fence->GetCompletedValue())
{
- this.d3d12CommandAllocatorQueue.Dequeue();
+ _ = this.d3d12CommandAllocatorQueue.Dequeue();
- d3D12CommandAllocatorEntry.D3D12CommandAllocator->Reset();
+ int result = d3D12CommandAllocator.Get()->Reset();
- return d3D12CommandAllocatorEntry.D3D12CommandAllocator;
+ ThrowHelper.ThrowIfFailed(result);
+
+ return d3D12CommandAllocator.Move();
}
}
@@ -75,18 +82,26 @@ namespace ComputeSharp.Graphics.Commands
///
public void Dispose()
{
- throw new NotImplementedException();
+ lock (this.d3d12CommandAllocatorQueue)
+ {
+ foreach (var entry in this.d3d12CommandAllocatorQueue)
+ {
+ entry.D3D12CommandAllocator.Dispose();
+ }
+
+ this.d3d12CommandAllocatorQueue.Clear();
+ }
}
///
/// A type that stores info on pooled references.
///
- private readonly unsafe struct ID3D12CommandAllocatorEntry
+ private unsafe struct ID3D12CommandAllocatorEntry
{
///
/// The current reference.
///
- public readonly ID3D12CommandAllocator* D3D12CommandAllocator;
+ public ComPtr D3D12CommandAllocator;
///
/// The fence value for the current reference.
@@ -98,7 +113,7 @@ namespace ComputeSharp.Graphics.Commands
///
/// The input reference.
/// The fence value for the input reference.
- public ID3D12CommandAllocatorEntry(ID3D12CommandAllocator* d3D12CommandAllocator, ulong fenceValue)
+ public ID3D12CommandAllocatorEntry(ComPtr d3D12CommandAllocator, ulong fenceValue)
{
D3D12CommandAllocator = d3D12CommandAllocator;
FenceValue = fenceValue;
diff --git a/src/ComputeSharp.Graphics/Commands/CommandList2.cs b/src/ComputeSharp.Graphics/Commands/CommandList2.cs
index 9fe49507..95a5409b 100644
--- a/src/ComputeSharp.Graphics/Commands/CommandList2.cs
+++ b/src/ComputeSharp.Graphics/Commands/CommandList2.cs
@@ -24,12 +24,12 @@ namespace ComputeSharp.Graphics.Commands
///
/// The object in use by the current instance.
///
- private readonly ID3D12CommandAllocator* d3D12CommandAllocator;
+ private readonly ComPtr d3D12CommandAllocator;
///
/// The object in use by the current instance.
///
- public readonly ID3D12GraphicsCommandList* D3D12GraphicsCommandList;
+ private readonly ComPtr d3D12GraphicsCommandList;
///
/// Creates a new instance with the specified parameters.
@@ -40,21 +40,13 @@ namespace ComputeSharp.Graphics.Commands
{
this.device = device;
this.d3d12CommandListType = d3d12CommandListType;
- this.d3D12CommandAllocator = d3d12CommandListType switch
- {
- D3D12_COMMAND_LIST_TYPE_COMPUTE => device.ComputeCommandAllocatorPool.GetCommandAllocator(device.D3D12Device, device.D3D12ComputeFence),
- D3D12_COMMAND_LIST_TYPE_COPY => device.ComputeCommandAllocatorPool.GetCommandAllocator(device.D3D12Device, device.D3D12CopyFence),
- _ => throw null!
- };
-
- D3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3d12CommandListType, d3D12CommandAllocator);
+ this.d3D12CommandAllocator = device.GetCommandAllocator(d3d12CommandListType);
+ this.d3D12GraphicsCommandList = device.D3D12Device->CreateCommandList(d3d12CommandListType, this.d3D12CommandAllocator);
// Set the heap descriptor if the command list is not for copy operations
if (d3d12CommandListType is not D3D12_COMMAND_LIST_TYPE_COPY)
{
- ID3D12DescriptorHeap* d3D12DescriptorHeap = device.ShaderResourceViewDescriptorAllocator.D3D12DescriptorHeap;
-
- D3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap);
+ device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList);
}
}
@@ -68,7 +60,7 @@ namespace ComputeSharp.Graphics.Commands
/// The total number of bytes to copy from one resource to another.
public void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes)
{
- D3D12GraphicsCommandList->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes);
+ this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes);
}
///
@@ -78,7 +70,7 @@ namespace ComputeSharp.Graphics.Commands
/// The input instance to bind.
public void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
- D3D12GraphicsCommandList->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
+ this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
}
///
@@ -87,8 +79,8 @@ namespace ComputeSharp.Graphics.Commands
/// The input to setup.
public void SetPipelineData(PipelineData pipelineData)
{
- D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature);
- D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState);
+ this.d3D12GraphicsCommandList.Get()->SetComputeRootSignature(pipelineData.D3D12RootSignature);
+ this.d3D12GraphicsCommandList.Get()->SetPipelineState(pipelineData.D3D12PipelineState);
}
///
@@ -99,7 +91,7 @@ namespace ComputeSharp.Graphics.Commands
/// The number of thread groups to schedule for the Z axis.
public void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ)
{
- D3D12GraphicsCommandList->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ);
+ this.d3D12GraphicsCommandList.Get()->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ);
}
///
@@ -107,7 +99,7 @@ namespace ComputeSharp.Graphics.Commands
///
public void ExecuteAndWaitForCompletion()
{
- D3D12GraphicsCommandList->Close();
+ this.d3D12GraphicsCommandList.Get()->Close();
// TODO: GraphicsDevice.ExecuteCommandList(this);
}
@@ -115,17 +107,9 @@ namespace ComputeSharp.Graphics.Commands
///
public void Dispose()
{
- switch (this.d3d12CommandListType)
- {
- case D3D12_COMMAND_LIST_TYPE_COMPUTE:
- device.ComputeCommandAllocatorPool.Enqueue(this.d3D12CommandAllocator, device.NextD3D12ComputeFenceValue);
- break;
- case D3D12_COMMAND_LIST_TYPE_COPY:
- device.CopyCommandAllocatorPool.Enqueue(this.d3D12CommandAllocator, device.NextD3D12CopyFenceValue);
- break;
- };
+ this.device.EnqueueCommandAllocator(this.d3D12CommandAllocator.Move(), this.d3d12CommandListType);
- D3D12GraphicsCommandList->Release();
+ this.d3D12GraphicsCommandList.Dispose();
}
}
}
diff --git a/src/ComputeSharp.Graphics/Commands/PipelineData.cs b/src/ComputeSharp.Graphics/Commands/PipelineData.cs
index a63f677c..eba2126d 100644
--- a/src/ComputeSharp.Graphics/Commands/PipelineData.cs
+++ b/src/ComputeSharp.Graphics/Commands/PipelineData.cs
@@ -33,18 +33,12 @@ namespace ComputeSharp.Graphics.Commands
///
/// Gets the instance for the current object.
///
- public ID3D12RootSignature* D3D12RootSignature
- {
- get => this.d3D12RootSignature;
- }
+ public ID3D12RootSignature* D3D12RootSignature => this.d3D12RootSignature;
///
/// Gets the instance for the current object.
///
- public ID3D12PipelineState* D3D12PipelineState
- {
- get => this.d3D12PipelineState;
- }
+ public ID3D12PipelineState* D3D12PipelineState => this.d3D12PipelineState;
///
protected override void Dispose(bool disposing)
diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs
index 3809cf06..7eb6bd6b 100644
--- a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs
+++ b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs
@@ -8,7 +8,7 @@ namespace ComputeSharp.Graphics
///
/// A type that provides logic to create resource descriptors for a instance.
///
- internal unsafe struct DescriptorAllocator2
+ internal unsafe struct DescriptorAllocator2 : IDisposable
{
///
/// The default number of available descriptors per heap.
@@ -18,7 +18,7 @@ namespace ComputeSharp.Graphics
///
/// The in use for the current instance.
///
- public readonly ID3D12DescriptorHeap* D3D12DescriptorHeap;
+ private ComPtr d3D12DescriptorHeap;
///
/// The dummy object used to handle concurrent allocation requests.
@@ -51,18 +51,24 @@ namespace ComputeSharp.Graphics
/// The instance to use
public DescriptorAllocator2(ID3D12Device* device)
{
- D3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap);
-
+ this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap);
this.allocationLock = new object();
this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
- this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart();
- this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart();
- this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors;
+ this.d3d12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
+ this.d3d12GpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
+ this.remainingHandles = this.d3D12DescriptorHeap.Get()->GetDesc().NumDescriptors;
}
+ ///
+ /// Gets the in use for the current instance.
+ ///
+ public ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap;
+
///
/// Allocates a new CPU and GPU handle pair to use in a memory buffer
///
+ /// The resulting value.
+ /// The resulting value.
public void Allocate(
out D3D12_CPU_DESCRIPTOR_HANDLE d3d12CpuDescriptorHandle,
out D3D12_GPU_DESCRIPTOR_HANDLE d3d12GpuDescriptorHandle)
@@ -71,9 +77,9 @@ namespace ComputeSharp.Graphics
{
if (this.remainingHandles == 0)
{
- this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart();
- this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart();
- this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors;
+ this.d3d12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
+ this.d3d12GpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
+ this.remainingHandles = this.d3D12DescriptorHeap.Get()->GetDesc().NumDescriptors;
}
d3d12CpuDescriptorHandle = this.d3d12CpuDescriptorHandle;
@@ -85,10 +91,10 @@ namespace ComputeSharp.Graphics
}
}
- ///
+ ///
public void Dispose()
{
- D3D12DescriptorHeap->Release();
+ this.d3D12DescriptorHeap.Dispose();
}
}
}
diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs
index 8a6e035c..c18713ed 100644
--- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs
+++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs
@@ -15,73 +15,74 @@ namespace ComputeSharp.Graphics
///
/// The underlying wrapped by the current instance.
///
- internal readonly ID3D12Device* D3D12Device;
+ private ComPtr d3D12Device;
///
/// The instance to use for compute operations.
///
- internal readonly ID3D12CommandQueue* D3D12ComputeCommandQueue;
+ private ComPtr d3D12ComputeCommandQueue;
///
/// The instance to use for copy operations.
///
- internal readonly ID3D12CommandQueue* D3D12CopyCommandQueue;
+ private ComPtr d3D12CopyCommandQueue;
///
/// The instance used for compute operations.
///
- internal readonly ID3D12Fence* D3D12ComputeFence;
+ private ComPtr d3D12ComputeFence;
///
/// The instance used for copy operations.
///
- internal readonly ID3D12Fence* D3D12CopyFence;
+ private ComPtr d3D12CopyFence;
///
/// The instance for compute operations.
///
- internal readonly CommandAllocatorPool2 ComputeCommandAllocatorPool;
+ private readonly CommandAllocatorPool2 computeCommandAllocatorPool;
///
/// Gets the instance for copy operations.
///
- internal readonly CommandAllocatorPool2 CopyCommandAllocatorPool;
+ private readonly CommandAllocatorPool2 copyCommandAllocatorPool;
///
/// The instance to use when allocating new buffers.
///
- internal DescriptorAllocator2 ShaderResourceViewDescriptorAllocator;
+ private DescriptorAllocator2 shaderResourceViewDescriptorAllocator;
///
- /// The next fence value for compute operations using .
+ /// The next fence value for compute operations using .
///
- internal ulong NextD3D12ComputeFenceValue = 1;
+ private ulong nextD3D12ComputeFenceValue = 1;
///
- /// The next fence value for copy operations using .
+ /// The next fence value for copy operations using .
///
- internal ulong NextD3D12CopyFenceValue = 1;
+ private ulong nextD3D12CopyFenceValue = 1;
///
/// Creates a new instance for the input .
///
/// The to use for the new instance.
/// The available info for the new instance.
- internal GraphicsDevice2(ID3D12Device* d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
+ internal GraphicsDevice2(ComPtr d3d12device, DXGI_ADAPTER_DESC1* dxgiDescription1)
{
- D3D12Device = d3d12device;
- D3D12ComputeCommandQueue = d3d12device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
- D3D12CopyCommandQueue = d3d12device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
- D3D12ComputeFence = d3d12device->CreateFence();
- D3D12CopyFence = d3d12device->CreateFence();
- ComputeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COMPUTE);
- CopyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COPY);
- ShaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device);
+ this.d3D12Device = d3d12device;
+ this.d3D12ComputeCommandQueue = d3d12device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE);
+ this.d3D12CopyCommandQueue = d3d12device.Get()->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY);
+ this.d3D12ComputeFence = d3d12device.Get()->CreateFence();
+ this.d3D12CopyFence = d3d12device.Get()->CreateFence();
+
+ this.computeCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COMPUTE);
+ this.copyCommandAllocatorPool = new CommandAllocatorPool2(D3D12_COMMAND_LIST_TYPE_COPY);
+ this.shaderResourceViewDescriptorAllocator = new DescriptorAllocator2(d3d12device);
Name = new string((char*)dxgiDescription1->Description);
MemorySize = dxgiDescription1->DedicatedVideoMemory;
- var d3D12Options1Data = d3d12device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1);
+ var d3D12Options1Data = d3d12device.Get()->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1);
ComputeUnits = d3D12Options1Data.TotalLaneCount;
WavefrontSize = d3D12Options1Data.WaveLaneCountMin;
@@ -107,17 +108,69 @@ namespace ComputeSharp.Graphics
///
public uint WavefrontSize { get; }
+ ///
+ /// Gets the underlying wrapped by the current instance.
+ ///
+ internal ID3D12Device* D3D12Device => this.d3D12Device;
+
+ ///
+ internal void AllocateShaderResourceViewDescriptorHandles(
+ out D3D12_CPU_DESCRIPTOR_HANDLE d3d12CpuDescriptorHandle,
+ out D3D12_GPU_DESCRIPTOR_HANDLE d3d12GpuDescriptorHandle)
+ {
+ this.shaderResourceViewDescriptorAllocator.Allocate(out d3d12CpuDescriptorHandle, out d3d12GpuDescriptorHandle);
+ }
+
+ ///
+ /// The instance to return to the pool.
+ /// The type of command allocator to return to the pool.
+ internal void EnqueueCommandAllocator(ComPtr d3D12CommandAllocator, D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
+ {
+ switch (d3d12CommandListType)
+ {
+ case D3D12_COMMAND_LIST_TYPE_COMPUTE:
+ this.computeCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12ComputeFenceValue);
+ break;
+ case D3D12_COMMAND_LIST_TYPE_COPY:
+ this.copyCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12CopyFenceValue);
+ break;
+ };
+ }
+
+ ///
+ /// The type of command allocator to rent.
+ internal ComPtr GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
+ {
+ return d3d12CommandListType switch
+ {
+ D3D12_COMMAND_LIST_TYPE_COMPUTE => this.computeCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12ComputeFence),
+ D3D12_COMMAND_LIST_TYPE_COPY => this.computeCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12CopyFence),
+ _ => throw null!
+ };
+ }
+
+ ///
+ /// Sets the descriptor heap for a given instance.
+ ///
+ /// The input instance to use.
+ internal void SetDescriptorHeapForCommandList(ID3D12GraphicsCommandList* d3D12GraphicsCommandList)
+ {
+ ID3D12DescriptorHeap* d3D12DescriptorHeap = this.shaderResourceViewDescriptorAllocator.D3D12DescriptorHeap;
+
+ d3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap);
+ }
+
///
protected override void Dispose(bool disposing)
{
- D3D12Device->Release();
- D3D12ComputeCommandQueue->Release();
- D3D12CopyCommandQueue->Release();
- D3D12ComputeFence->Release();
- D3D12CopyFence->Release();
- ComputeCommandAllocatorPool.Dispose();
- CopyCommandAllocatorPool.Dispose();
- ShaderResourceViewDescriptorAllocator.Dispose();
+ this.d3D12Device.Dispose();
+ this.d3D12ComputeCommandQueue.Dispose();
+ this.d3D12CopyCommandQueue.Dispose();
+ this.d3D12ComputeFence.Dispose();
+ this.d3D12CopyFence.Dispose();
+ this.computeCommandAllocatorPool.Dispose();
+ this.copyCommandAllocatorPool.Dispose();
+ this.shaderResourceViewDescriptorAllocator.Dispose();
}
}
}