From 0fefd207816d94c71ac81ac2ce1c5247c1b0c5de Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 May 2023 16:01:15 +0200 Subject: [PATCH 1/2] Add missing readonly modifiers --- .../Primitives/ComplexVector4.cs | 2 +- ...1ImmutableArrayResourceTextureDescriptionsLoader.cs | 2 +- ...2D1IndexBitmaskResourceTextureDescriptionsLoader.cs | 2 +- .../Interop/Effects/PixelShaderEffect.Properties.cs | 2 +- .../Loaders/D2D1BufferSizeDispatchDataLoader.cs | 2 +- .../Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs | 2 +- .../Loaders/D2D1ByteArrayInputDescriptionsLoader.cs | 2 +- .../D2D1ByteArrayResourceTextureDescriptionsLoader.cs | 2 +- .../Loaders/D2D1ByteBufferDispatchDataLoader.cs | 2 +- .../Shaders/Loaders/D2D1ShaderBytecodeLoader.cs | 2 +- src/ComputeSharp.SourceGeneration/Helpers/HashCode.cs | 2 +- src/ComputeSharp/Graphics/Commands/CommandList.cs | 2 +- .../Interop/ID3D12DescriptorHandleAllocator.cs | 2 +- src/ComputeSharp/Shaders/ComputeContext.cs | 2 +- .../Shaders/Dispatching/ShaderBytecodeLoader.cs | 2 +- .../Dispatching/__Internals/ArrayPoolStringBuilder.cs | 2 +- .../DirectX/other/d3dx12/D3D12_BOX.Manual.cs | 6 +++--- .../other/d3dx12/D3D12_CPU_DESCRIPTOR_HANDLE.Manual.cs | 6 +++--- .../other/d3dx12/D3D12_GPU_DESCRIPTOR_HANDLE.Manual.cs | 6 +++--- .../DirectX/other/d3dx12/D3D12_RESOURCE_DESC.Manual.cs | 10 +++++----- tests/Directory.Build.props | 3 +++ 21 files changed, 33 insertions(+), 30 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs index a7effc7b..d8dabc18 100644 --- a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs +++ b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs @@ -30,5 +30,5 @@ internal struct ComplexVector4 /// The 'b' parameter, for the imaginary component /// The resulting value [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 WeightedSum(float a, float b) => (this.Real * a) + (this.Imaginary * b); + public readonly Vector4 WeightedSum(float a, float b) => (this.Real * a) + (this.Imaginary * b); } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.UI/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs b/src/ComputeSharp.D2D1.UI/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs index 23b2a081..86ba37e6 100644 --- a/src/ComputeSharp.D2D1.UI/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs +++ b/src/ComputeSharp.D2D1.UI/Loaders/D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs @@ -24,7 +24,7 @@ internal struct D2D1ImmutableArrayResourceTextureDescriptionsLoader : ID2D1Resou /// /// An array of indices for resource textures for the current effect. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ImmutableArray GetResultingIndices() + public readonly ImmutableArray GetResultingIndices() { return this.indices; } diff --git a/src/ComputeSharp.D2D1.UI/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs b/src/ComputeSharp.D2D1.UI/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs index 3ceecfe4..354f6980 100644 --- a/src/ComputeSharp.D2D1.UI/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs +++ b/src/ComputeSharp.D2D1.UI/Loaders/D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs @@ -23,7 +23,7 @@ internal struct D2D1IndexBitmaskResourceTextureDescriptionsLoader : ID2D1Resourc /// /// A bitmask of indices for resource textures in the target shader. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int GetResultingIndexBitmask() + public readonly int GetResultingIndexBitmask() { return this.indexBitmask; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 34278eb8..335c4d19 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -404,7 +404,7 @@ unsafe partial struct PixelShaderEffect /// The index of the resource texture to validate. /// The number of dimensions for the resource texture at the gven index. /// Whether or not is valid for the current effect. - private bool IsResourceTextureManagerIndexValid(int resourceTextureIndex, out uint dimensions) + private readonly bool IsResourceTextureManagerIndexValid(int resourceTextureIndex, out uint dimensions) { foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount)) { diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs index d24559f6..24d38972 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1BufferSizeDispatchDataLoader.cs @@ -22,7 +22,7 @@ internal unsafe struct D2D1BufferSizeDispatchDataLoader : ID2D1DispatchDataLoade /// /// The size of the constant buffer. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int GetConstantBufferSize() + private readonly int GetConstantBufferSize() { return this.size; } diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs index f0baa491..b3cc46f4 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayDispatchDataLoader.cs @@ -22,7 +22,7 @@ internal struct D2D1ByteArrayDispatchDataLoader : ID2D1DispatchDataLoader /// /// A array with the constant buffer for the current shader. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public byte[] GetResultingDispatchData() + public readonly byte[] GetResultingDispatchData() { return this.data!; } diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs index bff3bb03..88c714f7 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayInputDescriptionsLoader.cs @@ -23,7 +23,7 @@ internal struct D2D1ByteArrayInputDescriptionsLoader : ID2D1InputDescriptionsLoa /// /// A array with the available input descriptions. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public D2D1InputDescription[] GetResultingInputDescriptions() + public readonly D2D1InputDescription[] GetResultingInputDescriptions() { return this.inputDescriptions!; } diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayResourceTextureDescriptionsLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayResourceTextureDescriptionsLoader.cs index 99574e9a..76f75c4a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayResourceTextureDescriptionsLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteArrayResourceTextureDescriptionsLoader.cs @@ -23,7 +23,7 @@ internal struct D2D1ByteArrayResourceTextureDescriptionsLoader : ID2D1ResourceTe /// /// A array with the available resource texture descriptions. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public D2D1ResourceTextureDescription[] GetResultingResourceTextureDescriptions() + public readonly D2D1ResourceTextureDescription[] GetResultingResourceTextureDescriptions() { return this.resourceTextureDescriptions!; } diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs index 379e16c5..087ac7da 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs @@ -46,7 +46,7 @@ internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoade /// The number of written bytes, of 0 in case of failure. /// Whether or not the constant buffer was retrieved successfully. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool TryGetWrittenBytes(out int writtenBytes) + public readonly bool TryGetWrittenBytes(out int writtenBytes) { // If the -1 marker is present, it means copying to the target span failed. // In this case, the returned number of written bytes is still reported as 0. diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs index 108610f1..26592bff 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ShaderBytecodeLoader.cs @@ -37,7 +37,7 @@ internal unsafe struct D2D1ShaderBytecodeLoader : ID2D1BytecodeLoader /// An with the shader bytecode, if dynamically compiled. /// Either is empty, or the returned pointer is . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ComPtr GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode) + public readonly ComPtr GetResultingShaderBytecode(out ReadOnlySpan precompiledBytecode) { precompiledBytecode = new ReadOnlySpan(this.embeddedBytecodePtr, this.embeddedBytecodeSize); diff --git a/src/ComputeSharp.SourceGeneration/Helpers/HashCode.cs b/src/ComputeSharp.SourceGeneration/Helpers/HashCode.cs index bd2a4c6f..1bb78a55 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/HashCode.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/HashCode.cs @@ -439,7 +439,7 @@ internal struct HashCode /// Gets the resulting hashcode from the current instance. /// /// The resulting hashcode from the current instance. - public int ToHashCode() + public readonly int ToHashCode() { uint length = this.length; uint position = length % 4; diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs index ade72ef0..2544d0c1 100644 --- a/src/ComputeSharp/Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs @@ -89,7 +89,7 @@ internal unsafe struct CommandList : IDisposable /// /// Gets the instance associated with the current command list. /// - public GraphicsDevice GraphicsDevice => this.device; + public readonly GraphicsDevice GraphicsDevice => this.device; /// /// Gets the command list type being used by the current instance. diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index 11a4e2cf..00c94b3f 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -83,7 +83,7 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// /// Gets the in use for the current instance. /// - public ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap; + public readonly ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap; /// /// Rents a new bundle of CPU and GPU handles to use in a resource. diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index b7be311f..a2b58cf8 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -55,7 +55,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// /// Gets the associated with the current instance. /// - public GraphicsDevice GraphicsDevice + public readonly GraphicsDevice GraphicsDevice { get { diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs index 2971e844..345473de 100644 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs @@ -24,7 +24,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader /// The current instance. /// Thrown if the shader has not been initialized. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ICachedShader GetCachedShader() + public readonly ICachedShader GetCachedShader() { default(InvalidOperationException).ThrowIf(this.cachedShader is null); diff --git a/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs b/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs index 7f2bc9f2..cb503640 100644 --- a/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs +++ b/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs @@ -136,7 +136,7 @@ public ref struct ArrayPoolStringBuilder } /// - public void Dispose() + public readonly void Dispose() { ArrayPool.Shared.Return(this.array); } diff --git a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_BOX.Manual.cs b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_BOX.Manual.cs index b22083a5..8df7faeb 100644 --- a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_BOX.Manual.cs +++ b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_BOX.Manual.cs @@ -49,9 +49,9 @@ internal partial struct D3D12_BOX : IEquatable return !(l == r); } - public override bool Equals(object? obj) => (obj is D3D12_BOX other) && Equals(other); + public override readonly bool Equals(object? obj) => (obj is D3D12_BOX other) && Equals(other); - public bool Equals(D3D12_BOX other) => this == other; + public readonly bool Equals(D3D12_BOX other) => this == other; - public override int GetHashCode() => HashCode.Combine(left, top, front, right, bottom, back); + public override readonly int GetHashCode() => HashCode.Combine(left, top, front, right, bottom, back); } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_CPU_DESCRIPTOR_HANDLE.Manual.cs b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_CPU_DESCRIPTOR_HANDLE.Manual.cs index 4d7ada80..ddf461a6 100644 --- a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_CPU_DESCRIPTOR_HANDLE.Manual.cs +++ b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_CPU_DESCRIPTOR_HANDLE.Manual.cs @@ -59,9 +59,9 @@ internal unsafe partial struct D3D12_CPU_DESCRIPTOR_HANDLE : IEquatable (obj is D3D12_CPU_DESCRIPTOR_HANDLE other) && Equals(other); + public override readonly bool Equals(object? obj) => (obj is D3D12_CPU_DESCRIPTOR_HANDLE other) && Equals(other); - public bool Equals(D3D12_CPU_DESCRIPTOR_HANDLE other) => this == other; + public readonly bool Equals(D3D12_CPU_DESCRIPTOR_HANDLE other) => this == other; - public override int GetHashCode() => ptr.GetHashCode(); + public override readonly int GetHashCode() => ptr.GetHashCode(); } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_GPU_DESCRIPTOR_HANDLE.Manual.cs b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_GPU_DESCRIPTOR_HANDLE.Manual.cs index e28da39d..c6f00196 100644 --- a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_GPU_DESCRIPTOR_HANDLE.Manual.cs +++ b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_GPU_DESCRIPTOR_HANDLE.Manual.cs @@ -59,9 +59,9 @@ internal unsafe partial struct D3D12_GPU_DESCRIPTOR_HANDLE : IEquatable (obj is D3D12_GPU_DESCRIPTOR_HANDLE other) && Equals(other); + public override readonly bool Equals(object? obj) => (obj is D3D12_GPU_DESCRIPTOR_HANDLE other) && Equals(other); - public bool Equals(D3D12_GPU_DESCRIPTOR_HANDLE other) => this == other; + public readonly bool Equals(D3D12_GPU_DESCRIPTOR_HANDLE other) => this == other; - public override int GetHashCode() => ptr.GetHashCode(); + public override readonly int GetHashCode() => ptr.GetHashCode(); } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_RESOURCE_DESC.Manual.cs b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_RESOURCE_DESC.Manual.cs index a8c36b0a..7e7f2e5a 100644 --- a/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_RESOURCE_DESC.Manual.cs +++ b/src/TerraFX.Interop.Windows/DirectX/other/d3dx12/D3D12_RESOURCE_DESC.Manual.cs @@ -53,9 +53,9 @@ internal unsafe partial struct D3D12_RESOURCE_DESC : IEquatable ((Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D) ? DepthOrArraySize : (ushort)(1)); + public readonly ushort Depth => ((Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D) ? DepthOrArraySize : (ushort)(1)); - public ushort ArraySize => ((Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) ? DepthOrArraySize : (ushort)(1)); + public readonly ushort ArraySize => ((Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) ? DepthOrArraySize : (ushort)(1)); public static bool operator ==([NativeTypeName("const D3D12_RESOURCE_DESC &")] in D3D12_RESOURCE_DESC l, [NativeTypeName("const D3D12_RESOURCE_DESC &")] in D3D12_RESOURCE_DESC r) { @@ -75,11 +75,11 @@ internal unsafe partial struct D3D12_RESOURCE_DESC : IEquatable !(l == r); - public override bool Equals(object? obj) => (obj is D3D12_RESOURCE_DESC other) && Equals(other); + public override readonly bool Equals(object? obj) => (obj is D3D12_RESOURCE_DESC other) && Equals(other); - public bool Equals(D3D12_RESOURCE_DESC other) => this == other; + public readonly bool Equals(D3D12_RESOURCE_DESC other) => this == other; - public override int GetHashCode() + public override readonly int GetHashCode() { var hashCode = new HashCode(); { diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props index 7e38b3c1..7997b1b0 100644 --- a/tests/Directory.Build.props +++ b/tests/Directory.Build.props @@ -20,5 +20,8 @@ $(NoWarn);IDE0240 + + + $(NoWarn);IDE0251 \ No newline at end of file From a5bda924d8e90aa9e41ef62d3d08ed0d4b355a84 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 May 2023 20:03:15 +0200 Subject: [PATCH 2/2] Strip readonly modifiers from methods in shaders --- .../Extensions/SyntaxNodeExtensions.cs | 24 +++++----- .../SyntaxRewriters/ShaderSourceRewriter.cs | 2 +- .../ComputeSharp.Tests/ShaderRewriterTests.cs | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) 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