Add generator support for struct instance methods
このコミットが含まれているのは:
1 親
43286be416
コミット
bddd2a395c
4ファイル変更
+75
-11
検索条件に一致するファイルはありません
+15
-3
@@ -52,6 +52,7 @@ partial class ID2D1ShaderGenerator
|
||||
// We need to sets to track all discovered custom types and static methods
|
||||
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Extract information on all captured fields
|
||||
@@ -64,11 +65,11 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var semanticModelProvider = new SemanticModelProvider(compilation);
|
||||
var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, out bool methodsNeedD2D1RequiresScenePosition);
|
||||
var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, out bool methodsNeedD2D1RequiresScenePosition);
|
||||
var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions, out bool fieldsNeedD2D1RequiresScenePosition);
|
||||
|
||||
// Process the discovered types and constants
|
||||
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes);
|
||||
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods);
|
||||
var definedConstants = GetDefinedConstants(constantDefinitions);
|
||||
|
||||
// Check whether the scene position is required
|
||||
@@ -258,6 +259,7 @@ partial class ID2D1ShaderGenerator
|
||||
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the type to process.</param>
|
||||
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <param name="needsD2D1RequiresScenePosition">Whether or not the shader needs the <c>[D2DRequiresScenePosition]</c> annotation.</param>
|
||||
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
|
||||
@@ -268,6 +270,7 @@ partial class ID2D1ShaderGenerator
|
||||
SemanticModelProvider semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions,
|
||||
out bool needsD2D1RequiresScenePosition)
|
||||
{
|
||||
@@ -303,6 +306,7 @@ partial class ID2D1ShaderGenerator
|
||||
semanticModel,
|
||||
discoveredTypes,
|
||||
staticMethods,
|
||||
instanceMethods,
|
||||
constantDefinitions,
|
||||
diagnostics,
|
||||
isShaderEntryPoint);
|
||||
@@ -374,11 +378,13 @@ partial class ID2D1ShaderGenerator
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
|
||||
/// <param name="types">The sequence of discovered custom types.</param>
|
||||
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
|
||||
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
|
||||
private static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(
|
||||
ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
IEnumerable<INamedTypeSymbol> types)
|
||||
IEnumerable<INamedTypeSymbol> types,
|
||||
IReadOnlyDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods)
|
||||
{
|
||||
ImmutableArray<(string, string)>.Builder builder = ImmutableArray.CreateBuilder<(string, string)>();
|
||||
IReadOnlyCollection<INamedTypeSymbol> invalidTypes;
|
||||
@@ -414,6 +420,12 @@ partial class ID2D1ShaderGenerator
|
||||
VariableDeclarator(Identifier(mappedName!)))));
|
||||
}
|
||||
|
||||
// Declare the methods of the current type
|
||||
foreach (var method in instanceMethods.Where(pair => SymbolEqualityComparer.Default.Equals(pair.Key.ContainingType, type)))
|
||||
{
|
||||
structDeclaration = structDeclaration.AddMembers(method.Value);
|
||||
}
|
||||
|
||||
// Insert the trailing ; right after the closing bracket (after normalization)
|
||||
builder.Add((
|
||||
structType,
|
||||
|
||||
@@ -31,6 +31,11 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
/// </summary>
|
||||
private readonly IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of discovered instance methods for custom struct types.
|
||||
/// </summary>
|
||||
private readonly IDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of processed local functions in the current tree.
|
||||
/// </summary>
|
||||
@@ -63,6 +68,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the target syntax tree.</param>
|
||||
/// <param name="discoveredTypes">The set of discovered custom types.</param>
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
/// <param name="isEntryPoint">Whether or not the current instance is processing a shader entry point.</param>
|
||||
@@ -71,6 +77,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
SemanticModelProvider semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions,
|
||||
ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
bool isEntryPoint)
|
||||
@@ -78,6 +85,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
{
|
||||
this.shaderType = shaderType;
|
||||
this.staticMethods = staticMethods;
|
||||
this.instanceMethods = instanceMethods;
|
||||
this.localFunctions = new();
|
||||
this.implicitVariables = new();
|
||||
this.isEntryPoint = isEntryPoint;
|
||||
@@ -89,17 +97,20 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the target syntax tree.</param>
|
||||
/// <param name="discoveredTypes">The set of discovered custom types.</param>
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
public ShaderSourceRewriter(
|
||||
SemanticModelProvider semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions,
|
||||
ImmutableArray<DiagnosticInfo>.Builder diagnostics)
|
||||
: base(semanticModel, discoveredTypes, constantDefinitions, diagnostics)
|
||||
{
|
||||
this.staticMethods = staticMethods;
|
||||
this.instanceMethods = instanceMethods;
|
||||
this.implicitVariables = new();
|
||||
this.localFunctions = new();
|
||||
}
|
||||
@@ -459,7 +470,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
return updatedNode;
|
||||
}
|
||||
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, ConstantDefinitions, Diagnostics);
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.staticMethods, this.instanceMethods, ConstantDefinitions, Diagnostics);
|
||||
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
|
||||
|
||||
this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
|
||||
@@ -472,6 +483,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
{
|
||||
string metadataName = method.GetFullMetadataName(includeParameters: true);
|
||||
|
||||
// Check whether this is a Sample(...) call on a normalized texture type
|
||||
if (HlslKnownMethods.TryGetMappedResourceSamplerAccessType(metadataName, out string? mapping))
|
||||
{
|
||||
// Get the syntax for the argument syntax transformation (adding the vector type constructor if needed)
|
||||
@@ -484,6 +496,32 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter
|
||||
// Rewrite texture resource sampled accesses, if needed
|
||||
return RewriteSampledTextureAccess(operation, updatedNode.Expression, coordinateSyntax);
|
||||
}
|
||||
|
||||
// If the instance is a struct type that is available in source, rewrite the instance method
|
||||
if (operation.TargetMethod.ContainingType is { TypeKind: TypeKind.Struct } structTypeSymbol)
|
||||
{
|
||||
DiscoveredTypes.Add(structTypeSymbol);
|
||||
|
||||
var methodIdentifier = method.GetFullMetadataName().ToHlslIdentifierName();
|
||||
|
||||
// Same as for static methods, ensure the method is available in source, and process it if so
|
||||
if (!this.instanceMethods.ContainsKey(method))
|
||||
{
|
||||
if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode)
|
||||
{
|
||||
Diagnostics.Add(InvalidMethodCall, node, method);
|
||||
|
||||
return updatedNode;
|
||||
}
|
||||
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(SemanticModel, DiscoveredTypes, this.instanceMethods, this.instanceMethods, ConstantDefinitions, Diagnostics);
|
||||
MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace(eol: "\n").WithoutTrivia();
|
||||
|
||||
this.instanceMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
|
||||
}
|
||||
|
||||
return updatedNode.WithExpression(((MemberAccessExpressionSyntax)updatedNode.Expression).WithName(IdentifierName(methodIdentifier)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ partial class IShaderGenerator
|
||||
// We need to sets to track all discovered custom types and static methods
|
||||
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// A given type can only represent a single shader type
|
||||
@@ -64,12 +65,12 @@ partial class IShaderGenerator
|
||||
var (resourceFields, valueFields) = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes, isComputeShader);
|
||||
var delegateInstanceFields = GetDispatchId.GetInfo(structDeclarationSymbol);
|
||||
var sharedBuffers = GetSharedBuffers(diagnostics, structDeclarationSymbol, discoveredTypes);
|
||||
var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader);
|
||||
var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader);
|
||||
var implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?);
|
||||
var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
|
||||
|
||||
// Process the discovered types and constants
|
||||
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes);
|
||||
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods);
|
||||
var definedConstants = GetDefinedConstants(constantDefinitions);
|
||||
|
||||
// Get the HLSL source data with the intermediate info
|
||||
@@ -315,6 +316,7 @@ partial class IShaderGenerator
|
||||
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the type to process.</param>
|
||||
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <param name="isComputeShader">Indicates whether or not <paramref name="structDeclarationSymbol"/> represents a compute shader.</param>
|
||||
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
|
||||
@@ -325,6 +327,7 @@ partial class IShaderGenerator
|
||||
SemanticModelProvider semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions,
|
||||
bool isComputeShader)
|
||||
{
|
||||
@@ -365,6 +368,7 @@ partial class IShaderGenerator
|
||||
semanticModel,
|
||||
discoveredTypes,
|
||||
staticMethods,
|
||||
instanceMethods,
|
||||
constantDefinitions,
|
||||
diagnostics,
|
||||
isShaderEntryPoint);
|
||||
@@ -436,14 +440,16 @@ partial class IShaderGenerator
|
||||
/// <summary>
|
||||
/// Gets the sequence of processed discovered custom types.
|
||||
/// </summary>
|
||||
/// <param name="types">The sequence of discovered custom types.</param>
|
||||
/// <param name="sourceSymbol">The symbol for the current object being processed.</param>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
|
||||
/// <param name="sourceSymbol">The symbol for the current object being processed.</param>
|
||||
/// <param name="types">The sequence of discovered custom types.</param>
|
||||
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
|
||||
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
|
||||
internal static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes(
|
||||
ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
ISymbol sourceSymbol,
|
||||
IEnumerable<INamedTypeSymbol> types)
|
||||
IEnumerable<INamedTypeSymbol> types,
|
||||
IReadOnlyDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods)
|
||||
{
|
||||
ImmutableArray<(string, string)>.Builder builder = ImmutableArray.CreateBuilder<(string, string)>();
|
||||
IReadOnlyCollection<INamedTypeSymbol> invalidTypes;
|
||||
@@ -479,6 +485,12 @@ partial class IShaderGenerator
|
||||
VariableDeclarator(Identifier(mappedName!)))));
|
||||
}
|
||||
|
||||
// Declare the methods of the current type
|
||||
foreach (var method in instanceMethods.Where(pair => SymbolEqualityComparer.Default.Equals(pair.Key.ContainingType, type)))
|
||||
{
|
||||
structDeclaration = structDeclaration.AddMembers(method.Value);
|
||||
}
|
||||
|
||||
// Insert the trailing ; right after the closing bracket (after normalization)
|
||||
builder.Add((
|
||||
structType,
|
||||
|
||||
@@ -84,11 +84,12 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
|
||||
// We need to sets to track all discovered custom types and static methods
|
||||
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods = new(SymbolEqualityComparer.Default); // Unused in this generator
|
||||
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var semanticModel = new SemanticModelProvider(compilation);
|
||||
var (entryPoint, dependentMethods) = GetProcessedMethods(builder, methodDeclaration, semanticModel, discoveredTypes, constantDefinitions);
|
||||
var definedTypes = IShaderGenerator.BuildHlslSource.GetDeclaredTypes(builder, methodSymbol, discoveredTypes);
|
||||
var definedTypes = IShaderGenerator.BuildHlslSource.GetDeclaredTypes(builder, methodSymbol, discoveredTypes, instanceMethods);
|
||||
var definedConstants = IShaderGenerator.BuildHlslSource.GetDefinedConstants(constantDefinitions);
|
||||
|
||||
diagnostics = builder.ToImmutable();
|
||||
@@ -118,8 +119,9 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions)
|
||||
{
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods = new(SymbolEqualityComparer.Default);
|
||||
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, diagnostics);
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, diagnostics);
|
||||
|
||||
// Process the possible syntax nodes
|
||||
SyntaxNode visitedMethod = methodDeclaration switch
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする