diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs index 467b900b..d72e9827 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.g.cs @@ -346,7 +346,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAdd(int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(int)}, {typeof(int)})"); + public static void InterlockedAdd(ref int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic add of a value to a destination. @@ -357,7 +357,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAdd(uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(uint)}, {typeof(uint)})"); + public static void InterlockedAdd(ref uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic add of a value to a destination. @@ -369,7 +369,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAdd(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedAdd(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic add of a value to a destination. @@ -381,7 +381,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAdd(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedAdd(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAdd)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic and of a value to a destination. @@ -392,7 +392,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAnd(int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(int)}, {typeof(int)})"); + public static void InterlockedAnd(ref int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic and of a value to a destination. @@ -403,7 +403,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAnd(uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(uint)}, {typeof(uint)})"); + public static void InterlockedAnd(ref uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic and of a value to a destination. @@ -415,7 +415,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAnd(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedAnd(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic and of a value to a destination. @@ -427,7 +427,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedAnd(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedAnd(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedAnd)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Atomically compares the destination with the comparison value. If they are identical, the destination @@ -441,7 +441,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedCompareExchange(int destination, int comparison, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareExchange)}({typeof(int)}, {typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedCompareExchange(ref int destination, int comparison, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareExchange)}({typeof(int)}, {typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Atomically compares the destination with the comparison value. If they are identical, the destination @@ -455,7 +455,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedCompareExchange(uint destination, uint comparison, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareExchange)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedCompareExchange(ref uint destination, uint comparison, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareExchange)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Atomically compares the destination to the comparison value. If they @@ -468,7 +468,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedCompareStore(int destination, int comparison, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareStore)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedCompareStore(ref int destination, int comparison, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareStore)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Atomically compares the destination to the comparison value. If they @@ -481,7 +481,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedCompareStore(uint destination, uint comparison, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareStore)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedCompareStore(ref uint destination, uint comparison, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedCompareStore)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Assigns value to dest and returns the original value. @@ -493,7 +493,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedExchange(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedExchange)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedExchange(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedExchange)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Assigns value to dest and returns the original value. @@ -505,7 +505,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedExchange(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedExchange)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedExchange(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedExchange)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic max. @@ -516,7 +516,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMax(int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(int)}, {typeof(int)})"); + public static void InterlockedMax(ref int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic max. @@ -527,7 +527,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMax(uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(uint)}, {typeof(uint)})"); + public static void InterlockedMax(ref uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic max. @@ -539,7 +539,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMax(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedMax(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic max. @@ -551,7 +551,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMax(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedMax(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMax)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic min. @@ -562,7 +562,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMin(int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(int)}, {typeof(int)})"); + public static void InterlockedMin(ref int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic min. @@ -573,7 +573,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMin(uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(uint)}, {typeof(uint)})"); + public static void InterlockedMin(ref uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic min. @@ -585,7 +585,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMin(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedMin(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic min. @@ -597,7 +597,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedMin(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedMin(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedMin)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic or. @@ -608,7 +608,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedOr(int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(int)}, {typeof(int)})"); + public static void InterlockedOr(ref int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic or. @@ -619,7 +619,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedOr(uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(uint)}, {typeof(uint)})"); + public static void InterlockedOr(ref uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic or. @@ -631,7 +631,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedOr(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedOr(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic or. @@ -643,7 +643,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedOr(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedOr(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedOr)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic xor. @@ -654,7 +654,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedXor(int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(int)}, {typeof(int)})"); + public static void InterlockedXor(ref int destination, int value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic xor. @@ -665,7 +665,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedXor(uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(uint)}, {typeof(uint)})"); + public static void InterlockedXor(ref uint destination, uint value) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(uint)}, {typeof(uint)})"); /// /// Performs a guaranteed atomic xor. @@ -677,7 +677,7 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedXor(int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); + public static void InterlockedXor(ref int destination, int value, out int original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(int)}, {typeof(int)}, {typeof(int)})"); /// /// Performs a guaranteed atomic xor. @@ -689,5 +689,5 @@ partial class Hlsl /// For more info, see . /// This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior. /// - public static void InterlockedXor(uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); + public static void InterlockedXor(ref uint destination, uint value, out uint original) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(InterlockedXor)}({typeof(uint)}, {typeof(uint)}, {typeof(uint)})"); } diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt index 9484c786..1536a67a 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.Void.tt @@ -72,7 +72,7 @@ foreach (var intrinsic in Intrinsics) } Write($"public static void {intrinsic.Name}("); - Write(string.Join(", ", intrinsic.Parameters.Where(p => p.Name is not "").Zip(overload, (a, b) => $"{(a.IsOut ? "out " : "")}{b} {a.Name}"))); + Write(string.Join(", ", intrinsic.Parameters.Where(p => p.Name is not "").Zip(overload, (a, b) => $"{(a.IsRef ? "ref " : a.IsOut ? "out " : "")}{b} {a.Name}"))); Write(")"); Write(" => throw new InvalidExecutionContextException("); diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 3167bf64..a436e08f 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -22,6 +22,7 @@ + diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs index ebb3db4a..ff9876a1 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -279,4 +279,38 @@ public partial class ShaderRewriterTests buffer2[7] = exponentUppercaseField; } } + + [CombinatorialTestMethod] + [AllDevices] + public void InterlockedOperations(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(16); + + device.Get().For(16, new InterlockedOperationsShader(buffer)); + + int[] result = buffer.ToArray(); + + for (int i = 0; i < result.Length; i++) + { + Assert.AreEqual(i * 2, result[i]); + } + } + + [AutoConstructor] + [EmbeddedBytecode(DispatchAxis.X)] + internal readonly partial struct InterlockedOperationsShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public void Execute() + { + // InterlockedExchange 0, which effectively just writes 0 to the target. + // Then InterlockedAdd to increment the value to ThreadIds.X. Finally, + // InterlockedCompareExchange with the expected value, setting twice as much. + // The result each value in the buffer should have after this is ThreadIds.X * 2. + Hlsl.InterlockedExchange(ref buffer[ThreadIds.X], 0, out _); + Hlsl.InterlockedAdd(ref buffer[ThreadIds.X], ThreadIds.X); + Hlsl.InterlockedCompareExchange(ref buffer[ThreadIds.X], ThreadIds.X, ThreadIds.X * 2, out _); + } + } }