diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index ecaf4fa1..0a817df6 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -441,4 +441,43 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // By default, constructors are not supported, so just return an empty value return CastExpression(targetType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); } + + /// + /// Visits a known named intrinsic invocation expression. + /// + /// The original input instance. + /// The updated instance with tweaked syntax. + /// The name of the intrinsic method being invoked. + /// The rewritten for the invocation expression, if valid. + /// Thrown if the named intrinsic is not recognized. + protected static SyntaxNode? VisitKnownNamedIntrinsicInvocationExpression( + InvocationExpressionSyntax node, + InvocationExpressionSyntax updatedNode, + string? intrinsicName) + { + // All named intrinsic methods start with a leading "__" prefix + if (intrinsicName.AsSpan() is not ['_', '_', .. ReadOnlySpan method]) + { + return null; + } + + // Handle and rewrite the current known named intrinsic. + // This path should only ever be reached for valid ones. + switch (method) + { + // Conditional select invocations are rewritten as follows: + // + // C#: Hlsl.ConditionalSelect(mask, left, right) + // HLSL: (mask ? left : right) + case "ConditionalSelect": + return + ParenthesizedExpression( + ConditionalExpression( + updatedNode.ArgumentList.Arguments[0].Expression, + updatedNode.ArgumentList.Arguments[1].Expression, + updatedNode.ArgumentList.Arguments[2].Expression)); + default: + throw new NotSupportedException($"""Unrecognized intrinsic "{intrinsicName}"."""); + } + } } \ 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 a1b85495..33e0dc62 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -517,6 +517,13 @@ internal sealed partial class ShaderSourceRewriter( // Allow specialized types to track the method invocation, if needed TrackKnownMethodInvocation(metadataName); + // Special case: handle known named intrinsics being invoked. + // These have special lowering to the right HLSL constructs. + if (VisitKnownNamedIntrinsicInvocationExpression(node, updatedNode, mapping) is SyntaxNode namedIntrinsic) + { + return namedIntrinsic; + } + return updatedNode.WithExpression(IdentifierName(mapping!)); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs index 346ac572..199e13ec 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs @@ -108,6 +108,12 @@ internal sealed partial class StaticFieldRewriter( mapping = HlslKnownMethods.GetMappedNameWithParameters(method.Name, method.Parameters.Select(static p => p.Type.Name)); } + // Handle named intrinsics (see ShaderSourceRewriter for more info) + if (VisitKnownNamedIntrinsicInvocationExpression(node, updatedNode, mapping) is SyntaxNode namedIntrinsic) + { + return namedIntrinsic; + } + return updatedNode.WithExpression(IdentifierName(mapping!)); } }