From a9f50c1e468577aee3d213499ecc9bf09280e049 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 13:12:01 +0100 Subject: [PATCH] Add non shader visible descriptor heap setup --- .../ID3D12DescriptorHandleAllocator.cs | 50 ++++++++++++++----- .../Interop/ID3D12DeviceExtensions.cs | 5 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 6 ++- .../Graphics/Resources/Abstract/Buffer{T}.cs | 15 +++++- .../Resources/Abstract/StructuredBuffer{T}.cs | 2 +- .../Resources/Abstract/Texture2D{T}.cs | 17 +++++-- .../Resources/Abstract/Texture3D{T}.cs | 10 +++- 7 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index 7fd7ca5b..cf4b905d 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -26,9 +26,14 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable private ComPtr d3D12DescriptorHeap; /// - /// The array of items with the available descriptor handles. + /// The non shader visible in use for the current instance. /// - private readonly D3D12DescriptorHandlePair[] d3D12DescriptorHandlePairs; + private ComPtr d3D12DescriptorHeapNonShaderVisible; + + /// + /// The array of items with the available descriptor handles. + /// + private readonly D3D12DescriptorHandleBundle[] d3D12DescriptorHandlePairs; /// /// The head index for the queue. @@ -51,20 +56,23 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// The instance to use. public ID3D12DescriptorHandleAllocator(ID3D12Device* device) { - this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap); - this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray((int)DescriptorsPerHeap); + this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: true); + this.d3D12DescriptorHeapNonShaderVisible = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: false); + 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_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible = this.d3D12DescriptorHeapNonShaderVisible.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( + this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandleBundle( in d3D12CpuDescriptorHandle, + in d3D12CpuDescriptorHandleNonShaderVisible, in d3D12GpuDescriptorHandleStart, i, descriptorIncrementSize); @@ -80,18 +88,21 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// Rents a new CPU and GPU handle pair to use in a memory buffer. /// /// The resulting value. + /// The resulting non shader visible value. /// The resulting value. public void Rent( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsGreaterThan(this.size, 0, nameof(size)); - ref readonly D3D12DescriptorHandlePair d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; + ref readonly D3D12DescriptorHandleBundle d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle; + d3D12CpuDescriptorHandleNonShaderVisible = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandleNonShaderVisible; d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle; if (this.head == DescriptorsPerHeap) @@ -107,16 +118,18 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// Returns a CPU and GPU handle pair for later use. /// /// The returned value. + /// The returned non shader visible value. /// The returned value. public void Return( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsLessThan(this.size, DescriptorsPerHeap, nameof(size)); - this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandlePair(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandleBundle(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonShaderVisible, d3D12GpuDescriptorHandle); if (this.tail == DescriptorsPerHeap) { @@ -134,47 +147,58 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable } /// - /// A type representing a pair of reusable descriptor handles. + /// A type representing a bundle of reusable descriptor handles. /// - private readonly struct D3D12DescriptorHandlePair + private readonly struct D3D12DescriptorHandleBundle { /// /// The value for the current entry. /// public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + /// + /// The value for the current entry, non shader visible. + /// + public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible; + /// /// The value for the current entry. /// public readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorhandle; /// - /// Creates a new instance with the given parameters. + /// Creates a new instance with the given parameters. /// /// The value to wrap. + /// The non shader visible value to wrap. /// The value to wrap. - public D3D12DescriptorHandlePair( + public D3D12DescriptorHandleBundle( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; + D3D12CpuDescriptorHandleNonShaderVisible = d3D12CpuDescriptorHandleNonShaderVisible; D3D12GpuDescriptorhandle = d3D12GpuDescriptorHandle; } /// - /// Creates a new instance with the given parameters. + /// Creates a new instance with the given parameters. /// /// The base value. + /// The base non shader visible value. /// The base value. /// The offset for the new pair of handles to compute. /// The increment size for each consecutive handles pair. - public D3D12DescriptorHandlePair( + public D3D12DescriptorHandleBundle( in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart, + in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, in D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart, int offset, uint descriptorIncrementSize) { D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, offset, descriptorIncrementSize); + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandleNonShaderVisible, in d3D12CpuDescriptorHandleNonShaderVisible, offset, descriptorIncrementSize); D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12GpuDescriptorhandle, in d3D12GpuDescriptorHandleStart, offset, descriptorIncrementSize); } } diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 477f68c1..2752ec6d 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -321,16 +321,17 @@ internal static unsafe class ID3D12DeviceExtensions /// /// The target to use to create the descriptor heap. /// The number of descriptors to allocate. + /// Whether or not the descriptor heap should be shader visible. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ComPtr CreateDescriptorHeap(this ref ID3D12Device d3D12Device, uint descriptorsCount) + public static ComPtr CreateDescriptorHeap(this ref ID3D12Device d3D12Device, uint descriptorsCount, bool isShaderVisible) { using ComPtr d3D12DescriptorHeap = default; D3D12_DESCRIPTOR_HEAP_DESC d3D12DescriptorHeapDesc; d3D12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; d3D12DescriptorHeapDesc.NumDescriptors = descriptorsCount; - d3D12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + d3D12DescriptorHeapDesc.Flags = isShaderVisible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_NONE; d3D12DescriptorHeapDesc.NodeMask = 0; d3D12Device.CreateDescriptorHeap( diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1ea769fe..1c7b4e7c 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -286,18 +286,20 @@ public sealed unsafe partial class GraphicsDevice : NativeObject [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { - this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12CpuDescriptorHandleNonGpuVisible, out d3D12GpuDescriptorHandle); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void ReturnShaderResourceViewDescriptorHandles( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { - this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonGpuVisible, d3D12GpuDescriptorHandle); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 5da1fd60..51000b13 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -42,6 +42,11 @@ public unsafe abstract class Buffer : NativeObject /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + /// + /// The non shader visible instance for the current resource. + /// + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; + /// /// The instance for the current resource. /// @@ -95,7 +100,7 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); switch (resourceType) { @@ -107,6 +112,7 @@ public unsafe abstract class Buffer : NativeObject break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -147,6 +153,11 @@ public unsafe abstract class Buffer : NativeObject /// internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// + /// Gets the non shader visible instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -208,7 +219,7 @@ public unsafe abstract class Buffer : NativeObject if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 659a8981..33e29398 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -311,7 +311,7 @@ public abstract class StructuredBuffer : Buffer ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4a109c27..802db69b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -43,6 +43,11 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + /// + /// The non shader visible instance for the current resource. + /// + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; + /// /// The instance for the current resource. /// @@ -120,7 +125,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); switch (resourceType) { @@ -129,6 +134,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -165,6 +171,11 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// + /// Gets the non shader visible instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -546,7 +557,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); } } @@ -568,7 +579,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 23bddb66..87327438 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -43,6 +43,11 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + /// + /// The non shader visible instance for the current resource. + /// + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; + /// /// The instance for the current resource. /// @@ -125,7 +130,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); switch (resourceType) { @@ -134,6 +139,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -599,7 +605,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); } }