using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Models; namespace ComputeSharp.SourceGenerators; /// partial class ComputeShaderDescriptorGenerator { /// partial class HlslBytecode { /// /// Writes the HlslBytecode property. /// /// The input instance with gathered shader info. /// The instance to write into. public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer) { writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.Write($"static global::System.ReadOnlyMemory global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.HlslBytecode => "); // Just like with the D2D1 generator, only return the MemoryManager if we do have HLSL bytecode if (info.HlslInfo is not HlslBytecodeInfo.Success) { writer.WriteLine("default;"); } else { writer.WriteLine("global::ComputeSharp.Generated.HlslBytecodeMemoryManager.Instance.Memory;"); } } /// /// 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( ShaderInfo 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(ShaderInfo 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); } } }