using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
#pragma warning disable IDE0053
namespace ComputeSharp.D2D1.SourceGenerators;
///
partial class D2DPixelShaderDescriptorGenerator
{
///
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("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.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("global::ComputeSharp.D2D1.Generated.Data.InputDescriptions;");
}
}
///
/// Registers a callback to generate additional types, if needed.
///
/// The input instance with gathered shader info.
/// The registered callbacks to generate additional types.
/// The using directives needed by the generated code.
public static void RegisterAdditionalTypesSyntax(
D2D1ShaderInfo info,
ImmutableArrayBuilder> callbacks,
ImmutableHashSetBuilder usingDirectives)
{
// If there are no necessary data members, stop here
if (info.InputDescriptions.IsEmpty &&
info.ResourceTextureDescriptions.IsEmpty)
{
return;
}
_ = usingDirectives.Add("global::System.CodeDom.Compiler");
_ = usingDirectives.Add("global::System.Diagnostics");
_ = usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
AddAdditionalDataMemberUsingDirectives(info, usingDirectives);
ResourceTextureDescriptions.AddAdditionalDataMemberUsingDirectives(info, usingDirectives);
// Declare the additional data type
static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine($$"""/// """);
writer.WriteLine($$"""/// A container type for additional data needed by the shader.""");
writer.WriteLine($$"""/// """);
writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false);
writer.WriteLine($$"""file static class Data""");
using (writer.WriteBlock())
{
using ImmutableArrayBuilder> dataMembers = new();
RegisterAdditionalDataMemberSyntax(info, dataMembers);
ResourceTextureDescriptions.RegisterAdditionalDataMemberSyntax(info, dataMembers);
for (int i = 0; i < dataMembers.WrittenSpan.Length; i++)
{
dataMembers.WrittenSpan[i](info, writer);
if (i < dataMembers.WrittenSpan.Length - 1)
{
writer.WriteLine();
}
}
}
}
callbacks.Add(Callback);
}
///
/// Adds any using directives for the additional data member, if needed.
///
/// The input instance with gathered shader info.
/// The using directives needed by the generated code.
private static void AddAdditionalDataMemberUsingDirectives(D2D1ShaderInfo info, ImmutableHashSetBuilder usingDirectives)
{
if (info.InputDescriptions.IsEmpty)
{
return;
}
_ = usingDirectives.Add("global::ComputeSharp.D2D1");
_ = usingDirectives.Add("global::ComputeSharp.D2D1.Interop");
}
///
/// 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.
private static void RegisterAdditionalDataMemberSyntax(D2D1ShaderInfo info, ImmutableArrayBuilder> callbacks)
{
// If there are no input descriptions, there is nothing to di
if (info.InputDescriptions.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 D2D1InputDescription[] InputDescriptions =""");
writer.WriteLine("""[""");
writer.IncreaseIndent();
// Initialize all input descriptions
writer.WriteInitializationExpressions(info.InputDescriptions.AsSpan(), static (description, writer) =>
{
writer.Write($$"""new D2D1InputDescription({{description.Index}}, D2D1Filter.{{description.Filter}}) { LevelOfDetailCount = {{description.LevelOfDetailCount}} }""");
});
writer.DecreaseIndent();
writer.WriteLine();
writer.WriteLine("];");
}
callbacks.Add(Callback);
}
}
}