Code refactoring, minor optimizations to shader loading

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-30 22:59:24 +01:00
1 親 e57d7a9354
コミット b6b80141c2
5ファイル変更
+39 -44

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

+16 -9
ファイルの表示
@@ -20,15 +20,17 @@ namespace ComputeSharp
/// Creates a new <see cref="IComputeShaderSourceAttribute"/> instance with the specified parameters.
/// </summary>
/// <param name="shaderTypeName">The fully qualified name of the shader type.</param>
/// <param name="args">The mapped collection of shader fields.</param>
/// <param name="methods">The mapped collection of shader methods.</param>
/// <param name="types">The collection of custom types.</param>
public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods, string[] types)
/// <param name="args">The mapped collection of shader fields.</param>
/// <param name="executeMethod">The source code for the <see cref="IComputeShader.Execute"/> method.</param>
/// <param name="methods">The collection of processed methods.</param>
public IComputeShaderSourceAttribute(string shaderTypeName, string[] types, object[] args, string executeMethod, string[] methods)
{
ShaderTypeName = shaderTypeName;
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
Methods = methods.Cast<string[]>().ToDictionary(static method => method[0], static method => method[1]);
Types = types;
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
ExecuteMethod = executeMethod;
Methods = methods;
}
/// <summary>
@@ -36,20 +38,25 @@ namespace ComputeSharp
/// </summary>
internal string ShaderTypeName { get; }
/// <summary>
/// Gets the collection of processed custom types.
/// </summary>
internal IReadOnlyCollection<string> Types { get; }
/// <summary>
/// Gets the mapping of field names.
/// </summary>
internal IReadOnlyDictionary<string, (string Name, string Type)> Fields { get; }
/// <summary>
/// Gets the mapping of methods and their source code.
/// Gets the source code for the <see cref="IComputeShader.Execute"/> method.
/// </summary>
internal IReadOnlyDictionary<string, string> Methods { get; }
internal string ExecuteMethod { get; }
/// <summary>
/// Gets the collection of processed custom types.
/// Gets the collection of processed methods.
/// </summary>
internal IReadOnlyCollection<string> Types { get; }
internal IReadOnlyCollection<string> Methods { get; }
/// <summary>
/// Gets the associated <see cref="IComputeShaderSourceAttribute"/> instance for a specified type.
+2 -2
ファイルの表示
@@ -100,8 +100,8 @@ namespace ComputeSharp.Shaders.Renderer
}
}
// Local functions
foreach (var function in info.LocalFunctionsInfo)
// Captured methods
foreach (var function in info.MethodsInfo)
{
builder.AppendLine();
builder.AppendLine(function);
+2 -2
ファイルの表示
@@ -24,9 +24,9 @@ namespace ComputeSharp.Shaders.Translation
public IReadOnlyList<CapturedFieldInfo> FieldsInfo { get; }
/// <summary>
/// Gets the collection of <see cref="LocalFunctionInfo"/> items for the shader.
/// Gets the collection of methods to be used in the shader.
/// </summary>
public IReadOnlyCollection<string> LocalFunctionsInfo { get; }
public IReadOnlyCollection<string> MethodsInfo { get; }
/// <summary>
/// Gets the collection of declared types for the shader.
+3 -17
ファイルの表示
@@ -56,11 +56,6 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private readonly List<CapturedFieldInfo> fieldsInfo = new();
/// <summary>
/// The <see cref="List{T}"/> with the <see cref="FunctionInfo"/> items for the shader local functions.
/// </summary>
private readonly List<string> localFunctionsInfo = new();
/// <summary>
/// Creates a new <see cref="ShaderLoader{T}"/> instance.
/// </summary>
@@ -86,7 +81,7 @@ namespace ComputeSharp.Shaders.Translation
public IReadOnlyList<CapturedFieldInfo> FieldsInfo => this.fieldsInfo;
/// <inheritdoc/>
public IReadOnlyCollection<string> LocalFunctionsInfo => this.localFunctionsInfo;
public IReadOnlyCollection<string> MethodsInfo { get; private set; }
/// <inheritdoc/>
public IReadOnlyCollection<string> DeclaredTypes { get; private set; }
@@ -201,18 +196,9 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private void LoadMethodMetadata()
{
// Extract the method sources
foreach (var method in Attribute.Methods)
{
switch (method.Key)
{
case nameof(IComputeShader.Execute): EntryPoint = method.Value; break;
default: this.localFunctionsInfo.Add(method.Value); break;
}
}
// Extract the dependent types
DeclaredTypes = Attribute.Types;
EntryPoint = Attribute.ExecuteMethod;
MethodsInfo = Attribute.Methods;
}
}
}
+16 -14
ファイルの表示
@@ -75,11 +75,9 @@ namespace ComputeSharp.SourceGenerators
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var processedMethods = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
// Merge the static methods as well
processedMethods = processedMethods.Concat(staticMethods.Select(foo => new[] { "", foo.Value.ToFullString() })).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values.Select(static method => method.ToFullString())).ToArray();
// Create the compilation unit with the source attribute
var source =
@@ -87,9 +85,10 @@ namespace ComputeSharp.SourceGenerators
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))),
AttributeArgument(ArrayExpression(processedTypes)),
AttributeArgument(NestedArrayExpression(processedMembers)),
AttributeArgument(NestedArrayExpression(processedMethods)),
AttributeArgument(ArrayExpression(processedTypes)))))
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
AttributeArgument(ArrayExpression(processedMethods)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
.NormalizeWhitespace()
@@ -134,9 +133,9 @@ namespace ComputeSharp.SourceGenerators
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the type 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>
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>.</returns>
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
[Pure]
private static IEnumerable<IEnumerable<string>> GetProcessedMethods(
private static (string EntryPoint, IEnumerable<string> Methods) GetProcessedMethods(
StructDeclarationSyntax structDeclaration,
SemanticModel semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
@@ -148,6 +147,9 @@ namespace ComputeSharp.SourceGenerators
where syntaxNode.IsKind(SyntaxKind.MethodDeclaration)
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
string? entryPoint = null;
List<string> methods = new();
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
{
IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!;
@@ -166,19 +168,19 @@ namespace ComputeSharp.SourceGenerators
var parameterName = methodDeclarationSymbol.Parameters[0].Name;
processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!;
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
// Produce the final method source
var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString();
yield return new[] { methodDeclarationSymbol.Name, processedMethodSource };
else methods.Add(processedMethod.NormalizeWhitespace().ToFullString());
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
yield return new[] { localFunction.Key, localFunction.Value.NormalizeWhitespace().ToFullString() };
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
}
}
return (entryPoint!, methods);
}
/// <summary>