diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs index 884a657d..58b2c3fc 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs @@ -680,7 +680,7 @@ public sealed partial class IShaderGenerator // Shared buffers foreach (var (bufferName, bufferType, bufferCount) in sharedBuffers) { - object count = (object?)bufferCount ?? "threadsX * threadsY * threadsZ"; + object count = (object?)bufferCount ?? "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z"; AppendLF(); AppendLineAndLF($"groupshared {bufferType} {bufferName} [{count}];"); diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index df57f287..b4078328 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -4,6 +4,8 @@ using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; +#pragma warning disable CS0649, CS8618 + namespace ComputeSharp.Tests; [TestClass] @@ -298,4 +300,90 @@ public partial class DispatchTests 1); } } + + [CombinatorialTestMethod] + [AllDevices] + public void Verify_GroupShared_WithFixedSize(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(128); + + device.Get().For(256, new FixedGroupSharedPixelShader(buffer)); + + int[] result = buffer.ToArray(); + + for (int i = 0; i < 128; i++) + { + Assert.AreEqual(result[i], i); + } + } + + [AutoConstructor] + internal readonly partial struct FixedGroupSharedPixelShader : IComputeShader + { + private readonly ReadWriteBuffer buffer; + + [GroupShared(128)] + private static readonly int[] cache; + + public void Execute() + { + int index = ThreadIds.X / 2; + bool isWritingToGroupShared = ThreadIds.X % 2 == 0; + + if (isWritingToGroupShared) + { + cache[index] = index; + } + + Hlsl.GroupMemoryBarrier(); + + if (!isWritingToGroupShared) + { + buffer[index] = cache[index]; + } + } + } + + [CombinatorialTestMethod] + [AllDevices] + public void Verify_GroupShared_WithDynamicSize(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(32); + + device.Get().For(64, 1, 1, 32, 1, 1, new DynamicGroupSharedPixelShader(buffer)); + + int[] result = buffer.ToArray(); + + for (int i = 0; i < 32; i++) + { + Assert.AreEqual(result[i], i); + } + } + + [AutoConstructor] + internal readonly partial struct DynamicGroupSharedPixelShader : IComputeShader + { + private readonly ReadWriteBuffer buffer; + + [GroupShared] + private static readonly int[] cache; + + public void Execute() + { + int index = ThreadIds.X / 2; + bool isWritingToGroupShared = ThreadIds.X % 2 == 0; + + if (isWritingToGroupShared) + { + cache[index] = index; + } + + Hlsl.GroupMemoryBarrier(); + + if (!isWritingToGroupShared) + { + buffer[index] = cache[index]; + } + } + } }