From fb1c24a623ee03e83b1fd0ed6d66a2b4ec3d97a7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 18 Aug 2022 12:35:45 +0200 Subject: [PATCH] Fix Interlocked HLSL intrinsics --- .../ComputeSharp.SourceGenerators.csproj | 1 + .../ComputeSharp.Tests/ShaderRewriterTests.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) 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..1d7f8ece 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -279,4 +279,32 @@ 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, result[i]); + } + } + + [AutoConstructor] + [EmbeddedBytecode(DispatchAxis.X)] + internal readonly partial struct InterlockedOperationsShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public void Execute() + { + Hlsl.InterlockedAdd(buffer[ThreadIds.X], ThreadIds.X); + } + } }