diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 6d1cb6ba..ea0c3099 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -23,6 +23,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// private ComPtr d3D12Resource; + /// + /// The instance for the current resource. + /// + internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + /// /// The instance for the current resource. /// @@ -61,18 +66,18 @@ namespace ComputeSharp.Graphics.Buffers.Abstract this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)SizeInBytes); - device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource, SizeInBytes, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource, SizeInBytes, D3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, (uint)length, elementSizeInBytes, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); break; } } @@ -147,6 +152,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract protected override void OnDispose() { this.d3D12Resource.Dispose(); + + if (!GraphicsDevice.IsDisposed) + { + GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + } } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 2f00b7dd..b9ead6eb 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -28,6 +28,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// private ComPtr d3D12Resource; + /// + /// The instance for the current resource. + /// + internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + /// /// The instance for the current resource. /// @@ -73,15 +78,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; - device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); break; } } @@ -251,6 +256,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract protected override void OnDispose() { this.d3D12Resource.Dispose(); + + if (!GraphicsDevice.IsDisposed) + { + GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + } } /// diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index 12599cdc..6fd52950 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -28,6 +28,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// private ComPtr d3D12Resource; + /// + /// The instance for the current resource. + /// + internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + /// /// The instance for the current resource. /// @@ -77,15 +82,15 @@ namespace ComputeSharp.Graphics.Buffers.Abstract ? D3D12_COMMAND_LIST_TYPE_COPY : D3D12_COMMAND_LIST_TYPE_COMPUTE; - device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); break; } } @@ -274,6 +279,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract protected override void OnDispose() { this.d3D12Resource.Dispose(); + + if (!GraphicsDevice.IsDisposed) + { + GraphicsDevice.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + } } /// diff --git a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index 0d96f337..f2df9cc7 100644 --- a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -1,4 +1,5 @@ using ComputeSharp.Graphics.Extensions; +using Microsoft.Toolkit.Diagnostics; using System; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; @@ -21,29 +22,24 @@ namespace ComputeSharp.Graphics private ComPtr d3D12DescriptorHeap; /// - /// The dummy object used to handle concurrent allocation requests. + /// The array of items with the available descriptor handles. /// - private readonly object allocationLock; + private readonly D3D12DescriptorHandlePair[] d3D12DescriptorHandlePairs; /// - /// The size of each new descriptor being allocated. + /// The head index for the queue. /// - private readonly uint descriptorSize; + private int head; /// - /// The current for the instance in use. + /// The tail index for the queue. /// - private D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + private int tail; /// - /// The current for the instance in use. + /// The current size of the queue (ie. the number of remaining pairs in ). /// - private D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; - - /// - /// The number of remaining handles to allocate on the current heap. - /// - private uint remainingHandles; + private int size; /// /// Creates a new instance with the specified parameters. @@ -52,11 +48,23 @@ namespace ComputeSharp.Graphics public ID3D12DescriptorHandleAllocator(ID3D12Device* device) { this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap); - this.allocationLock = new object(); - this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); - this.d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart(); - this.d3D12GpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart(); - this.remainingHandles = this.d3D12DescriptorHeap.Get()->GetDesc().NumDescriptors; + this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray((int)DescriptorsPerHeap); + this.head = 0; + this.tail = 0; + this.size = (int)DescriptorsPerHeap; + + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart(); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart(); + uint descriptorIncrementSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + + for (int i = 0; i < this.d3D12DescriptorHandlePairs.Length; i++) + { + this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandlePair( + in d3D12CpuDescriptorHandle, + in d3D12GpuDescriptorHandleStart, + i, + descriptorIncrementSize); + } } /// @@ -65,29 +73,53 @@ namespace ComputeSharp.Graphics public ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap; /// - /// Allocates a new CPU and GPU handle pair to use in a memory buffer. + /// Rents a new CPU and GPU handle pair to use in a memory buffer. /// /// The resulting value. /// The resulting value. - public void Allocate( + public void Rent( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { - lock (this.allocationLock) + lock (this.d3D12DescriptorHandlePairs) { - if (this.remainingHandles == 0) + Guard.IsGreaterThan(this.size, 0, nameof(size)); + + ref readonly D3D12DescriptorHandlePair d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; + + d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle; + d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle; + + if (this.head == DescriptorsPerHeap) { - this.d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart(); - this.d3D12GpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart(); - this.remainingHandles = this.d3D12DescriptorHeap.Get()->GetDesc().NumDescriptors; + this.head = 0; } - d3D12CpuDescriptorHandle = this.d3D12CpuDescriptorHandle; - d3D12GpuDescriptorHandle = this.d3D12GpuDescriptorHandle; + this.size--; + } + } - this.d3D12CpuDescriptorHandle.ptr += this.descriptorSize; - this.d3D12GpuDescriptorHandle.ptr += this.descriptorSize; - this.remainingHandles--; + /// + /// Returns a CPU and GPU handle pair for later use. + /// + /// The returned value. + /// The returned value. + public void Return( + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + { + lock (this.d3D12DescriptorHandlePairs) + { + Guard.IsLessThan(this.size, DescriptorsPerHeap, nameof(size)); + + this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandlePair(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + + if (this.tail == DescriptorsPerHeap) + { + this.tail = 0; + } + + this.size++; } } @@ -96,5 +128,51 @@ namespace ComputeSharp.Graphics { this.d3D12DescriptorHeap.Dispose(); } + + /// + /// A type representing a pair of reusable descriptor handles. + /// + private readonly struct D3D12DescriptorHandlePair + { + /// + /// The value for the current entry. + /// + public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + + /// + /// The value for the current entry. + /// + public readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorhandle; + + /// + /// Creates a new instance with the given parameters. + /// + /// The value to wrap. + /// The value to wrap. + public D3D12DescriptorHandlePair( + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + { + D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; + D3D12GpuDescriptorhandle = d3D12GpuDescriptorHandle; + } + + /// + /// Creates a new instance with the given parameters. + /// + /// The base value. + /// The base value. + /// The offset for the new pair of handles to compute. + /// The increment size for each consecutive handles pair. + public D3D12DescriptorHandlePair( + in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart, + in D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart, + int offset, + uint descriptorIncrementSize) + { + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, offset, descriptorIncrementSize); + D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12GpuDescriptorhandle, in d3D12GpuDescriptorHandleStart, offset, descriptorIncrementSize); + } + } } } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index cdfad124..a6cc52f9 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -126,11 +126,19 @@ namespace ComputeSharp.Graphics internal ID3D12Device* D3D12Device => this.d3D12Device; /// - internal void AllocateShaderResourceViewDescriptorHandles( + internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { - this.shaderResourceViewDescriptorAllocator.Allocate(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle); + } + + /// + internal void ReturnShaderResourceViewDescriptorHandles( + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + { + this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); } /// diff --git a/src/ComputeSharp.Graphics/Interop/NativeObject.cs b/src/ComputeSharp.Graphics/Interop/NativeObject.cs index 9a79f8f0..7c22b77b 100644 --- a/src/ComputeSharp.Graphics/Interop/NativeObject.cs +++ b/src/ComputeSharp.Graphics/Interop/NativeObject.cs @@ -8,11 +8,6 @@ namespace ComputeSharp.Core.Interop /// public abstract class NativeObject : IDisposable { - /// - /// Indicates whether or not the current instance has already been disposed. - /// - private bool isDisposed; - /// /// Releases unmanaged resources and performs other cleanup operations. /// @@ -21,6 +16,11 @@ namespace ComputeSharp.Core.Interop CheckAndDispose(); } + /// + /// Gets whether or not the current instance has already been disposed. + /// + internal bool IsDisposed { get; private set; } + /// /// Releases all the native resources for the current instance. /// @@ -36,9 +36,9 @@ namespace ComputeSharp.Core.Interop /// private void CheckAndDispose() { - if (!this.isDisposed) + if (!IsDisposed) { - this.isDisposed = true; + IsDisposed = true; OnDispose(); } @@ -55,7 +55,7 @@ namespace ComputeSharp.Core.Interop [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void ThrowIfDisposed() { - if (this.isDisposed) + if (IsDisposed) { // We can't use ThrowHelper here as we only want to invoke ToString if we // are about to throw an exception. The JIT will recognize this pattern