From c2f6e81e76d8dce07260caf3633a0c747fb24168 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 16:09:36 +0100 Subject: [PATCH 1/6] Added unorm texture interface types --- .../Interfaces/IReadOnlyTexture2D{TPixel}.cs | 26 ++++++++++++++++++ .../Interfaces/IReadOnlyTexture3D{TPixel}.cs | 27 +++++++++++++++++++ .../Interfaces/IReadWriteTexture2D{TPixel}.cs | 26 ++++++++++++++++++ .../Interfaces/IReadWriteTexture3D{TPixel}.cs | 27 +++++++++++++++++++ .../Resources/ReadOnlyTexture2D{T,TPixel}.cs | 15 +++-------- .../Resources/ReadOnlyTexture3D{T,TPixel}.cs | 16 +++-------- .../Resources/ReadWriteTexture2D{T,TPixel}.cs | 15 +++-------- .../Resources/ReadWriteTexture3D{T,TPixel}.cs | 16 +++-------- 8 files changed, 118 insertions(+), 50 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs create mode 100644 src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs create mode 100644 src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs create mode 100644 src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs new file mode 100644 index 00000000..b03ba5c9 --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs @@ -0,0 +1,26 @@ +namespace ComputeSharp +{ + /// + /// An interface representing a typed readonly 2D texture containing normalized pixel data stored on GPU memory. + /// This interface can only be used to wrap instances. + /// + /// The type of normalized pixels used on the GPU side. + public interface IReadOnlyTexture2D + where TPixel : unmanaged + { + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[int x, int y] { get; } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[Int2 xy] { get; } + } +} diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs new file mode 100644 index 00000000..a718f10f --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs @@ -0,0 +1,27 @@ +namespace ComputeSharp +{ + /// + /// An interface representing a typed readonly 3D texture containing normalized pixel data stored on GPU memory. + /// This interface can only be used to wrap instances. + /// + /// The type of normalized pixels used on the GPU side. + public interface IReadOnlyTexture3D + where TPixel : unmanaged + { + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[int x, int y, int z] { get; } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[Int3 xyz] { get; } + } +} diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs new file mode 100644 index 00000000..62cd23f9 --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs @@ -0,0 +1,26 @@ +namespace ComputeSharp +{ + /// + /// An interface representing a typed writeable 2D texture containing normalized pixel data stored on GPU memory. + /// This interface can only be used to wrap instances. + /// + /// The type of normalized pixels used on the GPU side. + public interface IReadWriteTexture2D + where TPixel : unmanaged + { + /// + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[int x, int y] { get; } + + /// + /// Gets or sets a single value from the current writeable texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[Int2 xy] { get; } + } +} diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs new file mode 100644 index 00000000..7e04187e --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs @@ -0,0 +1,27 @@ +namespace ComputeSharp +{ + /// + /// An interface representing a typed writeable 2D texture containing normalized pixel data stored on GPU memory. + /// This interface can only be used to wrap instances. + /// + /// The type of normalized pixels used on the GPU side. + public interface IReadWriteTexture3D + where TPixel : unmanaged + { + /// + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[int x, int y, int z] { get; } + + /// + /// Gets a single value from the current writeable texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[Int3 xyz] { get; } + } +} diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs index b6a6f190..e2a1ab08 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs @@ -16,7 +16,7 @@ namespace ComputeSharp /// The type of pixels used on the GPU side. [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] - public sealed class ReadOnlyTexture2D : Texture2D + public sealed class ReadOnlyTexture2D : Texture2D, IReadOnlyTexture2D where T : unmanaged, IUnorm where TPixel : unmanaged { @@ -31,19 +31,10 @@ namespace ComputeSharp { } - /// - /// Gets a single value from the current readonly texture. - /// - /// The horizontal offset of the value to get. - /// The vertical offset of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[int,int]"); - /// - /// Gets a single value from the current readonly texture. - /// - /// The coordinates of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[Int2]"); /// diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs index 2580c375..07d4d98a 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs @@ -16,7 +16,7 @@ namespace ComputeSharp /// The type of pixels used on the GPU side. [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] - public sealed class ReadOnlyTexture3D : Texture3D + public sealed class ReadOnlyTexture3D : Texture3D, IReadOnlyTexture3D where T : unmanaged, IUnorm where TPixel : unmanaged { @@ -32,20 +32,10 @@ namespace ComputeSharp { } - /// - /// Gets a single value from the current readonly texture. - /// - /// The horizontal offset of the value to get. - /// The vertical offset of the value to get. - /// The depthwise offset of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public TPixel this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[int,int,int]"); - /// - /// Gets a single value from the current readonly texture. - /// - /// The coordinates of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public TPixel this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[Int3]"); /// diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs index c1e586d0..28366eb3 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs @@ -16,7 +16,7 @@ namespace ComputeSharp /// The type of pixels used on the GPU side. [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] - public sealed class ReadWriteTexture2D : Texture2D + public sealed class ReadWriteTexture2D : Texture2D, IReadWriteTexture2D where T : unmanaged, IUnorm where TPixel : unmanaged { @@ -31,19 +31,10 @@ namespace ComputeSharp { } - /// - /// Gets a single value from the current writeable texture. - /// - /// The horizontal offset of the value to get. - /// The vertical offset of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public ref TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[int,int]"); - /// - /// Gets or sets a single value from the current writeable texture. - /// - /// The coordinates of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public ref TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[Int2]"); /// diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs index 03dc47c5..7a0caaa2 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs @@ -16,7 +16,7 @@ namespace ComputeSharp /// The type of pixels used on the GPU side. [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] - public sealed class ReadWriteTexture3D : Texture3D + public sealed class ReadWriteTexture3D : Texture3D, IReadWriteTexture3D where T : unmanaged, IUnorm where TPixel : unmanaged { @@ -32,20 +32,10 @@ namespace ComputeSharp { } - /// - /// Gets a single value from the current writeable texture. - /// - /// The horizontal offset of the value to get. - /// The vertical offset of the value to get. - /// The depthwise offset of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public ref TPixel this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[int,int,int]"); - /// - /// Gets a single value from the current writeable texture. - /// - /// The coordinates of the value to get. - /// This API can only be used from a compute shader, and will always throw if used anywhere else. + /// public ref TPixel this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[Int3]"); /// From 226481aff6920ef3b7b79d5b880f1df68531e084 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 16:15:10 +0100 Subject: [PATCH 2/6] Tweaked visibility of some resource fields --- src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs | 2 +- src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs | 2 +- src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs index 0bab9ab9..85085968 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs @@ -30,7 +30,7 @@ namespace ComputeSharp.Resources /// /// The instance for the current resource. /// - internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; /// /// The instance for the current resource. diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs index 89434f8f..b2cf57b9 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs @@ -32,7 +32,7 @@ namespace ComputeSharp.Resources /// /// The instance for the current resource. /// - internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; /// /// The instance for the current resource. diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index da06d3fb..cc6a528c 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -32,7 +32,7 @@ namespace ComputeSharp.Resources /// /// The instance for the current resource. /// - internal readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; /// /// The instance for the current resource. From 958fa15a11d5572e6964229f43e8eb1ddd5a0dc4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 17:28:55 +0100 Subject: [PATCH 3/6] 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) From 2c90b7a92ccdb290a7507bd5a4fdc5e47c31952f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 18:01:03 +0100 Subject: [PATCH 4/6] Implemented source generator support for interfaces --- .../__Internals/GraphicsResourceHelper.cs | 46 +++++++++++++++++-- .../Translation/Mappings/HlslKnownTypes.cs | 8 +++- .../Mappings/HlslKnownMembers.cs | 6 ++- .../Mappings/HlslKnownTypes.cs | 8 ++++ 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 9abde184..fdf9a066 100644 --- a/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -50,13 +50,32 @@ namespace ComputeSharp.__Internals /// 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 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 texture, GraphicsDevice device) + public static ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) + where T : unmanaged + { + texture.ThrowIfDisposed(); + texture.ThrowIfDeviceMismatch(device); + + 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 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(ReadWriteTexture2D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); @@ -115,13 +134,32 @@ namespace ComputeSharp.__Internals /// 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 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) + public static ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) + where T : unmanaged + { + texture.ThrowIfDisposed(); + texture.ThrowIfDeviceMismatch(device); + + 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 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(ReadWriteTexture3D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); diff --git a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs index b2c84d86..5776c971 100644 --- a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs @@ -79,7 +79,9 @@ namespace ComputeSharp.Shaders.Mappings genericType == typeof(ReadOnlyTexture2D<>) || genericType == typeof(ReadOnlyTexture2D<,>) || genericType == typeof(ReadOnlyTexture3D<>) || - genericType == typeof(ReadOnlyTexture3D<,>); + genericType == typeof(ReadOnlyTexture3D<,>) || + genericType == typeof(IReadOnlyTexture2D<>) || + genericType == typeof(IReadOnlyTexture3D<>); } /// @@ -99,7 +101,9 @@ namespace ComputeSharp.Shaders.Mappings genericType == typeof(ReadWriteTexture2D<>) || genericType == typeof(ReadWriteTexture2D<,>) || genericType == typeof(ReadWriteTexture3D<>) || - genericType == typeof(ReadWriteTexture3D<,>); + genericType == typeof(ReadWriteTexture3D<,>) || + genericType == typeof(IReadWriteTexture2D<>) || + genericType == typeof(IReadWriteTexture3D<>); ; } } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index e3751ef4..45200de1 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -21,10 +21,14 @@ namespace ComputeSharp.SourceGenerators.Mappings [$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.ReadWriteTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.IReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", [$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", [$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", - [$"ComputeSharp.ReadWriteTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" + [$"ComputeSharp.ReadWriteTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", + [$"ComputeSharp.IReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", + [$"ComputeSharp.IReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" }; /// diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 2bc27d99..22aefe96 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -78,6 +78,10 @@ namespace ComputeSharp.SourceGenerators.Mappings case "ComputeSharp.ReadOnlyTexture3D`2": case "ComputeSharp.ReadWriteTexture3D`1": case "ComputeSharp.ReadWriteTexture3D`2": + case "ComputeSharp.IReadOnlyTexture2D`1": + case "ComputeSharp.IReadWriteTexture2D`1": + case "ComputeSharp.IReadOnlyTexture3D`1": + case "ComputeSharp.IReadWriteTexture3D`1": return true; default: return false; }; @@ -134,6 +138,10 @@ namespace ComputeSharp.SourceGenerators.Mappings "ComputeSharp.ReadOnlyTexture3D`2" => $"Texture3D", "ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mapped}>", "ComputeSharp.ReadWriteTexture3D`2" => $"RWTexture3D", + "ComputeSharp.IReadOnlyTexture2D`1" => $"Texture2D", + "ComputeSharp.IReadWriteTexture2D`1" => $"RWTexture2D", + "ComputeSharp.IReadOnlyTexture3D`1" => $"Texture3D", + "ComputeSharp.IReadWriteTexture3D`1" => $"RWTexture3D", _ => throw new ArgumentException() }; } From da339566cca5b88371f689fb0b27cecb85e512a2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 18:14:37 +0100 Subject: [PATCH 5/6] Added size properties to texture interfaces --- .../Interfaces/IReadOnlyTexture2D{TPixel}.cs | 10 ++++++++++ .../Interfaces/IReadOnlyTexture3D{TPixel}.cs | 15 +++++++++++++++ .../Interfaces/IReadWriteTexture2D{TPixel}.cs | 10 ++++++++++ .../Interfaces/IReadWriteTexture3D{TPixel}.cs | 15 +++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs index b03ba5c9..e2d18faf 100644 --- a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture2D{TPixel}.cs @@ -8,6 +8,16 @@ public interface IReadOnlyTexture2D where TPixel : unmanaged { + /// + /// Gets the width of the current texture. + /// + public int Width { get; } + + /// + /// Gets the height of the current texture. + /// + public int Height { get; } + /// /// Gets a single value from the current readonly texture. /// diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs index a718f10f..df101b43 100644 --- a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadOnlyTexture3D{TPixel}.cs @@ -8,6 +8,21 @@ public interface IReadOnlyTexture3D where TPixel : unmanaged { + /// + /// Gets the width of the current texture. + /// + public int Width { get; } + + /// + /// Gets the height of the current texture. + /// + public int Height { get; } + + /// + /// Gets the depth of the current texture. + /// + public int Depth { get; } + /// /// Gets a single value from the current readonly texture. /// diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs index 62cd23f9..dede8cfb 100644 --- a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture2D{TPixel}.cs @@ -8,6 +8,16 @@ public interface IReadWriteTexture2D where TPixel : unmanaged { + /// + /// Gets the width of the current texture. + /// + public int Width { get; } + + /// + /// Gets the height of the current texture. + /// + public int Height { get; } + /// /// Gets a single value from the current writeable texture. /// diff --git a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs index 7e04187e..6bbfb12a 100644 --- a/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/Interfaces/IReadWriteTexture3D{TPixel}.cs @@ -8,6 +8,21 @@ public interface IReadWriteTexture3D where TPixel : unmanaged { + /// + /// Gets the width of the current texture. + /// + public int Width { get; } + + /// + /// Gets the height of the current texture. + /// + public int Height { get; } + + /// + /// Gets the depth of the current texture. + /// + public int Depth { get; } + /// /// Gets a single value from the current writeable texture. /// From f702a1a7374abba88ddb2764dcd171b196616671 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Jan 2021 18:14:50 +0100 Subject: [PATCH 6/6] Refactored image samples to texture interfaces --- .../HlslBokehBlurProcessor{TPixel}.cs | 18 +++++++++--------- .../HlslGaussianBlurProcessor{TPixel}.cs | 12 ++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 25d0d23b..7fb37dd0 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -288,13 +288,13 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Performs a vertical 1D complex convolution with the specified parameters. /// - /// The source to read data from. + /// The source to read data from. /// The target to write the real results to. /// The target to write the imaginary results to. /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyVerticalConvolution( - ReadWriteTexture2D source, + IReadWriteTexture2D source, ReadWriteTexture2D reals, ReadWriteTexture2D imaginaries, ReadOnlyBuffer kernel) @@ -324,7 +324,7 @@ namespace ComputeSharp.BokehBlur.Processors public int maxX; public int kernelLength; - public ReadWriteTexture2D source; + public IReadWriteTexture2D source; public ReadWriteTexture2D reals; public ReadWriteTexture2D imaginaries; public ReadOnlyBuffer kernel; @@ -431,9 +431,9 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Applies the gamma highlight pass to compute the final pixel values for a target image. /// - /// The source instance to modify. + /// The source instance to modify. [Pure] - private void ApplyGammaHighlight(ReadWriteTexture2D source) + private void ApplyGammaHighlight(IReadWriteTexture2D source) { Gpu.Default.For(source.Height, new GammaHighlightProcessor(source.Width, source)); } @@ -446,7 +446,7 @@ namespace ComputeSharp.BokehBlur.Processors { public readonly int width; - public readonly ReadWriteTexture2D source; + public readonly IReadWriteTexture2D source; /// public void Execute(ThreadIds ids) @@ -466,11 +466,11 @@ namespace ComputeSharp.BokehBlur.Processors /// Applies the inverse gamma highlight pass to compute the final pixel values for a target image. /// /// The source instance to read from. - /// The target instance to write to. + /// The target instance to write to. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyInverseGammaExposure( ReadWriteTexture2D source, - ReadWriteTexture2D target) + IReadWriteTexture2D target) { Gpu.Default.For(source.Height, new InverseGammaHighlightProcessor(source.Width, source, target)); } @@ -484,7 +484,7 @@ namespace ComputeSharp.BokehBlur.Processors public readonly int width; public readonly ReadWriteTexture2D source; - public readonly ReadWriteTexture2D target; + public readonly IReadWriteTexture2D target; /// public void Execute(ThreadIds ids) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 5f1fa2f5..bc72a6a8 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -107,12 +107,12 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Performs a vertical 1D complex convolution with the specified parameters. /// - /// The source to read data from. + /// The source to read data from. /// The target to write the results to. /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyVerticalConvolution( - ReadWriteTexture2D source, + IReadWriteTexture2D source, ReadWriteTexture2D target, ReadOnlyBuffer kernel) { @@ -140,7 +140,7 @@ namespace ComputeSharp.BokehBlur.Processors public readonly int maxX; public readonly int kernelLength; - public readonly ReadWriteTexture2D source; + public readonly IReadWriteTexture2D source; public readonly ReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; @@ -168,12 +168,12 @@ namespace ComputeSharp.BokehBlur.Processors /// Performs an horizontal 1D complex convolution with the specified parameters. /// /// The source to read data from. - /// The target to write the results to. + /// The target to write the results to. /// The with the values for the current complex kernel. [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyHorizontalConvolution( ReadWriteTexture2D source, - ReadWriteTexture2D target, + IReadWriteTexture2D target, ReadOnlyBuffer kernel) { int height = Source.Height; @@ -201,7 +201,7 @@ namespace ComputeSharp.BokehBlur.Processors public readonly int kernelLength; public readonly ReadWriteTexture2D source; - public readonly ReadWriteTexture2D target; + public readonly IReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; ///