From f1e0560053ec1805cc5c1de77bafc22eac445aac Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 19:19:03 +0100 Subject: [PATCH] Support 'this.' expressions in shader custom types --- .../SyntaxRewriters/ShaderSourceRewriter.cs | 14 ++++++++++++-- ...InvertWithUserTypeAndMutuallyInvokingMethods.cs | 4 ++-- .../InvertWithUserTypeWithConstructorEffect.cs | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) 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()