Optimized shader dispatch parameter declarations

このコミットが含まれているのは:
Sergio Pedri committed 2021-01-13 20:05:09 +01:00
1 親 073cfa64ab
コミット 602ca37516
3ファイル変更
+61 -8

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

+1 -1
ファイルの表示
@@ -179,7 +179,7 @@ namespace ComputeSharp.SourceGenerators
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0)
{
processedMethod = new ExecuteMethodRewriter().Visit(processedMethod)!;
processedMethod = new ExecuteMethodRewriter(shaderSourceRewriter).Visit(processedMethod)!;
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
+32 -5
ファイルの表示
@@ -10,6 +10,20 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// </summary>
internal sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter
{
/// <summary>
/// The <see cref="ShaderSourceRewriter"/> instance used to process the input tree.
/// </summary>
private readonly ShaderSourceRewriter shaderSourceRewriter;
/// <summary>
/// Creates a new <see cref="ExecuteMethodRewriter"/> instance with the specified parameters.
/// </summary>
/// <param name="shaderSourceRewriter">The <see cref="ShaderSourceRewriter"/> instance used to process the input tree.</param>
public ExecuteMethodRewriter(ShaderSourceRewriter shaderSourceRewriter)
{
this.shaderSourceRewriter = shaderSourceRewriter;
}
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
public TNode? Visit<TNode>(TNode? node)
where TNode : SyntaxNode
@@ -22,11 +36,24 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = (ParameterListSyntax)base.VisitParameterList(node)!;
return updatedNode.AddParameters(
Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID")),
Parameter(Identifier($"uint3 {nameof(GroupIds)} : SV_GroupThreadID")),
Parameter(Identifier($"uint __{nameof(GroupIds)}__get_Index : SV_GroupIndex")),
Parameter(Identifier($"uint3 {nameof(WarpIds)} : SV_GroupID")));
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID")));
if (this.shaderSourceRewriter.IsGroupIdsUsed)
{
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(GroupIds)} : SV_GroupThreadID")));
}
if (this.shaderSourceRewriter.IsGroupIdsIndexUsed)
{
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint __{nameof(GroupIds)}__get_Index : SV_GroupIndex")));
}
if (this.shaderSourceRewriter.IsWarpIdsUsed)
{
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(WarpIds)} : SV_GroupID")));
}
return updatedNode;
}
/// <inheritdoc/>
+28 -2
ファイルの表示
@@ -106,6 +106,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
/// </summary>
public IReadOnlyDictionary<string, LocalFunctionStatementSyntax> LocalFunctions => this.localFunctions;
/// <summary>
/// Gets whether or not the shader uses <see cref="GroupIds"/> at least once (except <see cref="GroupIds.Index"/>).
/// </summary>
public bool IsGroupIdsUsed { get; private set; }
/// <summary>
/// Gets whether or not the shader uses <see cref="GroupIds.Index/> at least once.
/// </summary>
public bool IsGroupIdsIndexUsed { get; private set; }
/// <summary>
/// Gets whether or not the shader uses <see cref="WarpIds"/> at least once.
/// </summary>
public bool IsWarpIdsUsed { get; set; }
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/>
public MethodDeclarationSyntax? Visit(MethodDeclarationSyntax? node)
{
@@ -307,11 +322,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// to see if the member should be rewritten for HLSL compliance, and rewrite if needed.
if (HlslKnownMembers.TryGetMappedName(operation.Member.ToDisplayString(), out string? mapping))
{
// Mark which dispatch properties have been used, to optimize the declaration afterwards
if (operation.Member.IsStatic)
{
string typeName = operation.Member.ContainingType.GetFullMetadataName();
if (mapping == $"__{nameof(GroupIds)}__get_Index") IsGroupIdsIndexUsed = true;
else if (typeName == typeof(GroupIds).FullName) IsGroupIdsUsed = true;
else if (typeName == typeof(WarpIds).FullName) IsWarpIdsUsed = true;
}
// Static and instance members are handled differently, with static members being
// converted into a literal expression, and instance getting an updated identifier.
// For instance, consider these two cases:
// For instance, consider these three cases:
// - Vector3.One (static) => float3(1.0f, 1.0f, 1.0f)
// - ThredIds.X (instance) => [arg].x
// - ThreadIds.X (custom) => ThreadIds.x
// - uint3.X (instance) => [arg].x
return operation.Member.IsStatic switch
{
true => ParseExpression(mapping!),