diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs
index b038e7b9..7baa3856 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/Extensions/SyntaxNodeExtensions.cs
@@ -206,19 +206,23 @@ internal static class SyntaxNodeExtensions
}
///
- /// Returns a instance with no accessibility modifiers.
+ /// Returns a instance with no invalid HLSL modifiers.
///
/// The input node.
- /// A node just like but with no accessibility modifiers.
- public static MethodDeclarationSyntax WithoutAccessibilityModifiers(this MethodDeclarationSyntax node)
+ /// A node just like but with no invalid HLSL modifiers.
+ /// This method will only strip modifiers that are expected and allowed on HLSL methods.
+ public static MethodDeclarationSyntax WithoutInvalidHlslModifiers(this MethodDeclarationSyntax node)
{
- return node.WithModifiers(TokenList(node.Modifiers.Where(static modifier => modifier.Kind() switch
+ static bool IsAllowedHlslModifier(SyntaxToken syntaxToken)
{
- SyntaxKind.PublicKeyword => false,
- SyntaxKind.PrivateKeyword => false,
- SyntaxKind.ProtectedKeyword => false,
- SyntaxKind.InternalKeyword => false,
- _ => true
- }).ToArray()));
+ return syntaxToken.Kind() is not (
+ SyntaxKind.PublicKeyword or
+ SyntaxKind.PrivateKeyword or
+ SyntaxKind.ProtectedKeyword or
+ SyntaxKind.InternalKeyword or
+ SyntaxKind.ReadOnlyKeyword);
+ }
+
+ return node.WithModifiers(TokenList(node.Modifiers.Where(IsAllowedHlslModifier)));
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs
index ee9544a2..100ea479 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -245,7 +245,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
return
((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!)
.WithBlockBody()
- .WithoutAccessibilityModifiers()
+ .WithoutInvalidHlslModifiers()
.WithAttributeLists(List());
}
diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs
index 0363872b..1037d14e 100644
--- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs
+++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs
@@ -823,4 +823,48 @@ public partial class ShaderRewriterTests
buffer2[11] = r2.Z;
}
}
+
+ // See https://github.com/Sergio0694/ComputeSharp/issues/525
+ [CombinatorialTestMethod]
+ [AllDevices]
+ public void ReadonlyModifierInMethods(Device device)
+ {
+ int[] data = { 0 };
+
+ using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(data);
+
+ device.Get().For(1, new ReadonlyModifierInMethodsShader(buffer, new MyStructWithReadonlyMethod { Number = 40 }));
+
+ int[] results = buffer.ToArray();
+
+ CollectionAssert.AreEqual(expected: new[] { 42 }, actual: results);
+ }
+
+ [AutoConstructor]
+ internal readonly partial struct ReadonlyModifierInMethodsShader : IComputeShader
+ {
+ private readonly ReadWriteBuffer buffer;
+ private readonly MyStructWithReadonlyMethod myStruct;
+
+ ///
+ public void Execute()
+ {
+ this.buffer[ThreadIds.X] = myStruct.GetNumber() + Foo();
+ }
+
+ private readonly int Foo()
+ {
+ return 2;
+ }
+ }
+
+ public struct MyStructWithReadonlyMethod
+ {
+ public int Number;
+
+ public readonly int GetNumber()
+ {
+ return this.Number;
+ }
+ }
}
\ No newline at end of file