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);
+ }
+ }
}
}