Added forward declarations for shader methods
Fixes https://github.com/Sergio0694/ComputeSharp/issues/117
このコミットが含まれているのは:
1 親
979b89dba2
コミット
003a322912
8ファイル変更
+118
-16
検索条件に一致するファイルはありません
@@ -126,6 +126,22 @@ namespace ComputeSharp.SourceGenerators.Extensions
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="MethodDeclarationSyntax"/> as a method definition.
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
|
||||
/// <returns>A node like the one in input, but just as a definition.</returns>
|
||||
[Pure]
|
||||
public static MethodDeclarationSyntax AsDefinition(this MethodDeclarationSyntax node)
|
||||
{
|
||||
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
|
||||
{
|
||||
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
|
||||
}
|
||||
|
||||
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="LocalFunctionStatementSyntax"/> with a block body.
|
||||
/// </summary>
|
||||
@@ -155,6 +171,22 @@ namespace ComputeSharp.SourceGenerators.Extensions
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="LocalFunctionStatementSyntax"/> as a method definition.
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
|
||||
/// <returns>A node like the one in input, but just as a definition.</returns>
|
||||
[Pure]
|
||||
public static LocalFunctionStatementSyntax AsDefinition(this LocalFunctionStatementSyntax node)
|
||||
{
|
||||
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
|
||||
{
|
||||
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
|
||||
}
|
||||
|
||||
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no accessibility modifiers.
|
||||
/// </summary>
|
||||
|
||||
@@ -77,9 +77,8 @@ namespace ComputeSharp.SourceGenerators
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var processedMembers = GetProcessedFields(context, structDeclarationSymbol, discoveredTypes).ToArray();
|
||||
var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
|
||||
var (entryPoint, localFunctions) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
|
||||
var (entryPoint, processedMethods, forwardDeclarations) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
|
||||
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
|
||||
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
|
||||
var processedConstants = GetProcessedConstants(constantDefinitions);
|
||||
var staticFields = GetStaticFields(context, semanticModel, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
|
||||
|
||||
@@ -92,6 +91,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
AttributeArgument(TypeOfExpression(IdentifierName(structDeclarationSymbol.ToDisplayString()))),
|
||||
AttributeArgument(ArrayExpression(processedTypes)),
|
||||
AttributeArgument(NestedArrayExpression(processedMembers)),
|
||||
AttributeArgument(ArrayExpression(forwardDeclarations)),
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
|
||||
AttributeArgument(ArrayExpression(processedMethods)),
|
||||
AttributeArgument(NestedArrayExpression(processedConstants)),
|
||||
@@ -305,7 +305,7 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
|
||||
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
|
||||
[Pure]
|
||||
private static (string EntryPoint, IEnumerable<SyntaxNode> Methods) GetProcessedMethods(
|
||||
private static (string EntryPoint, IEnumerable<string> Methods, IEnumerable<string> Declarations) GetProcessedMethods(
|
||||
GeneratorExecutionContext context,
|
||||
StructDeclarationSyntax structDeclaration,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
@@ -321,7 +321,8 @@ namespace ComputeSharp.SourceGenerators
|
||||
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
|
||||
|
||||
string? entryPoint = null;
|
||||
List<SyntaxNode> methods = new();
|
||||
List<string> methods = new();
|
||||
List<string> declarations = new();
|
||||
|
||||
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
|
||||
{
|
||||
@@ -348,7 +349,8 @@ namespace ComputeSharp.SourceGenerators
|
||||
// Emit the extracted local functions first
|
||||
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
|
||||
{
|
||||
methods.Add(localFunction.Value);
|
||||
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
|
||||
declarations.Add(localFunction.Value.AsDefinition().NormalizeWhitespace().ToFullString());
|
||||
}
|
||||
|
||||
// If the method is the shader entry point, do additional processing
|
||||
@@ -358,10 +360,21 @@ namespace ComputeSharp.SourceGenerators
|
||||
|
||||
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
|
||||
}
|
||||
else methods.Add(processedMethod);
|
||||
else
|
||||
{
|
||||
methods.Add(processedMethod.NormalizeWhitespace().ToFullString());
|
||||
declarations.Add(processedMethod.AsDefinition().NormalizeWhitespace().ToFullString());
|
||||
}
|
||||
}
|
||||
|
||||
return (entryPoint!, methods);
|
||||
// Process static methods as well
|
||||
foreach (MethodDeclarationSyntax staticMethod in staticMethods.Values)
|
||||
{
|
||||
methods.Add(staticMethod.NormalizeWhitespace().ToFullString());
|
||||
declarations.Add(staticMethod.AsDefinition().NormalizeWhitespace().ToFullString());
|
||||
}
|
||||
|
||||
return (entryPoint!, methods, declarations);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -102,9 +102,9 @@ namespace ComputeSharp.SourceGenerators
|
||||
/// <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>
|
||||
/// <returns>A sequence of processed methods in <paramref name="methodDeclaration"/> (main method and local functions).</returns>
|
||||
[Pure]
|
||||
private static (string InvokeMethod, IEnumerable<string> Methods) GetProcessedMethods(
|
||||
private static (string TargetMethod, IEnumerable<string> LocalFunctions) GetProcessedMethods(
|
||||
GeneratorExecutionContext context,
|
||||
MethodDeclarationSyntax methodDeclaration,
|
||||
SemanticModelProvider semanticModel,
|
||||
@@ -115,22 +115,22 @@ namespace ComputeSharp.SourceGenerators
|
||||
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, context);
|
||||
|
||||
// Rewrite the method syntax tree
|
||||
var processedMethod = shaderSourceRewriter
|
||||
var targetMethod = shaderSourceRewriter
|
||||
.Visit(methodDeclaration)!
|
||||
.WithIdentifier(Identifier(ShaderMethodSourceAttribute.InvokeMethodIdentifier))
|
||||
.WithoutTrivia()
|
||||
.NormalizeWhitespace()
|
||||
.ToFullString();
|
||||
|
||||
List<string> methods = new(shaderSourceRewriter.LocalFunctions.Count);
|
||||
List<string> localFunctions = new(shaderSourceRewriter.LocalFunctions.Count);
|
||||
|
||||
// Emit the extracted local functions
|
||||
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
|
||||
{
|
||||
methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
|
||||
localFunctions.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
|
||||
}
|
||||
|
||||
return (processedMethod, methods);
|
||||
return (targetMethod, localFunctions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ namespace ComputeSharp.__Internals
|
||||
/// </summary>
|
||||
/// <param name="shaderType">The type of the current shader.</param>
|
||||
/// <param name="types">The collection of custom types.</param>
|
||||
/// <param name="args">The mapped collection of shader fields.</param>
|
||||
/// <param name="fields">The mapped collection of shader fields.</param>
|
||||
/// <param name="forwardDeclarations">The forward declarations for static methods.</param>
|
||||
/// <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="defines">The collection of discovered defines.</param>
|
||||
@@ -31,7 +32,8 @@ namespace ComputeSharp.__Internals
|
||||
public IComputeShaderSourceAttribute(
|
||||
Type shaderType,
|
||||
string[] types,
|
||||
object[] args,
|
||||
object[] fields,
|
||||
string[] forwardDeclarations,
|
||||
string executeMethod,
|
||||
string[] methods,
|
||||
object[] defines,
|
||||
@@ -40,7 +42,8 @@ namespace ComputeSharp.__Internals
|
||||
{
|
||||
ShaderType = shaderType;
|
||||
Types = types;
|
||||
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
|
||||
Fields = fields.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
|
||||
ForwardDeclarations = forwardDeclarations;
|
||||
ExecuteMethod = executeMethod;
|
||||
Methods = methods;
|
||||
Defines = defines.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
@@ -63,6 +66,11 @@ namespace ComputeSharp.__Internals
|
||||
/// </summary>
|
||||
internal IReadOnlyDictionary<string, (string Name, string Type)> Fields { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The forward declarations for static methods.
|
||||
/// </summary>
|
||||
internal IReadOnlyCollection<string> ForwardDeclarations { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the source code for the <see cref="IComputeShader.Execute"/> method.
|
||||
/// </summary>
|
||||
|
||||
@@ -155,6 +155,13 @@ namespace ComputeSharp.Shaders.Renderer
|
||||
builder.AppendLine("];");
|
||||
}
|
||||
|
||||
// Forward declarations
|
||||
foreach (var declaration in info.ForwardDeclarations)
|
||||
{
|
||||
builder.AppendLine();
|
||||
builder.AppendLine(declaration);
|
||||
}
|
||||
|
||||
// Captured methods
|
||||
foreach (var function in info.MethodsInfo)
|
||||
{
|
||||
|
||||
@@ -23,6 +23,11 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
IReadOnlyList<CapturedFieldInfo> FieldsInfo { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the forward declarations for static methods.
|
||||
/// </summary>
|
||||
IReadOnlyCollection<string> ForwardDeclarations { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of methods to be used in the shader.
|
||||
/// </summary>
|
||||
|
||||
@@ -104,6 +104,9 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <inheritdoc/>
|
||||
public IReadOnlyList<CapturedFieldInfo> FieldsInfo => this.fieldsInfo;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IReadOnlyCollection<string> ForwardDeclarations { get; private set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IReadOnlyCollection<string> MethodsInfo => this.methodsInfo;
|
||||
|
||||
@@ -145,6 +148,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
private void LoadMethodMetadata()
|
||||
{
|
||||
ForwardDeclarations = Attribute.ForwardDeclarations;
|
||||
EntryPoint = Attribute.ExecuteMethod;
|
||||
StaticFields = Attribute.StaticFields;
|
||||
SharedBuffers = Attribute.SharedBuffers;
|
||||
|
||||
@@ -120,5 +120,38 @@ namespace ComputeSharp.Tests
|
||||
buffer[ThreadIds.X] = ExternalStructType.Sum(type);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OutOfOrderMethods_Ok()
|
||||
{
|
||||
ReflectionServices.GetShaderInfo<OutOfOrderMethodsShader>(out var info);
|
||||
}
|
||||
|
||||
[AutoConstructor]
|
||||
public readonly partial struct OutOfOrderMethodsShader : IComputeShader
|
||||
{
|
||||
public readonly ReadWriteBuffer<float> buffer;
|
||||
|
||||
static int A() => B();
|
||||
|
||||
static int B() => 1 + C();
|
||||
|
||||
static int C() => 1;
|
||||
|
||||
int D() => A() + E() + F();
|
||||
|
||||
int E() => 1;
|
||||
|
||||
static int F() => 1;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
float value = buffer[ThreadIds.X];
|
||||
ExternalStructType type = ExternalStructType.New((int)value, Hlsl.Abs(value));
|
||||
|
||||
buffer[ThreadIds.X] = ExternalStructType.Sum(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする