From 67571a7626e4c892ab38550238a4e0a57ce5bba2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 7 Jan 2022 11:46:31 +0100 Subject: [PATCH 1/7] Rename IUnorm to IPixel, remove [Obsolete] Also move to the root ComputeSharp namespace --- src/ComputeSharp/Core/Imaging/Bgra32.cs | 5 +- .../Imaging/Interfaces/IPixel{T,TPixel}.cs | 12 ++++ src/ComputeSharp/Core/Imaging/R16.cs | 5 +- src/ComputeSharp/Core/Imaging/R8.cs | 5 +- src/ComputeSharp/Core/Imaging/Rg16.cs | 5 +- src/ComputeSharp/Core/Imaging/Rg32.cs | 5 +- src/ComputeSharp/Core/Imaging/Rgba32.cs | 5 +- src/ComputeSharp/Core/Imaging/Rgba64.cs | 5 +- .../Imaging/__Internals/IUnorm{TPixel}.cs | 15 ----- .../GraphicsDeviceExtensions.Resources.cs | 59 +++++++++---------- .../Resources/ReadOnlyTexture2D{T,TPixel}.cs | 5 +- .../Resources/ReadOnlyTexture3D{T,TPixel}.cs | 5 +- .../Resources/ReadWriteTexture2D{T,TPixel}.cs | 5 +- .../Resources/ReadWriteTexture3D{T,TPixel}.cs | 5 +- .../Extensions/ComputeContextExtensions.cs | 8 +-- 15 files changed, 55 insertions(+), 94 deletions(-) create mode 100644 src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs delete mode 100644 src/ComputeSharp/Core/Imaging/__Internals/IUnorm{TPixel}.cs diff --git a/src/ComputeSharp/Core/Imaging/Bgra32.cs b/src/ComputeSharp/Core/Imaging/Bgra32.cs index 2c01623b..27ed1544 100644 --- a/src/ComputeSharp/Core/Imaging/Bgra32.cs +++ b/src/ComputeSharp/Core/Imaging/Bgra32.cs @@ -2,9 +2,6 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -18,7 +15,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Bgra32 : IEquatable, IUnorm, IUnorm +public struct Bgra32 : IEquatable, IPixel, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs b/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs new file mode 100644 index 00000000..be3f2740 --- /dev/null +++ b/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs @@ -0,0 +1,12 @@ +namespace ComputeSharp; + +/// +/// An interface representing a pixel type and its normalized representation on the GPU side, when used within a shader. +/// +/// The pixel type, when stored in memory (either on the CPU or the GPU side). +/// The type of pixel when normalized and used within a shader. +public interface IPixel + where T : unmanaged, IPixel + where TPixel : unmanaged +{ +} diff --git a/src/ComputeSharp/Core/Imaging/R16.cs b/src/ComputeSharp/Core/Imaging/R16.cs index d79580d7..850d9222 100644 --- a/src/ComputeSharp/Core/Imaging/R16.cs +++ b/src/ComputeSharp/Core/Imaging/R16.cs @@ -1,9 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -14,7 +11,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct R16 : IEquatable, IUnorm +public struct R16 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/R8.cs b/src/ComputeSharp/Core/Imaging/R8.cs index 84bf2230..7f95b8c4 100644 --- a/src/ComputeSharp/Core/Imaging/R8.cs +++ b/src/ComputeSharp/Core/Imaging/R8.cs @@ -1,9 +1,6 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -14,7 +11,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct R8 : IEquatable, IUnorm +public struct R8 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rg16.cs b/src/ComputeSharp/Core/Imaging/Rg16.cs index 863fe74b..c6aab570 100644 --- a/src/ComputeSharp/Core/Imaging/Rg16.cs +++ b/src/ComputeSharp/Core/Imaging/Rg16.cs @@ -2,9 +2,6 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -15,7 +12,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct Rg16 : IEquatable, IUnorm, IUnorm +public struct Rg16 : IEquatable, IPixel, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rg32.cs b/src/ComputeSharp/Core/Imaging/Rg32.cs index 1b4170cc..dda953ce 100644 --- a/src/ComputeSharp/Core/Imaging/Rg32.cs +++ b/src/ComputeSharp/Core/Imaging/Rg32.cs @@ -2,9 +2,6 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -15,7 +12,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct Rg32 : IEquatable, IUnorm, IUnorm +public struct Rg32 : IEquatable, IPixel, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rgba32.cs b/src/ComputeSharp/Core/Imaging/Rgba32.cs index 66a82ab6..5cc7cd9c 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba32.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba32.cs @@ -2,9 +2,6 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -17,7 +14,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Rgba32 : IEquatable, IUnorm, IUnorm +public struct Rgba32 : IEquatable, IPixel, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rgba64.cs b/src/ComputeSharp/Core/Imaging/Rgba64.cs index c689737a..6652717b 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba64.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba64.cs @@ -2,9 +2,6 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.__Internals; - -#pragma warning disable CS0618 namespace ComputeSharp; @@ -17,7 +14,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Rgba64 : IEquatable, IUnorm, IUnorm +public struct Rgba64 : IEquatable, IPixel, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/__Internals/IUnorm{TPixel}.cs b/src/ComputeSharp/Core/Imaging/__Internals/IUnorm{TPixel}.cs deleted file mode 100644 index 3a5d812f..00000000 --- a/src/ComputeSharp/Core/Imaging/__Internals/IUnorm{TPixel}.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.ComponentModel; - -namespace ComputeSharp.__Internals; - -/// -/// Indicates the supported HLSL pixel types to represent a given normalized pixel type. -/// -/// The type of pixels used on the GPU side. -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This interface is not intended to be used directly by user code")] -public interface IUnorm - where TPixel : unmanaged -{ -} diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs index c504ef37..c235c555 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Resources.cs @@ -1,12 +1,9 @@ using System; using System.IO; -using ComputeSharp.__Internals; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Resources; using Microsoft.Toolkit.Diagnostics; -#pragma warning disable CS0618 - namespace ComputeSharp; /// @@ -227,7 +224,7 @@ public static partial class GraphicsDeviceExtensions /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return new(device, width, height, allocationMode); @@ -244,7 +241,7 @@ public static partial class GraphicsDeviceExtensions /// The height of the texture. /// A instance with the contents of the input array. public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] source, int width, int height) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadOnlyTexture2D(source.AsSpan(), width, height); @@ -262,7 +259,7 @@ public static partial class GraphicsDeviceExtensions /// The height of the texture. /// A instance with the contents of the input array. public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] source, int offset, int width, int height) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadOnlyTexture2D(source.AsSpan(offset), width, height); @@ -277,7 +274,7 @@ public static partial class GraphicsDeviceExtensions /// The input array with the data to copy on the allocated texture. /// A read write instance with the contents of the input array. public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[,] source) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadOnlyTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -298,7 +295,7 @@ public static partial class GraphicsDeviceExtensions /// The height of the texture. /// A instance with the contents of the input . public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan source, int width, int height) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadOnlyTexture2D texture = new(device, width, height, AllocationMode.Default); @@ -410,7 +407,7 @@ public static partial class GraphicsDeviceExtensions /// The allocation mode to use for the new resource. /// A zeroed instance of size [, , ]. public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return new(device, width, height, depth, allocationMode); @@ -428,7 +425,7 @@ public static partial class GraphicsDeviceExtensions /// The depth of the texture. /// A instance with the contents of the input array. public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] source, int width, int height, int depth) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadOnlyTexture3D(source.AsSpan(), width, height, depth); @@ -447,7 +444,7 @@ public static partial class GraphicsDeviceExtensions /// The depth of the texture. /// A instance with the contents of the input array. public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] source, int offset, int width, int height, int depth) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadOnlyTexture3D(source.AsSpan(offset), width, height, depth); @@ -466,7 +463,7 @@ public static partial class GraphicsDeviceExtensions /// That is, the expected layout of the input array has to be [depth, height, width]. /// public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] source) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadOnlyTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -488,7 +485,7 @@ public static partial class GraphicsDeviceExtensions /// The depth of the texture. /// A instance with the contents of the input . public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan source, int width, int height, int depth) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadOnlyTexture3D texture = new(device, width, height, depth, AllocationMode.Default); @@ -652,7 +649,7 @@ public static partial class GraphicsDeviceExtensions /// The allocation mode to use for the new resource. /// A zeroed instance of size [, ]. public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height, AllocationMode allocationMode = AllocationMode.Default) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return new(device, width, height, allocationMode); @@ -669,7 +666,7 @@ public static partial class GraphicsDeviceExtensions /// The height of the texture. /// A instance with the contents of the input array. public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] source, int width, int height) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadWriteTexture2D(source.AsSpan(), width, height); @@ -687,7 +684,7 @@ public static partial class GraphicsDeviceExtensions /// The height of the texture. /// A instance with the contents of the input array. public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] source, int offset, int width, int height) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadWriteTexture2D(source.AsSpan(offset), width, height); @@ -702,7 +699,7 @@ public static partial class GraphicsDeviceExtensions /// The input array with the data to copy on the allocated texture. /// A read write instance with the contents of the input array. public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] source) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadWriteTexture2D texture = new(device, source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -723,7 +720,7 @@ public static partial class GraphicsDeviceExtensions /// The height of the texture. /// A instance with the contents of the input . public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan source, int width, int height) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadWriteTexture2D texture = new(device, width, height, AllocationMode.Default); @@ -835,7 +832,7 @@ public static partial class GraphicsDeviceExtensions /// The allocation mode to use for the new resource. /// A instance of size [, , ]. public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return new(device, width, height, depth, allocationMode); @@ -853,7 +850,7 @@ public static partial class GraphicsDeviceExtensions /// The depth of the texture. /// A instance with the contents of the input array. public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] source, int width, int height, int depth) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadWriteTexture3D(source.AsSpan(), width, height, depth); @@ -872,7 +869,7 @@ public static partial class GraphicsDeviceExtensions /// The depth of the texture. /// A instance with the contents of the input array. public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] source, int offset, int width, int height, int depth) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return device.AllocateReadWriteTexture3D(source.AsSpan(offset), width, height, depth); @@ -891,7 +888,7 @@ public static partial class GraphicsDeviceExtensions /// That is, the expected layout of the input array has to be [depth, height, width]. /// public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] source) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadWriteTexture3D texture = new(device, source.GetLength(2), source.GetLength(1), source.GetLength(0), AllocationMode.Default); @@ -913,7 +910,7 @@ public static partial class GraphicsDeviceExtensions /// The depth of the texture. /// A instance with the contents of the input . public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan source, int width, int height, int depth) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { ReadWriteTexture3D texture = new(device, width, height, depth, AllocationMode.Default); @@ -1022,7 +1019,7 @@ public static partial class GraphicsDeviceExtensions /// The filename of the image file to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadOnlyTexture2D LoadReadOnlyTexture2D(this GraphicsDevice device, string filename) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { Guard.IsNotNull(filename, nameof(filename)); @@ -1039,7 +1036,7 @@ public static partial class GraphicsDeviceExtensions /// The filename of the image file to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadOnlyTexture2D LoadReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan filename) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); @@ -1060,7 +1057,7 @@ public static partial class GraphicsDeviceExtensions /// The buffer with the image data to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadOnlyTexture2D LoadReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan span) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); @@ -1081,7 +1078,7 @@ public static partial class GraphicsDeviceExtensions /// The stream with the image data to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadOnlyTexture2D LoadReadOnlyTexture2D(this GraphicsDevice device, Stream stream) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { Guard.IsNotNull(stream, nameof(stream)); @@ -1104,7 +1101,7 @@ public static partial class GraphicsDeviceExtensions /// The filename of the image file to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadWriteTexture2D LoadReadWriteTexture2D(this GraphicsDevice device, string filename) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { Guard.IsNotNull(filename, nameof(filename)); @@ -1121,7 +1118,7 @@ public static partial class GraphicsDeviceExtensions /// The filename of the image file to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadWriteTexture2D LoadReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan filename) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, filename); @@ -1142,7 +1139,7 @@ public static partial class GraphicsDeviceExtensions /// The buffer with the image data to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadWriteTexture2D LoadReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { using UploadTexture2D upload = WICHelper.Instance.LoadTexture(device, span); @@ -1163,7 +1160,7 @@ public static partial class GraphicsDeviceExtensions /// The stream with the image data to load and decode into the texture. /// A instance with the contents of the specified file. public static ReadWriteTexture2D LoadReadWriteTexture2D(this GraphicsDevice device, Stream stream) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { Guard.IsNotNull(stream, nameof(stream)); diff --git a/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs index 2e8ec3e4..ec1c9cfc 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs @@ -1,13 +1,10 @@ using System.Diagnostics; -using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; -#pragma warning disable CS0618 - namespace ComputeSharp; /// @@ -18,7 +15,7 @@ namespace ComputeSharp; [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadOnlyTexture2D : Texture2D, IReadOnlyTexture2D - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs index a97300b5..e67020da 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs @@ -1,13 +1,10 @@ using System.Diagnostics; -using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; -#pragma warning disable CS0618 - namespace ComputeSharp; /// @@ -18,7 +15,7 @@ namespace ComputeSharp; [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadOnlyTexture3D : Texture3D, IReadOnlyTexture3D - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs index 09472c1e..ac9b2214 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs @@ -1,13 +1,10 @@ using System.Diagnostics; -using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; -#pragma warning disable CS0618 - namespace ComputeSharp; /// @@ -18,7 +15,7 @@ namespace ComputeSharp; [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadWriteTexture2D : Texture2D, IReadWriteTexture2D - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs index 25765ebe..3007240b 100644 --- a/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp/Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs @@ -1,13 +1,10 @@ using System.Diagnostics; -using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; -#pragma warning disable CS0618 - namespace ComputeSharp; /// @@ -18,7 +15,7 @@ namespace ComputeSharp; [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadWriteTexture3D : Texture3D, IReadWriteTexture3D - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index ac2019ed..d7da40f7 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -54,7 +54,7 @@ public static partial class ComputeContextExtensions /// The to use to insert the resource barrier. /// The input instance to insert the barrier for. public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture2D texture) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); @@ -68,7 +68,7 @@ public static partial class ComputeContextExtensions /// The to use to insert the resource barrier. /// The input instance to insert the barrier for. public static unsafe void Barrier(this in ComputeContext context, ReadWriteTexture3D texture) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { context.Barrier(texture.ValidateAndGetID3D12Resource(context.GraphicsDevice)); @@ -148,7 +148,7 @@ public static partial class ComputeContextExtensions /// The to use to clear the resource. /// The input instance to clear. public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); @@ -164,7 +164,7 @@ public static partial class ComputeContextExtensions /// The to use to clear the resource. /// The input instance to clear. public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); From a0eb6427ebdb3e654dbf5ab736dcd5feae926fb5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 7 Jan 2022 11:50:23 +0100 Subject: [PATCH 2/7] Fix unit tests --- tests/ComputeSharp.Tests/ComputeContextTests.cs | 11 ++++------- .../Extensions/GraphicsDeviceExtensions.cs | 13 +++++-------- tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs | 5 +---- tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs | 5 +---- 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 907818cc..4265c32f 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1,15 +1,12 @@ using System; using System.Linq; using System.Threading.Tasks; -using ComputeSharp.__Internals; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using ComputeSharp.Tests.Helpers; using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests; [TestClass] @@ -182,7 +179,7 @@ public partial class ComputeContextTests public void Clear_ReadWriteTexture2D_WithPixel(Device device, Type cpuType, Type gpuType) { static void Test(Device device) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { if (!device.Get().IsReadWriteTexture2DSupportedForType()) @@ -224,7 +221,7 @@ public partial class ComputeContextTests public void Clear_ReadWriteTexture3D_WithPixel(Device device, Type cpuType, Type gpuType) { static void Test(Device device) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { if (!device.Get().IsReadWriteTexture2DSupportedForType()) @@ -266,7 +263,7 @@ public partial class ComputeContextTests public void Clear_ReadWriteTexture2D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) { static void Test(Device device) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { if (!device.Get().IsReadWriteTexture2DSupportedForType()) @@ -308,7 +305,7 @@ public partial class ComputeContextTests public void Clear_ReadWriteTexture3D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) { static void Test(Device device) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { if (!device.Get().IsReadWriteTexture2DSupportedForType()) diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index d143dbee..09d85ec9 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -1,10 +1,7 @@ using System; using System.IO; -using ComputeSharp.__Internals; using ComputeSharp.Resources; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests.Extensions; /// @@ -66,7 +63,7 @@ public static class GraphicsDeviceExtensions /// The allocation mode to use for the new resource. /// A instance of the requested size. public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height, AllocationMode allocationMode = AllocationMode.Default) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return type switch @@ -112,7 +109,7 @@ public static class GraphicsDeviceExtensions /// The allocation mode to use for the new resource. /// A instance of the requested size. public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth, AllocationMode allocationMode = AllocationMode.Default) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return type switch @@ -322,7 +319,7 @@ public static class GraphicsDeviceExtensions /// The filename of the image file to load and decode into the texture. /// A instance with the contents of the specified file. public static Texture2D LoadTexture2D(this GraphicsDevice device, Type textureType, Type inputType, string filename) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return textureType switch @@ -353,7 +350,7 @@ public static class GraphicsDeviceExtensions /// The buffer with the image data to load and decode into the texture. /// A instance with the contents of the specified buffer. public static Texture2D LoadTexture2D(this GraphicsDevice device, Type type, byte[] buffer) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return type switch @@ -374,7 +371,7 @@ public static class GraphicsDeviceExtensions /// The stream with the image data to load and decode into the texture. /// A instance with the contents of the specified stream. public static Texture2D LoadTexture2D(this GraphicsDevice device, Type type, Stream stream) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { return type switch diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index c4103d96..63a8943f 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -2,7 +2,6 @@ using System.IO; using System.Numerics; using System.Reflection; -using ComputeSharp.__Internals; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -10,8 +9,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests; public partial class Texture2DTests @@ -35,7 +32,7 @@ public partial class Texture2DTests public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel) { static void Test(Device device, Type textureType) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { using Texture2D texture = device.Get().AllocateTexture2D(textureType, 128, 128); diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs index 4d18fe94..0441ae8e 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs @@ -1,14 +1,11 @@ using System; using System.Numerics; using System.Reflection; -using ComputeSharp.__Internals; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable CS0618 - namespace ComputeSharp.Tests; public partial class Texture3DTests @@ -32,7 +29,7 @@ public partial class Texture3DTests public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel) { static void Test(Device device, Type textureType) - where T : unmanaged, IUnorm + where T : unmanaged, IPixel where TPixel : unmanaged { using Texture3D texture = device.Get().AllocateTexture3D(textureType, 128, 128, 2); From c5d4822e9f2efbdff60b18133cb41560a2cca331 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 8 Jan 2022 15:25:37 +0100 Subject: [PATCH 3/7] Add ComputeContext.Fill APIs --- .../ID3D12GraphicsCommandListExtensions.cs | 24 ++++ .../Graphics/Helpers/DXGIFormatHelper.cs | 106 +++++++++++++++++- src/ComputeSharp/Shaders/ComputeContext.cs | 30 ++++- .../Extensions/ComputeContextExtensions.cs | 101 ++++++++++++++--- 4 files changed, 241 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index 56b4548a..ba806042 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -259,6 +259,30 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions } } + /// + /// Fills a target UAV resource. + /// + /// The instance in use. + /// The to fill. + /// The value for the target resource. + /// The value for the target resource. + /// The value to use to fill the view. + public static unsafe void FillUnorderedAccessView( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + Float4 value) + { + d3D12GraphicsCommandList.ClearUnorderedAccessViewFloat( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (float*)&value, + NumRects: 0, + pRects: null); + } + /// /// Binds an input constant buffer to the first root parameter. /// diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index 6fd25e28..e14a142a 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -50,7 +50,7 @@ internal static class DXGIFormatHelper /// Gets whether or not the input type corresponds to a normalized format. /// /// The input type argument to check. - /// TWhether or not the input type corresponds to a normalized format.. + /// Whether or not the input type corresponds to a normalized format. /// Thrown when the input type is not supported. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsNormalizedType() @@ -88,4 +88,108 @@ internal static class DXGIFormatHelper return ThrowHelper.ThrowArgumentException("Invalid texture type"); } + + /// + /// Extends a given pixel type to its equivalent. + /// + /// The input pixel value to convert. + /// The equivalent value for . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Float4 ExtendToNormalizedValue(ref T value) + where T : unmanaged + { + if (typeof(T) == typeof(Float4)) + { + return Unsafe.As(ref value); + } + + Float4 result = default; + + Unsafe.As(ref result) = value; + + return result; + } + + /// + /// Converts a given value to a normalized value. + /// + /// The input value to convert. + /// The normalized value for . + /// Thrown when the input type is not supported. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Float4 ConvertToNormalizedValue(ref T value) + where T : unmanaged + { + if (typeof(T) == typeof(Bgra32)) + { + ref Bgra32 bgra = ref Unsafe.As(ref value); + + Vector4 linear = new(bgra.R, bgra.G, bgra.B, bgra.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rgba32)) + { + ref Rgba32 rgba = ref Unsafe.As(ref value); + + Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rgba64)) + { + ref Rgba64 rgba = ref Unsafe.As(ref value); + + Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(R8)) + { + ref R8 r = ref Unsafe.As(ref value); + + Vector4 linear = new(r.R, 0, 0, 0); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(R16)) + { + ref R16 r = ref Unsafe.As(ref value); + + Vector4 linear = new(r.R, 0, 0, 0); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rg16)) + { + ref Rg16 rg = ref Unsafe.As(ref value); + + Vector4 linear = new(rg.R, rg.G, 0, 0); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rg32)) + { + ref Rg32 rg = ref Unsafe.As(ref value); + + Vector4 linear = new(rg.R, rg.G, 0, 0); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + + return ThrowHelper.ThrowArgumentException("Invalid pixel type"); + } } diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 12e884ad..aa28245a 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -52,6 +52,19 @@ public struct ComputeContext : IDisposable, IAsyncDisposable } } + /// + /// Inserts a resource barrier for a specific resource. + /// + /// The to insert the barrier for. + internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource) + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + ref CommandList commandList = ref GetCommandList(in this); + + commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); + } + /// /// Clears a specific resource. /// @@ -73,16 +86,23 @@ public struct ComputeContext : IDisposable, IAsyncDisposable } /// - /// Inserts a resource barrier for a specific resource. + /// Fills a specific resource. /// - /// The to insert the barrier for. - internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource) + /// The to fill. + /// The value for the target resource. + /// The value for the target resource. + /// The value to use to fill the resource. + internal readonly unsafe void Fill( + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + Float4 value) { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this); + ref CommandList commandList = ref GetCommandList(in this, null); - commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); + commandList.D3D12GraphicsCommandList->FillUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, value); } /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index d7da40f7..131c035d 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,5 +1,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Helpers; #pragma warning disable CS0618 @@ -113,7 +114,7 @@ public static partial class ComputeContextExtensions } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The to use to clear the resource. @@ -127,7 +128,7 @@ public static partial class ComputeContextExtensions } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The to use to clear the resource. @@ -141,7 +142,7 @@ public static partial class ComputeContextExtensions } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The type of pixels used on the GPU side. @@ -151,13 +152,13 @@ public static partial class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true); } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The type of pixels used on the GPU side. @@ -167,13 +168,13 @@ public static partial class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true); } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of pixels stored on the texture. /// The to use to clear the resource. @@ -181,13 +182,13 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture2D texture) where TPixel : unmanaged { - var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true); } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of pixels stored on the texture. /// The to use to clear the resource. @@ -195,9 +196,81 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture3D texture) where TPixel : unmanaged { - var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, ReadWriteTexture2D texture, T value) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, ReadWriteTexture3D texture, T value) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of pixels stored on the texture. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, IReadWriteTexture2D texture, TPixel value) + where TPixel : unmanaged + { + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill( + ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), + handles.Gpu, + handles.Cpu, + DXGIFormatHelper.ExtendToNormalizedValue(ref value)); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of pixels stored on the texture. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, IReadWriteTexture3D texture, TPixel value) + where TPixel : unmanaged + { + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill( + ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), + handles.Gpu, + handles.Cpu, + DXGIFormatHelper.ExtendToNormalizedValue(ref value)); } /// From c72b0c7fd2d820102633d853e7ba31406a176c2d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 8 Jan 2022 16:34:48 +0100 Subject: [PATCH 4/7] Remove System.Numerics types from pixel format conversions --- .../HlslBokehBlurProcessor{TPixel}.cs | 40 +++---- .../HlslGaussianBlurProcessor{TPixel}.cs | 20 ++-- src/ComputeSharp/Core/Imaging/Bgra32.cs | 3 +- src/ComputeSharp/Core/Imaging/Rg16.cs | 3 +- src/ComputeSharp/Core/Imaging/Rg32.cs | 3 +- src/ComputeSharp/Core/Imaging/Rgba32.cs | 3 +- src/ComputeSharp/Core/Imaging/Rgba64.cs | 3 +- .../Texture2DTests.Pixels.cs | 8 +- .../Texture2DTests.Pixels.g.cs | 101 ------------------ .../Texture2DTests.Pixels.tt | 11 +- .../Texture3DTests.Pixels.cs | 8 +- .../Texture3DTests.Pixels.g.cs | 101 ------------------ .../Texture3DTests.Pixels.tt | 11 +- 13 files changed, 47 insertions(+), 268 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 25c159d1..e8d3318b 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -270,10 +270,10 @@ public sealed partial class HlslBokehBlurProcessor Span span = MemoryMarshal.Cast(pixelSpan); - using ReadWriteTexture2D texture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height); - using ReadWriteTexture2D temporary = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height, AllocationMode.Clear); - using ReadWriteTexture2D reals = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); - using ReadWriteTexture2D imaginaries = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D texture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D temporary = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height, AllocationMode.Clear); + using ReadWriteTexture2D reals = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D imaginaries = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernel = GraphicsDevice.AllocateReadOnlyBuffer(KernelSize); // Preliminary gamma highlight pass @@ -315,16 +315,16 @@ public sealed partial class HlslBokehBlurProcessor [AutoConstructor] internal partial struct VerticalConvolutionProcessor : IComputeShader { - public IReadWriteTexture2D source; - public ReadWriteTexture2D reals; - public ReadWriteTexture2D imaginaries; + public IReadWriteTexture2D source; + public ReadWriteTexture2D reals; + public ReadWriteTexture2D imaginaries; public ReadOnlyBuffer kernel; /// public void Execute() { - Vector4 real = Vector4.Zero; - Vector4 imaginary = Vector4.Zero; + float4 real = float4.Zero; + float4 imaginary = float4.Zero; int maxY = source.Height; int maxX = source.Width; int kernelLength = kernel.Length; @@ -334,7 +334,7 @@ public sealed partial class HlslBokehBlurProcessor { int offsetY = Hlsl.Clamp(ThreadIds.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(ThreadIds.X, 0, maxX); - Vector4 color = source[offsetX, offsetY]; + float4 color = source[offsetX, offsetY]; Complex64 factors = kernel[i]; real += factors.Real * color; @@ -355,16 +355,16 @@ public sealed partial class HlslBokehBlurProcessor public float z; public float w; - public ReadWriteTexture2D reals; - public ReadWriteTexture2D imaginaries; - public ReadWriteTexture2D target; + public ReadWriteTexture2D reals; + public ReadWriteTexture2D imaginaries; + public ReadWriteTexture2D target; public ReadOnlyBuffer kernel; /// public void Execute() { - Vector4 real = Vector4.Zero; - Vector4 imaginary = Vector4.Zero; + float4 real = float4.Zero; + float4 imaginary = float4.Zero; int maxY = target.Height; int maxX = target.Width; int kernelLength = kernel.Length; @@ -374,8 +374,8 @@ public sealed partial class HlslBokehBlurProcessor for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(ThreadIds.X + i - radiusX, 0, maxX); - Vector4 sourceReal = reals[offsetX, offsetY]; - Vector4 sourceImaginary = imaginaries[offsetX, offsetY]; + float4 sourceReal = reals[offsetX, offsetY]; + float4 sourceImaginary = imaginaries[offsetX, offsetY]; Complex64 factors = kernel[i]; real += factors.Real * sourceReal - factors.Imaginary * sourceImaginary; @@ -392,7 +392,7 @@ public sealed partial class HlslBokehBlurProcessor [AutoConstructor] internal readonly partial struct GammaHighlightProcessor : IComputeShader { - public readonly IReadWriteTexture2D source; + public readonly IReadWriteTexture2D source; /// public void Execute() @@ -416,8 +416,8 @@ public sealed partial class HlslBokehBlurProcessor [AutoConstructor] internal readonly partial struct InverseGammaHighlightProcessor : IComputeShader { - public readonly ReadWriteTexture2D source; - public readonly IReadWriteTexture2D target; + public readonly ReadWriteTexture2D source; + public readonly IReadWriteTexture2D target; /// public void Execute() diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index c993f9d2..0079ac2f 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -101,8 +101,8 @@ public sealed partial class HlslGaussianBlurProcessor Span span = MemoryMarshal.Cast(pixelSpan); - using ReadWriteTexture2D sourceTexture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height); - using ReadWriteTexture2D firstPassTexture = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D sourceTexture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D firstPassTexture = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = GraphicsDevice.AllocateReadOnlyBuffer(Kernel); using (var context = GraphicsDevice.CreateComputeContext()) @@ -121,14 +121,14 @@ public sealed partial class HlslGaussianBlurProcessor [AutoConstructor] internal readonly partial struct VerticalConvolutionProcessor : IComputeShader { - public readonly IReadWriteTexture2D source; - public readonly IReadWriteTexture2D target; + public readonly IReadWriteTexture2D source; + public readonly IReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; /// public void Execute() { - Vector4 result = Vector4.Zero; + float4 result = float4.Zero; int maxY = source.Height - 1; int maxX = source.Width - 1; int kernelLength = kernel.Length; @@ -138,7 +138,7 @@ public sealed partial class HlslGaussianBlurProcessor { int offsetY = Hlsl.Clamp(ThreadIds.Y + i - radiusY, 0, maxY); int offsetX = Hlsl.Clamp(ThreadIds.X, 0, maxX); - Vector4 color = source[offsetX, offsetY]; + float4 color = source[offsetX, offsetY]; result += kernel[i] * color; } @@ -153,14 +153,14 @@ public sealed partial class HlslGaussianBlurProcessor [AutoConstructor] internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader { - public readonly IReadWriteTexture2D source; - public readonly IReadWriteTexture2D target; + public readonly IReadWriteTexture2D source; + public readonly IReadWriteTexture2D target; public readonly ReadOnlyBuffer kernel; /// public void Execute() { - Vector4 result = Vector4.Zero; + float4 result = float4.Zero; int maxY = source.Height - 1; int maxX = source.Width - 1; int kernelLength = kernel.Length; @@ -170,7 +170,7 @@ public sealed partial class HlslGaussianBlurProcessor for (int i = 0; i < kernelLength; i++) { int offsetX = Hlsl.Clamp(ThreadIds.X + i - radiusX, 0, maxX); - Vector4 color = source[offsetX, offsetY]; + float4 color = source[offsetX, offsetY]; result += kernel[i] * color; } diff --git a/src/ComputeSharp/Core/Imaging/Bgra32.cs b/src/ComputeSharp/Core/Imaging/Bgra32.cs index 27ed1544..4a06092b 100644 --- a/src/ComputeSharp/Core/Imaging/Bgra32.cs +++ b/src/ComputeSharp/Core/Imaging/Bgra32.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -15,7 +14,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Bgra32 : IEquatable, IPixel, IPixel +public struct Bgra32 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rg16.cs b/src/ComputeSharp/Core/Imaging/Rg16.cs index c6aab570..0f1e9fc0 100644 --- a/src/ComputeSharp/Core/Imaging/Rg16.cs +++ b/src/ComputeSharp/Core/Imaging/Rg16.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -12,7 +11,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct Rg16 : IEquatable, IPixel, IPixel +public struct Rg16 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rg32.cs b/src/ComputeSharp/Core/Imaging/Rg32.cs index dda953ce..f8fc621d 100644 --- a/src/ComputeSharp/Core/Imaging/Rg32.cs +++ b/src/ComputeSharp/Core/Imaging/Rg32.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -12,7 +11,7 @@ namespace ComputeSharp; /// /// [StructLayout(LayoutKind.Sequential)] -public struct Rg32 : IEquatable, IPixel, IPixel +public struct Rg32 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rgba32.cs b/src/ComputeSharp/Core/Imaging/Rgba32.cs index 5cc7cd9c..4a54a90c 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba32.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba32.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -14,7 +13,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Rgba32 : IEquatable, IPixel, IPixel +public struct Rgba32 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/src/ComputeSharp/Core/Imaging/Rgba64.cs b/src/ComputeSharp/Core/Imaging/Rgba64.cs index 6652717b..6655f2b4 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba64.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba64.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -14,7 +13,7 @@ namespace ComputeSharp; /// /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] -public struct Rgba64 : IEquatable, IPixel, IPixel +public struct Rgba64 : IEquatable, IPixel #if NET6_0_OR_GREATER , ISpanFormattable #endif diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index 63a8943f..e7c7624c 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Numerics; using System.Reflection; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; @@ -17,17 +16,12 @@ public partial class Texture2DTests [AllDevices] [Resource(typeof(ReadOnlyTexture2D<,>))] [Resource(typeof(ReadWriteTexture2D<,>))] - [Data(typeof(Bgra32), typeof(Vector4))] [Data(typeof(Bgra32), typeof(float4))] [Data(typeof(R16), typeof(float))] [Data(typeof(R8), typeof(float))] - [Data(typeof(Rg16), typeof(Vector2))] [Data(typeof(Rg16), typeof(float2))] - [Data(typeof(Rg32), typeof(Vector2))] [Data(typeof(Rg32), typeof(float2))] - [Data(typeof(Rgba32), typeof(Vector4))] [Data(typeof(Rgba32), typeof(float4))] - [Data(typeof(Rgba64), typeof(Vector4))] [Data(typeof(Rgba64), typeof(float4))] public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel) { @@ -54,7 +48,7 @@ public partial class Texture2DTests try { - new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); } catch (TargetInvocationException e) when (e.InnerException is not null) { diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs index b7ca519a..a32e6235 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -9,27 +8,15 @@ public partial class Texture2DTests { [CombinatorialTestMethod] [AllDevices] - [Data(typeof(Bgra32), typeof(Vector4))] [Data(typeof(Bgra32), typeof(Float4))] [Data(typeof(R16), typeof(float))] [Data(typeof(R8), typeof(float))] - [Data(typeof(Rg16), typeof(Vector2))] [Data(typeof(Rg16), typeof(Float2))] - [Data(typeof(Rg32), typeof(Vector2))] [Data(typeof(Rg32), typeof(Float2))] - [Data(typeof(Rgba32), typeof(Vector4))] [Data(typeof(Rgba32), typeof(Float4))] - [Data(typeof(Rgba64), typeof(Vector4))] [Data(typeof(Rgba64), typeof(Float4))] public void Dispatch_NormalizedTexture2D(Device device, Type t, Type tPixel) { - if (t == typeof(Bgra32) && tPixel == typeof(Vector4)) - { - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - - device.Get().For(128, 128, new Shader_Unorm_Bgra32_Vector4(source, destination)); - } if (t == typeof(Bgra32) && tPixel == typeof(Float4)) { using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); @@ -51,13 +38,6 @@ public partial class Texture2DTests device.Get().For(128, 128, new Shader_Unorm_R8_float(source, destination)); } - if (t == typeof(Rg16) && tPixel == typeof(Vector2)) - { - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - - device.Get().For(128, 128, new Shader_Unorm_Rg16_Vector2(source, destination)); - } if (t == typeof(Rg16) && tPixel == typeof(Float2)) { using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); @@ -65,13 +45,6 @@ public partial class Texture2DTests device.Get().For(128, 128, new Shader_Unorm_Rg16_Float2(source, destination)); } - if (t == typeof(Rg32) && tPixel == typeof(Vector2)) - { - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - - device.Get().For(128, 128, new Shader_Unorm_Rg32_Vector2(source, destination)); - } if (t == typeof(Rg32) && tPixel == typeof(Float2)) { using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); @@ -79,13 +52,6 @@ public partial class Texture2DTests device.Get().For(128, 128, new Shader_Unorm_Rg32_Float2(source, destination)); } - if (t == typeof(Rgba32) && tPixel == typeof(Vector4)) - { - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - - device.Get().For(128, 128, new Shader_Unorm_Rgba32_Vector4(source, destination)); - } if (t == typeof(Rgba32) && tPixel == typeof(Float4)) { using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); @@ -93,13 +59,6 @@ public partial class Texture2DTests device.Get().For(128, 128, new Shader_Unorm_Rgba32_Float4(source, destination)); } - if (t == typeof(Rgba64) && tPixel == typeof(Vector4)) - { - using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - - device.Get().For(128, 128, new Shader_Unorm_Rgba64_Vector4(source, destination)); - } if (t == typeof(Rgba64) && tPixel == typeof(Float4)) { using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); @@ -109,18 +68,6 @@ public partial class Texture2DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Bgra32_Vector4 : IComputeShader - { - public readonly ReadOnlyTexture2D source; - public readonly ReadWriteTexture2D destination; - - public void Execute() - { - destination[ThreadIds.XY] = (source[ThreadIds.XY] + destination[ThreadIds.XY]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Bgra32_Float4 : IComputeShader { @@ -157,18 +104,6 @@ public partial class Texture2DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rg16_Vector2 : IComputeShader - { - public readonly ReadOnlyTexture2D source; - public readonly ReadWriteTexture2D destination; - - public void Execute() - { - destination[ThreadIds.XY] = (source[ThreadIds.XY] + destination[ThreadIds.XY]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rg16_Float2 : IComputeShader { @@ -181,18 +116,6 @@ public partial class Texture2DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rg32_Vector2 : IComputeShader - { - public readonly ReadOnlyTexture2D source; - public readonly ReadWriteTexture2D destination; - - public void Execute() - { - destination[ThreadIds.XY] = (source[ThreadIds.XY] + destination[ThreadIds.XY]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rg32_Float2 : IComputeShader { @@ -205,18 +128,6 @@ public partial class Texture2DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rgba32_Vector4 : IComputeShader - { - public readonly ReadOnlyTexture2D source; - public readonly ReadWriteTexture2D destination; - - public void Execute() - { - destination[ThreadIds.XY] = (source[ThreadIds.XY] + destination[ThreadIds.XY]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rgba32_Float4 : IComputeShader { @@ -229,18 +140,6 @@ public partial class Texture2DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rgba64_Vector4 : IComputeShader - { - public readonly ReadOnlyTexture2D source; - public readonly ReadWriteTexture2D destination; - - public void Execute() - { - destination[ThreadIds.XY] = (source[ThreadIds.XY] + destination[ThreadIds.XY]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rgba64_Float4 : IComputeShader { diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt index f62aa010..43d11ee2 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt @@ -7,13 +7,13 @@ <# var typesMap = new Dictionary { - ["Bgra32"] = new[] { "Vector4", "Float4" }, + ["Bgra32"] = new[] { "Float4" }, ["R16"] = new[] { "float" }, ["R8"] = new[] { "float" }, - ["Rg16"] = new[] { "Vector2", "Float2" }, - ["Rg32"] = new[] { "Vector2", "Float2" }, - ["Rgba32"] = new[] { "Vector4", "Float4" }, - ["Rgba64"] = new[] { "Vector4", "Float4" } + ["Rg16"] = new[] { "Float2" }, + ["Rg32"] = new[] { "Float2" }, + ["Rgba32"] = new[] { "Float4" }, + ["Rgba64"] = new[] { "Float4" } }; IEnumerable<(string T, string TPixel)> EnumerateTypes() @@ -24,7 +24,6 @@ IEnumerable<(string T, string TPixel)> EnumerateTypes() } #> using System; -using System.Numerics; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs index 0441ae8e..ec8f9a64 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Reflection; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; @@ -14,17 +13,12 @@ public partial class Texture3DTests [AllDevices] [Resource(typeof(ReadOnlyTexture3D<,>))] [Resource(typeof(ReadWriteTexture3D<,>))] - [Data(typeof(Bgra32), typeof(Vector4))] [Data(typeof(Bgra32), typeof(float4))] [Data(typeof(R16), typeof(float))] [Data(typeof(R8), typeof(float))] - [Data(typeof(Rg16), typeof(Vector2))] [Data(typeof(Rg16), typeof(float2))] - [Data(typeof(Rg32), typeof(Vector2))] [Data(typeof(Rg32), typeof(float2))] - [Data(typeof(Rgba32), typeof(Vector4))] [Data(typeof(Rgba32), typeof(float4))] - [Data(typeof(Rgba64), typeof(Vector4))] [Data(typeof(Rgba64), typeof(float4))] public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel) { @@ -52,7 +46,7 @@ public partial class Texture3DTests try { - new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); } catch (TargetInvocationException e) when (e.InnerException is not null) { diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs index 23a9d4c5..9debe150 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -9,27 +8,15 @@ public partial class Texture3DTests { [CombinatorialTestMethod] [AllDevices] - [Data(typeof(Bgra32), typeof(Vector4))] [Data(typeof(Bgra32), typeof(Float4))] [Data(typeof(R16), typeof(float))] [Data(typeof(R8), typeof(float))] - [Data(typeof(Rg16), typeof(Vector2))] [Data(typeof(Rg16), typeof(Float2))] - [Data(typeof(Rg32), typeof(Vector2))] [Data(typeof(Rg32), typeof(Float2))] - [Data(typeof(Rgba32), typeof(Vector4))] [Data(typeof(Rgba32), typeof(Float4))] - [Data(typeof(Rgba64), typeof(Vector4))] [Data(typeof(Rgba64), typeof(Float4))] public void Dispatch_NormalizedTexture3D(Device device, Type t, Type tPixel) { - if (t == typeof(Bgra32) && tPixel == typeof(Vector4)) - { - using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - - device.Get().For(128, 128, 2, new Shader_Unorm_Bgra32_Vector4(source, destination)); - } if (t == typeof(Bgra32) && tPixel == typeof(Float4)) { using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); @@ -51,13 +38,6 @@ public partial class Texture3DTests device.Get().For(128, 128, 2, new Shader_Unorm_R8_float(source, destination)); } - if (t == typeof(Rg16) && tPixel == typeof(Vector2)) - { - using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - - device.Get().For(128, 128, 2, new Shader_Unorm_Rg16_Vector2(source, destination)); - } if (t == typeof(Rg16) && tPixel == typeof(Float2)) { using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); @@ -65,13 +45,6 @@ public partial class Texture3DTests device.Get().For(128, 128, 2, new Shader_Unorm_Rg16_Float2(source, destination)); } - if (t == typeof(Rg32) && tPixel == typeof(Vector2)) - { - using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - - device.Get().For(128, 128, 2, new Shader_Unorm_Rg32_Vector2(source, destination)); - } if (t == typeof(Rg32) && tPixel == typeof(Float2)) { using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); @@ -79,13 +52,6 @@ public partial class Texture3DTests device.Get().For(128, 128, 2, new Shader_Unorm_Rg32_Float2(source, destination)); } - if (t == typeof(Rgba32) && tPixel == typeof(Vector4)) - { - using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - - device.Get().For(128, 128, 2, new Shader_Unorm_Rgba32_Vector4(source, destination)); - } if (t == typeof(Rgba32) && tPixel == typeof(Float4)) { using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); @@ -93,13 +59,6 @@ public partial class Texture3DTests device.Get().For(128, 128, 2, new Shader_Unorm_Rgba32_Float4(source, destination)); } - if (t == typeof(Rgba64) && tPixel == typeof(Vector4)) - { - using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - - device.Get().For(128, 128, 2, new Shader_Unorm_Rgba64_Vector4(source, destination)); - } if (t == typeof(Rgba64) && tPixel == typeof(Float4)) { using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); @@ -109,18 +68,6 @@ public partial class Texture3DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Bgra32_Vector4 : IComputeShader - { - public readonly ReadOnlyTexture3D source; - public readonly ReadWriteTexture3D destination; - - public void Execute() - { - destination[ThreadIds.XYZ] = (source[ThreadIds.XYZ] + destination[ThreadIds.XYZ]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Bgra32_Float4 : IComputeShader { @@ -157,18 +104,6 @@ public partial class Texture3DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rg16_Vector2 : IComputeShader - { - public readonly ReadOnlyTexture3D source; - public readonly ReadWriteTexture3D destination; - - public void Execute() - { - destination[ThreadIds.XYZ] = (source[ThreadIds.XYZ] + destination[ThreadIds.XYZ]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rg16_Float2 : IComputeShader { @@ -181,18 +116,6 @@ public partial class Texture3DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rg32_Vector2 : IComputeShader - { - public readonly ReadOnlyTexture3D source; - public readonly ReadWriteTexture3D destination; - - public void Execute() - { - destination[ThreadIds.XYZ] = (source[ThreadIds.XYZ] + destination[ThreadIds.XYZ]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rg32_Float2 : IComputeShader { @@ -205,18 +128,6 @@ public partial class Texture3DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rgba32_Vector4 : IComputeShader - { - public readonly ReadOnlyTexture3D source; - public readonly ReadWriteTexture3D destination; - - public void Execute() - { - destination[ThreadIds.XYZ] = (source[ThreadIds.XYZ] + destination[ThreadIds.XYZ]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rgba32_Float4 : IComputeShader { @@ -229,18 +140,6 @@ public partial class Texture3DTests } } - [AutoConstructor] - internal readonly partial struct Shader_Unorm_Rgba64_Vector4 : IComputeShader - { - public readonly ReadOnlyTexture3D source; - public readonly ReadWriteTexture3D destination; - - public void Execute() - { - destination[ThreadIds.XYZ] = (source[ThreadIds.XYZ] + destination[ThreadIds.XYZ]) / 2; - } - } - [AutoConstructor] internal readonly partial struct Shader_Unorm_Rgba64_Float4 : IComputeShader { diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt index b1fdbeed..e83e615a 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt @@ -7,13 +7,13 @@ <# var typesMap = new Dictionary { - ["Bgra32"] = new[] { "Vector4", "Float4" }, + ["Bgra32"] = new[] { "Float4" }, ["R16"] = new[] { "float" }, ["R8"] = new[] { "float" }, - ["Rg16"] = new[] { "Vector2", "Float2" }, - ["Rg32"] = new[] { "Vector2", "Float2" }, - ["Rgba32"] = new[] { "Vector4", "Float4" }, - ["Rgba64"] = new[] { "Vector4", "Float4" } + ["Rg16"] = new[] { "Float2" }, + ["Rg32"] = new[] { "Float2" }, + ["Rgba32"] = new[] { "Float4" }, + ["Rgba64"] = new[] { "Float4" } }; IEnumerable<(string T, string TPixel)> EnumerateTypes() @@ -24,7 +24,6 @@ IEnumerable<(string T, string TPixel)> EnumerateTypes() } #> using System; -using System.Numerics; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; From e0cae9083203508923b65875ef1bc7275877bbdd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 8 Jan 2022 17:24:09 +0100 Subject: [PATCH 5/7] Add IPixel.ToPixel() API, minor code refactoring --- src/ComputeSharp/Core/Imaging/Bgra32.cs | 29 +++++-- .../Imaging/Interfaces/IPixel{T,TPixel}.cs | 10 +++ src/ComputeSharp/Core/Imaging/R16.cs | 11 ++- src/ComputeSharp/Core/Imaging/R8.cs | 11 ++- src/ComputeSharp/Core/Imaging/Rg16.cs | 13 ++- src/ComputeSharp/Core/Imaging/Rg32.cs | 13 ++- src/ComputeSharp/Core/Imaging/Rgba32.cs | 13 ++- src/ComputeSharp/Core/Imaging/Rgba64.cs | 13 ++- .../Graphics/Helpers/DXGIFormatHelper.cs | 85 +------------------ .../Extensions/ComputeContextExtensions.cs | 8 +- 10 files changed, 97 insertions(+), 109 deletions(-) diff --git a/src/ComputeSharp/Core/Imaging/Bgra32.cs b/src/ComputeSharp/Core/Imaging/Bgra32.cs index 4a06092b..c1f1ab2a 100644 --- a/src/ComputeSharp/Core/Imaging/Bgra32.cs +++ b/src/ComputeSharp/Core/Imaging/Bgra32.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -48,10 +49,10 @@ public struct Bgra32 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public Bgra32(byte r, byte g, byte b) { - R = r; - G = g; - B = b; - A = byte.MaxValue; + this.R = r; + this.G = g; + this.B = b; + this.A = byte.MaxValue; } /// @@ -64,10 +65,10 @@ public struct Bgra32 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public Bgra32(byte r, byte g, byte b, byte a) { - R = r; - G = g; - B = b; - A = a; + this.R = r; + this.G = g; + this.B = b; + this.A = a; } /// @@ -76,12 +77,22 @@ public struct Bgra32 : IEquatable, IPixel public uint PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float4 ToPixel() + { + Vector4 linear = new(this.R, this.G, this.B, this.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs b/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs index be3f2740..4c32dba5 100644 --- a/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs +++ b/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs @@ -9,4 +9,14 @@ public interface IPixel where T : unmanaged, IPixel where TPixel : unmanaged { + /// + /// Converts the current value into its normalized representation. + /// + /// The representation for the current value. + /// + /// This method is primarily meant to be used to support + /// + /// and . + /// + TPixel ToPixel(); } diff --git a/src/ComputeSharp/Core/Imaging/R16.cs b/src/ComputeSharp/Core/Imaging/R16.cs index 850d9222..f096e73b 100644 --- a/src/ComputeSharp/Core/Imaging/R16.cs +++ b/src/ComputeSharp/Core/Imaging/R16.cs @@ -27,7 +27,7 @@ public struct R16 : IEquatable, IPixel /// The red component. public R16(ushort r) { - R = r; + this.R = r; } /// @@ -36,12 +36,19 @@ public struct R16 : IEquatable, IPixel public ushort PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly float ToPixel() + { + return this.R / (float)ushort.MaxValue; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/R8.cs b/src/ComputeSharp/Core/Imaging/R8.cs index 7f95b8c4..e438980c 100644 --- a/src/ComputeSharp/Core/Imaging/R8.cs +++ b/src/ComputeSharp/Core/Imaging/R8.cs @@ -27,7 +27,7 @@ public struct R8 : IEquatable, IPixel /// The red component. public R8(byte r) { - R = r; + this.R = r; } /// @@ -36,12 +36,19 @@ public struct R8 : IEquatable, IPixel public byte PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly float ToPixel() + { + return this.R / (float)byte.MaxValue; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rg16.cs b/src/ComputeSharp/Core/Imaging/Rg16.cs index 0f1e9fc0..97baee2c 100644 --- a/src/ComputeSharp/Core/Imaging/Rg16.cs +++ b/src/ComputeSharp/Core/Imaging/Rg16.cs @@ -33,8 +33,8 @@ public struct Rg16 : IEquatable, IPixel /// The green component. public Rg16(byte r, byte g) { - R = r; - G = g; + this.R = r; + this.G = g; } /// @@ -43,12 +43,19 @@ public struct Rg16 : IEquatable, IPixel public ushort PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float2 ToPixel() + { + return new(this.R / (float)byte.MaxValue, this.G / (float)byte.MaxValue); + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rg32.cs b/src/ComputeSharp/Core/Imaging/Rg32.cs index f8fc621d..99742d81 100644 --- a/src/ComputeSharp/Core/Imaging/Rg32.cs +++ b/src/ComputeSharp/Core/Imaging/Rg32.cs @@ -33,8 +33,8 @@ public struct Rg32 : IEquatable, IPixel /// The green component. public Rg32(ushort r, ushort g) { - R = r; - G = g; + this.R = r; + this.G = g; } /// @@ -43,12 +43,19 @@ public struct Rg32 : IEquatable, IPixel public uint PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float2 ToPixel() + { + return new(this.R / (float)ushort.MaxValue, this.G / (float)ushort.MaxValue); + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rgba32.cs b/src/ComputeSharp/Core/Imaging/Rgba32.cs index 4a54a90c..5daab793 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba32.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba32.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,12 +76,22 @@ public struct Rgba32 : IEquatable, IPixel public uint PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float4 ToPixel() + { + Vector4 linear = new(this.R, this.G, this.B, this.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rgba64.cs b/src/ComputeSharp/Core/Imaging/Rgba64.cs index 6655f2b4..59f6dd1d 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba64.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba64.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,12 +76,22 @@ public struct Rgba64 : IEquatable, IPixel public ulong PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float4 ToPixel() + { + Vector4 linear = new(this.R, this.G, this.B, this.A); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index e14a142a..985f2271 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -95,7 +95,7 @@ internal static class DXGIFormatHelper /// The input pixel value to convert. /// The equivalent value for . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Float4 ExtendToNormalizedValue(ref T value) + public static Float4 ExtendToNormalizedValue(T value) where T : unmanaged { if (typeof(T) == typeof(Float4)) @@ -109,87 +109,4 @@ internal static class DXGIFormatHelper return result; } - - /// - /// Converts a given value to a normalized value. - /// - /// The input value to convert. - /// The normalized value for . - /// Thrown when the input type is not supported. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Float4 ConvertToNormalizedValue(ref T value) - where T : unmanaged - { - if (typeof(T) == typeof(Bgra32)) - { - ref Bgra32 bgra = ref Unsafe.As(ref value); - - Vector4 linear = new(bgra.R, bgra.G, bgra.B, bgra.A); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rgba32)) - { - ref Rgba32 rgba = ref Unsafe.As(ref value); - - Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rgba64)) - { - ref Rgba64 rgba = ref Unsafe.As(ref value); - - Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); - Vector4 normalized = linear / ushort.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(R8)) - { - ref R8 r = ref Unsafe.As(ref value); - - Vector4 linear = new(r.R, 0, 0, 0); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(R16)) - { - ref R16 r = ref Unsafe.As(ref value); - - Vector4 linear = new(r.R, 0, 0, 0); - Vector4 normalized = linear / ushort.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rg16)) - { - ref Rg16 rg = ref Unsafe.As(ref value); - - Vector4 linear = new(rg.R, rg.G, 0, 0); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rg32)) - { - ref Rg32 rg = ref Unsafe.As(ref value); - - Vector4 linear = new(rg.R, rg.G, 0, 0); - Vector4 normalized = linear / ushort.MaxValue; - - return normalized; - } - - return ThrowHelper.ThrowArgumentException("Invalid pixel type"); - } } diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 131c035d..c2f3863f 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -215,7 +215,7 @@ public static partial class ComputeContextExtensions { var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel())); } /// @@ -232,7 +232,7 @@ public static partial class ComputeContextExtensions { var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel())); } /// @@ -251,7 +251,7 @@ public static partial class ComputeContextExtensions ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, - DXGIFormatHelper.ExtendToNormalizedValue(ref value)); + DXGIFormatHelper.ExtendToNormalizedValue(value)); } /// @@ -270,7 +270,7 @@ public static partial class ComputeContextExtensions ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, - DXGIFormatHelper.ExtendToNormalizedValue(ref value)); + DXGIFormatHelper.ExtendToNormalizedValue(value)); } /// From 34450b270f9639a59dd4f20f84f937fff841a134 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 8 Jan 2022 18:59:20 +0100 Subject: [PATCH 6/7] Add unit tests for ComputeContext.Fill APIs --- .../ComputeSharp.Tests/ComputeContextTests.cs | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 4265c32f..86b14b8c 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -335,6 +335,212 @@ public partial class ComputeContextTests TestHelper.Run(Test, cpuType, gpuType, device); } + /// + /// Gets a sample pixel color of the specified type. + /// + /// The type of pixel to get. + /// A sample pixel color of the specified type. + /// Thrown when isn't valid. + private static T GetColor() + { + if (typeof(T) == typeof(Bgra32)) + { + return (T)(object)new Bgra32(0x6B, 0x6D, 0x9E, 0xC0); + } + + if (typeof(T) == typeof(Rgba32)) + { + return (T)(object)new Rgba32(0x6B, 0x6D, 0x9E, 0xC0); + } + + if (typeof(T) == typeof(Rgba64)) + { + return (T)(object)new Rgba64(0x6B, 0x6D05, 0x9E, 0xC0); + } + + if (typeof(T) == typeof(R8)) + { + return (T)(object)new R8(0x6B); + } + + if (typeof(T) == typeof(R16)) + { + return (T)(object)new R16(0x6D05); + } + + if (typeof(T) == typeof(Rg16)) + { + return (T)(object)new Rg16(0x6B, 0x6D); + } + + if (typeof(T) == typeof(Rg32)) + { + return (T)(object)new Rg32(0x6B, 0x6D05); + } + + throw new ArgumentException("Invalid pixel type"); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Fill_ReadWriteTexture2D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill(texture, color); + } + + T[,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Fill_ReadWriteTexture3D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill(texture, color); + } + + T[,,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Fill_ReadWriteTexture2D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill((IReadWriteTexture2D)texture, color.ToPixel()); + } + + T[,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Fill_ReadWriteTexture3D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T color = GetColor(); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Fill((IReadWriteTexture3D)texture, color.ToPixel()); + } + + T[,,] result = texture.ToArray(); + + foreach (T value in result) + { + Assert.AreEqual(value, color); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + [CombinatorialTestMethod] [AllDevices] public void For_Batched(Device device) From 67138ca14fbf271d94f0cab00b6c36b578948829 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 8 Jan 2022 19:05:04 +0100 Subject: [PATCH 7/7] Remove unnecessary partial modifier --- src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index c2f3863f..d288d243 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -9,7 +9,7 @@ namespace ComputeSharp; /// /// A that contains extension methods for the type, used to run compute shaders. /// -public static partial class ComputeContextExtensions +public static class ComputeContextExtensions { /// /// Inserts a resource barrier for a specific resource.