From 958fa15a11d5572e6964229f43e8eb1ddd5a0dc4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 17:28:55 +0100 Subject: [PATCH] Added dispatch helpers for normalized texture interfaces --- .../Commands/CommandList.cs | 2 +- .../__Internals/GraphicsResourceHelper.cs | 136 ++++++++++++++++-- .../Resources/Abstract/Texture2D{T}.cs | 14 +- .../Resources/Abstract/Texture3D{T}.cs | 14 +- .../Mappings/HlslKnownKeywords.cs | 2 +- 5 files changed, 152 insertions(+), 16 deletions(-) diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 3b30eca0..f75edc4f 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -9,7 +9,7 @@ using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Graphics.Commands { /// - /// A command list to set and execute operaations on the GPU. + /// A command list to set and execute operations on the GPU. /// internal unsafe ref struct CommandList { diff --git a/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 03a2c82f..9abde184 100644 --- a/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using ComputeSharp.Resources; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; namespace ComputeSharp.__Internals @@ -13,11 +14,26 @@ namespace ComputeSharp.__Internals [Obsolete("This type is not intended to be used directly by user code")] public static class GraphicsResourceHelper { + /// + /// An interface for non-generic graphics resource types. + /// + internal interface IGraphicsResource + { + /// + /// Validates the given resource for usage with a specified device, and retrieves its GPU descriptor handle. + /// + /// The target instance in use. + /// The GPU descriptor handle for the resource. + D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); + } + /// /// Validates the given buffer for usage with a specified device, and retrieves its GPU descriptor handle. /// + /// The type of values stored in the input buffer. /// The input instance to check. /// The target instance in use. + /// The GPU descriptor handle for the buffer. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -31,37 +47,133 @@ namespace ComputeSharp.__Internals } /// - /// Validates the given buffer for usage with a specified device, and retrieves its GPU descriptor handle. + /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. /// - /// The input instance to check. + /// The type of values stored in the input texture. + /// The input instance to check. /// The target instance in use. + /// The GPU descriptor handle for the texture. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(Texture2D buffer, GraphicsDevice device) + public static ulong ValidateAndGetGpuDescriptorHandle(Texture2D texture, GraphicsDevice device) where T : unmanaged { - buffer.ThrowIfDisposed(); - buffer.ThrowIfDeviceMismatch(device); + texture.ThrowIfDisposed(); + texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in buffer.D3D12GpuDescriptorHandle)); + return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); } /// - /// Validates the given buffer for usage with a specified device, and retrieves its GPU descriptor handle. + /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. /// - /// The input instance to check. + /// The type of normalized values stored in the input texture. + /// The input instance to check. /// The target instance in use. + /// The GPU descriptor handle for the texture. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(Texture3D buffer, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture2D texture, GraphicsDevice device) + where TPixel : unmanaged + { + if (texture is IGraphicsResource resource) + { + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); + + return *(ulong*)&d3D12GpuDescriptorHandle; + } + + return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance"); + } + + /// + /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. + /// + /// The type of normalized values stored in the input texture. + /// The input instance to check. + /// The target instance in use. + /// The GPU descriptor handle for the texture. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteTexture2D texture, GraphicsDevice device) + where TPixel : unmanaged + { + if (texture is IGraphicsResource resource) + { + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); + + return *(ulong*)&d3D12GpuDescriptorHandle; + } + + return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance"); + } + + /// + /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. + /// + /// The type of values stored in the input texture. + /// The input instance to check. + /// The target instance in use. + /// The GPU descriptor handle for the texture. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong ValidateAndGetGpuDescriptorHandle(Texture3D texture, GraphicsDevice device) where T : unmanaged { - buffer.ThrowIfDisposed(); - buffer.ThrowIfDeviceMismatch(device); + texture.ThrowIfDisposed(); + texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in buffer.D3D12GpuDescriptorHandle)); + return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + } + + /// + /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. + /// + /// The type of normalized values stored in the input texture. + /// The input instance to check. + /// The target instance in use. + /// The GPU descriptor handle for the texture. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadOnlyTexture3D texture, GraphicsDevice device) + where TPixel : unmanaged + { + if (texture is IGraphicsResource resource) + { + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); + + return *(ulong*)&d3D12GpuDescriptorHandle; + } + + return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance"); + } + + /// + /// Validates the given texture for usage with a specified device, and retrieves its GPU descriptor handle. + /// + /// The type of normalized values stored in the input texture. + /// The input instance to check. + /// The target instance in use. + /// The GPU descriptor handle for the texture. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is not intended to be called directly by user code")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(IReadWriteTexture3D texture, GraphicsDevice device) + where TPixel : unmanaged + { + if (texture is IGraphicsResource resource) + { + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device); + + return *(ulong*)&d3D12GpuDescriptorHandle; + } + + return ThrowHelper.ThrowArgumentException("The input texture is not a valid instance"); } } } diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs index b2cf57b9..6ea4d30e 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; @@ -15,13 +16,15 @@ using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; using FX = TerraFX.Interop.Windows; +#pragma warning disable CS0618 + namespace ComputeSharp.Resources { /// /// A representing a typed 2D texture stored on GPU memory. /// /// The type of items stored on the texture. - public unsafe abstract class Texture2D : NativeObject + public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { /// @@ -281,5 +284,14 @@ namespace ComputeSharp.Resources GraphicsDeviceMismatchException.Throw(this, device); } } + + /// + D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; + } } } diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index cc6a528c..207afc44 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; @@ -15,13 +16,15 @@ using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; using FX = TerraFX.Interop.Windows; +#pragma warning disable CS0618 + namespace ComputeSharp.Resources { /// /// A representing a typed 3D texture stored on GPU memory. /// /// The type of items stored on the texture. - public unsafe abstract class Texture3D : NativeObject + public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper.IGraphicsResource where T : unmanaged { /// @@ -304,5 +307,14 @@ namespace ComputeSharp.Resources GraphicsDeviceMismatchException.Throw(this, device); } } + + /// + D3D12_GPU_DESCRIPTOR_HANDLE GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuDescriptorHandle(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12GpuDescriptorHandle; + } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs index 750ce0e5..c8f03de0 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownKeywords.cs @@ -28,7 +28,7 @@ namespace ComputeSharp.SourceGenerators.Mappings /// Tries to get the mapped HLSL-compatible identifier name for the input identifier name. /// /// The input identifier name. - /// The mapped identifier name, if aa replacement is needed. + /// The mapped identifier name, if a replacement is needed. /// The HLSL-compatible identifier name that can be used in an HLSL shader. [Pure] public static bool TryGetMappedName(string name, out string? mapped)