From 1365f963e9080df7636377128fb0806043207ace Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 13:36:59 +0100 Subject: [PATCH 1/9] Added DispatchSize type --- .../ComputeSharp.SourceGenerators.csproj | 3 +- .../Mappings/HlslKnownMembers.cs | 22 ++ .../Core/Dispatch/DispatchSize.cs | 211 ++++++++++++++++++ tests/ComputeSharp.Tests/DispatchTests.cs | 37 +++ 4 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp/Core/Dispatch/DispatchSize.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index b09d6663..f028f792 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -17,10 +17,11 @@ + + - diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 24da01ea..1274cf16 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -249,6 +249,28 @@ namespace ComputeSharp.SourceGenerators.Mappings } } + // Programmatically load mappings for the dispatch size + foreach (var property in typeof(DispatchSize).GetProperties(BindingFlags.Static | BindingFlags.Public)) + { + string key = $"{typeof(DispatchSize).FullName}{Type.Delimiter}{property.Name}"; + + switch (property.Name) + { + case nameof(DispatchSize.Count): + knownMembers.Add(key, "__x * __y * __z"); + break; + case string name when name.Length == 1: + knownMembers.Add(key, $"__{char.ToLower(name[0])}"); + break; + case string name when name.Length == 2: + knownMembers.Add(key, $"int2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})"); + break; + case string name when name.Length == 3: + knownMembers.Add(key, $"int3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})"); + break; + } + } + return knownMembers; } diff --git a/src/ComputeSharp/Core/Dispatch/DispatchSize.cs b/src/ComputeSharp/Core/Dispatch/DispatchSize.cs new file mode 100644 index 00000000..d2f19ee3 --- /dev/null +++ b/src/ComputeSharp/Core/Dispatch/DispatchSize.cs @@ -0,0 +1,211 @@ +using ComputeSharp.Exceptions; + +namespace ComputeSharp +{ + /// + /// A that indicates the size of the current shader dispatch being executed. + /// That is, it enables a shader to access info on the targeted number of invocations along each axis. + /// + public static class DispatchSize + { + /// + /// Gets the total number of dispatch invocations being executed. + /// + public static int Count => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(Count)}"); + + /// + /// Gets the number of dispatch invocations being executed on the X axis. + /// + public static int X => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(X)}"); + + /// + /// Gets the number of dispatch invocations being executed on the Y axis. + /// + public static int Y => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(Y)}"); + + /// + /// Gets the number of dispatch invocations being executed on the Z axis. + /// + public static int Z => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(Z)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 XX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XX)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 XY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XY)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 XZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZ)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 YX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YX)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 YY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YY)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 YZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZ)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 ZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZX)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 ZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZY)}"); + + /// + /// Gets a value with the components , . + /// + public static Int2 ZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XXX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XXX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XXY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XXY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XXZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XXZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XYX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XYX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XYY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XYY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XYZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XYZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 XZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YXX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YXX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YXY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YXY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YXZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YXZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YYX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YYX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YYY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YYY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YYZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YYZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 YZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZXX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZXX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZXY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZXY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZXZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZXZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZYX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZYX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZYY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZYY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZYZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZYZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Int3 ZZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZZ)}"); + } +} diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index 030adbbd..e0fca485 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -1,4 +1,5 @@ using System; +using ComputeSharp.Interop; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -180,5 +181,41 @@ namespace ComputeSharp.Tests buffer[ThreadIds.X] = GridIds.X; } } + + [CombinatorialTestMethod] + [AllDevices] + public void Verify_DispatchSize(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(6); + + device.Get().For(11, 22, 3, new DispatchSizeShader(buffer)); + + ReflectionServices.GetShaderInfo(out var info); + + int[] data = buffer.ToArray(); + + Assert.AreEqual(data[0], 11 * 22 * 3); + Assert.AreEqual(data[1], 11); + Assert.AreEqual(data[2], 22); + Assert.AreEqual(data[3], 3); + Assert.AreEqual(data[4], 11 + 22); + Assert.AreEqual(data[5], 11 + 22 + 3); + } + + [AutoConstructor] + internal readonly partial struct DispatchSizeShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public void Execute() + { + buffer[0] = DispatchSize.Count; + buffer[1] = DispatchSize.X; + buffer[2] = DispatchSize.Y; + buffer[3] = DispatchSize.Z; + buffer[4] = (int)Hlsl.Dot(DispatchSize.XY, Float2.One); + buffer[5] = (int)Hlsl.Dot(DispatchSize.XYZ, Float3.One); + } + } } } From 1b8b139c7aef3a6c8a527b77882255e40b8bd57a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 13:42:39 +0100 Subject: [PATCH 2/9] Fixed transformation for GroupSize vector properties --- .../Mappings/HlslKnownMembers.cs | 8 ++++---- tests/ComputeSharp.Tests/DispatchTests.cs | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 1274cf16..727ac2c8 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -240,11 +240,11 @@ namespace ComputeSharp.SourceGenerators.Mappings case string name when name.Length == 1: knownMembers.Add(key, $"__GroupSize__get_{name}"); break; - case string name when name.Length == 1: - knownMembers.Add(key, $"__GroupSize__get_{name[0]} * __GroupSize__get_{name[1]}"); + case string name when name.Length == 2: + knownMembers.Add(key, $"int2(__GroupSize__get_{name[0]}, __GroupSize__get_{name[1]})"); break; - case string name when name.Length == 1: - knownMembers.Add(key, $"__GroupSize__get_{name[0]} * __GroupSize__get_{name[1]} * __GroupSize__get_{name[2]}"); + case string name when name.Length == 3: + knownMembers.Add(key, $"int3(__GroupSize__get_{name[0]}, __GroupSize__get_{name[1]}, __GroupSize__get_{name[2]})"); break; } } diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index e0fca485..e6cd8194 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -139,6 +139,8 @@ namespace ComputeSharp.Tests Assert.AreEqual(15, data[1]); Assert.AreEqual(7, data[2]); Assert.AreEqual(4 * 15 * 7, data[3]); + Assert.AreEqual(4 + 15, data[4]); + Assert.AreEqual(4 + 15 + 7, data[5]); } [AutoConstructor] @@ -152,6 +154,8 @@ namespace ComputeSharp.Tests buffer[1] = GroupSize.Y; buffer[2] = GroupSize.Z; buffer[3] = GroupSize.Count; + buffer[4] = (int)Hlsl.Dot(GroupSize.XY, Float2.One); + buffer[5] = (int)Hlsl.Dot(GroupSize.XYZ, Float3.One); } } From b7ab492ff1ced8f49f2e290170bfddefbd08792d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 13:47:33 +0100 Subject: [PATCH 3/9] Minor XML docs improvements --- .../Core/Dispatch/DispatchSize.cs | 72 +++++++++--------- src/ComputeSharp/Core/Dispatch/GridIds.cs | 73 +++++++++--------- src/ComputeSharp/Core/Dispatch/GroupIds.cs | 73 +++++++++--------- src/ComputeSharp/Core/Dispatch/GroupSize.cs | 75 ++++++++++--------- src/ComputeSharp/Core/Dispatch/ThreadIds.cs | 73 +++++++++--------- 5 files changed, 185 insertions(+), 181 deletions(-) diff --git a/src/ComputeSharp/Core/Dispatch/DispatchSize.cs b/src/ComputeSharp/Core/Dispatch/DispatchSize.cs index d2f19ee3..d29c13cf 100644 --- a/src/ComputeSharp/Core/Dispatch/DispatchSize.cs +++ b/src/ComputeSharp/Core/Dispatch/DispatchSize.cs @@ -29,182 +29,182 @@ namespace ComputeSharp public static int Z => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(Z)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(XZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(YZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZX => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZY => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZZ => throw new InvalidExecutionContextException($"{typeof(DispatchSize)}.{nameof(ZZZ)}"); } diff --git a/src/ComputeSharp/Core/Dispatch/GridIds.cs b/src/ComputeSharp/Core/Dispatch/GridIds.cs index 5e6f82e5..d2439ae9 100644 --- a/src/ComputeSharp/Core/Dispatch/GridIds.cs +++ b/src/ComputeSharp/Core/Dispatch/GridIds.cs @@ -4,6 +4,7 @@ namespace ComputeSharp { /// /// A that indicates the ids of the current thread group within the dispatch grid. + /// That is, it enables a shader to access info on the index of the current thread group with respect to the dispatch grid. /// public static class GridIds { @@ -23,182 +24,182 @@ namespace ComputeSharp public static int Z => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(Z)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(XZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(YZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZX => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZY => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZZ => throw new InvalidExecutionContextException($"{typeof(GridIds)}.{nameof(ZZZ)}"); } diff --git a/src/ComputeSharp/Core/Dispatch/GroupIds.cs b/src/ComputeSharp/Core/Dispatch/GroupIds.cs index 1d4a5581..27c639d5 100644 --- a/src/ComputeSharp/Core/Dispatch/GroupIds.cs +++ b/src/ComputeSharp/Core/Dispatch/GroupIds.cs @@ -4,6 +4,7 @@ namespace ComputeSharp { /// /// A that indicates the ids of a given GPU thread running a compute shader within a dispatch group. + /// That is, it enables a shader to access info on the index of the current thread with respect to the currently running group. /// public static class GroupIds { @@ -28,182 +29,182 @@ namespace ComputeSharp public static int Z => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(Z)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(XZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(YZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZX => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZY => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZZ => throw new InvalidExecutionContextException($"{typeof(GroupIds)}.{nameof(ZZZ)}"); } diff --git a/src/ComputeSharp/Core/Dispatch/GroupSize.cs b/src/ComputeSharp/Core/Dispatch/GroupSize.cs index 77cb797c..92f757bd 100644 --- a/src/ComputeSharp/Core/Dispatch/GroupSize.cs +++ b/src/ComputeSharp/Core/Dispatch/GroupSize.cs @@ -3,7 +3,8 @@ namespace ComputeSharp { /// - /// A that indicates the size info of a given GPU thread group running a compute shader within. + /// A that indicates the size info of a given GPU thread group running a compute shader. + /// That is, it enables a shader to access info on the size of the thread groups being used. /// public static class GroupSize { @@ -28,182 +29,182 @@ namespace ComputeSharp public static int Z => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(Z)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(XZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(YZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZX => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZY => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZZ => throw new InvalidExecutionContextException($"{typeof(GroupSize)}.{nameof(ZZZ)}"); } diff --git a/src/ComputeSharp/Core/Dispatch/ThreadIds.cs b/src/ComputeSharp/Core/Dispatch/ThreadIds.cs index c46ec733..0606d526 100644 --- a/src/ComputeSharp/Core/Dispatch/ThreadIds.cs +++ b/src/ComputeSharp/Core/Dispatch/ThreadIds.cs @@ -4,6 +4,7 @@ namespace ComputeSharp { /// /// A that indicates the ids of a given GPU thread running a compute shader. + /// That is, it enables a shader to access info on the current iteration index along each axis. /// public static class ThreadIds { @@ -23,182 +24,182 @@ namespace ComputeSharp public static int Z => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(Z)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 XZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 YZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YZ)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZX)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZY)}"); /// - /// Gets a value with the components , . + /// Gets an value with the components , . /// public static Int2 ZZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XXZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XYZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 XZZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(XZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YXZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YYZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 YZZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(YZZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZXX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZXY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZXZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZXZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZYX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZYY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZYZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZYZ)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZX => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZZX)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZY => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZZY)}"); /// - /// Gets a value with the components , , . + /// Gets an value with the components , , . /// public static Int3 ZZZ => throw new InvalidExecutionContextException($"{typeof(ThreadIds)}.{nameof(ZZZ)}"); } From 5794b43fe0ac11a80f4dea451110a544b167581b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 14:25:22 +0100 Subject: [PATCH 4/9] Added ThreadIds.Normalized type --- .../ComputeSharp.SourceGenerators.csproj | 1 + .../Mappings/HlslKnownMembers.cs | 31 +++ .../Core/Dispatch/ThreadIds.Normalized.cs | 211 ++++++++++++++++++ src/ComputeSharp/Core/Dispatch/ThreadIds.cs | 2 +- tests/ComputeSharp.Tests/DispatchTests.cs | 41 ++++ 5 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index f028f792..423e9d88 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -22,6 +22,7 @@ + diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 727ac2c8..97011619 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -227,6 +227,37 @@ namespace ComputeSharp.SourceGenerators.Mappings } } + // Programmatically load mappings for the normalized thread ids + foreach (var property in typeof(ThreadIds.Normalized).GetProperties(BindingFlags.Static | BindingFlags.Public)) + { + string key = $"{typeof(ThreadIds).FullName}{Type.Delimiter}{typeof(ThreadIds.Normalized).Name}{Type.Delimiter}{property.Name}"; + + switch (property.Name) + { + case string name when name.Length == 1: + knownMembers.Add(key, $"{typeof(ThreadIds).Name}.{char.ToLower(name[0])} / (float)__{char.ToLower(name[0])}"); + break; + case string name when name.Length == 2: + { + string + numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])})", + denominator = $"float2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})"; + + knownMembers.Add(key, $"{numerator} / {denominator}"); + break; + } + case string name when name.Length == 3: + { + string + numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])}, {typeof(ThreadIds).Name}.{char.ToLower(name[2])})", + denominator = $"float3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})"; + + knownMembers.Add(key, $"{numerator} / {denominator}"); + break; + } + } + } + // Programmatically load mappings for the group size foreach (var property in typeof(GroupSize).GetProperties(BindingFlags.Static | BindingFlags.Public)) { diff --git a/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs b/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs new file mode 100644 index 00000000..b637dd51 --- /dev/null +++ b/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs @@ -0,0 +1,211 @@ +using ComputeSharp.Exceptions; + +namespace ComputeSharp +{ + /// + public static partial class ThreadIds + { + /// + /// A that indicates the normalized ids of a given GPU thread running a compute shader. + /// These ids represent equivalent info to those from , but normalized in the [0, 1] range. + /// The range used for the normalization is the one given by the target dispatch size (see ). + /// + public static class Normalized + { + /// + /// Gets the normalized X id of the current thread. + /// + public static float X => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(X)}"); + + /// + /// Gets the normalized Y id of the current thread. + /// + public static float Y => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(Y)}"); + + /// + /// Gets the normalized Z id of the current thread. + /// + public static float Z => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(Z)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 XX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XX)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 XY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XY)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 XZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZ)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 YX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YX)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 YY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YY)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 YZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZ)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 ZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZX)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 ZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZY)}"); + + /// + /// Gets a value with the components , . + /// + public static Float2 ZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XXX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XXX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XXY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XXY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XXZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XXZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XYX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XYX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XYY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XYY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XYZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XYZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 XZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(XZZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YXX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YXX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YXY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YXY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YXZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YXZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YYX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YYX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YYY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YYY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YYZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YYZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 YZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(YZZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZXX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZXX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZXY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZXY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZXZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZXZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZYX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZYX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZYY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZYY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZYZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZYZ)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZZX => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZX)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZZY => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZY)}"); + + /// + /// Gets a value with the components , , . + /// + public static Float3 ZZZ => throw new InvalidExecutionContextException($"{typeof(Normalized)}.{nameof(ZZZ)}"); + } + } +} diff --git a/src/ComputeSharp/Core/Dispatch/ThreadIds.cs b/src/ComputeSharp/Core/Dispatch/ThreadIds.cs index 0606d526..c7f9beed 100644 --- a/src/ComputeSharp/Core/Dispatch/ThreadIds.cs +++ b/src/ComputeSharp/Core/Dispatch/ThreadIds.cs @@ -6,7 +6,7 @@ namespace ComputeSharp /// A that indicates the ids of a given GPU thread running a compute shader. /// That is, it enables a shader to access info on the current iteration index along each axis. /// - public static class ThreadIds + public static partial class ThreadIds { /// /// Gets the X id of the current thread. diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index e6cd8194..a7a19fc5 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -65,6 +65,47 @@ namespace ComputeSharp.Tests } } + [CombinatorialTestMethod] + [AllDevices] + public unsafe void Verify_ThreadIdsNormalized(Device device) + { + using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(10, 20, 30); + + device.Get().For(buffer.Width, buffer.Height, buffer.Depth, new ThreadIdsNormalizedShader(buffer)); + + Float4[,,] data = buffer.ToArray(); + float* value = stackalloc float[4]; + + for (int z = 0; z < 30; z++) + { + for (int x = 0; x < 10; x++) + { + for (int y = 0; y < 20; y++) + { + *(Float4*)value = data[z, y, x]; + + Assert.AreEqual(x / (float)buffer.Width, value[0], 0.000001f); + Assert.AreEqual(y / (float)buffer.Height, value[1], 0.000001f); + Assert.AreEqual(z / (float)buffer.Depth, value[2], 0.000001f); + Assert.AreEqual(x / (float)buffer.Width, value[3], 0.000001f); + } + } + } + } + + [AutoConstructor] + internal readonly partial struct ThreadIdsNormalizedShader : IComputeShader + { + public readonly ReadWriteTexture3D buffer; + + public void Execute() + { + buffer[ThreadIds.XYZ].XYZ = ThreadIds.Normalized.XYZ; + buffer[ThreadIds.XYZ].XY = ThreadIds.Normalized.XY; + buffer[ThreadIds.XYZ].W = ThreadIds.Normalized.X; + } + } + [CombinatorialTestMethod] [AllDevices] public unsafe void Verify_GroupIds(Device device) From ced4a12afde32a10fbf1a30d6cbf55dec3465ba7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 14:27:37 +0100 Subject: [PATCH 5/9] Minor tweaks to some unit tests --- tests/ComputeSharp.Tests/ShaderMembersTests.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/ComputeSharp.Tests/ShaderMembersTests.cs b/tests/ComputeSharp.Tests/ShaderMembersTests.cs index f782c338..df8b0d7f 100644 --- a/tests/ComputeSharp.Tests/ShaderMembersTests.cs +++ b/tests/ComputeSharp.Tests/ShaderMembersTests.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -21,13 +19,13 @@ namespace ComputeSharp.Tests float[] results = buffer.ToArray(); - Assert.IsTrue(Math.Abs(3.14f - results[0]) < 0.00001f); - Assert.IsTrue(Math.Abs(results[1] - results[2]) < 0.00001f); - Assert.IsTrue(Math.Abs(1 - results[3]) < 0.00001f); - Assert.IsTrue(Math.Abs(2 - results[4]) < 0.00001f); - Assert.IsTrue(Math.Abs(3 - results[5]) < 0.00001f); - Assert.IsTrue(Math.Abs(4 - results[6]) < 0.00001f); - Assert.IsTrue(Math.Abs(3.14f - results[7]) < 0.00001f); + Assert.AreEqual(3.14f, results[0], 0.00001f); + Assert.AreEqual(results[1], results[2], 0.00001f); + Assert.AreEqual(1, results[3], 0.00001f); + Assert.AreEqual(2, results[4], 0.00001f); + Assert.AreEqual(3, results[5], 0.00001f); + Assert.AreEqual(4, results[6], 0.00001f); + Assert.AreEqual(3.14f, results[7], 0.00001f); } [AutoConstructor] From 1b531f1dd733eec18f126b934b3564f087f42338 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 14:38:35 +0100 Subject: [PATCH 6/9] Added diagnostics for invalid DispatchSize usage --- .../AnalyzerReleases.Unshipped.md | 3 +- .../Diagnostics/DiagnosticDescriptors.cs | 13 +++++++ .../ConstantPropertyRewriter.cs | 1 + .../SyntaxRewriters/ShaderSourceRewriter.cs | 1 + .../DiagnosticsTests.cs | 37 +++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md index 6bbf251f..97be482d 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md @@ -50,4 +50,5 @@ CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, CMPS0043 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0044 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0045 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) -CMPS0046 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file +CMPS0046 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0047 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 3edab2e8..2b3f33ba 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -652,5 +652,18 @@ namespace ComputeSharp.SourceGenerators.Diagnostics isEnabledByDefault: true, description: "A type representing a compute shader contains an instance property declaration.", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an invalid usage. + /// + public static readonly DiagnosticDescriptor InvalidDispatchSizeUsage = new( + id: "CMPS0047", + title: "Invalid DispatchSize usage", + messageFormat: "The DispatchSize type can only be used within the main body of a compute shader", + category: typeof(IComputeShader).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The DispatchSize type can only be used within the main body of a compute shader.", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs index 4d2449e4..6e221561 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs @@ -87,6 +87,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters _ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage, _ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage, _ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage, + _ when typeName == typeof(DispatchSize).FullName => InvalidDispatchSizeUsage, _ => null }; diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index e35144d5..69a7c9fd 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -288,6 +288,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters _ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage, _ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage, _ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage, + _ when typeName == typeof(DispatchSize).FullName => InvalidDispatchSizeUsage, _ => null }; diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 6b020e63..429d4afd 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -142,6 +142,7 @@ namespace ComputeSharp.Tests.SourceGenerators [DataRow(nameof(GroupIds), "CMPS0007")] [DataRow(nameof(GroupSize), "CMPS0008")] [DataRow(nameof(GridIds), "CMPS0009")] + [DataRow(nameof(DispatchSize), "CMPS0047")] public void InvalidDispatchInfoUsage_LocalFunction(string typeName, string diagnosticsId) { string source = $@" @@ -176,6 +177,7 @@ namespace ComputeSharp.Tests.SourceGenerators [DataRow(nameof(GroupIds), "CMPS0007")] [DataRow(nameof(GroupSize), "CMPS0008")] [DataRow(nameof(GridIds), "CMPS0009")] + [DataRow(nameof(DispatchSize), "CMPS0047")] public void InvalidDispatchInfoUsage_StaticMethod(string typeName, string diagnosticsId) { string source = $@" @@ -202,6 +204,41 @@ namespace ComputeSharp.Tests.SourceGenerators VerifyGeneratedDiagnostics(source, diagnosticsId); } + [TestMethod] + [DataRow(nameof(ThreadIds), "CMPS0006")] + [DataRow(nameof(GroupIds), "CMPS0007")] + [DataRow(nameof(GroupSize), "CMPS0008")] + [DataRow(nameof(GridIds), "CMPS0009")] + [DataRow(nameof(DispatchSize), "CMPS0047")] + public void InvalidDispatchInfoUsage_ConstantProperty(string typeName, string diagnosticsId) + { + string source = $@" + using ComputeSharp; + + namespace ComputeSharp + {{ + public class ReadWriteBuffer {{ }} + public static class {typeName} {{ public static int X => 0; }} + }} + + namespace MyFancyApp.Sample + {{ + public struct MyShader : IComputeShader + {{ + public ReadWriteBuffer buffer; + + private static int Foo => {typeName}.X; + + public void Execute() + {{ + buffer[0] = Foo; + }} + }} + }}"; + + VerifyGeneratedDiagnostics(source, diagnosticsId); + } + [TestMethod] public void InvalidObjectCreationExpression_Explicit() { From 0ff4da84801b3b7c795cdc3820b935bc3f4f98cf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 14:42:12 +0100 Subject: [PATCH 7/9] Enabled diagnostics for invalid ThreadIds.Normalized usage --- .../ConstantPropertyRewriter.cs | 2 +- .../SyntaxRewriters/ShaderSourceRewriter.cs | 2 +- .../DiagnosticsTests.cs | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs index 6e221561..800d6595 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs @@ -83,7 +83,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters // Special dispatch types are not supported from static constants DiagnosticDescriptor? descriptor = typeName switch { - _ when typeName == typeof(ThreadIds).FullName => InvalidThreadIdsUsage, + _ when typeName == typeof(ThreadIds).FullName || typeName == typeof(ThreadIds.Normalized).FullName => InvalidThreadIdsUsage, _ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage, _ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage, _ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage, diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 69a7c9fd..55579e2c 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -284,7 +284,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters { DiagnosticDescriptor? descriptor = typeName switch { - _ when typeName == typeof(ThreadIds).FullName => InvalidThreadIdsUsage, + _ when typeName == typeof(ThreadIds).FullName || typeName == typeof(ThreadIds.Normalized).FullName => InvalidThreadIdsUsage, _ when typeName == typeof(GroupIds).FullName => InvalidGroupIdsUsage, _ when typeName == typeof(GroupSize).FullName => InvalidGroupSizeUsage, _ when typeName == typeof(GridIds).FullName => InvalidGridIdsUsage, diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs index 429d4afd..5924b562 100644 --- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs +++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs @@ -239,6 +239,36 @@ namespace ComputeSharp.Tests.SourceGenerators VerifyGeneratedDiagnostics(source, diagnosticsId); } + [TestMethod] + public void InvalidThreadIdsNormalizedUsage() + { + string source = $@" + using ComputeSharp; + + namespace ComputeSharp + {{ + public class ReadWriteBuffer {{ }} + public static class ThreadIds {{ public static class Normalized {{ public static int X => 0; }} }} + }} + + namespace MyFancyApp.Sample + {{ + public struct MyShader : IComputeShader + {{ + public ReadWriteBuffer buffer; + + private static int Foo => ThreadIds.Normalized.X; + + public void Execute() + {{ + buffer[0] = Foo; + }} + }} + }}"; + + VerifyGeneratedDiagnostics(source, "CMPS0006"); + } + [TestMethod] public void InvalidObjectCreationExpression_Explicit() { From c5983e2f635f2b6f81767c65b6b75dbdccac6045 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 15:26:13 +0100 Subject: [PATCH 8/9] Refactored sample shaders to use DispatchSize --- .../ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs | 5 +---- samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs | 2 +- samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs | 7 +++---- samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs | 3 +-- samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs | 7 +++---- .../ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs | 9 +++------ 6 files changed, 12 insertions(+), 21 deletions(-) diff --git a/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs b/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs index 7ab620e1..034330fa 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs @@ -51,10 +51,7 @@ /// public void Execute() { - int width = texture.Width; - int height = texture.Height; - - Float2 uv = ((Float2)ThreadIds.XY - (Float2)(new Int2(width, height)) * 0.5f) / height; + Float2 uv = (ThreadIds.XY - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y; Float3 col = 0; float t = time * 0.3f; diff --git a/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs b/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs index db12f72d..8831eaec 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs @@ -22,7 +22,7 @@ /// public void Execute() { - Float2 position = ((Float2)(256 * ThreadIds.XY)) / texture.Width + time; + Float2 position = ((Float2)(256 * ThreadIds.XY)) / DispatchSize.X + time; Float4 color = 0; for (int i = 0; i < 6; i++) diff --git a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs index f4446dc6..e443be64 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs @@ -158,15 +158,14 @@ Float3 camPos = 0.5f * time * new Float3(1.0f, 0.0f, 0.0f); Float3 target = camPos + new Float3(1.0f, 0.0f * Hlsl.Cos(time), 0.0f * Hlsl.Sin(0.4f * time)); Float3 camDir = Hlsl.Normalize(target - camPos); - Float3 camUp = new Float3(0.0f, 1.0f, 0.0f); + Float3 camUp = new(0.0f, 1.0f, 0.0f); camUp = Hlsl.Normalize(camUp - Hlsl.Dot(camDir, camUp) * camDir); Float3 camRight = Hlsl.Normalize(Hlsl.Cross(camDir, camUp)); - Float2 resolution = new(texture.Width, texture.Height); - Float2 coord = -1.0f + 2.0f * (Float2)ThreadIds.XY / resolution; + Float2 coord = -1.0f + 2.0f * (Float2)ThreadIds.XY / DispatchSize.XY; - coord.X *= resolution.X / resolution.Y; + coord.X *= (float)DispatchSize.X / DispatchSize.Y; Float3 rayDir = Hlsl.Normalize(camDir + (coord.X * camRight + coord.Y * camUp) * FieldOfView); diff --git a/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs b/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs index dfd79d8b..4bca70ef 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs @@ -90,8 +90,7 @@ /// public void Execute() { - Int2 resolution = new(texture.Width, texture.Height); - Float2 p = ((Float2)ThreadIds.XY * 2.0f - resolution) / Hlsl.Min(resolution.X, resolution.Y); + Float2 p = ((Float2)ThreadIds.XY * 2.0f - DispatchSize.XY) / Hlsl.Min(DispatchSize.X, DispatchSize.Y); Float3 ro = new(0.0f, -0.2f, time * 4.0f); Float3 ray = Hlsl.Normalize(new Float3(p, 1.5f)); diff --git a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs index 5909f259..04f08476 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs @@ -133,10 +133,9 @@ /// public void Execute() { - Int2 resolution = new(texture.Width, texture.Height); - Float2 q = (Float2)ThreadIds.XY / resolution; - Float2 p = (ThreadIds.XY - 0.5f * (Float2)resolution) / resolution.Y; - Float2 bsMo = -0.5f * (Float2)resolution / resolution.Y; + Float2 q = (Float2)ThreadIds.XY / DispatchSize.XY; + Float2 p = (ThreadIds.XY - 0.5f * (Float2)DispatchSize.XY) / DispatchSize.Y; + Float2 bsMo = -0.5f * (Float2)DispatchSize.XY / DispatchSize.Y; float scaledTime = time * 3.0f; Float3 ro = new(0, 0, scaledTime); diff --git a/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs b/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs index f56709a8..d1d90f34 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs @@ -116,17 +116,14 @@ /// public void Execute() { - int width = texture.Width; - int height = texture.Height; - - Float2 uv = ((Float2)ThreadIds.XY - (Float2)(new Int2(width, height)) * 0.5f) / height; + Float2 uv = (ThreadIds.XY - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y; float gSc = 7.0f; Float2 p = uv * gSc - new Float2(-1, -0.5f) * time / 2.0f; - float sf = 2.0f / height * gSc; + float sf = 2.0f / DispatchSize.Y * gSc; float lSc = 6.0f; float lw = 1.0f / lSc / gSc; Float2 d = DistanceField(p, out Float2 ang) - 2.5f / lSc; - Float3 col = new Float3(1.0f, 0.9f, 0.8f); + Float3 col = new(1.0f, 0.9f, 0.8f); for (int i = 0; i < 2; i++) { From f7a0d76652f90891d803a08e21ea87df78c87b03 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 8 Mar 2021 17:32:01 +0100 Subject: [PATCH 9/9] Added docs on the dispatch info types --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 29a454e2..0a50ed28 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ - [HLSL vector and matrix types](#hlsl-vector-and-matrix-types) - [HLSL intrinsics](#hlsl-intrinsics) - [Shader constants](#shader-constants) + - [Dispatch info](#dispatch-info) - [Working with images](#working-with-images) - [Shader metaprogramming](#shader-metaprogramming) - [Requirements](#requirements) @@ -172,6 +173,19 @@ public readonly struct SampleShaderWithConstants : IComputeShader } ``` +## Dispatch info + +As shown in the [Quick start](#quick-start) paragraph, **ComputeSharp** includes a number of special types that allow shaders to access a number of useful dispatch info values, such as the current iteration coordinate or the target dispatch range. Here is a list of all the available types in this category: + +- `ThreadIds`: indicates the ids of a given GPU thread running a compute shader. That is, it enables a shader to access info on the current iteration index along each axis. +- `ThreadIds.Normalized`: indicates the normalized ids of a given GPU thread running a compute shader. These ids represent equivalent info to those from `ThreadIds`, but normalized in the [0, 1] range. The range used for the normalization is the one given by the target dispatch size. +- `DispatchSize`: indicates the size of the current shader dispatch being executed. That is, it enables a shader to access info on the targeted number of invocations along each axis. +- `GroupIds`: indicates the ids of a given GPU thread running a compute shader within a dispatch group. That is, it enables a shader to access info on the index of the current thread with respect to the currently running group. +- `GroupSize`: indicates the size info of a given GPU thread group running a compute shader. That is, it enables a shader to access info on the size of the thread groups being used. +- `GridIds`: indicates the ids of the current thread group within the dispatch grid. That is, it enables a shader to access info on the index of the current thread group with respect to the dispatch grid. + +For more info on how all these values relate to the corresponding HLSL inputs, see the `[numthreads]` docs [here](https://docs.microsoft.com/windows/win32/direct3dhlsl/sm5-attributes-numthreads). Note that `ThreadIds`, `GroupIds` and `GridIds` map to `SV_DispatchThreadID`, `SV_GroupThreadID` and `SV_GroupID` respectively, and the `GroupIds.Index` property maps to `SV_GroupIndex`. + ## Working with images As mentioned in the [GPU resource types](#gpu-resource-types) paragraph, there are several texture types that are specialized to work on image pixels, such as `ReadWriteTexture2D`. Let's imagine we want to write a compute shader that applies some image processing filter: we will need to load an image (eg. using [ImageSharp](https://github.com/SixLabors/ImageSharp), or just the `System.Drawing` APIs) and then process it on the GPU, but without spending time on the CPU to convert pixels from a format such as RGBA32 to the normalized `float` values we want our shader to work on. We can do this by utilizing the `ReadWriteTexture2D` type as follows: @@ -194,7 +208,7 @@ try using var texture = Gpu.Default.AllocateReadWriteTexture2D(bitmapSpan, bitmap.Width, bitmap.Height); // Run our shader on the texture we just created - Gpu.Default.For(bitmap.Width, bitmap.Height, new GaussianBlur(texture)); + Gpu.Default.For(bitmap.Width, bitmap.Height, new GrayscaleEffect(texture)); // When the shader has completed, copy the processed texture back texture.CopyTo(bitmapSpan); @@ -211,7 +225,7 @@ With the compute shader being like this: ```csharp [AutoConstructor] -public readonly partial struct GaussianBlur : IComputeShader +public readonly partial struct GrayscaleEffect : IComputeShader { public readonly IReadWriteTexture2D texture;