From 2d0e8a51c7d3b3b0a8d7b642c1e8972e983c4cc6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Aug 2019 00:24:16 +0200 Subject: [PATCH] DescriptorAllocator class refactored --- src/ComputeSharp.Graphics/Buffers/Buffer.cs | 4 +- .../DescriptorAllocator.cs | 119 +++++++++--------- src/ComputeSharp.Graphics/GraphicsDevice.cs | 2 +- 3 files changed, 66 insertions(+), 59 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer.cs b/src/ComputeSharp.Graphics/Buffers/Buffer.cs index 557e8428..0a1da014 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer.cs @@ -66,7 +66,7 @@ namespace ComputeSharp.Graphics.Buffers [Pure] private (CpuDescriptorHandle, GpuDescriptorHandle) CreateConstantBufferView() { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(); int constantBufferSize = (SizeInBytes + 255) & ~255; @@ -88,7 +88,7 @@ namespace ComputeSharp.Graphics.Buffers [Pure] private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); + (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(); UnorderedAccessViewDescription description = new UnorderedAccessViewDescription { diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator.cs b/src/ComputeSharp.Graphics/DescriptorAllocator.cs index 5a041bf2..ee34f95f 100644 --- a/src/ComputeSharp.Graphics/DescriptorAllocator.cs +++ b/src/ComputeSharp.Graphics/DescriptorAllocator.cs @@ -3,88 +3,95 @@ using SharpDX.Direct3D12; namespace ComputeSharp.Graphics { + /// + /// A that provides logic to create resource descriptors for a instance + /// internal sealed class DescriptorAllocator : IDisposable { + /// + /// The default number of available descriptors per heap + /// private const int DescriptorsPerHeap = 4096; - private readonly object allocatorLock = new object(); - private readonly int descriptorSize; + /// + /// The dummy object used to handle concurrent allocation requests + /// + private readonly object Lock = new object(); - private CpuDescriptorHandle currentCpuHandle; - private GpuDescriptorHandle currentGpuHandle; - private int remainingHandles; + /// + /// The size of each new descriptor being allocated + /// + private readonly int DescriptorSize; - public DescriptorAllocator(GraphicsDevice device, DescriptorHeapType descriptorHeapType, DescriptorHeapFlags descriptorHeapFlags = DescriptorHeapFlags.None, int descriptorCount = DescriptorsPerHeap) + /// + /// The current for the instance in use + /// + private CpuDescriptorHandle _CurrentCpuHandle; + + /// + /// The current for the instance in use + /// + private GpuDescriptorHandle _CurrentGpuHandle; + + /// + /// The number of remaining handles to allocate on the current heap + /// + private int _RemainingHandles; + + /// + /// Creates a new instance with the specified parameters + /// + /// The instance to use + public DescriptorAllocator(GraphicsDevice device) { - if (descriptorCount < 1 || descriptorCount > DescriptorsPerHeap) - { - throw new ArgumentOutOfRangeException(nameof(descriptorCount), $"Descriptor count must be between 1 and {DescriptorsPerHeap}."); - } + DescriptorSize = device.NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); - descriptorSize = device.NativeDevice.GetDescriptorHandleIncrementSize(descriptorHeapType); - - DescriptorHeapDescription rtvHeapDescription = new DescriptorHeapDescription + DescriptorHeapDescription descriptorHeapDescription = new DescriptorHeapDescription { - DescriptorCount = descriptorCount, - Flags = descriptorHeapFlags, - Type = descriptorHeapType + DescriptorCount = DescriptorsPerHeap, + Flags = DescriptorHeapFlags.ShaderVisible, + Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; - DescriptorHeap = device.NativeDevice.CreateDescriptorHeap(rtvHeapDescription); + DescriptorHeap = device.NativeDevice.CreateDescriptorHeap(descriptorHeapDescription); - remainingHandles = descriptorCount; - currentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart; - currentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart; + _RemainingHandles = DescriptorsPerHeap; + _CurrentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart; + _CurrentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart; } + /// + /// Gets the object in use for the current instance + /// public DescriptorHeap DescriptorHeap { get; } - public (CpuDescriptorHandle, GpuDescriptorHandle) Allocate(int count) + /// + /// Allocates a new CPU and GPU handle pair to use in a memory buffer + /// + /// A pair of and to use + public (CpuDescriptorHandle, GpuDescriptorHandle) Allocate() { - if (count < 1 || (count > remainingHandles && remainingHandles != 0)) + lock (Lock) { - throw new ArgumentOutOfRangeException(nameof(count), "Count must be between 1 and the remaining handles if the remaining handles are not 0."); - } - - lock (allocatorLock) - { - if (remainingHandles == 0) + if (_RemainingHandles == 0) { - remainingHandles = DescriptorHeap.Description.DescriptorCount; - currentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart; - currentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart; + _RemainingHandles = DescriptorHeap.Description.DescriptorCount; + _CurrentCpuHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart; + _CurrentGpuHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart; } - CpuDescriptorHandle cpuDescriptorHandle = currentCpuHandle; - GpuDescriptorHandle gpuDescriptorHandle = currentGpuHandle; + CpuDescriptorHandle cpuDescriptorHandle = _CurrentCpuHandle; + GpuDescriptorHandle gpuDescriptorHandle = _CurrentGpuHandle; - currentCpuHandle += descriptorSize * count; - currentGpuHandle += descriptorSize * count; - remainingHandles -= count; + _CurrentCpuHandle += DescriptorSize; + _CurrentGpuHandle += DescriptorSize; + _RemainingHandles -= 1; return (cpuDescriptorHandle, gpuDescriptorHandle); } } - public (CpuDescriptorHandle, GpuDescriptorHandle) AllocateSlot(int slot) - { - if (slot < 0 || slot > DescriptorHeap.Description.DescriptorCount - 1) - { - throw new ArgumentOutOfRangeException(nameof(slot), "Slot must be between 0 and the descript count - 1."); - } - - lock (allocatorLock) - { - CpuDescriptorHandle cpuDescriptorHandle = DescriptorHeap.CPUDescriptorHandleForHeapStart + descriptorSize * slot; - GpuDescriptorHandle gpuDescriptorHandle = DescriptorHeap.GPUDescriptorHandleForHeapStart + descriptorSize * slot; - - return (cpuDescriptorHandle, gpuDescriptorHandle); - } - } - - public void Dispose() - { - DescriptorHeap.Dispose(); - } + /// + public void Dispose() => DescriptorHeap.Dispose(); } } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 51f33d7e..c5c3cffb 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -62,7 +62,7 @@ namespace ComputeSharp.Graphics NativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None); NativeDirectFence = NativeDevice.CreateFence(0, FenceFlags.None); - ShaderResourceViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, DescriptorHeapFlags.ShaderVisible); + ShaderResourceViewAllocator = new DescriptorAllocator(this); } ///