using System; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; /// partial class D2DPixelShaderSourceGenerator { /// /// A container for all the logic for . /// private static partial class Execute { /// /// Validates that the return type of the annotated method is valid and returns the type name. /// /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. public static string? GetInvalidReturnType(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.ReturnType is not INamedTypeSymbol { Name: "ReadOnlySpan", ContainingNamespace.Name: "System", IsGenericType: true, TypeArguments: [{ SpecialType: SpecialType.System_Byte }] }) { diagnostics.Add( InvalidD2DPixelShaderSourceMethodReturnType, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType, methodSymbol.ReturnType); return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } return null; } /// /// Extracts the HLSL source from a method with the annotation. /// /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. public static string GetHlslSource(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { _ = methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData); if (!attributeData!.TryGetConstructorArgument(0, out string? hlslSource)) { diagnostics.Add( InvalidD2DPixelShaderSource, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType); } return hlslSource ?? ""; } /// /// Extracts the shader profile for a target method, if present. /// /// The collection of produced instances. /// The input instance to process. /// The shader profile to use to compile the shader, if present. public static D2D1ShaderProfile GetShaderProfile(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } if (methodSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out attributeData)) { return (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!; } diagnostics.Add( MissingShaderProfileForD2DPixelShaderSource, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType); return D2D1ShaderProfile.PixelShader50; } /// /// Extracts the compile options for the current shader. /// /// The collection of produced instances. /// The input instance to process. /// The compile options to use to compile the shader, if present. public static D2D1CompileOptions GetCompileOptions(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; } if (methodSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out attributeData)) { return (D2D1CompileOptions)attributeData.ConstructorArguments[0].Value!; } diagnostics.Add( MissingCompileOptionsForD2DPixelShaderSource, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType); return D2D1CompileOptions.Default; } /// /// Gets any diagnostics from a processed instance. /// /// The input instance to process. /// The source instance. /// The collection of produced instances. public static void GetInfoDiagnostics( IMethodSymbol methodSymbol, HlslBytecodeInfo info, ImmutableArrayBuilder diagnostics) { DiagnosticInfo? diagnostic = null; if (info is HlslBytecodeInfo.Win32Error win32Error) { diagnostic = DiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithWin32Exception, methodSymbol, [methodSymbol, methodSymbol.ContainingType, win32Error.HResult, win32Error.Message]); } else if (info is HlslBytecodeInfo.CompilerError fxcError) { diagnostic = DiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithFxcCompilationException, methodSymbol, [methodSymbol, methodSymbol.ContainingType, fxcError.Message]); } if (diagnostic is not null) { diagnostics.Add(diagnostic); } } /// /// Writes the generated method for the compiled HLSL bytecode. /// /// The input instance with gathered shader info. /// The instance to write into. public static void WriteSyntax(D2D1PixelShaderSourceInfo info, IndentedTextWriter writer) { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); // Write the modifiers, if any foreach (uint modifier in info.Modifiers) { writer.Write(SyntaxFacts.GetText((SyntaxKind)modifier)); writer.Write(" "); } // Write the return type if (info.InvalidReturnType is string invalidReturnType) { writer.Write(invalidReturnType); writer.Write(" "); } else { writer.Write("global::System.ReadOnlySpan "); } writer.WriteLine($"{info.MethodName}()"); // Write the bytecode expression using (writer.WriteBlock()) { if (info.HlslInfo is HlslBytecodeInfo.Success success) { writer.Write("return ["); SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(success.Bytecode.AsSpan(), writer); writer.WriteLine("];"); } else { writer.Write("return default;"); } } } } }