diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 0e0e3986..e50afcf4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -19,25 +19,19 @@ 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, bool useRawMultiLineStringLiteralExpression) + public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth) { - // If raw multiline string literal expressions are used, create a token to represent it. Here some spaces are + // Create a token to represent the raw multiline string literal expression with the HLSL source. 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) - }; + SyntaxToken hlslSourceLiteralToken = Token( + TriviaList(), + SyntaxKind.MultiLineRawStringLiteralToken, + $"\"\"\"\n{indentation}{hlslSource.Replace("\n", $"\n{indentation}")}\"\"\"", + hlslSource, + TriviaList()); // This code produces a method declaration as follows: // diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index fad72415..e070c17b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -218,18 +218,13 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Select(static (item, token) => (item.Hierarchy, item.Source.HlslSource)) .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); - // Check whether raw multiline string literals can be used (C# preview) - IncrementalValueProvider canUseRawMultiLineStringLiterals = - context.ParseOptionsProvider - .Select((item, _) => item is CSharpParseOptions { LanguageVersion: >= LanguageVersion.Preview }); - // Generate the BuildHlslSource() methods - context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseRawMultiLineStringLiterals), static (context, item) => + context.RegisterSourceOutput(hlslSourceInfo, 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); + MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); - context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get a filtered sequence to enable caching diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 8f7b86c7..524e7fd5 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -24,13 +24,12 @@ 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, bool useRawMultiLineStringLiteralExpression) + public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders, int hierarchyDepth) { // Generate the necessary body statements depending on whether dynamic shaders are supported ImmutableArray bodyStatements = supportsDynamicShaders - ? GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth, useRawMultiLineStringLiteralExpression) + ? GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth) : GenerateEmptyMethodBody(); // This code produces a method declaration as follows: @@ -82,9 +81,8 @@ 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, bool useRawMultiLineStringLiteralExpression) + private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth) { ImmutableArray.Builder statements = ImmutableArray.CreateBuilder(); StringBuilder textBuilder = new(); @@ -119,24 +117,19 @@ partial class IShaderGenerator sizeHint += textBuilder.Length; - // If raw multiline string literal expressions are used, create a token to represent it. Here some spaces are + // Create a token to represent the raw multiline string literal expression. 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) - }; + SyntaxToken textToken = Token( + TriviaList(), + SyntaxKind.MultiLineRawStringLiteralToken, + $"\"\"\"\n{indentation}{text.Replace("\n", $"\n{indentation}")}{(text.EndsWith("\n") ? "" : $"\n{indentation}")}\"\"\"", + text, + TriviaList()); statements.Add( ExpressionStatement( diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 10ce11fb..4a8e2b5e 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -170,18 +170,13 @@ 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); - // Check whether raw multiline string literals can be used (C# preview) - IncrementalValueProvider canUseRawMultiLineStringLiterals = - context.ParseOptionsProvider - .Select((item, _) => item is CSharpParseOptions { LanguageVersion: >= LanguageVersion.Preview }); - // Generate the BuildHlslSource() methods - context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseSkipLocalsInit).Combine(canUseRawMultiLineStringLiterals), static (context, item) => + context.RegisterSourceOutput(hlslSourceInfo.Combine(canUseSkipLocalsInit), 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, canUseSkipLocalsInit: item.Left.Right); + MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.SourceInfo, item.Left.SupportsDynamicShaders, item.Left.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: item.Right); - context.AddSource($"{item.Left.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the dispatch metadata info