From 81da4482001ad7b41f5238d142f4dde16025a89c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 28 Dec 2021 20:42:20 +0100 Subject: [PATCH] Add IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles API --- .../__Internals/GraphicsResourceHelper.cs | 37 ++++++++++++++----- .../Graphics/Resources/Abstract/Buffer{T}.cs | 22 ++++++++--- .../Resources/Abstract/StructuredBuffer{T}.cs | 9 +++++ .../Resources/Abstract/Texture2D{T}.cs | 35 +++++++++++++++--- .../Resources/Abstract/Texture3D{T}.cs | 35 +++++++++++++++--- 5 files changed, 112 insertions(+), 26 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 48df0aa0..b88ac3b4 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -26,6 +26,13 @@ public static class GraphicsResourceHelper /// The GPU descriptor handle for the resource. D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); + /// + /// Validates the given resource for usage with a specified device, and retrieves its GPU and CPU descriptor handles. + /// + /// The target instance in use. + /// The GPU and CPU descriptor handles for the resource. + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device); + /// /// Validates the given resource for usage with a specified device, and retrieves the underlying object. /// @@ -42,13 +49,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the buffer. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) where T : unmanaged { buffer.ThrowIfDisposed(); buffer.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in buffer.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -59,13 +68,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -76,13 +87,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -135,13 +148,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -152,13 +167,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index c9fd0889..5da1fd60 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -40,12 +40,12 @@ public unsafe abstract class Buffer : NativeObject /// /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; /// /// The size in bytes of the current buffer (this value is never negative). @@ -95,18 +95,18 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandle); break; } @@ -137,6 +137,16 @@ public unsafe abstract class Buffer : NativeObject /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// 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; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index d9dd43dd..659a8981 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -305,6 +305,15 @@ public abstract class StructuredBuffer : Buffer } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + } + /// internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index ad3ad054..4a109c27 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -41,12 +41,12 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; /// /// The default value for the current resource. @@ -120,15 +120,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandle); break; } @@ -155,6 +155,16 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// 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; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -552,6 +562,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + } + /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { @@ -570,6 +589,12 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + return ValidateAndGetGpuAndCpuDescriptorHandles(device); + } + /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 8cd5ef65..23bddb66 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -41,12 +41,12 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; /// /// The default value for the current resource. @@ -125,15 +125,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandle); break; } @@ -165,6 +165,16 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// 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; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -605,6 +615,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + } + /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { @@ -623,6 +642,12 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + return ValidateAndGetGpuAndCpuDescriptorHandles(device); + } + /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) {