Added source generator support for shared buffers

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-12 16:22:23 +01:00
1 親 80fd4d96e0
コミット 67d1d92cae
6ファイル変更
+53 -10

検索条件に一致するファイルはありません

+9 -1
ファイルの表示
@@ -25,13 +25,15 @@ namespace ComputeSharp.__Internals
/// <param name="executeMethod">The source code for the <see cref="IComputeShader.Execute"/> method.</param>
/// <param name="methods">The collection of processed methods.</param>
/// <param name="constants">The collection of discovered constants.</param>
/// <param name="sharedBuffers">The collection of group shared buffers</param>
public IComputeShaderSourceAttribute(
string shaderTypeName,
string[] types,
object[] args,
string executeMethod,
string[] methods,
object[] constants)
object[] constants,
object[] sharedBuffers)
{
ShaderTypeName = shaderTypeName;
Types = types;
@@ -39,6 +41,7 @@ namespace ComputeSharp.__Internals
ExecuteMethod = executeMethod;
Methods = methods;
Constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
SharedBuffers = sharedBuffers.Cast<object[]>().ToDictionary(static t => (string)t[0], static t => ((string)t[1], (int?)t[2]));
}
/// <summary>
@@ -71,6 +74,11 @@ namespace ComputeSharp.__Internals
/// </summary>
internal IReadOnlyDictionary<string, string> Constants { get; }
/// <summary>
/// Gets the collection of discovered constants.
/// </summary>
internal IReadOnlyDictionary<string, (string Type, int? Count)> SharedBuffers { get; }
/// <summary>
/// Gets the associated <see cref="IComputeShaderSourceAttribute"/> instance for a specified type.
/// </summary>
+1 -1
ファイルの表示
@@ -43,7 +43,7 @@ namespace ComputeSharp.SourceGenerators
var structModifiers = structDeclaration.Modifiers;
var fields = (
from fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>()
where !fieldSymbol.IsConst
where !fieldSymbol.IsConst && !fieldSymbol.IsStatic
let typeName = fieldSymbol.Type!.ToDisplayString()
let fieldFullType = ParseTypeName(typeName)
select (Type: fieldFullType, Identifier: Identifier(fieldSymbol.Name))).ToImmutableArray();
+32
ファイルの表示
@@ -57,5 +57,37 @@ namespace ComputeSharp.SourceGenerators.Helpers
ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).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", I1), ("K2", "V2", null) } => new object[] { new object[] { "K1", "V1", I1 }, new object[] { "K2", "V2", null } }
/// </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<(string A, string B, int? C)> groups)
{
return
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
.AddExpressions(groups.Select(static group =>
ArrayCreationExpression(
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
.AddExpressions(
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.A)),
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(group.B)),
group.C is int i
? LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(i))
: LiteralExpression(SyntaxKind.NullLiteralExpression)))).ToArray()));
}
}
}
+2
ファイルの表示
@@ -128,6 +128,8 @@ namespace ComputeSharp.SourceGenerators
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
string typeName = typeSymbol.GetFullMetadataName();
+2
ファイルの表示
@@ -120,6 +120,8 @@ namespace ComputeSharp.SourceGenerators
{
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
if (typeSymbol.TypeKind != TypeKind.Delegate) continue;
+7 -8
ファイルの表示
@@ -50,6 +50,7 @@ namespace ComputeSharp.SourceGenerators
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
@@ -66,7 +67,8 @@ namespace ComputeSharp.SourceGenerators
AttributeArgument(NestedArrayExpression(processedMembers)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
AttributeArgument(ArrayExpression(processedMethods)),
AttributeArgument(NestedArrayExpression(processedConstants)))))
AttributeArgument(NestedArrayExpression(processedConstants)),
AttributeArgument(NestedArrayExpression(sharedBuffers)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
.NormalizeWhitespace()
@@ -115,7 +117,7 @@ namespace ComputeSharp.SourceGenerators
/// <param name="types">The collection of currently discovered types.</param>
/// <returns>A sequence of captured members in <paramref name="structDeclarationSymbol"/>.</returns>
[Pure]
private static IEnumerable<IEnumerable<string>> GetGroupSharedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection<INamedTypeSymbol> types)
private static IEnumerable<(string Name, string Type, int? Count)> GetGroupSharedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection<INamedTypeSymbol> types)
{
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
@@ -129,12 +131,9 @@ namespace ComputeSharp.SourceGenerators
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
yield return new[]
{
mapping ?? fieldSymbol.Name,
typeName,
bufferSize?.ToString() ?? "-1"
};
yield return (mapping ?? fieldSymbol.Name, typeName, bufferSize);
types.Add((INamedTypeSymbol)typeSymbol.ElementType);
}
}