diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
index 24881627..3167bf64 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
+++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj
@@ -83,7 +83,7 @@
-
+
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
index 9140019e..364da166 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
@@ -3,6 +3,7 @@ using System.Collections.Immutable;
using System.Text;
using ComputeSharp.__Internals;
using ComputeSharp.SourceGenerators.Models;
+using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -22,12 +23,14 @@ partial class IShaderGenerator
///
/// The input instance to use.
/// Indicates whether or not dynamic shaders are supported.
+ /// The depth of the hierarchy for this type (used to calculate the right indentation).
+ /// Whether to use a raw multiline string literal expression
/// The resulting instance for the BuildHlslSource method.
- public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders)
+ public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression)
{
// Generate the necessary body statements depending on whether dynamic shaders are supported
ImmutableArray bodyStatements = supportsDynamicShaders
- ? GenerateRenderMethodBody(hlslSourceInfo)
+ ? GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth, useRawMultiLineStringLiteralExpression)
: GenerateEmptyMethodBody();
// This code produces a method declaration as follows:
@@ -78,8 +81,10 @@ partial class IShaderGenerator
/// Produces the series of statements to build the current HLSL source.
///
/// The input instance to use.
+ /// The depth of the hierarchy for this type (used to calculate the right indentation).
+ /// Whether to use a raw multiline string literal expression
/// The series of statements to build the HLSL source to compile to execute the current shader.
- private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo)
+ private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression)
{
ImmutableArray.Builder statements = ImmutableArray.CreateBuilder();
StringBuilder textBuilder = new();
@@ -114,11 +119,30 @@ partial class IShaderGenerator
sizeHint += textBuilder.Length;
+ // If raw multiline string literal expressions are used, create a token to represent it. Here some spaces are
+ // also added to properly align the resulting text with one indentation below the declaring string constant.
+ // The spaces are: 4 for each containing type, 4 for the containing method, and 4 for the one additional indentation.
+ // 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 * hierarchyDepth + 4 + 4);
+ SyntaxToken textToken = useRawMultiLineStringLiteralExpression switch
+ {
+ true =>
+ Token(
+ TriviaList(),
+ SyntaxKind.MultiLineRawStringLiteralToken,
+ $"\"\"\"\n{indentation}{text.Replace("\n", $"\n{indentation}")}{(text.EndsWith("\n") ? "" : $"\n{indentation}")}\"\"\"",
+ text,
+ TriviaList()),
+ false => Literal(text)
+ };
+
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
- .AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(text))))));
+ .AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, textToken)))));
textBuilder.Clear();
}
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index 2eb5473a..8786be8b 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -171,13 +171,18 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
.Select(static (item, token) => (item.Left.Dispatch.Hierarchy, item.Left.Hlsl, item.Right))
.WithComparers(HierarchyInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default, EqualityComparer.Default);
- // Generate the BuildHlslSource() methods
- context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseSkipLocalsInit), static (context, item) =>
- {
- MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.SourceInfo, item.Left.SupportsDynamicShaders);
- CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, buildHlslStringMethod, item.Right);
+ // Check whether raw multiline string literals can be used (C# preview)
+ IncrementalValueProvider canUseRawMultiLineStringLiterals =
+ context.ParseOptionsProvider
+ .Select((item, _) => item is CSharpParseOptions options && options.LanguageVersion >= LanguageVersion.Preview);
- context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}", compilationUnit.GetText(Encoding.UTF8));
+ // Generate the BuildHlslSource() methods
+ context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseSkipLocalsInit).Combine(canUseRawMultiLineStringLiterals), static (context, item) =>
+ {
+ MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.Left.SourceInfo, item.Left.Left.SupportsDynamicShaders, item.Left.Left.Hierarchy.Hierarchy.Length, item.Right);
+ CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Left.Hierarchy, buildHlslStringMethod, item.Right);
+
+ context.AddSource($"{item.Left.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}", compilationUnit.GetText(Encoding.UTF8));
});
// Get the dispatch metadata info