diff --git a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index 46677c52..8a217a7a 100644 --- a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -2,12 +2,12 @@ net5.0-windows10.0.19041.0 - 8.0 enable true + diff --git a/src/ComputeSharp.Graphics/DescriptorAllocator2.cs b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs new file mode 100644 index 00000000..395616f1 --- /dev/null +++ b/src/ComputeSharp.Graphics/DescriptorAllocator2.cs @@ -0,0 +1,103 @@ +using System; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics +{ + /// + /// A type that provides logic to create resource descriptors for a instance. + /// + internal unsafe struct DescriptorAllocator2 + { + /// + /// The default number of available descriptors per heap. + /// + private const uint DescriptorsPerHeap = 4096; + + /// + /// The in use for the current instance. + /// + public readonly ID3D12DescriptorHeap* D3D12DescriptorHeap; + + /// + /// The dummy object used to handle concurrent allocation requests. + /// + private readonly object allocationLock; + + /// + /// The size of each new descriptor being allocated. + /// + private readonly uint descriptorSize; + + /// + /// The current for the instance in use. + /// + private D3D12_CPU_DESCRIPTOR_HANDLE d3d12CpuDescriptorHandle; + + /// + /// The current for the instance in use. + /// + private D3D12_GPU_DESCRIPTOR_HANDLE d3d12GpuDescriptorHandle; + + /// + /// The number of remaining handles to allocate on the current heap. + /// + private uint remainingHandles; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The instance to use + public DescriptorAllocator2(ID3D12Device* device) + { + D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc; + d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + d3d12DescriptorHeapDesc.NumDescriptors = DescriptorsPerHeap; + d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + d3d12DescriptorHeapDesc.NodeMask = 0; + Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap; + ID3D12DescriptorHeap* d3d12DescriptorHeap; + + int result = device->CreateDescriptorHeap( + &d3d12DescriptorHeapDesc, + &d3d12DescriptorHeapDescGuid, + (void**)&d3d12DescriptorHeap); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + D3D12DescriptorHeap = d3d12DescriptorHeap; + + this.allocationLock = new object(); + this.descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + this.d3d12CpuDescriptorHandle = d3d12DescriptorHeap->GetCPUDescriptorHandleForHeapStart(); + this.d3d12GpuDescriptorHandle = d3d12DescriptorHeap->GetGPUDescriptorHandleForHeapStart(); + this.remainingHandles = DescriptorsPerHeap; + } + + /// + /// Allocates a new CPU and GPU handle pair to use in a memory buffer + /// + public void Allocate( + out D3D12_CPU_DESCRIPTOR_HANDLE d3d12CpuDescriptorHandle, + out D3D12_GPU_DESCRIPTOR_HANDLE d3d12GpuDescriptorHandle) + { + lock (this.allocationLock) + { + if (this.remainingHandles == 0) + { + this.remainingHandles = D3D12DescriptorHeap->GetDesc().NumDescriptors; + this.d3d12CpuDescriptorHandle = D3D12DescriptorHeap->GetCPUDescriptorHandleForHeapStart(); + this.d3d12GpuDescriptorHandle = D3D12DescriptorHeap->GetGPUDescriptorHandleForHeapStart(); + } + + d3d12CpuDescriptorHandle = this.d3d12CpuDescriptorHandle; + d3d12GpuDescriptorHandle = this.d3d12GpuDescriptorHandle; + + this.d3d12CpuDescriptorHandle.ptr += this.descriptorSize; + this.d3d12GpuDescriptorHandle.ptr += this.descriptorSize; + this.remainingHandles--; + } + } + } +}