using System.Collections.Immutable;
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 input instance to process.
/// The fully qualified type name, if invalid.
public static string? GetInvalidReturnType(IMethodSymbol methodSymbol)
{
if (methodSymbol.ReturnType is not INamedTypeSymbol
{
Name: "ReadOnlySpan",
ContainingNamespace.Name: "System",
IsGenericType: true,
TypeArguments: [{ SpecialType: SpecialType.System_Byte }]
})
{
return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
return null;
}
///
/// Extracts the HLSL source from an annotated method.
///
/// The input instance to process.
/// The HLSL source to compile, if present.
public static string? GetHlslSource(IMethodSymbol methodSymbol)
{
if (methodSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData) &&
attributeData!.TryGetConstructorArgument(0, out string? hlslSource))
{
return hlslSource;
}
return null;
}
///
/// Extracts the shader profile for a target method, if present.
///
/// The input instance to process.
/// The shader profile to use to compile the shader, if present.
public static D2D1ShaderProfile? GetShaderProfile(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!;
}
return null;
}
///
/// Extracts the compile options for the current shader.
///
/// The input instance to process.
/// The compile options to use to compile the shader, if present.
public static D2D1CompileOptions GetCompileOptions(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!;
}
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 ImmutableArray GetInfoDiagnostics(IMethodSymbol methodSymbol, HlslBytecodeInfo info)
{
if (info is HlslBytecodeInfo.Win32Error win32Error)
{
DiagnosticInfo diagnostic = DiagnosticInfo.Create(
D2DPixelShaderSourceCompilationFailedWithWin32Exception,
methodSymbol,
methodSymbol,
methodSymbol.ContainingType,
win32Error.HResult,
win32Error.Message);
return ImmutableArray.Create(diagnostic);
}
else if (info is HlslBytecodeInfo.CompilerError fxcError)
{
DiagnosticInfo diagnostic = DiagnosticInfo.Create(
D2DPixelShaderSourceCompilationFailedWithFxcCompilationException,
methodSymbol,
methodSymbol,
methodSymbol.ContainingType,
fxcError.Message);
return ImmutableArray.Create(diagnostic);
}
return ImmutableArray.Empty;
}
///
/// 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;");
}
}
}
}
}