Update HLSL rewriters to use new Sample APIs

このコミットが含まれているのは:
Sergio Pedri committed 2022-08-09 17:37:55 +02:00
1 親 0dd0bd587a
コミット 945f6514b8
6ファイル変更
+111 -136

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

-15
ファイルの表示
@@ -251,12 +251,6 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
return updatedNode.WithArgumentList(BracketedArgumentList(SingletonSeparatedList(Argument(index))));
}
// Rewrite texture resource sampled accesses, if needed
if (HlslKnownProperties.TryGetMappedResourceSamplerAccessType(propertyName, out mapping))
{
return RewriteSampledTextureAccess(operation, updatedNode, mapping);
}
// If the current property is a swizzled matrix indexer, ensure all the arguments are constants, and rewrite
// the property access to the corresponding HLSL syntax. For instance, m[M11, M12] will become m._m00_m01.
if (HlslKnownProperties.IsKnownMatrixIndexer(propertyName))
@@ -331,13 +325,4 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
return updatedToken.WithoutTrivia();
}
/// <summary>
/// Rewrites a sampled texture access.
/// </summary>
/// <param name="operation">The <see cref="IPropertyReferenceOperation"/> instance for the sampled texture access.</param>
/// <param name="node">The input <see cref="ElementAccessExpressionSyntax"/> instance for the node to rewrite.</param>
/// <param name="mappedType">The mapped type name to use for the sampled access, if any.</param>
/// <returns>A <see cref="SyntaxNode"/> representing the rewritten sampled texture access.</returns>
private partial SyntaxNode RewriteSampledTextureAccess(IPropertyReferenceOperation operation, ElementAccessExpressionSyntax node, string? mappedType);
}
+59 -42
ファイルの表示
@@ -344,56 +344,62 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
{
var updatedNode = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!;
if (SemanticModel.For(node).GetOperation(node) is IInvocationOperation operation &&
operation.TargetMethod is IMethodSymbol method &&
method.IsStatic)
if (SemanticModel.For(node).GetOperation(node) is IInvocationOperation { TargetMethod: IMethodSymbol method } operation)
{
string metadataName = method.GetFullMetadataName();
// If the invocation consists of invoking a static method that has a direct
// mapping to HLSL, rewrite the expression in the current invocation node.
// For instance: Math.Abs(expr) => abs(expr).
if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping))
if (method.IsStatic)
{
// Allow specialized types to track the method invocation, if needed
TrackKnownMethodInvocation(metadataName);
return updatedNode.WithExpression(ParseExpression(mapping!));
}
// Update the name if the target is a local function. The exact schema for the
// updated name is detailed in the override handling the local function statement.
if (method.MethodKind == MethodKind.LocalFunction)
{
var functionIdentifier = $"__{this.currentMethod!.Identifier.Text}__{method.Name}";
return updatedNode.WithExpression(ParseExpression(functionIdentifier));
}
// If the method is an external static method, import and rewrite it as well.
// This assumes that the target method is actually part of the same compilation.
// We need to check the declaring type to avoid rewriting static methods in the shader
// type as well, as they will be processed by the generator in a different path.
if (!SymbolEqualityComparer.Default.Equals(this.shaderType, method.ContainingType))
{
var methodIdentifier = method.GetFullMetadataName().ToHlslIdentifierName();
if (!this.staticMethods.ContainsKey(method))
// If the invocation consists of invoking a static method that has a direct
// mapping to HLSL, rewrite the expression in the current invocation node.
// For instance: Math.Abs(expr) => abs(expr).
if (HlslKnownMethods.TryGetMappedName(metadataName, out string? mapping))
{
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
{
Diagnostics.Add(InvalidMethodCall, node, method);
// Allow specialized types to track the method invocation, if needed
TrackKnownMethodInvocation(metadataName);
return updatedNode;
}
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
return updatedNode.WithExpression(ParseExpression(mapping!));
}
return updatedNode.WithExpression(ParseExpression(methodIdentifier));
// Update the name if the target is a local function. The exact schema for the
// updated name is detailed in the override handling the local function statement.
if (method.MethodKind == MethodKind.LocalFunction)
{
var functionIdentifier = $"__{this.currentMethod!.Identifier.Text}__{method.Name}";
return updatedNode.WithExpression(ParseExpression(functionIdentifier));
}
// If the method is an external static method, import and rewrite it as well.
// This assumes that the target method is actually part of the same compilation.
// We need to check the declaring type to avoid rewriting static methods in the shader
// type as well, as they will be processed by the generator in a different path.
if (!SymbolEqualityComparer.Default.Equals(this.shaderType, method.ContainingType))
{
var methodIdentifier = method.GetFullMetadataName().ToHlslIdentifierName();
if (!this.staticMethods.ContainsKey(method))
{
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
{
Diagnostics.Add(InvalidMethodCall, node, method);
return updatedNode;
}
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
}
return updatedNode.WithExpression(ParseExpression(methodIdentifier));
}
}
else if (HlslKnownMethods.TryGetMappedResourceSamplerAccessType(metadataName, out string? mapping))
{
// Rewrite texture resource sampled accesses, if needed
return RewriteSampledTextureAccess(operation, updatedNode, mapping);
}
}
@@ -423,6 +429,17 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
return updatedNode;
}
/// <summary>
/// Rewrites a sampled texture access.
/// </summary>
/// <param name="operation">The <see cref="IInvocationOperation"/> instance for the sampled texture access.</param>
/// <param name="node">The input <see cref="InvocationExpressionSyntax"/> instance for the node to rewrite.</param>
/// <param name="mappedType">The mapped type name to use for the sampled access, if any.</param>
/// <returns>A <see cref="SyntaxNode"/> representing the rewritten sampled texture access.</returns>
private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, InvocationExpressionSyntax node, string? mappedType);
/// <summary>
/// Tracks a property access to a known HLSL property.
/// </summary>