diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index af5faf55..6344b7fc 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -195,17 +195,32 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter { ParameterSyntax updatedNode = (ParameterSyntax)base.VisitParameter(node)!; + // By default, parameters just have no modifier + SyntaxToken modifier = Token(SyntaxKind.None); + // Convert the C# parameter modifiers to the HLSL equivalent - SyntaxToken modifier = - node.Modifiers - .Where(static m => m.Kind() is SyntaxKind.OutKeyword or SyntaxKind.RefKeyword or SyntaxKind.InKeyword) - .Select(static m => m.Kind() switch + for (int i = 0; i < node.Modifiers.Count; i++) + { + SyntaxToken currentModifier = node.Modifiers[i]; + + // We're only looking for in, ref, readonly, and out. The readonly modifier + // needs special handling, as it only makes sense right after a ref modifier. + switch (currentModifier.Kind()) { - SyntaxKind.OutKeyword => m, - SyntaxKind.InKeyword => m, - SyntaxKind.RefKeyword => ParseToken("inout"), - _ => Token(SyntaxKind.None) - }).FirstOrDefault(); + case SyntaxKind.InKeyword: + case SyntaxKind.OutKeyword: + modifier = currentModifier; + goto ExitLoop; + case SyntaxKind.RefKeyword when node.Modifiers.Count >= 2 && i < node.Modifiers.Count - 1 && node.Modifiers[i + 1].IsKind(SyntaxKind.ReadOnlyKeyword): + modifier = Token(SyntaxKind.InKeyword); + goto ExitLoop; + case SyntaxKind.RefKeyword: + modifier = ParseToken("inout"); + goto ExitLoop; + } + } + + ExitLoop: return updatedNode .WithAttributeLists(default) diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 71e22aeb..39b68c80 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -516,12 +516,12 @@ namespace ComputeSharp.Tests } [TestMethod] - public void PixelShaderWithScopedParameterInMethods() + public void ComputeShaderWithScopedParameterInMethods() { - _ = ReflectionServices.GetShaderInfo(); + _ = ReflectionServices.GetShaderInfo(); } - internal static class HelpersForPixelShaderWithScopedParameterInMethods + internal static class HelpersForComputeShaderWithScopedParameterInMethods { public static void Baz(scoped in float a, scoped ref float b) { @@ -532,7 +532,7 @@ namespace ComputeSharp.Tests [AutoConstructor] [ThreadGroupSize(DefaultThreadGroupSizes.X)] [GeneratedComputeShaderDescriptor] - internal readonly partial struct PixelShaderWithScopedParameterInMethodsShader : IComputeShader + internal readonly partial struct ComputeShaderWithScopedParameterInMethodsShader : IComputeShader { public readonly ReadWriteBuffer buffer; public readonly float number; @@ -554,7 +554,7 @@ namespace ComputeSharp.Tests Foo(ref this.buffer[ThreadIds.X], ref x); Bar(ref this.buffer[ThreadIds.X], ref x); - HelpersForPixelShaderWithScopedParameterInMethods.Baz(in this.buffer[ThreadIds.X], ref x); + HelpersForComputeShaderWithScopedParameterInMethods.Baz(in this.buffer[ThreadIds.X], ref x); this.buffer[ThreadIds.X] *= x; } @@ -611,7 +611,7 @@ namespace ComputeSharp.Tests [TestMethod] public void GloballyCoherentBuffers() { - ShaderInfo shaderInfo = ReflectionServices.GetShaderInfo(); + ShaderInfo info = ReflectionServices.GetShaderInfo(); Assert.AreEqual( """ @@ -643,7 +643,7 @@ namespace ComputeSharp.Tests } } """, - shaderInfo.HlslSource); + info.HlslSource); } [AutoConstructor] @@ -659,6 +659,134 @@ namespace ComputeSharp.Tests Hlsl.InterlockedAdd(ref this.buffer[0], 1); } } + + [TestMethod] + public void ComputeShaderWithRefReadonlyParameterInMethods() + { + _ = ReflectionServices.GetShaderInfo(); + } + + internal static class HelpersForCommputeShaderWithRefReadonlyParameterInMethods + { + public static float Baz(ref readonly float a, scoped ref readonly float b) + { + return a + b; + } + } + + [AutoConstructor] + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct ComputeShaderWithRefReadonlyParameterInMethodsShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly float number; + + private static float Foo(ref readonly float a, scoped ref readonly float b) + { + return a + b; + } + + private float Bar(ref readonly float a, scoped ref readonly float b) + { + return a + b; + } + + /// + public void Execute() + { + float x = this.number + ThreadIds.X; + + x += Foo(ref x, in x); + x += Foo(in this.number, in this.number); + x += Bar(ref x, in x); + x += HelpersForCommputeShaderWithRefReadonlyParameterInMethods.Baz(in this.buffer[ThreadIds.X], ref x); + + this.buffer[ThreadIds.X] = x; + } + } + + [TestMethod] + public void AllRefTypesShader_RewritesRefParametersCorrectly() + { + ShaderInfo info = ReflectionServices.GetShaderInfo(); + + Assert.AreEqual( + """ + // ================================================ + // AUTO GENERATED + // ================================================ + // This shader was created by ComputeSharp. + // See: https://github.com/Sergio0694/ComputeSharp. + + #define __GroupSize__get_X 64 + #define __GroupSize__get_Y 1 + #define __GroupSize__get_Z 1 + + cbuffer _ : register(b0) + { + uint __x; + uint __y; + uint __z; + } + + RWStructuredBuffer __reserved__buffer : register(u0); + + static void Foo(in int a, in int b, inout int c, out int d); + + static void Bar(in int a, in int b, inout int c, out int d); + + static void Foo(in int a, in int b, inout int c, out int d) + { + d = 0; + } + + static void Bar(in int a, in int b, inout int c, out int d) + { + d = 0; + } + + [NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)] + void Execute(uint3 ThreadIds : SV_DispatchThreadID) + { + if (ThreadIds.x < __x && ThreadIds.y < __y && ThreadIds.z < __z) + { + } + } + """, + info.HlslSource); + } + + [AutoConstructor] + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct AllRefTypesShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public static void Foo( + in int a, + ref readonly int b, + ref int c, + out int d) + { + d = 0; + } + + public static void Bar( + scoped in int a, + scoped ref readonly int b, + scoped ref int c, + scoped out int d) + { + d = 0; + } + + /// + public void Execute() + { + } + } } }