From 437d4a0688becd5ad1c9db18e568786743410846 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 13 Jan 2021 19:30:59 +0100 Subject: [PATCH] Added GroupIds class --- src/ComputeSharp.Core/Dispatch/GroupIds.cs | 210 ++++++++++++++++++ .../{Primitives => Dispatch}/ThreadIds.cs | 0 .../ComputeSharp.SourceGenerators.csproj | 3 +- .../Mappings/HlslKnownMembers.cs | 14 +- .../Mappings/HlslKnownTypes.cs | 2 +- .../SyntaxRewriters/ExecuteMethodRewriter.cs | 5 +- 6 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 src/ComputeSharp.Core/Dispatch/GroupIds.cs rename src/ComputeSharp.Core/{Primitives => Dispatch}/ThreadIds.cs (100%) diff --git a/src/ComputeSharp.Core/Dispatch/GroupIds.cs b/src/ComputeSharp.Core/Dispatch/GroupIds.cs new file mode 100644 index 00000000..c871eb06 --- /dev/null +++ b/src/ComputeSharp.Core/Dispatch/GroupIds.cs @@ -0,0 +1,210 @@ +using ComputeSharp.Exceptions; + +namespace ComputeSharp +{ + /// + /// A that indicates the ids of a given GPU thread running a compute shader within aa dispatch group. + /// + public static class GroupIds + { + /// + /// Gets the flattened index of the current thread with respect to the dispatch group. + /// + public static int Index => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(Index)}"); + + /// + /// Gets the X id of the current thread with respect to the dispatch group. + /// + public static int X => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(X)}"); + + /// + /// Gets the Y id of the current thread with respect to the dispatch group. + /// + public static int Y => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(Y)}"); + + /// + /// Gets the Z id of the current thread with respect to the dispatch group. + /// + public static int Z => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(Z)}"); + + /// + /// Gets an value with the value for all components. + /// + public static Int2 XX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XX)}"); + + /// + /// Gets or sets an value with the and values. + /// + public static Int2 XY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XY)}"); + + /// + /// Gets or sets an value with the and values. + /// + public static Int2 XZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XZ)}"); + + /// + /// Gets or sets an value with the and values. + /// + public static Int2 YX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YX)}"); + + /// + /// Gets an value with the value for all components. + /// + public static Int2 YY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YY)}"); + + /// + /// Gets or sets an value with the and values. + /// + public static Int2 YZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YZ)}"); + + /// + /// Gets or sets an value with the and values. + /// + public static Int2 ZX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZX)}"); + + /// + /// Gets or sets an value with the and values. + /// + public static Int2 ZY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZY)}"); + + /// + /// Gets an value with the value for all components. + /// + public static Int2 ZZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZZ)}"); + + /// + /// Gets an value with the value for all components. + /// + public static Int3 XXX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XXX)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 XXY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XXY)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 XXZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XXZ)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 XYX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XYX)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 XYY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XYY)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public static Int3 XYZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XYZ)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 XZX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XZX)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public static Int3 XZY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XZY)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 XZZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(XZZ)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 YXX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YXX)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 YXY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YXY)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public static Int3 YXZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YXZ)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 YYX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YYX)}"); + + /// + /// Gets an value with the value for all components. + /// + public static Int3 YYY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YYY)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 YYZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YYZ)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public static Int3 YZX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YZX)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 YZY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YZY)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 YZZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(YZZ)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 ZXX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZXX)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public static Int3 ZXY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZXY)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 ZXZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZXZ)}"); + + /// + /// Gets or sets an value with the , and values. + /// + public static Int3 ZYX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZYX)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 ZYY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZYY)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 ZYZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZYZ)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 ZZX => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZZX)}"); + + /// + /// Gets an value with the , and values. + /// + public static Int3 ZZY => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZZY)}"); + + /// + /// Gets an value with the value for all components. + /// + public static Int3 ZZZ => throw new InvalidExecutionContextException($"{nameof(GroupIds)}.{nameof(ZZZ)}"); + } +} diff --git a/src/ComputeSharp.Core/Primitives/ThreadIds.cs b/src/ComputeSharp.Core/Dispatch/ThreadIds.cs similarity index 100% rename from src/ComputeSharp.Core/Primitives/ThreadIds.cs rename to src/ComputeSharp.Core/Dispatch/ThreadIds.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 91506957..d007ccd6 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -18,6 +18,8 @@ + + @@ -38,7 +40,6 @@ - diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index ddd54ccd..ccf68a33 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -190,13 +190,25 @@ namespace ComputeSharp.SourceGenerators.Mappings knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}"); } + // Store GroupIds.Index for a quicker comparison afterwards + PropertyInfo groupindexProperty = typeof(GroupIds).GetProperty(nameof(GroupIds.Index), BindingFlags.Static | BindingFlags.Public); + // 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()}"); + if (item.Property == groupindexProperty) + { + // The thread group index is a standalone parameter, so if this property is used, we + // just map the access directly to that implicit and hidden parameter name instead. + knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"__{nameof(GroupIds)}__get_Index"); + } + else + { + 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 b01f4e15..041a3bb0 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -61,7 +61,7 @@ namespace ComputeSharp.SourceGenerators.Mappings /// public static IReadOnlyCollection HlslDispatchTypes { get; } = new[] { - typeof(ThreadIds) + typeof(ThreadIds), typeof(GroupIds) }; /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs index 70c4f77a..58908f46 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs @@ -22,7 +22,10 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters { var updatedNode = (ParameterListSyntax)base.VisitParameterList(node)!; - return updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID"))); + 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"))); } ///