diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
index 675fd0e5..3ba67836 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
@@ -1,5 +1,6 @@
using ComputeSharp.D2D1.__Internals;
using ComputeSharp.SourceGeneration.Helpers;
+using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -19,9 +20,16 @@ partial class ID2D1ShaderGenerator
///
/// The input HLSL source.
/// 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(string hlslSource, int hierarchyDepth)
+ public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression)
{
+ SyntaxToken hlslSourceLiteralExpression = useRawMultiLineStringLiteralExpression switch
+ {
+ true => SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth),
+ false => Literal(hlslSource)
+ };
+
// This code produces a method declaration as follows:
//
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.BuildHlslSource(out string hlslSource)
@@ -38,9 +46,7 @@ partial class ID2D1ShaderGenerator
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("hlslSource"),
- LiteralExpression(
- SyntaxKind.StringLiteralExpression,
- SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth))))));
+ LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
}
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index 447d3bce..906a9bff 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -208,18 +208,24 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
context.AddSource($"{item.Info.Hierarchy.FilenameHint}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
- // Get the BuildHlslSource() info (hierarchy and HLSL source)
- IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource)> hlslSourceInfo =
+ // Check whether raw multiline string literals can be used (C# 11)
+ IncrementalValueProvider canUseRawMultilineStringLiterals =
+ context.ParseOptionsProvider
+ .Select((item, _) => item is CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp11 });
+
+ // Get the BuildHlslSource() info (hierarchy, HLSL source and parsing options)
+ IncrementalValuesProvider<((HierarchyInfo Hierarchy, string HlslSource) Info, bool CanUseRawMultilineStringLiterals)> hlslSourceInfo =
shaderInfoWithErrors
- .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource.HlslSource));
+ .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource.HlslSource))
+ .Combine(canUseSkipLocalsInit);
// Generate the BuildHlslSource() methods
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
{
- MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length);
- CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false);
+ MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Info.HlslSource, item.Info.Hierarchy.Hierarchy.Length, item.CanUseRawMultilineStringLiterals);
+ CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false);
- context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
+ context.AddSource($"{item.Info.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
// Get a filtered sequence to enable caching for the HLSL source info, before the shader compilation step
diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems
index 6adf7894..834ad93c 100644
--- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems
+++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems
@@ -10,6 +10,7 @@
+
diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs
new file mode 100644
index 00000000..56dfed31
--- /dev/null
+++ b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs
@@ -0,0 +1,35 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.CodeAnalysis;
+
+namespace ComputeSharp.SourceGeneration.Extensions;
+
+///
+/// Extension methods for the type.
+///
+internal static class IncrementalValueProviderExtensions
+{
+ ///
+ /// Combines three instances.
+ ///
+ /// The type of values produced by the first input.
+ /// The type of values produced by the second input.
+ /// The type of values produced by the third input.
+ /// The first input.
+ /// The second input.
+ /// The third input.
+ /// The resulting combined result.
+ public static IncrementalValueProvider<(T1, T2, T3)> Combine(
+ this IncrementalValueProvider source1,
+ IncrementalValueProvider source2,
+ IncrementalValueProvider source3)
+ {
+ return
+ source1
+ .Combine(source2)
+ .Combine(source3)
+ .Select(static (items, _) => (items.Left.Left, items.Left.Right, items.Right));
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
index 4d5b5186..94c7b991 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs
@@ -4,6 +4,7 @@ using System.Text;
using ComputeSharp.__Internals;
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;
@@ -24,12 +25,13 @@ 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, int hierarchyDepth)
+ 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, hierarchyDepth)
+ ? GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth, useRawMultiLineStringLiteralExpression)
: GenerateEmptyMethodBody();
// This code produces a method declaration as follows:
@@ -81,8 +83,9 @@ partial class IShaderGenerator
///
/// 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, int hierarchyDepth)
+ private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression)
{
using ImmutableArrayBuilder statements = ImmutableArrayBuilder.Rent();
@@ -112,19 +115,23 @@ partial class IShaderGenerator
{
if (textBuilder.Length > 0)
{
- string text = textBuilder.ToString();
+ string hlslSource = textBuilder.ToString();
- textBuilder.Append(text);
+ textBuilder.Append(hlslSource);
sizeHint += textBuilder.Length;
+ SyntaxToken hlslSourceLiteralExpression = useRawMultiLineStringLiteralExpression switch
+ {
+ true => SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth),
+ false => Literal(hlslSource)
+ };
+
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
- .AddArgumentListArguments(Argument(LiteralExpression(
- SyntaxKind.StringLiteralExpression,
- SyntaxTokenHelper.CreateRawMultilineStringLiteral(text, hierarchyDepth))))));
+ .AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
textBuilder.Clear();
}
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index c5b1da2b..f10dc388 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -154,16 +154,21 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
context.AddSource($"{item.Info.Hierarchy.FilenameHint}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
+ // Check whether raw multiline string literals can be used (C# 11)
+ IncrementalValueProvider canUseRawMultilineStringLiterals =
+ context.ParseOptionsProvider
+ .Select((item, _) => item is CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp11 });
+
// Get the BuildHlslSource info (hierarchy, HLSL source and parsing options)
- IncrementalValuesProvider<((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Info, (bool CanUseSkipLocalsInit, bool SupportsDynamicShaders) Options)> hlslSourceInfo =
+ IncrementalValuesProvider<((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Info, (bool CanUseSkipLocalsInit, bool SupportsDynamicShaders, bool CanUseRawMultilineStringLiterals) Options)> hlslSourceInfo =
shaderInfoWithErrors
.Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource))
- .Combine(canUseSkipLocalsInit.Combine(supportsDynamicShaders));
+ .Combine(canUseSkipLocalsInit.Combine(supportsDynamicShaders, canUseRawMultilineStringLiterals));
// Generate the BuildHlslSource() methods
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
{
- MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Info.HlslShaderSource, item.Options.SupportsDynamicShaders, item.Info.Hierarchy.Hierarchy.Length);
+ MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Info.HlslShaderSource, item.Options.SupportsDynamicShaders, item.Info.Hierarchy.Hierarchy.Length, item.Options.CanUseRawMultilineStringLiterals);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, buildHlslStringMethod, item.Options.CanUseSkipLocalsInit);
context.AddSource($"{item.Info.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8));