diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 04a9263a..a1b85495 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -383,10 +383,20 @@ internal sealed partial class ShaderSourceRewriter( return IdentifierName(constantName); } - // If the member access is a this. access, rewrite it to strip "this." + // Special handling for 'this.' expressions, which are not always allowed in HLSL if (node.Expression.IsKind(SyntaxKind.ThisExpression)) { - return updatedNode.Name; + // If the member access is a this. access, rewrite it to strip 'this.', but only if accessing instance + // fields from the shader type itself (as using 'this.' in that case is not allowed in HLSL, and also it's not + // really useful anyway given that those fields are readonly, so less likely to cause observable conflicts). + if (SymbolEqualityComparer.Default.Equals(fieldOperation.Field.ContainingType, this.shaderType)) + { + return updatedNode.Name; + } + + // For all other cases, we can keep it in, as HLSL does allow using it to access instance fields of custom + // struct types. This is allowed by both FXC and DXC, on all shader profiles, so it's always safe to do. + return updatedNode; } // Handle static fields, which can be either in the shader type itself, or in external types diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeAndMutuallyInvokingMethods.cs b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeAndMutuallyInvokingMethods.cs index 8a056311..420757c9 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeAndMutuallyInvokingMethods.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeAndMutuallyInvokingMethods.cs @@ -6,9 +6,9 @@ public struct ColorWrapperUsingExternalType { private float4 value; - public ColorWrapperUsingExternalType(float4 color) + public ColorWrapperUsingExternalType(float4 value) { - this.value = color; + this.value = value; } public readonly float4 Invert() diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeWithConstructorEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeWithConstructorEffect.cs index 7ef7edfb..fb3cc00a 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeWithConstructorEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/InvertWithUserTypeWithConstructorEffect.cs @@ -6,9 +6,9 @@ public struct ColorWrapper { private float4 value; - public ColorWrapper(float4 color) + public ColorWrapper(float4 value) { - this.value = color; + this.value = value; } public float4 Invert() diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 200498f4..7cdcdab3 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -1193,6 +1193,91 @@ namespace ComputeSharp.Tests return x * 4; } } + + // See https://github.com/Sergio0694/ComputeSharp/issues/726 + [TestMethod] + public void ShaderUsingThisExpressions_IsProcessedCorrectly() + { + 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 + + struct ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data; + + struct ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data + { + int value; + void SetValue(int value); + }; + + cbuffer _ : register(b0) + { + uint __x; + uint __y; + uint __z; + float alpha; + } + + RWStructuredBuffer __reserved__buffer : register(u0); + + void ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data::SetValue(int value) + { + this.value = value; + } + + [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) + { + ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data data = (ComputeSharp_Tests_ShaderCompilerTests_ShaderUsingThisExpressions_Data)0; + data.SetValue(3); + __reserved__buffer[ThreadIds.x] = float4(data.value, data.value, data.value, alpha); + } + } + """, + info.HlslSource); + } + + [AutoConstructor] + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct ShaderUsingThisExpressions : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly float alpha; + + private struct Data + { + public int value; + + public void SetValue(int value) + { + this.value = value; + } + } + + public void Execute() + { + Data data = default; + + data.SetValue(3); + + // The 'this.' expression for shader captured values should be stripped. + // The one for accessing struct instance members, however, should not be. + this.buffer[ThreadIds.X] = new float4(data.value, data.value, data.value, this.alpha); + } + } } }