Enabled discovered types generation in shaders

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-29 16:23:47 +01:00
1 親 ba135ef76d
コミット 1a8e653a39
6ファイル変更
+57 -17

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

+10 -3
ファイルの表示
@@ -20,13 +20,15 @@ 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="methodName">The name of the current method.</param>
/// <param name="source">The source code of the target method.</param>
public IComputeShaderSourceAttribute(string shaderTypeName, object[] args, object[] methods)
/// <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)
{
ShaderTypeName = shaderTypeName;
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => arg[1]);
Methods = methods.Cast<string[]>().ToDictionary(static method => method[0], static method => method[1]);
Types = types;
}
/// <summary>
@@ -44,6 +46,11 @@ namespace ComputeSharp
/// </summary>
internal IReadOnlyDictionary<string, string> Methods { get; }
/// <summary>
/// Gets the collection of processed custom types.
/// </summary>
internal IReadOnlyCollection<string> Types { get; }
/// <summary>
/// Gets the associated <see cref="IComputeShaderSourceAttribute"/> instance for a specified type.
/// </summary>
+9 -2
ファイルの表示
@@ -27,10 +27,17 @@ namespace ComputeSharp.Shaders.Renderer
builder.AppendLine("// AUTO GENERATED");
builder.AppendLine("// ================================================");
builder.AppendLine("// This shader was created by ComputeSharp.");
builder.AppendLine("// See: https://github.com/Sergio0694/ComputeSharp.");
builder.AppendLine();
builder.AppendLine("// See: https://github.com/Sergio0694/ComputeSharp.");
// Declared types
foreach (var type in info.DeclaredTypes)
{
builder.AppendLine();
builder.AppendLine(type);
}
// Captured variables
builder.AppendLine();
builder.AppendLine("cbuffer _ : register(b0)");
builder.AppendLine('{');
builder.AppendLine(" uint __x;");
+7 -2
ファイルの表示
@@ -47,11 +47,16 @@ namespace ComputeSharp.Shaders.Renderer.Models
/// <summary>
/// Gets or sets the list of static functions used by the shader.
/// </summary>
public IReadOnlyList<object> FunctionsList { get; init; } // TODO
public IReadOnlyCollection<object> FunctionsList { get; init; } // TODO
/// <summary>
/// Gets or sets the list of local functions used by the shader.
/// </summary>
public IReadOnlyList<string> LocalFunctionsList { get; init; }
public IReadOnlyCollection<string> LocalFunctionsList { get; init; }
/// <summary>
/// Gets or sets the list of dependent types for the shader.
/// </summary>
public IReadOnlyCollection<string> DeclaredTypes { get; init; }
}
}
+2 -1
ファイルの表示
@@ -178,7 +178,8 @@ namespace ComputeSharp.Shaders
ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName,
EntryPoint = shaderLoader.EntryPoint,
FunctionsList = shaderLoader.FunctionsInfo,
LocalFunctionsList = shaderLoader.LocalFunctionsInfo
LocalFunctionsList = shaderLoader.LocalFunctionsInfo,
DeclaredTypes = shaderLoader.DeclaredTypes
});
// Compile the loaded shader to HLSL bytecode
+13 -4
ファイルの表示
@@ -110,7 +110,12 @@ namespace ComputeSharp.Shaders.Translation
/// <summary>
/// Gets the collection of <see cref="LocalFunctionInfo"/> items for the shader.
/// </summary>
public IReadOnlyList<string> LocalFunctionsInfo => this.localFunctionsInfo;
public IReadOnlyCollection<string> LocalFunctionsInfo => this.localFunctionsInfo;
/// <summary>
/// Gets the collection of declared types for the shader.
/// </summary>
public IReadOnlyCollection<string> DeclaredTypes { get; private set; }
/// <summary>
/// Loads and processes an input<typeparamref name="T"/> shadeer
@@ -128,7 +133,7 @@ namespace ComputeSharp.Shaders.Translation
object box = shader;
@this.LoadFieldsInfo(box);
@this.LoadMethodSource();
@this.LoadMethodMetadata();
@this.BuildDispatchDataLoader();
return @this;
@@ -234,10 +239,11 @@ namespace ComputeSharp.Shaders.Translation
}
/// <summary>
/// Loads the entry method for the current shader being loaded
/// Loads the metadata info for the current shader.
/// </summary>
private void LoadMethodSource()
private void LoadMethodMetadata()
{
// Extract the method sources
foreach (var method in Attribute.Methods)
{
switch (method.Key)
@@ -246,6 +252,9 @@ namespace ComputeSharp.Shaders.Translation
default: this.localFunctionsInfo.Add(method.Value); break;
}
}
// Extract the dependent types
DeclaredTypes = Attribute.Types;
}
}
}
+16 -5
ファイルの表示
@@ -39,7 +39,7 @@ namespace ComputeSharp.SourceGenerators
INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!;
// Only process compute shader types
if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
// The list to use to track all discovered custom types
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
@@ -54,12 +54,22 @@ namespace ComputeSharp.SourceGenerators
ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
.WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
.AddExpressions(pairs.Select(pair =>
.AddExpressions(pairs.Select(static pair =>
ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Key)),
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Value))))).ToArray()));
}
// Helper that converts a sequence of strings into an array expression.
// That is, this applies the following transformation:
// - { "S1", "S2" } => new[] { "S1", "S2" }
static ImplicitArrayCreationExpressionSyntax ImplicitArrayExpression(IEnumerable<string> values)
{
return
ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
}
// Create the compilation unit with the source attribute
var source =
CompilationUnit().AddAttributeLists(
@@ -67,7 +77,8 @@ namespace ComputeSharp.SourceGenerators
Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))),
AttributeArgument(NestedPairsArrayExpression(GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray())),
AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())))))
AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())),
AttributeArgument(ImplicitArrayExpression(GetProcessedTypes(discoveredTypes))))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
.NormalizeWhitespace()
@@ -160,7 +171,7 @@ namespace ComputeSharp.SourceGenerators
/// </summary>
/// <param name="types">The sequence of discovered custom types.</param>
/// <returns>A sequence of custom type definitions to add to the shader source.</returns>
public static IEnumerable<MemberDeclarationSyntax> GetProcessedTypes(IEnumerable<INamedTypeSymbol> types)
public static IEnumerable<string> GetProcessedTypes(IEnumerable<INamedTypeSymbol> types)
{
foreach (var type in HlslKnownTypes.GetCustomTypes(types))
{
@@ -187,7 +198,7 @@ namespace ComputeSharp.SourceGenerators
currentNamespace = currentNamespace.ContainingNamespace;
}
yield return memberDeclaration;
yield return memberDeclaration.NormalizeWhitespace().ToFullString();
}
}
}