Wire up support for known named intrinsics

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-27 18:21:36 +01:00
1 親 651232b906
コミット 29fee3b59d
3ファイル変更
+52

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

+39
ファイルの表示
@@ -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)));
}
/// <summary>
/// Visits a known named intrinsic invocation expression.
/// </summary>
/// <param name="node">The original input <see cref="BaseObjectCreationExpressionSyntax"/> instance.</param>
/// <param name="updatedNode">The updated <see cref="BaseObjectCreationExpressionSyntax"/> instance with tweaked syntax.</param>
/// <param name="intrinsicName">The name of the intrinsic method being invoked.</param>
/// <returns>The rewritten <see cref="SyntaxNode"/> for the invocation expression, if valid.</returns>
/// <exception cref="NotSupportedException">Thrown if the named intrinsic is not recognized.</exception>
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<char> 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}".""");
}
}
}
+7
ファイルの表示
@@ -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!));
}
+6
ファイルの表示
@@ -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!));
}
}