using System.Linq;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.D2D1.SourceGenerators;
///
partial class D2DPixelShaderDescriptorGenerator
{
///
partial class HlslBytecode
{
///
/// Writes the ShaderProfile property.
///
/// The input instance with gathered shader info.
/// The instance to write into.
public static void WriteShaderProfileSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"static ComputeSharp.D2D1.D2D1ShaderProfile global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.ShaderProfile => global::ComputeSharp.D2D1.D2D1ShaderProfile.{info.HlslInfoKey.ShaderProfile};");
}
///
/// Writes the CompileOptions property.
///
/// The input instance with gathered shader info.
/// The instance to write into.
public static void WriteCompileOptionsSyntax(D2D1ShaderInfo info, IndentedTextWriter writer)
{
// Get a formatted representation of the compile options being used
string compileOptionsExpression =
info.HlslInfoKey.CompileOptions
.ToString()
.Split(',')
.Select(static name => $"global::ComputeSharp.D2D1.D2D1CompileOptions.{name.Trim()}")
.Aggregate("", static (left, right) => left.Length > 0 ? $"{left} | {right}" : right);
writer.WriteLine("/// ");
writer.WriteGeneratedAttributes(GeneratorName);
writer.WriteLine($"static ComputeSharp.D2D1.D2D1CompileOptions global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.CompileOptions => {compileOptionsExpression};");
}
///
/// Writes the HlslBytecode property.
///
/// The input instance with gathered shader info.
/// The instance to write into.
public static void WriteHlslBytecodeSyntax(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}>.HlslBytecode => ");
// If there is no bytecode, just return a default expression.
// Otherwise, return the memory manager backed memory instance.
if (info.HlslInfo is not HlslBytecodeInfo.Success)
{
writer.WriteLine("default;");
}
else
{
writer.WriteLine("global::ComputeSharp.D2D1.Generated.HlslBytecodeMemoryManager.Instance.Memory;");
}
}
///
/// Registers a callback to generate an additional type, 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 RegisterAdditionalTypeSyntax(
D2D1ShaderInfo info,
ImmutableArrayBuilder> callbacks,
ImmutableHashSetBuilder usingDirectives)
{
// If there is no bytecode, no memory manager is needed
if (info.HlslInfo is not HlslBytecodeInfo.Success)
{
return;
}
usingDirectives.Add("global::System");
usingDirectives.Add("global::System.Buffers");
usingDirectives.Add("global::System.CodeDom.Compiler");
usingDirectives.Add("global::System.Diagnostics");
usingDirectives.Add("global::System.Diagnostics.CodeAnalysis");
usingDirectives.Add("global::System.Runtime.CompilerServices");
usingDirectives.Add("global::System.Runtime.InteropServices");
// Declare the HlslBytecodeMemoryManager custom memory manager type
static void Callback(D2D1ShaderInfo info, IndentedTextWriter writer)
{
writer.WriteLine($$"""/// """);
writer.WriteLine($$"""/// implementation to get the HLSL bytecode.""");
writer.WriteLine($$"""/// """);
writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false);
writer.WriteLine($$"""file sealed class HlslBytecodeMemoryManager : MemoryManager""");
using (writer.WriteBlock())
{
// Static singleton instance (to avoid the static constructor, just like with the input types)
writer.WriteLine($$"""/// The singleton instance to use.""");
writer.WriteLine($$"""public static readonly HlslBytecodeMemoryManager Instance = new();""");
// RVA field (with the compiled HLSL bytecode, on a single line)
writer.WriteLine();
writer.WriteLine("/// The RVA data with the HLSL bytecode.");
writer.Write("private static ReadOnlySpan Data => new byte[] { ");
SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(((HlslBytecodeInfo.Success)info.HlslInfo).Bytecode.AsSpan(), writer);
writer.WriteLine(" };");
writer.WriteLine();
// Add the remaining members for the memory manager
writer.WriteLine("""
///
public override unsafe Span GetSpan()
{
return new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(Data)), Data.Length);
}
///
public override Memory Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => CreateMemory(Data.Length);
}
///
public override unsafe MemoryHandle Pin(int elementIndex)
{
return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
}
///
public override void Unpin()
{
}
///
protected override void Dispose(bool disposing)
{
}
""", isMultiline: true);
}
}
callbacks.Add(Callback);
}
}
}