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)