diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index cf4b905d..30363d12 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -31,9 +31,9 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable private ComPtr d3D12DescriptorHeapNonShaderVisible; /// - /// The array of items with the available descriptor handles. + /// The array of items with the available descriptor handles. /// - private readonly D3D12DescriptorHandleBundle[] d3D12DescriptorHandlePairs; + private readonly ID3D12ResourceDescriptorHandles[] d3D12DescriptorHandlePairs; /// /// The head index for the queue. @@ -58,24 +58,26 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: true); this.d3D12DescriptorHeapNonShaderVisible = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: false); - this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray((int)DescriptorsPerHeap); + 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_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart(); D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart(); + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisibleStart = this.d3D12DescriptorHeapNonShaderVisible.Get()->GetCPUDescriptorHandleForHeapStart(); uint descriptorIncrementSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); for (int i = 0; i < this.d3D12DescriptorHandlePairs.Length; i++) { - this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandleBundle( - in d3D12CpuDescriptorHandle, - in d3D12CpuDescriptorHandleNonShaderVisible, - in d3D12GpuDescriptorHandleStart, - i, - descriptorIncrementSize); + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, i, descriptorIncrementSize); + D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, in d3D12GpuDescriptorHandleStart, i, descriptorIncrementSize); + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, in d3D12CpuDescriptorHandleNonShaderVisibleStart, i, descriptorIncrementSize); + + this.d3D12DescriptorHandlePairs[i] = new ID3D12ResourceDescriptorHandles( + d3D12CpuDescriptorHandle, + d3D12GpuDescriptorHandle, + d3D12CpuDescriptorHandleNonShaderVisible); } } @@ -85,25 +87,16 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable public ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap; /// - /// Rents a new CPU and GPU handle pair to use in a memory buffer. + /// Rents a new bundle of CPU and GPU handles to use in a resource. /// - /// 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) + /// The resulting value. + public void Rent(out ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsGreaterThan(this.size, 0, nameof(size)); - ref readonly D3D12DescriptorHandleBundle d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; - - d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle; - d3D12CpuDescriptorHandleNonShaderVisible = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandleNonShaderVisible; - d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle; + d3D12ResourceDescriptorHandles = this.d3D12DescriptorHandlePairs[this.head++]; if (this.head == DescriptorsPerHeap) { @@ -117,19 +110,14 @@ 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) + /// The returned value. + public void Return(in ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsLessThan(this.size, DescriptorsPerHeap, nameof(size)); - this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandleBundle(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonShaderVisible, d3D12GpuDescriptorHandle); + this.d3D12DescriptorHandlePairs[this.tail++] = d3D12ResourceDescriptorHandles; if (this.tail == DescriptorsPerHeap) { @@ -145,61 +133,4 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { this.d3D12DescriptorHeap.Dispose(); } - - /// - /// A type representing a bundle of reusable descriptor handles. - /// - 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. - /// - /// The value to wrap. - /// The non shader visible value to wrap. - /// The value to wrap. - 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. - /// - /// 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 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/Commands/Interop/ID3D12ResourceDescriptorHandles.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12ResourceDescriptorHandles.cs new file mode 100644 index 00000000..e416d833 --- /dev/null +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12ResourceDescriptorHandles.cs @@ -0,0 +1,40 @@ +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Graphics.Commands.Interop; + +/// +/// A type representing a bundle of reusable resource descriptor handles. +/// +internal readonly struct ID3D12ResourceDescriptorHandles +{ + /// + /// 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; + + /// + /// The value for the current entry, non shader visible. + /// + public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible; + + /// + /// Creates a new instance with the given parameters. + /// + /// The value to wrap. + /// The value to wrap. + /// The non shader visible value to wrap. + public ID3D12ResourceDescriptorHandles( + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible) + { + D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; + D3D12GpuDescriptorHandle = d3D12GpuDescriptorHandle; + D3D12CpuDescriptorHandleNonShaderVisible = d3D12CpuDescriptorHandleNonShaderVisible; + } +} diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1c7b4e7c..fd87c436 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -284,22 +284,16 @@ 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) + internal void RentShaderResourceViewDescriptorHandles(out ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { - this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12CpuDescriptorHandleNonGpuVisible, out d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Rent(out d3D12ResourceDescriptorHandles); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void ReturnShaderResourceViewDescriptorHandles( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + internal void ReturnShaderResourceViewDescriptorHandles(in ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { - this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonGpuVisible, d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Return(in d3D12ResourceDescriptorHandles); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 40b7db1a..9c815e7f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; @@ -38,19 +39,9 @@ public unsafe abstract class Buffer : NativeObject private ComPtr d3D12Resource; /// - /// The instance for the current resource. + /// The instance for the current resource. /// - 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. - /// - private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The size in bytes of the current buffer (this value is never negative). @@ -100,19 +91,19 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); 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); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -146,17 +137,7 @@ public unsafe abstract class Buffer : NativeObject /// /// Gets the instance for the current resource. /// - internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12GpuDescriptorHandle; - - /// - /// Gets the instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; - - /// - /// Gets the non shader visible instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. @@ -219,7 +200,7 @@ public unsafe abstract class Buffer : NativeObject if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -234,4 +215,22 @@ public unsafe abstract class Buffer : NativeObject GraphicsDeviceMismatchException.Throw(this, device); } } + + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + } + + /// + internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index c0c96b40..5b30dad3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -304,22 +304,4 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } } - - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) - { - ThrowIfDisposed(); - ThrowIfDeviceMismatch(device); - - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); - } - - /// - internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) - { - ThrowIfDisposed(); - ThrowIfDeviceMismatch(device); - - return D3D12Resource; - } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index c8e0f27f..3b828db5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -2,6 +2,7 @@ using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; @@ -39,19 +40,9 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper private ComPtr d3D12Resource; /// - /// The instance for the current resource. + /// The instance for the current resource. /// - 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. - /// - private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The default value for the current resource. @@ -125,16 +116,16 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); 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); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -164,17 +155,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// Gets the instance for the current resource. /// - internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12GpuDescriptorHandle; - - /// - /// Gets the instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; - - /// - /// Gets the non shader visible instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. @@ -557,7 +538,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -581,7 +562,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper isNormalized = DXGIFormatHelper.IsNormalizedType(); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index ab71fe6e..55d913df 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -2,6 +2,7 @@ using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; @@ -39,19 +40,9 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper private ComPtr d3D12Resource; /// - /// The instance for the current resource. + /// The instance for the current resource. /// - 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. - /// - private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The default value for the current resource. @@ -130,16 +121,16 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); 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); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -174,17 +165,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// Gets the instance for the current resource. /// - internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12GpuDescriptorHandle; - - /// - /// Gets the instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; - - /// - /// Gets the non shader visible instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. @@ -610,7 +591,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -634,7 +615,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper isNormalized = DXGIFormatHelper.IsNormalizedType(); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } ///