Switch IShader.BuildHlslSource to use indented writer
このコミットが含まれているのは:
1 親
bbdacca5cf
コミット
61f155bfb6
4ファイル変更
+35
-72
検索条件に一致するファイルはありません
@@ -14,7 +14,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SemanticModelProvider.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SyntaxFormattingHelper.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SyntaxTokenHelper.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownFields.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownKeywords.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Mappings\HlslKnownMethods.cs" />
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Helpers to create and work with <see cref="SyntaxToken"/>-s.
|
||||
/// </summary>
|
||||
internal static class SyntaxTokenHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="SyntaxToken"/> representing a raw multiline string literal with the given text.
|
||||
/// </summary>
|
||||
/// <param name="text">The text to use for the created raw multiline string literal token.</param>
|
||||
/// <param name="indentationLevel">The current indentation level to use.</param>
|
||||
/// <returns>A <see cref="SyntaxToken"/> representing a raw multiline string literal with the value of <paramref name="text"/>.</returns>
|
||||
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());
|
||||
}
|
||||
}
|
||||
+17
-34
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.
|
||||
/// Writes the <c>BuildHlslSource</c> method.
|
||||
/// </summary>
|
||||
/// <param name="hlslSourceInfo">The input <see cref="HlslShaderSourceInfo"/> instance to use.</param>
|
||||
/// <param name="hierarchyDepth">The depth of the hierarchy for this type (used to calculate the right indentation).</param>
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.</returns>
|
||||
public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth)
|
||||
/// <param name="info">The input <see cref="D2D1ShaderInfo"/> instance with gathered shader info.</param>
|
||||
/// <param name="writer">The <see cref="IndentedTextWriter"/> instance to write into.</param>
|
||||
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("/// <inheritdoc/>");
|
||||
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 = <HLSL_SOURCE>;
|
||||
// }
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,11 @@ namespace ComputeSharp.SourceGenerators;
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
public sealed partial class IShaderGenerator : IIncrementalGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of generator to include in the generated code.
|
||||
/// </summary>
|
||||
private const string GeneratorName = "ComputeSharp.IShaderGenerator";
|
||||
|
||||
/// <inheritdoc/>
|
||||
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<IndentedTextWriter.Callback<ShaderInfo>> 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<string>.Empty,
|
||||
memberCallbacks: declaredMembers.WrittenSpan);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", writer.ToString());
|
||||
});
|
||||
|
||||
// Get the LoadDispatchMetadata() info (hierarchy and dispatch metadata info)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする