From 088698a0b9a7bb55eb9ef1f0975e3ff33c893101 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Sep 2020 01:39:56 +0200 Subject: [PATCH 1/3] Added placeholders for interlocked intrinsics --- .../Attributes/PreserveMemberNameAttribute.cs | 11 + src/ComputeSharp.Core/Intrinsics/Hlsl.cs | 201 ++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 src/ComputeSharp.Core/Attributes/PreserveMemberNameAttribute.cs diff --git a/src/ComputeSharp.Core/Attributes/PreserveMemberNameAttribute.cs b/src/ComputeSharp.Core/Attributes/PreserveMemberNameAttribute.cs new file mode 100644 index 00000000..6140a4d2 --- /dev/null +++ b/src/ComputeSharp.Core/Attributes/PreserveMemberNameAttribute.cs @@ -0,0 +1,11 @@ +using System; + +namespace ComputeSharp.Core.Attributes +{ + /// + /// An attribute to indicate that a given member has the same name when mapped to HLSL + /// + internal sealed class PreserveMemberNameAttribute : Attribute + { + } +} diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs index 365fbf5c..ef489415 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs @@ -1,5 +1,6 @@ using System.Diagnostics.Contracts; using System.Numerics; +using ComputeSharp.Core.Attributes; using ComputeSharp.Exceptions; namespace ComputeSharp @@ -1383,6 +1384,206 @@ namespace ComputeSharp [Pure] public static Vector4 Frexp(Vector4 x, out Vector4 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frexp)}({nameof(Vector4)},{nameof(Vector4)})"); + /// + /// Performs a guaranteed atomic add of a value to a destination. + /// + /// The destination value. + /// The input value + [PreserveMemberName] + public static void InterlockedAdd(int destination, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAdd)}(int,int)"); + + /// + /// Performs a guaranteed atomic add of a value to a destination. + /// + /// The destination value. + /// The input value + [PreserveMemberName] + public static void InterlockedAdd(uint destination, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAdd)}(uint,uint)"); + + /// + /// Performs a guaranteed atomic add of a value to a destination. + /// + /// The destination value. + /// The input value + /// The original input value + [PreserveMemberName] + public static void InterlockedAdd(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAdd)}(int,int,int)"); + + /// + /// Performs a guaranteed atomic add of a value to a destination. + /// + /// The destination value. + /// The input value + /// The original input value + [PreserveMemberName] + public static void InterlockedAdd(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAdd)}(uint,uint,uint)"); + + /// + /// Performs a guaranteed atomic and of a value to a destination. + /// + /// The destination value. + /// The input value + [PreserveMemberName] + public static void InterlockedAnd(int destination, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAnd)}(int,int)"); + + /// + /// Performs a guaranteed atomic and of a value to a destination. + /// + /// The destination value. + /// The input value + [PreserveMemberName] + public static void InterlockedAnd(uint destination, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAnd)}(uint,uint)"); + + /// + /// Performs a guaranteed atomic and of a value to a destination. + /// + /// The destination value. + /// The input value + /// The original input value + [PreserveMemberName] + public static void InterlockedAnd(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAnd)}(int,int,int)"); + + /// + /// Performs a guaranteed atomic and of a value to a destination. + /// + /// The destination value. + /// The input value + /// The original input value + [PreserveMemberName] + public static void InterlockedAnd(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedAnd)}(uint,uint,uint)"); + + /// + /// Atomically compares the destination with the comparison value. If they are identical, the destination + /// is overwritten with the input value. The original value is set to the destination's original value. + /// + /// The destination value. + /// The comparison value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedCompareExchange(int destination, int comparison, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedCompareExchange)}(int,int,int,int)"); + + /// + /// Atomically compares the destination with the comparison value. If they are identical, the destination + /// is overwritten with the input value. The original value is set to the destination's original value. + /// + /// The destination value. + /// The comparison value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedCompareExchange(uint destination, uint comparison, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedCompareExchange)}(uint,uint,uint,uint)"); + + /// + /// Atomically compares the destination to the comparison value. If they + /// are identical, the destination is overwritten with the input value. + /// + /// The destination value. + /// The comparison value. + /// The input value. + [PreserveMemberName] + public static void InterlockedCompareStore(int destination, int comparison, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedCompareStore)}(int,int,int)"); + + /// + /// Atomically compares the destination to the comparison value. If they + /// are identical, the destination is overwritten with the input value. + /// + /// The destination value. + /// The comparison value. + /// The input value. + [PreserveMemberName] + public static void InterlockedCompareStore(uint destination, uint comparison, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedCompareStore)}(uint,uint,uint)"); + + /// + /// Assigns value to dest and returns the original value. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedExchange(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedExchange)}(int,int,int)"); + + /// + /// Assigns value to dest and returns the original value. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedExchange(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedExchange)}(uint,uint,uint)"); + + /// + /// Performs a guaranteed atomic max. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedMax(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMax)}(int,int,int)"); + + /// + /// Performs a guaranteed atomic max. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedMax(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMax)}(uint,uint,uint)"); + + /// + /// Performs a guaranteed atomic min. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedMin(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMin)}(int,int,int)"); + + /// + /// Performs a guaranteed atomic min. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedMin(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMin)}(uint,uint,uint)"); + + /// + /// Performs a guaranteed atomic or. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedOr(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedOr)}(int,int,int)"); + + /// + /// Performs a guaranteed atomic or. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedOr(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedOr)}(uint,uint,uint)"); + + /// + /// Performs a guaranteed atomic xor. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedXor(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedXor)}(int,int,int)"); + + /// + /// Performs a guaranteed atomic xor. + /// + /// The destination value. + /// The input value. + /// The original value. + [PreserveMemberName] + public static void InterlockedXor(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedXor)}(uint,uint,uint)"); + /// /// Determines if the specified floating-point value is finite /// From d4f341e6458983f22443ab76e406f97152d976f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Sep 2020 01:51:13 +0200 Subject: [PATCH 2/3] Enabled mapping of UpperCamelCase intrinsics --- src/ComputeSharp.Core/Properties/AssemblyInfo.cs | 1 + .../Mappings/HlslKnownMethods.cs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.Core/Properties/AssemblyInfo.cs b/src/ComputeSharp.Core/Properties/AssemblyInfo.cs index 87f5b80a..97fd8747 100644 --- a/src/ComputeSharp.Core/Properties/AssemblyInfo.cs +++ b/src/ComputeSharp.Core/Properties/AssemblyInfo.cs @@ -1,3 +1,4 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("ComputeSharp.Graphics")] +[assembly: InternalsVisibleTo("ComputeSharp.Shaders")] diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs index 930c9caa..8f8e92dd 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownMethods.cs @@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; +using ComputeSharp.Core.Attributes; using Microsoft.CodeAnalysis; namespace ComputeSharp.Shaders.Mappings @@ -236,13 +237,20 @@ namespace ComputeSharp.Shaders.Mappings }; // Programmatically load mappings from the Hlsl class as well - foreach (string name in + foreach (var method in from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static) group method by method.Name into groups - select groups.Key) + select (Name: groups.Key, MethodInfo: groups.First())) { - knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{name}", name.ToLowerInvariant()); + // Check whether the current method should be translated with the original name + // or with the lowercase version. This is needed because all C# methods are exposed + // with the upper camel case format, while HLSL intrinsics use multiple different formats. + string hlslName = method.MethodInfo.GetCustomAttribute() != null + ? method.Name + : method.Name.ToLowerInvariant(); + + knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName); } // Programmatically load mappings for the vector types From 5320bbfb049cead563d7e1767efd171502d61b4a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Sep 2020 01:57:35 +0200 Subject: [PATCH 3/3] Added missing intrinsics overloads --- src/ComputeSharp.Core/Intrinsics/Hlsl.cs | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs index ef489415..e4740576 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs @@ -1512,6 +1512,22 @@ namespace ComputeSharp [PreserveMemberName] public static void InterlockedExchange(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedExchange)}(uint,uint,uint)"); + /// + /// Performs a guaranteed atomic max. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedMax(int destination, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMax)}(int,int)"); + + /// + /// Performs a guaranteed atomic max. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedMax(uint destination, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMax)}(uint,uint)"); + /// /// Performs a guaranteed atomic max. /// @@ -1530,6 +1546,22 @@ namespace ComputeSharp [PreserveMemberName] public static void InterlockedMax(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMax)}(uint,uint,uint)"); + /// + /// Performs a guaranteed atomic min. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedMin(int destination, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMin)}(int,int)"); + + /// + /// Performs a guaranteed atomic min. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedMin(uint destination, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMin)}(uint,uint)"); + /// /// Performs a guaranteed atomic min. /// @@ -1548,6 +1580,22 @@ namespace ComputeSharp [PreserveMemberName] public static void InterlockedMin(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedMin)}(uint,uint,uint)"); + /// + /// Performs a guaranteed atomic or. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedOr(int destination, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedOr)}(int,int)"); + + /// + /// Performs a guaranteed atomic or. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedOr(uint destination, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedOr)}(uint,uint)"); + /// /// Performs a guaranteed atomic or. /// @@ -1566,6 +1614,22 @@ namespace ComputeSharp [PreserveMemberName] public static void InterlockedOr(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedOr)}(uint,uint,uint)"); + /// + /// Performs a guaranteed atomic xor. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedXor(int destination, int value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedXor)}(int,int)"); + + /// + /// Performs a guaranteed atomic xor. + /// + /// The destination value. + /// The input value. + [PreserveMemberName] + public static void InterlockedXor(uint destination, uint value) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(InterlockedXor)}(uint,uint)"); + /// /// Performs a guaranteed atomic xor. ///