diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index e53c3b74..84a806b7 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -91,7 +91,7 @@ - + diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index df18aab4..0e0e3986 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -1,4 +1,5 @@ using ComputeSharp.D2D1.__Internals; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -17,9 +18,27 @@ partial class ID2D1ShaderGenerator /// Creates a instance for the BuildHlslSource method. /// /// 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) + public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression) { + // 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. + string indentation = new(' ', 4 * hierarchyDepth + 4 + 4); + SyntaxToken hlslSourceLiteralToken = useRawMultiLineStringLiteralExpression switch + { + true => + Token( + TriviaList(), + SyntaxKind.MultiLineRawStringLiteralToken, + $"\"\"\"\n{indentation}{hlslSource.Replace("\n", $"\n{indentation}")}\"\"\"", + hlslSource, + TriviaList()), + false => Literal(hlslSource) + }; + // This code produces a method declaration as follows: // // readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.BuildHlslSource(out string hlslSource) @@ -36,7 +55,7 @@ partial class ID2D1ShaderGenerator AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName("hlslSource"), - LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(hlslSource)))))); + LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralToken))))); } } } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index a301af50..625b10d6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -193,13 +193,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Select(static (item, token) => (item.Hierarchy, item.Source.HlslSource)) .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); - // Generate the BuildHlslSource() methods - context.RegisterSourceOutput(hlslSourceInfo, static (context, item) => - { - MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); + // 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.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}", compilationUnit.GetText(Encoding.UTF8)); + // Generate the BuildHlslSource() methods + context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseRawMultiLineStringLiterals), static (context, item) => + { + MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.HlslSource, item.Left.Hierarchy.Hierarchy.Length, item.Right); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); + + context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}", compilationUnit.GetText(Encoding.UTF8)); }); // Get a filtered sequence to enable caching diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 8c49cd29..126ec9db 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -4,6 +4,7 @@ net6.0 false $(NoWarn);CS0419;CA1416 + preview