Implemented constant support for methods, code refactoring
このコミットが含まれているのは:
1 親
4b5c422bc3
コミット
075c7cdf9a
5ファイル変更
+88
-49
検索条件に一致するファイルはありません
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Exceptions;
|
||||
@@ -35,13 +36,15 @@ namespace ComputeSharp.__Internals
|
||||
/// <param name="types">The collection of custom types.</param>
|
||||
/// <param name="invokeMethod">The source code for the target entry point method.</param>
|
||||
/// <param name="methods">The collection of processed methods.</param>
|
||||
public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods)
|
||||
/// <param name="constants">The collection of discovered constants.</param>
|
||||
public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods, object[] constants)
|
||||
{
|
||||
this.invokeMethod = invokeMethod;
|
||||
|
||||
MethodName = methodName;
|
||||
Types = types;
|
||||
Methods = methods;
|
||||
Constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -59,6 +62,11 @@ namespace ComputeSharp.__Internals
|
||||
/// </summary>
|
||||
internal IReadOnlyCollection<string> Methods { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of discovered constants.
|
||||
/// </summary>
|
||||
internal IReadOnlyDictionary<string, string> Constants { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped source code for the current method.
|
||||
/// </summary>
|
||||
|
||||
@@ -132,14 +132,13 @@ namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
EntryPoint = Attribute.ExecuteMethod;
|
||||
|
||||
this.declaredTypes.AddRange(Attribute.Types);
|
||||
this.methodsInfo.AddRange(Attribute.Methods);
|
||||
|
||||
foreach (var pair in Attribute.Constants)
|
||||
{
|
||||
this.constantsInfo.Add(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
this.declaredTypes.AddRange(Attribute.Types);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -223,6 +222,11 @@ namespace ComputeSharp.Shaders.Translation
|
||||
this.declaredTypes.AddRange(methodSource.Types);
|
||||
this.methodsInfo.Add(methodSource.GetMappedInvokeMethod(hlslName));
|
||||
this.methodsInfo.AddRange(methodSource.Methods);
|
||||
|
||||
foreach (var pair in methodSource.Constants)
|
||||
{
|
||||
this.constantsInfo[pair.Key] = pair.Value;
|
||||
}
|
||||
}
|
||||
else ThrowHelper.ThrowArgumentException("Invalid captured variable");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// A class containing additional factory methods for constructing syntax nodes, tokens and trivia.
|
||||
/// </summary>
|
||||
internal static class SyntaxFactoryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="string"/>[] array expression from the given sequence of <see cref="string"/> instances.
|
||||
/// <para>
|
||||
/// That it, it applies the following transformation:
|
||||
/// <code>
|
||||
/// { "S1", "S2" } => new string[] { "S1", "S2" }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="values">The input sequence of <see cref="string"/> instances.</param>
|
||||
/// <returns>An <see cref="ArrayCreationExpression"/> instance with the described contents.</returns>
|
||||
[Pure]
|
||||
public static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable<string> values)
|
||||
{
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="object"/>[] array expression with the nested groups in the input sequence.
|
||||
/// <para>
|
||||
/// That it, it applies the following transformation:
|
||||
/// <code>
|
||||
/// { ("K1", "V1"), ("K2", "V2") } => new object[] { new[] { "K1", "V1" }, new[] { "K2", "V2" } }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="values">The input sequence of <see cref="string"/> groups.</param>
|
||||
/// <returns>An <see cref="ArrayCreationExpression"/> instance with the described contents.</returns>
|
||||
[Pure]
|
||||
public static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable<IEnumerable<string>> groups)
|
||||
{
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(groups.Select(static group =>
|
||||
ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
|
||||
group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
using static ComputeSharp.SourceGenerators.Helpers.SyntaxFactoryHelper;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
@@ -47,34 +48,6 @@ namespace ComputeSharp.SourceGenerators
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Helper that converts a sequence of string sequences into a nested array expression.
|
||||
// That is, this applies the following transformation:
|
||||
// - { ("K1", "V1"), ("K2", "V2") } => new object[] { new[] { "K1", "V1" }, new[] { "K2", "V2" } }
|
||||
static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable<IEnumerable<string>> groups)
|
||||
{
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(groups.Select(static group =>
|
||||
ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
|
||||
group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).ToArray()));
|
||||
}
|
||||
|
||||
// Helper that converts a sequence of strings into an array expression.
|
||||
// That is, this applies the following transformation:
|
||||
// - { "S1", "S2" } => new string[] { "S1", "S2" }
|
||||
static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable<string> values)
|
||||
{
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
|
||||
}
|
||||
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
|
||||
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
|
||||
@@ -200,7 +173,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <returns>A sequence of discovered constants to declare in the shader.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<IEnumerable<string>> GetProcessedConstants(IReadOnlyDictionary<IFieldSymbol, string> constantDefinitions)
|
||||
internal static IEnumerable<IEnumerable<string>> GetProcessedConstants(IReadOnlyDictionary<IFieldSymbol, string> constantDefinitions)
|
||||
{
|
||||
foreach (var constant in constantDefinitions)
|
||||
{
|
||||
|
||||
@@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
using static ComputeSharp.SourceGenerators.Helpers.SyntaxFactoryHelper;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
@@ -44,23 +45,12 @@ namespace ComputeSharp.SourceGenerators
|
||||
// We need to sets to track all discovered custom types and static methods
|
||||
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods = new(SymbolEqualityComparer.Default);
|
||||
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods);
|
||||
var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
|
||||
var processedTypes = IComputeShaderSourceGenerator.GetProcessedTypes(discoveredTypes).ToArray();
|
||||
|
||||
// Helper that converts a sequence of strings into an array expression.
|
||||
// That is, this applies the following transformation:
|
||||
// - { "S1", "S2" } => new string[] { "S1", "S2" }
|
||||
static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable<string> values)
|
||||
{
|
||||
return
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
|
||||
.AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
|
||||
}
|
||||
var processedConstants = IComputeShaderSourceGenerator.GetProcessedConstants(constantDefinitions);
|
||||
|
||||
// Create the compilation unit with the source attribute
|
||||
var source =
|
||||
@@ -70,7 +60,8 @@ namespace ComputeSharp.SourceGenerators
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))),
|
||||
AttributeArgument(ArrayExpression(processedTypes)),
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))),
|
||||
AttributeArgument(ArrayExpression(processedMethods)))))
|
||||
AttributeArgument(ArrayExpression(processedMethods)),
|
||||
AttributeArgument(NestedArrayExpression(processedConstants)))))
|
||||
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
|
||||
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
|
||||
.NormalizeWhitespace()
|
||||
@@ -88,15 +79,17 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the method to process.</param>
|
||||
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
|
||||
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/>.</returns>
|
||||
[Pure]
|
||||
private static (string InvokeMethod, IEnumerable<string> Methods) GetProcessedMethods(
|
||||
MethodDeclarationSyntax methodDeclaration,
|
||||
SemanticModel semanticModel,
|
||||
ICollection<INamedTypeSymbol> discoveredTypes,
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods)
|
||||
IDictionary<IMethodSymbol, MethodDeclarationSyntax> staticMethods,
|
||||
IDictionary<IFieldSymbol, string> constantDefinitions)
|
||||
{
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, null); // TODO: implement constant support
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions);
|
||||
|
||||
// Rewrite the method syntax tree
|
||||
var processedMethod = shaderSourceRewriter
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする