From c6aa194749b7372e446c4851425b2b22bfddb5b8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 13 Jan 2021 19:07:53 +0100 Subject: [PATCH] Switched ThreadIds to static type --- .../Interfaces/IComputeShader.cs | 3 +- src/ComputeSharp.Core/Primitives/ThreadIds.cs | 82 +++++++++---------- .../IComputeShaderSourceGenerator.cs | 9 +- .../Mappings/HlslKnownMembers.cs | 9 ++ .../Mappings/HlslKnownTypes.cs | 16 ++-- .../SyntaxRewriters/ExecuteMethodRewriter.cs | 32 ++------ 6 files changed, 73 insertions(+), 78 deletions(-) diff --git a/src/ComputeSharp.Core/Interfaces/IComputeShader.cs b/src/ComputeSharp.Core/Interfaces/IComputeShader.cs index 66f145dd..37f5396d 100644 --- a/src/ComputeSharp.Core/Interfaces/IComputeShader.cs +++ b/src/ComputeSharp.Core/Interfaces/IComputeShader.cs @@ -8,7 +8,6 @@ /// /// Executes the current compute shader. /// - /// The value with the current execution indices. - void Execute(ThreadIds ids); + void Execute(); } } diff --git a/src/ComputeSharp.Core/Primitives/ThreadIds.cs b/src/ComputeSharp.Core/Primitives/ThreadIds.cs index 69f58032..175d5f5a 100644 --- a/src/ComputeSharp.Core/Primitives/ThreadIds.cs +++ b/src/ComputeSharp.Core/Primitives/ThreadIds.cs @@ -3,203 +3,203 @@ namespace ComputeSharp { /// - /// A that indicates the ids of a given GPU thread running a compute shader. + /// A that indicates the ids of a given GPU thread running a compute shader. /// - public readonly ref struct ThreadIds + public static class ThreadIds { /// /// Gets the X id of the current thread. /// - public int X => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(X)}"); + public static int X => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(X)}"); /// /// Gets the Y id of the current thread. /// - public int Y => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Y)}"); + public static int Y => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Y)}"); /// /// Gets the Z id of the current thread. /// - public int Z => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Z)}"); + public static int Z => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(Z)}"); /// /// Gets an value with the value for all components. /// - public Int2 XX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XX)}"); + public static Int2 XX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XX)}"); /// /// Gets or sets an value with the and values. /// - public Int2 XY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XY)}"); + public static Int2 XY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XY)}"); /// /// Gets or sets an value with the and values. /// - public Int2 XZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZ)}"); + public static Int2 XZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZ)}"); /// /// Gets or sets an value with the and values. /// - public Int2 YX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YX)}"); + public static Int2 YX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YX)}"); /// /// Gets an value with the value for all components. /// - public Int2 YY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YY)}"); + public static Int2 YY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YY)}"); /// /// Gets or sets an value with the and values. /// - public Int2 YZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZ)}"); + public static Int2 YZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZ)}"); /// /// Gets or sets an value with the and values. /// - public Int2 ZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZX)}"); + public static Int2 ZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZX)}"); /// /// Gets or sets an value with the and values. /// - public Int2 ZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZY)}"); + public static Int2 ZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZY)}"); /// /// Gets an value with the value for all components. /// - public Int2 ZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZ)}"); + public static Int2 ZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZ)}"); /// /// Gets an value with the value for all components. /// - public Int3 XXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXX)}"); + public static Int3 XXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXX)}"); /// /// Gets an value with the , and values. /// - public Int3 XXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXY)}"); + public static Int3 XXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXY)}"); /// /// Gets an value with the , and values. /// - public Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXZ)}"); + public static Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XXZ)}"); /// /// Gets an value with the , and values. /// - public Int3 XYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYX)}"); + public static Int3 XYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYX)}"); /// /// Gets an value with the , and values. /// - public Int3 XYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYY)}"); + public static Int3 XYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYY)}"); /// /// Gets or sets an value with the , and values. /// - public Int3 XYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYZ)}"); + public static Int3 XYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XYZ)}"); /// /// Gets an value with the , and values. /// - public Int3 XZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZX)}"); + public static Int3 XZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZX)}"); /// /// Gets or sets an value with the , and values. /// - public Int3 XZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZY)}"); + public static Int3 XZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZY)}"); /// /// Gets an value with the , and values. /// - public Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZZ)}"); + public static Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(XZZ)}"); /// /// Gets an value with the , and values. /// - public Int3 YXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXX)}"); + public static Int3 YXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXX)}"); /// /// Gets an value with the , and values. /// - public Int3 YXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXY)}"); + public static Int3 YXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXY)}"); /// /// Gets or sets an value with the , and values. /// - public Int3 YXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXZ)}"); + public static Int3 YXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YXZ)}"); /// /// Gets an value with the , and values. /// - public Int3 YYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYX)}"); + public static Int3 YYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYX)}"); /// /// Gets an value with the value for all components. /// - public Int3 YYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYY)}"); + public static Int3 YYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYY)}"); /// /// Gets an value with the , and values. /// - public Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYZ)}"); + public static Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YYZ)}"); /// /// Gets or sets an value with the , and values. /// - public Int3 YZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZX)}"); + public static Int3 YZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZX)}"); /// /// Gets an value with the , and values. /// - public Int3 YZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZY)}"); + public static Int3 YZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZY)}"); /// /// Gets an value with the , and values. /// - public Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZZ)}"); + public static Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(YZZ)}"); /// /// Gets an value with the , and values. /// - public Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXX)}"); + public static Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXX)}"); /// /// Gets or sets an value with the , and values. /// - public Int3 ZXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXY)}"); + public static Int3 ZXY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXY)}"); /// /// Gets an value with the , and values. /// - public Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXZ)}"); + public static Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZXZ)}"); /// /// Gets or sets an value with the , and values. /// - public Int3 ZYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYX)}"); + public static Int3 ZYX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYX)}"); /// /// Gets an value with the , and values. /// - public Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYY)}"); + public static Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYY)}"); /// /// Gets an value with the , and values. /// - public Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYZ)}"); + public static Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZYZ)}"); /// /// Gets an value with the , and values. /// - public Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZX)}"); + public static Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZX)}"); /// /// Gets an value with the , and values. /// - public Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZY)}"); + public static Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZY)}"); /// /// Gets an value with the value for all components. /// - public Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZZ)}"); + public static Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(ThreadIds)}.{nameof(ZZZ)}"); } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index 93f2d6a7..7e5787bc 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -171,18 +171,15 @@ namespace ComputeSharp.SourceGenerators ShaderSourceRewriter shaderSourceRewriter = new(structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions); // Rewrite the method syntax tree - var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); + MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); // If the method is the shader entry point, do additional processing if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) && methodDeclarationSymbol.ReturnsVoid && methodDeclarationSymbol.TypeParameters.Length == 0 && - methodDeclarationSymbol.Parameters.Length == 1 && - methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == typeof(ThreadIds).FullName) + methodDeclarationSymbol.Parameters.Length == 0) { - var parameterName = methodDeclarationSymbol.Parameters[0].Name; - - processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!; + processedMethod = new ExecuteMethodRewriter().Visit(processedMethod)!; entryPoint = processedMethod.NormalizeWhitespace().ToFullString(); } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index c2c58e37..ddd54ccd 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -190,6 +190,15 @@ namespace ComputeSharp.SourceGenerators.Mappings knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}"); } + // Programmatically load mappings for the dispatch types + foreach (var item in + from type in HlslKnownTypes.HlslDispatchTypes + from property in type.GetProperties(BindingFlags.Static | BindingFlags.Public) + select (Type: type, Property: property)) + { + knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Type.Name}.{item.Property.Name.ToLower()}"); + } + return knownMembers; } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 04ed2673..b01f4e15 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -41,8 +41,7 @@ namespace ComputeSharp.SourceGenerators.Mappings [typeof(double).FullName] = "double", [typeof(Double2).FullName] = "double2", [typeof(Double3).FullName] = "double3", - [typeof(Double4).FullName] = "double4", - [typeof(ThreadIds).FullName] = "int3" + [typeof(Double4).FullName] = "double4" }; /// @@ -50,7 +49,6 @@ namespace ComputeSharp.SourceGenerators.Mappings /// public static IReadOnlyCollection HlslMappedVectorTypes { get; } = new[] { - typeof(ThreadIds), typeof(Bool2), typeof(Bool3), typeof(Bool4), typeof(Int2), typeof(Int3), typeof(Int4), typeof(UInt2), typeof(UInt3), typeof(UInt4), @@ -58,6 +56,14 @@ namespace ComputeSharp.SourceGenerators.Mappings typeof(Double2), typeof(Double3), typeof(Double4) }; + /// + /// Gets the known HLSL dispatch types. + /// + public static IReadOnlyCollection HlslDispatchTypes { get; } = new[] + { + typeof(ThreadIds) + }; + /// /// Checks whether or not a given type name matches a typed resource type. /// @@ -94,9 +100,7 @@ namespace ComputeSharp.SourceGenerators.Mappings /// Whether or not represents a typed resource type. public static bool IsScalarOrVectorType(string typeName) { - return - KnownTypes.ContainsKey(typeName) && - typeName != typeof(ThreadIds).FullName; + return KnownTypes.ContainsKey(typeName); } /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs index 63245d3b..70c4f77a 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs @@ -10,20 +10,6 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters /// internal sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter { - /// - /// The identifier name for the input parameter. - /// - private readonly string threadIdsIdentifier; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The identifier name for the input parameter. - public ExecuteMethodRewriter(string threadIdsIdentifier) - { - this.threadIdsIdentifier = threadIdsIdentifier; - } - /// public TNode? Visit(TNode? node) where TNode : SyntaxNode @@ -32,11 +18,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters } /// - public override SyntaxNode VisitParameter(ParameterSyntax node) + public override SyntaxNode? VisitParameterList(ParameterListSyntax node) { - var updatedNode = (ParameterSyntax)base.VisitParameter(node)!; + var updatedNode = (ParameterListSyntax)base.VisitParameterList(node)!; - return updatedNode.WithIdentifier(Identifier($"{updatedNode.Identifier.Text} : SV_DispatchThreadId")); + return updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID"))); } /// @@ -48,9 +34,9 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters // check to ensure that invocation outside of the requested range are discarded. // The following snippet creates this prologue before the user provided body: // - // if (ids.X < __x && - // ids.Y < __y && - // ids.Z < __z) + // if (ThreadIds.x < __x && + // ThreadIds.y < __y && + // ThreadIds.z < __z) // { // // } @@ -60,19 +46,19 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters BinaryExpression(SyntaxKind.LessThanExpression, MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(this.threadIdsIdentifier), + IdentifierName(nameof(ThreadIds)), IdentifierName("x")), IdentifierName("__x")), BinaryExpression(SyntaxKind.LessThanExpression, MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(this.threadIdsIdentifier), + IdentifierName(nameof(ThreadIds)), IdentifierName("y")), IdentifierName("__y"))), BinaryExpression(SyntaxKind.LessThanExpression, MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(this.threadIdsIdentifier), + IdentifierName(nameof(ThreadIds)), IdentifierName("z")), IdentifierName("__z")));