using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Helpers; using ComputeSharp.SourceGenerators.Mappings; using ComputeSharp.SourceGenerators.SyntaxRewriters; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle; namespace ComputeSharp.SourceGenerators { /// public sealed partial class IShaderGenerator { /// private static partial MethodDeclarationSyntax CreateBuildHlslStringMethod( GeneratorExecutionContext context, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, out string? implicitTextureType, out bool isSamplerUsed) { // Properties are not supported DetectAndReportPropertyDeclarations(context, structDeclarationSymbol); // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); Dictionary staticMethods = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); // A given type can only represent a single shader type if (structDeclarationSymbol.AllInterfaces.Count(static interfaceSymbol => interfaceSymbol is { Name: nameof(IComputeShader) } or { IsGenericType: true, Name: nameof(IPixelShader) }) > 1) { context.ReportDiagnostic(MultipleShaderTypesImplemented, structDeclarationSymbol, structDeclarationSymbol); } // Explore the syntax tree and extract the processed info var semanticModel = new SemanticModelProvider(context.Compilation); var pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IPixelShader) }); var isComputeShader = pixelShaderSymbol is null; var pixelShaderTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!); var processedMembers = GetProcessedFields(context, structDeclarationSymbol, discoveredTypes, isComputeShader).ToArray(); var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray(); var (entryPoint, processedMethods, forwardDeclarations, accessesStaticSampler) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions, isComputeShader); var implicitSamplerField = accessesStaticSampler ? ("SamplerState", "__sampler") : default((string, string)?); var processedTypes = GetProcessedTypes(discoveredTypes).ToArray(); var processedConstants = GetProcessedConstants(constantDefinitions); var staticFields = GetStaticFields(context, semanticModel, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions); string namespaceName = structDeclarationSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)); string structName = structDeclaration.Identifier.Text; SyntaxTokenList structModifiers = structDeclaration.Modifiers; IEnumerable bodyStatements = GenerateRenderMethodBody( processedConstants, staticFields, processedTypes, isComputeShader, processedMembers, pixelShaderTextureType, accessesStaticSampler, sharedBuffers, forwardDeclarations, processedMethods, entryPoint); implicitTextureType = pixelShaderTextureType; isSamplerUsed = accessesStaticSampler; // This code produces a method declaration as follows: // // public readonly void BuildHlslString(ref ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) // { // // } return MethodDeclaration( PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("BuildHlslString")) .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) .AddParameterListParameters( Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("ArrayPoolStringBuilder")), Parameter(Identifier("threadsX")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("threadsY")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("threadsZ")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword)))) .WithBody(Block(bodyStatements)); } /// /// Gets a sequence of captured fields and their mapped names. /// /// The current generator context in use. /// The input instance to process. /// The collection of currently discovered types. /// Indicates whether or not represents a compute shader. /// A sequence of captured fields in . [Pure] private static IEnumerable<(IFieldSymbol Symbol, string HlslName, string HlslType)> GetProcessedFields( GeneratorExecutionContext context, INamedTypeSymbol structDeclarationSymbol, ICollection types, bool isComputeShader) { bool hlslResourceFound = false; foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { if (fieldSymbol.IsStatic) continue; AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) }); // Group shared fields must be static if (attribute is not null) { context.ReportDiagnostic(InvalidGroupSharedFieldDeclaration, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name); } // Captured fields must be named type symbols if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol) { context.ReportDiagnostic(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); continue; } string metadataName = typeSymbol.GetFullMetadataName(); // Allowed fields must be either resources, unmanaged values or delegates if (HlslKnownTypes.IsTypedResourceType(metadataName)) { hlslResourceFound = true; // Track the type of items in the current buffer if (HlslKnownTypes.IsStructuredBufferType(metadataName)) { types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); } } else if (!typeSymbol.IsUnmanagedType && typeSymbol.TypeKind != TypeKind.Delegate) { // Shaders can only capture valid HLSL resource types or unmanaged types context.ReportDiagnostic(InvalidShaderField, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, typeSymbol); continue; } else if (typeSymbol.IsUnmanagedType && !HlslKnownTypes.IsKnownHlslType(metadataName)) { // Track the type if it's a custom struct types.Add(typeSymbol); } string typeName = HlslKnownTypes.GetMappedName(typeSymbol); _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); // Yield back the current mapping for the name (if the name used a reserved keyword) yield return (fieldSymbol, mapping ?? fieldSymbol.Name, typeName); } // If the shader is a compute one (so no implicit output texture), it has to contain at least one resource if (!hlslResourceFound && isComputeShader) { context.ReportDiagnostic(MissingShaderResources, structDeclarationSymbol, structDeclarationSymbol); } } /// /// Gets a sequence of shader static fields and their mapped names. /// /// The current generator context in use. /// The instance for the type to process. /// The instance for the current type. /// The type symbol for the shader type. /// The collection of currently discovered types. /// The collection of discovered constant definitions. /// A sequence of static constant fields in . [Pure] private static IEnumerable<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( GeneratorExecutionContext context, SemanticModelProvider semanticModel, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary constantDefinitions) { foreach (var fieldDeclaration in structDeclaration.Members.OfType()) { foreach (var variableDeclarator in fieldDeclaration.Declaration.Variables) { IFieldSymbol fieldSymbol = (IFieldSymbol)semanticModel.For(variableDeclarator).GetDeclaredSymbol(variableDeclarator)!; if (!fieldSymbol.IsStatic || fieldSymbol.IsConst) { continue; } AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) }); if (attribute is not null) continue; // Constant properties must be of a primitive, vector or matrix type if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol || !HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullMetadataName())) { context.ReportDiagnostic(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); continue; } _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); string typeDeclaration = fieldSymbol.IsReadOnly switch { true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}", false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" }; StaticFieldRewriter staticFieldRewriter = new( semanticModel, discoveredTypes, constantDefinitions, context); string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace().ToFullString(); yield return (mapping ?? fieldSymbol.Name, typeDeclaration, assignment); } } } /// /// Gets a sequence of captured members and their mapped names. /// /// The current generator context in use. /// The input instance to process. /// The collection of currently discovered types. /// A sequence of captured members in . [Pure] private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers( GeneratorExecutionContext context, INamedTypeSymbol structDeclarationSymbol, ICollection types) { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { if (!fieldSymbol.IsStatic) continue; AttributeData? attribute = fieldSymbol.GetAttributes().FirstOrDefault(static a => a.AttributeClass is { Name: nameof(GroupSharedAttribute) }); if (attribute is null) continue; if (fieldSymbol.Type is not IArrayTypeSymbol typeSymbol) { context.ReportDiagnostic(InvalidGroupSharedFieldType, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); continue; } if (!typeSymbol.ElementType.IsUnmanagedType) { context.ReportDiagnostic(InvalidGroupSharedFieldElementType, fieldSymbol, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); continue; } int? bufferSize = (int?)attribute.ConstructorArguments.FirstOrDefault().Value; string typeName = HlslKnownTypes.GetMappedElementName(typeSymbol); _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); yield return (mapping ?? fieldSymbol.Name, typeName, bufferSize); types.Add((INamedTypeSymbol)typeSymbol.ElementType); } } /// /// Gets a sequence of processed methods declared within a given type. /// /// The current generator context in use. /// The type symbol for the shader type. /// The instance for the current type. /// The instance for the type to process. /// The collection of currently discovered types. /// The set of discovered and processed static methods. /// The collection of discovered constant definitions. /// Indicates whether or not represents a compute shader. /// A sequence of processed methods in , and the entry point. [Pure] private static (string EntryPoint, IEnumerable Methods, IEnumerable Declarations, bool IsSamplerUser) GetProcessedMethods( GeneratorExecutionContext context, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary staticMethods, IDictionary constantDefinitions, bool isComputeShader) { // Find all declared methods in the type ImmutableArray methodDeclarations = ( from syntaxNode in structDeclaration.DescendantNodes() where syntaxNode.IsKind(SyntaxKind.MethodDeclaration) select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray(); string? entryPoint = null; List methods = new(); List declarations = new(); bool isSamplerUsed = false; foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) { IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!; bool isShaderEntryPoint = (isComputeShader && methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) && methodDeclarationSymbol.ReturnsVoid && methodDeclarationSymbol.TypeParameters.Length == 0 && methodDeclarationSymbol.Parameters.Length == 0) || (!isComputeShader && methodDeclarationSymbol.Name == nameof(IPixelShader.Execute) && methodDeclarationSymbol.ReturnType is not null && // TODO: match for pixel type methodDeclarationSymbol.TypeParameters.Length == 0 && methodDeclarationSymbol.Parameters.Length == 0); // Create the source rewriter for the current method ShaderSourceRewriter shaderSourceRewriter = new( structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions, context, isShaderEntryPoint); // Rewrite the method syntax tree MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); // Track the implicit sampler, if used isSamplerUsed = isSamplerUsed || shaderSourceRewriter.IsSamplerUsed; // Emit the extracted local functions first foreach (var localFunction in shaderSourceRewriter.LocalFunctions) { methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString()); declarations.Add(localFunction.Value.AsDefinition().NormalizeWhitespace().ToFullString()); } // If the method is the shader entry point, do additional processing if (isShaderEntryPoint) { processedMethod = isComputeShader switch { true => new ExecuteMethodRewriter.Compute(shaderSourceRewriter).Visit(processedMethod)!, false => new ExecuteMethodRewriter.Pixel(shaderSourceRewriter).Visit(processedMethod)! }; entryPoint = processedMethod.NormalizeWhitespace().ToFullString(); } else { methods.Add(processedMethod.NormalizeWhitespace().ToFullString()); declarations.Add(processedMethod.AsDefinition().NormalizeWhitespace().ToFullString()); } } // Process static methods as well foreach (MethodDeclarationSyntax staticMethod in staticMethods.Values) { methods.Add(staticMethod.NormalizeWhitespace().ToFullString()); declarations.Add(staticMethod.AsDefinition().NormalizeWhitespace().ToFullString()); } return (entryPoint!, methods, declarations, isSamplerUsed); } /// /// Gets a sequence of discovered constants. /// /// The collection of discovered constant definitions. /// A sequence of discovered constants to declare in the shader. [Pure] internal static IEnumerable<(string Name, string Value)> GetProcessedConstants(IReadOnlyDictionary constantDefinitions) { foreach (var constant in constantDefinitions) { var ownerTypeName = ((INamedTypeSymbol)constant.Key.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); var constantName = $"__{ownerTypeName}__{constant.Key.Name}"; yield return (constantName, constant.Value); } } /// /// Gets the sequence of processed discovered custom types. /// /// The sequence of discovered custom types. /// A sequence of custom type definitions to add to the shader source. internal static IEnumerable GetProcessedTypes(IEnumerable types) { foreach (var type in HlslKnownTypes.GetCustomTypes(types)) { var structType = type.GetFullMetadataName().ToHlslIdentifierName(); var structDeclaration = StructDeclaration(structType); // Declare the fields of the current type foreach (var field in type.GetMembers().OfType()) { INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type; // Convert the name to the fully qualified HLSL version if (!HlslKnownTypes.TryGetMappedName(fieldType.GetFullMetadataName(), out string? mapped)) { mapped = fieldType.GetFullMetadataName().ToHlslIdentifierName(); } structDeclaration = structDeclaration.AddMembers( FieldDeclaration(VariableDeclaration( IdentifierName(mapped!)).AddVariables( VariableDeclarator(Identifier(field.Name))))); } // Insert the trailing ; right after the closing bracket (after normalization) yield return structDeclaration .NormalizeWhitespace() .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)) .ToFullString(); } } /// /// Finds and reports all declared properties in a shader. /// /// The current generator context in use. /// The input instance to process. private static void DetectAndReportPropertyDeclarations(GeneratorExecutionContext context, INamedTypeSymbol structDeclarationSymbol) { foreach (var propertySymbol in structDeclarationSymbol.GetMembers().OfType()) { context.ReportDiagnostic(DiagnosticDescriptors.PropertyDeclaration, propertySymbol); } } private static IEnumerable GenerateRenderMethodBody( IEnumerable<(string Name, string Value)> definedConstants, IEnumerable<(string Name, string TypeDeclaration, string? Assignment)> staticFields, IEnumerable declaredTypes, bool isComputeShader, IEnumerable<(IFieldSymbol Symbol, string HlslName, string HlslType)> instanceFields, string? implicitTextureType, bool isSamplerUsed, IEnumerable<(string Name, string Type, int? Count)> sharedBuffers, IEnumerable? forwardDeclarations, IEnumerable processedMethods, string executeMethod) { // builder = ArrayPoolStringBuilder.Create(1024); yield return ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName("builder"), InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("ArrayPoolStringBuilder"), IdentifierName("Create"))) .AddArgumentListArguments( Argument(LiteralExpression( SyntaxKind.NumericLiteralExpression, Literal(1024)))))); // Header yield return ParseStatement("builder.AppendLine(\"// ================================================\");"); yield return ParseStatement("builder.AppendLine(\"// AUTO GENERATED\");"); yield return ParseStatement("builder.AppendLine(\"// ================================================\");"); yield return ParseStatement("builder.AppendLine(\"// This shader was created by ComputeSharp.\");"); yield return ParseStatement("builder.AppendLine(\"// See: https://github.com/Sergio0694/ComputeSharp.\");"); // Group size constants yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement("builder.Append(\"#define __GroupSize__get_X \");"); yield return ParseStatement("builder.AppendLine(threadsX.ToString());"); yield return ParseStatement("builder.Append(\"#define __GroupSize__get_Y \");"); yield return ParseStatement("builder.AppendLine(threadsY.ToString());"); yield return ParseStatement("builder.Append(\"#define __GroupSize__get_Z \");"); yield return ParseStatement("builder.AppendLine(threadsZ.ToString());"); // Define declarations foreach (var (name, value) in definedConstants) { yield return ParseStatement($"builder.AppendLine(\"#define {name} {value}\");"); } // Static fields if (staticFields.Any()) { yield return ParseStatement("builder.AppendLine();"); foreach (var field in staticFields) { if (field.Assignment is string assignment) { yield return ParseStatement($"builder.AppendLine(\"{field.TypeDeclaration} {field.Name} = {assignment};\");"); } else { yield return ParseStatement($"builder.AppendLine(\"{field.TypeDeclaration} {field.Name};\");"); } } } // Declared types foreach (var type in declaredTypes) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"{type}\");"); } // Captured variables yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement("builder.AppendLine(\"cbuffer _ : register(b0)\");"); yield return ParseStatement("builder.AppendLine('{');"); yield return ParseStatement("builder.AppendLine(\" uint __x;\");"); yield return ParseStatement("builder.AppendLine(\" uint __y;\");"); if (isComputeShader) { yield return ParseStatement("builder.AppendLine(\" uint __z;\");"); } // User-defined values foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields) { if (fieldSymbol.Type.IsUnmanagedType) { yield return ParseStatement($"builder.AppendLine(\" {fieldType} {fieldName};\");"); } } yield return ParseStatement("builder.AppendLine('}');"); int constantBuffersCount = 0, readOnlyBuffersCount = 0, readWriteBuffersCount = 0; // Optional implicit texture field if (!isComputeShader) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"{implicitTextureType} __outputTexture : register(u{readWriteBuffersCount++});\");"); } // Optional sampler field if (isSamplerUsed) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement("builder.AppendLine(\"SamplerState __sampler : register(s);\");"); } // Resources foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields) { string metadataName = fieldSymbol.GetFullMetadataName(); if (HlslKnownTypes.IsConstantBufferType(metadataName)) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"cbuffer _{fieldName} : register(b{constantBuffersCount++})\");"); yield return ParseStatement("builder.AppendLine('{');"); yield return ParseStatement($"builder.AppendLine(\" {fieldType} {fieldName}[2];\");"); yield return ParseStatement("builder.AppendLine('}');"); } else if (HlslKnownTypes.IsReadOnlyTypedResourceType(metadataName)) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"{fieldType} : register(t{readOnlyBuffersCount++});\");"); } else if (HlslKnownTypes.IsTypedResourceType(metadataName)) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"{fieldType} : register(u{readWriteBuffersCount++});\");"); } } // Shared buffers foreach (var (bufferName, bufferType, bufferCount) in sharedBuffers) { object count = (object?)bufferCount ?? "threadsX * threadsY * threadsZ"; yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"groupshared {bufferType} {bufferName} [{count}];\");"); } // Forward declarations if (forwardDeclarations is not null) { foreach (var forwardDeclaration in forwardDeclarations) { yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement($"builder.AppendLine(\"{forwardDeclaration}\");"); } } // Captured methods if (processedMethods is not null) { foreach (var method in processedMethods) { yield return ParseStatement("builder.AppendLine();"); yield return ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("AppendLine"))) .AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(method))))); } } // Entry point yield return ParseStatement("builder.AppendLine();"); yield return ParseStatement("builder.AppendLine(\"[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]\");"); yield return ExpressionStatement( InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("AppendLine"))) .AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(executeMethod))))); } } }