diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
index f6f89407..34a382a5 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
@@ -14,7 +14,6 @@
-
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs
deleted file mode 100644
index 8210504c..00000000
--- a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/SyntaxTokenHelper.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.CSharp;
-using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
-
-namespace ComputeSharp.SourceGeneration.Helpers;
-
-///
-/// Helpers to create and work with -s.
-///
-internal static class SyntaxTokenHelper
-{
- ///
- /// Creates a representing a raw multiline string literal with the given text.
- ///
- /// The text to use for the created raw multiline string literal token.
- /// The current indentation level to use.
- /// A representing a raw multiline string literal with the value of .
- public static SyntaxToken CreateRawMultilineStringLiteral(string text, int indentationLevel)
- {
- // Create a token to represent the raw multiline string literal expression. An extra newline and indentation
- // has to be added to the raw text when there is no trailing newline, as not doing so would otherwise case
- // the terminating """ token to fall on the same line, which is invalid syntax. This is only needed to make
- // the code valid, as the actual literal context of the string is not affected and remains the same.
- string indentation = new(' ', 4 * indentationLevel);
-
- return Token(
- TriviaList(),
- SyntaxKind.MultiLineRawStringLiteralToken,
- $"\"\"\"\n{indentation}{text.Replace("\n", $"\n{indentation}")}{(text.EndsWith("\n") ? "" : $"\n{indentation}")}\"\"\"",
- text,
- TriviaList());
- }
-}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
index cf9d323a..a5f53439 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
@@ -1,12 +1,6 @@
-using ComputeSharp.__Internals;
+using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGenerators.Models;
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.CSharp;
-using Microsoft.CodeAnalysis.CSharp.Syntax;
-using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
-
-#pragma warning disable CS0618
namespace ComputeSharp.SourceGenerators;
@@ -17,36 +11,25 @@ partial class IShaderGenerator
partial class BuildHlslSource
{
///
- /// Creates a instance for the BuildHlslSource method.
+ /// Writes the BuildHlslSource method.
///
- /// The input instance to use.
- /// The depth of the hierarchy for this type (used to calculate the right indentation).
- /// The resulting instance for the BuildHlslSource method.
- public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth)
+ /// The input instance with gathered shader info.
+ /// The instance to write into.
+ public static void WriteSyntax(ShaderInfo info, IndentedTextWriter writer)
{
- // The indentation level is the current depth + 2 (the method, and the return expression)
- SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSourceInfo.HlslSource, hierarchyDepth + 2);
+ writer.WriteLine("/// ");
+ writer.WriteGeneratedAttributes(GeneratorName);
+ writer.WriteLine($"readonly void global::ComputeSharp.__Internals.IShader.BuildHlslSource(out string hlslSource)");
- // This code produces a method declaration as follows:
- //
- // readonly void global::ComputeSharp.__Internals.IShader.BuildHlslSource(out string hlslSource)
- // {
- // hlslSource = ;
- // }
- return
- MethodDeclaration(
- PredefinedType(Token(SyntaxKind.VoidKeyword)),
- Identifier("BuildHlslSource"))
- .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.__Internals.{nameof(IShader)}")))
- .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
- .AddParameterListParameters(
- Parameter(Identifier("hlslSource")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.StringKeyword))))
- .WithBody(Block(
- ExpressionStatement(
- AssignmentExpression(
- SyntaxKind.SimpleAssignmentExpression,
- IdentifierName("hlslSource"),
- LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
+ using (writer.WriteBlock())
+ {
+ writer.WriteLine("hlslSource =");
+ writer.IncreaseIndent();
+ writer.WriteLine("\"\"\"");
+ writer.Write(info.HlslShaderSource.HlslSource, isMultiline: true);
+ writer.WriteLine("\"\"\";");
+ writer.DecreaseIndent();
+ }
}
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index ed27be1b..391b730e 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -19,6 +19,11 @@ namespace ComputeSharp.SourceGenerators;
[Generator(LanguageNames.CSharp)]
public sealed partial class IShaderGenerator : IIncrementalGenerator
{
+ ///
+ /// The name of generator to include in the generated code.
+ ///
+ private const string GeneratorName = "ComputeSharp.IShaderGenerator";
+
///
public void Initialize(IncrementalGeneratorInitializationContext context)
{
@@ -132,12 +137,21 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
.Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource));
// Generate the BuildHlslSource() methods
- context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
+ context.RegisterSourceOutput(shaderInfoWithErrors, static (context, item) =>
{
- MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslShaderSource, item.Hierarchy.Hierarchy.Length);
- CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, addSkipLocalsInitAttribute: true);
+ using ImmutableArrayBuilder> declaredMembers = new();
- context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
+ declaredMembers.Add(BuildHlslSource.WriteSyntax);
+
+ using IndentedTextWriter writer = new();
+
+ item.Hierarchy.WriteSyntax(
+ state: item,
+ writer: writer,
+ baseTypes: ReadOnlySpan.Empty,
+ memberCallbacks: declaredMembers.WrittenSpan);
+
+ context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", writer.ToString());
});
// Get the LoadDispatchMetadata() info (hierarchy and dispatch metadata info)