From 1760c8d3ff9ed46e0ff0f642225a766b758d059e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 21 Oct 2023 16:25:08 +0200 Subject: [PATCH] Split Resources syntax generation code into partial file --- ...derDescriptorGenerator.Resources.Syntax.cs | 125 ++++++++++++++++++ ...puteShaderDescriptorGenerator.Resources.cs | 114 +--------------- 2 files changed, 126 insertions(+), 113 deletions(-) create mode 100644 src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.Syntax.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.Syntax.cs new file mode 100644 index 00000000..ab9df011 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.Syntax.cs @@ -0,0 +1,125 @@ +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; +using ComputeSharp.SourceGenerators.Models; + +namespace ComputeSharp.SourceGenerators; + +/// +partial class ComputeShaderDescriptorGenerator +{ + /// + partial class Resources + { + /// + /// Writes the LoadGraphicsResources method. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer) + { + string typeName = info.Hierarchy.Hierarchy[0].QualifiedName; + + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(GeneratorName); + writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)"); + + using (writer.WriteBlock()) + { + // Nothing to do if there are no resources + if (info.Resources.IsEmpty) + { + return; + } + + // Delegate to the generated loader type + writer.WriteLine("global::ComputeSharp.Generated.GraphicsResourcesLoader.LoadGraphicsResources(in shader, ref loader);"); + } + } + + /// + /// 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 are no resources, there's no need for any additional types + if (info.Resources.IsEmpty) + { + return; + } + + _ = 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::ComputeSharp.Descriptors"); + + // Declare the GraphicsResourcesLoader type + static void Callback(ShaderInfo info, IndentedTextWriter writer) + { + string fullyQualifiedTypeName = info.Hierarchy.GetFullyQualifiedTypeName(); + + writer.WriteLine($"""/// """); + writer.WriteLine($"""/// A type containing loading logic for graphics resources in shaders of type ."""); + writer.WriteLine($"""/// """); + writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false); + writer.WriteLine($"""file static class GraphicsResourcesLoader"""); + + using (writer.WriteBlock()) + { + // Define the FromManaged method (managed shader type to native constant buffer) + writer.WriteLine($$"""/// """); + writer.WriteLine($"""[MethodImpl(MethodImplOptions.AggressiveInlining)]"""); + writer.WriteLine($"""[SkipLocalsInit]"""); + writer.WriteLine($"public static void LoadGraphicsResources(in {fullyQualifiedTypeName} shader, ref TLoader loader)"); + writer.WriteLine(" where TLoader : struct, IGraphicsResourceLoader"); + + using (writer.WriteBlock()) + { + // Generate loading statements for each captured resource. + // Each loading statement will invoke the generated accessor. + for (int i = 0; i < info.Resources.Length; i++) + { + writer.WriteLine($"loader.LoadGraphicsResource(shader.{info.Resources[i].FieldName}(), {i});"); + } + } + + // Define all field accessors (always used, just like for constant buffer fields, see remarks there) + foreach (ResourceInfo resourceInfo in info.Resources) + { + writer.WriteLine(); + + // Generate the correct field accessor depending on whether the field can be referenced directly + if (resourceInfo.UnspeakableName is null) + { + writer.WriteLine($""" + /// + /// The input value. + /// A reference to . + [UnsafeAccessor(UnsafeAccessorKind.Field)] + private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value); + """, isMultiline: true); + } + else + { + writer.WriteLine($""" + /// Gets a reference to the unspeakable field "{resourceInfo.FieldName}" of type . + /// The input value. + /// A reference to the unspeakable field "{resourceInfo.FieldName}". + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{resourceInfo.UnspeakableName}")] + private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value); + """, isMultiline: true); + } + } + } + } + + callbacks.Add(Callback); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs index e475aa08..1e7b84e3 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.Resources.cs @@ -17,7 +17,7 @@ partial class ComputeShaderDescriptorGenerator /// /// A helper with all logic to generate the resources loading code. /// - private static class Resources + private static partial class Resources { /// /// Explores a given type hierarchy and generates statements to load fields. @@ -80,117 +80,5 @@ partial class ComputeShaderDescriptorGenerator diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); } } - - /// - /// Writes the LoadGraphicsResources method. - /// - /// The input instance with gathered shader info. - /// The instance to write into. - public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer) - { - string typeName = info.Hierarchy.Hierarchy[0].QualifiedName; - - writer.WriteLine("/// "); - writer.WriteGeneratedAttributes(GeneratorName); - writer.WriteLine($"static void global::ComputeSharp.Descriptors.IComputeShaderDescriptor<{typeName}>.LoadGraphicsResources(in {typeName} shader, ref TLoader loader)"); - - using (writer.WriteBlock()) - { - // Nothing to do if there are no resources - if (info.Resources.IsEmpty) - { - return; - } - - // Delegate to the generated loader type - writer.WriteLine("global::ComputeSharp.Generated.GraphicsResourcesLoader.LoadGraphicsResources(in shader, ref loader);"); - } - } - - /// - /// 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 are no resources, there's no need for any additional types - if (info.Resources.IsEmpty) - { - return; - } - - _ = 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::ComputeSharp.Descriptors"); - - // Declare the GraphicsResourcesLoader type - static void Callback(ShaderInfo info, IndentedTextWriter writer) - { - string fullyQualifiedTypeName = info.Hierarchy.GetFullyQualifiedTypeName(); - - writer.WriteLine($"""/// """); - writer.WriteLine($"""/// A type containing loading logic for graphics resources in shaders of type ."""); - writer.WriteLine($"""/// """); - writer.WriteGeneratedAttributes(GeneratorName, useFullyQualifiedTypeNames: false); - writer.WriteLine($"""file static class GraphicsResourcesLoader"""); - - using (writer.WriteBlock()) - { - // Define the FromManaged method (managed shader type to native constant buffer) - writer.WriteLine($$"""/// """); - writer.WriteLine($"""[MethodImpl(MethodImplOptions.AggressiveInlining)]"""); - writer.WriteLine($"""[SkipLocalsInit]"""); - writer.WriteLine($"public static void LoadGraphicsResources(in {fullyQualifiedTypeName} shader, ref TLoader loader)"); - writer.WriteLine(" where TLoader : struct, IGraphicsResourceLoader"); - - using (writer.WriteBlock()) - { - // Generate loading statements for each captured resource. - // Each loading statement will invoke the generated accessor. - for (int i = 0; i < info.Resources.Length; i++) - { - writer.WriteLine($"loader.LoadGraphicsResource(shader.{info.Resources[i].FieldName}(), {i});"); - } - } - - // Define all field accessors (always used, just like for constant buffer fields, see remarks there) - foreach (ResourceInfo resourceInfo in info.Resources) - { - writer.WriteLine(); - - // Generate the correct field accessor depending on whether the field can be referenced directly - if (resourceInfo.UnspeakableName is null) - { - writer.WriteLine($""" - /// - /// The input value. - /// A reference to . - [UnsafeAccessor(UnsafeAccessorKind.Field)] - private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value); - """, isMultiline: true); - } - else - { - writer.WriteLine($""" - /// Gets a reference to the unspeakable field "{resourceInfo.FieldName}" of type . - /// The input value. - /// A reference to the unspeakable field "{resourceInfo.FieldName}". - [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{resourceInfo.UnspeakableName}")] - private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value); - """, isMultiline: true); - } - } - } - } - - callbacks.Add(Callback); - } } } \ No newline at end of file