Strip readonly modifiers from methods in shaders

このコミットが含まれているのは:
Sergio Pedri committed 2023-05-17 20:03:15 +02:00
1 親 0fefd20781
コミット a5bda924d8
3ファイル変更
+59 -11

検索条件に一致するファイルはありません

+14 -10
ファイルの表示
@@ -206,19 +206,23 @@ internal static class SyntaxNodeExtensions
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no accessibility modifiers.
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no invalid HLSL modifiers.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node just like <paramref name="node"/> but with no accessibility modifiers.</returns>
public static MethodDeclarationSyntax WithoutAccessibilityModifiers(this MethodDeclarationSyntax node)
/// <returns>A node just like <paramref name="node"/> but with no invalid HLSL modifiers.</returns>
/// <remarks>This method will only strip modifiers that are expected and allowed on HLSL methods.</remarks>
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)));
}
}
+1 -1
ファイルの表示
@@ -245,7 +245,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
return
((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!)
.WithBlockBody()
.WithoutAccessibilityModifiers()
.WithoutInvalidHlslModifiers()
.WithAttributeLists(List<AttributeListSyntax>());
}
+44
ファイルの表示
@@ -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<int> 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<int> buffer;
private readonly MyStructWithReadonlyMethod myStruct;
/// <inheritdoc/>
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;
}
}
}