using System;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.D2D1.SourceGenerators;
///
partial class ID2D1ShaderGenerator
{
///
private static partial class InputDescriptions
{
///
/// Writes the InputDescriptions property.
///
/// The input instance with gathered shader info.
/// The instance to write into.
public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("readonly global::System.ReadOnlyMemory global::ComputeSharp.D2D1.__Internals.ID2D1Shader.InputDescriptions => ");
// If there are no input descriptions, just return a default expression.
// Otherwise, return the shared array with cached input descriptions.
if (info.InputDescriptions.IsEmpty)
{
writer.WriteLine("default;");
}
else
{
writer.WriteLine("Data.InputDescriptions;");
}
}
///
/// Registers a callback to generate an additional data member, if needed.
///
/// The input instance with gathered shader info.
/// The registered callbacks to generate additional data members.
public static void RegisterAdditionalDataMemberSyntax(D2D1ShaderInfo info, ImmutableArrayBuilder> callbacks)
{
// If there are no input descriptions, there is nothing to di
if (info.ResourceTextureDescriptions.IsEmpty)
{
return;
}
// Declare the shared array with input descriptions
static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("""/// The singleton array instance.""");
writer.WriteLine("""public static readonly global::ComputeSharp.D2D1.Interop.D2D1InputDescription[] InputDescriptions = """);
// Initialize all input descriptions
using (writer.WriteBlock())
{
for (int i = 0; i < info.InputDescriptions.Length; i++)
{
InputDescription description = info.InputDescriptions[i];
writer.Write($$"""new global::ComputeSharp.D2D1.Interop.D2D1InputDescription({description.Index}, {description.Filter}) { LevelOfDetailCount = {{description.LevelOfDetailCount}} }""");
if (i < info.InputDescriptions.Length - 1)
{
writer.WriteLine(",");
}
}
}
}
callbacks.Add(Callback);
}
///
/// Gets any type declarations for additional members.
///
/// The additional members that are needed.
/// Any type declarations for additional members.
public static TypeDeclarationSyntax[] GetDataTypeDeclarations(MemberDeclarationSyntax[] memberDeclarations)
{
if (memberDeclarations.Length == 0)
{
return Array.Empty();
}
// Create the container type declaration:
//
// ///
// /// A container type for additional data needed by the shader.
// ///
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.DebuggerNonUserCode]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// file static class Data
// {
//
// }
TypeDeclarationSyntax dataTypeDeclaration =
ClassDeclaration("Data")
.AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.StaticKeyword))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.AddMembers(memberDeclarations)
.WithLeadingTrivia(
Comment("/// "),
Comment("/// A container type for input descriptions."),
Comment("/// "));
return new[] { dataTypeDeclaration };
}
}
}