From 219db68545a9b409cdfc03342c70743ab5ea1c92 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 May 2022 23:19:57 +0200 Subject: [PATCH] Fix ThreadIds.Normalized logic to be in [0, 1] range --- .../Mappings/HlslKnownProperties.cs | 14 ++++++-- .../Core/Dispatch/ThreadIds.Normalized.cs | 12 +++++++ .../DispatchTests.cs | 4 +-- tests/ComputeSharp.Tests/DispatchTests.cs | 33 ++++++++++++++----- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs index 982a2344..531893de 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownProperties.cs @@ -94,15 +94,23 @@ partial class HlslKnownProperties { string key = $"{typeof(ThreadIds).FullName}{Type.Delimiter}{typeof(ThreadIds.Normalized).Name}{Type.Delimiter}{property.Name}"; + // The normalized value must be in the [0, 1] range, inclusive. As such, the expression is rewritten to be: + // 1D: (float)ThreadIds. / (float)(max(1, __ - 1)); + // 2D: float2(ThreadIds., ThreadIds.) / float2(max(1, int2(__, __) -1)); + // 3D: float3(ThreadIds., ThreadIds., ThreadIds.) / float3(max(1, int3(__, __, __) - 1)); + // + // All these expressions will result in the values going in the target [0, 1] range, with the values at the two ends being + // exactly 0 and 1. This logic assumes that the __ values are always > 0, which is guaranteed by the fact those are the + // dispatching upper bounds, which is validated whenever a shader is run (as there can never be 0 iterations on any given axis). switch (property.Name) { case string name when name.Length == 1: - knownProperties.Add(key, $"{typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[0])} / (float)__{char.ToLowerInvariant(name[0])}"); + knownProperties.Add(key, $"(float){typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[0])} / (float)(max(1, __{char.ToLowerInvariant(name[0])} - 1))"); break; case string name when name.Length == 2: { string numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[0])}, {typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[1])})"; - string denominator = $"float2(__{char.ToLowerInvariant(name[0])}, __{char.ToLowerInvariant(name[1])})"; + string denominator = $"float2(max(1, int2(__{char.ToLowerInvariant(name[0])}, __{char.ToLowerInvariant(name[1])}) - 1))"; knownProperties.Add(key, $"{numerator} / {denominator}"); break; @@ -110,7 +118,7 @@ partial class HlslKnownProperties case string name when name.Length == 3: { string numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[0])}, {typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[1])}, {typeof(ThreadIds).Name}.{char.ToLowerInvariant(name[2])})"; - string denominator = $"float3(__{char.ToLowerInvariant(name[0])}, __{char.ToLowerInvariant(name[1])}, __{char.ToLowerInvariant(name[2])})"; + string denominator = $"float3(max(1, int3(__{char.ToLowerInvariant(name[0])}, __{char.ToLowerInvariant(name[1])}, __{char.ToLowerInvariant(name[2])}) - 1))"; knownProperties.Add(key, $"{numerator} / {denominator}"); break; diff --git a/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs b/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs index 75023d16..79c366ba 100644 --- a/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs +++ b/src/ComputeSharp/Core/Dispatch/ThreadIds.Normalized.cs @@ -10,6 +10,18 @@ partial class ThreadIds /// 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 ). /// + /// + /// When the dispatch size on a given axis is 1, the normalized value along that axis will be 0. Otherwise, it will just + /// be the normalization over [0, 1] for the current dispatch progress along that axis. That is, for instance: + /// + /// [0]. + /// [0, 1]. + /// [0, 0.5, 1]. + /// [0, 0.333..., 0.666..., 1]. + /// [0, 0.25, 0.5, 0.75, 1]. + /// + /// And so on depending on the dispatch size along the requested dispatch axis. + /// public static class Normalized { /// diff --git a/tests/ComputeSharp.Tests.DisableDynamicCompilation/DispatchTests.cs b/tests/ComputeSharp.Tests.DisableDynamicCompilation/DispatchTests.cs index 62549288..e0815693 100644 --- a/tests/ComputeSharp.Tests.DisableDynamicCompilation/DispatchTests.cs +++ b/tests/ComputeSharp.Tests.DisableDynamicCompilation/DispatchTests.cs @@ -68,8 +68,8 @@ public partial class DispatchTests { for (int j = 0; j < texture.Width; j++) { - byte r = (byte)(j / (float)texture.Width * 255f); - byte g = (byte)(i / (float)texture.Height * 255f); + byte r = (byte)(j / (float)(texture.Width - 1) * 255f); + byte g = (byte)(i / (float)(texture.Height - 1) * 255f); const byte b = (byte)(0.3f * 255f); const byte a = (byte)(0.6f * 255f); Rgba32 pixel = result[i, j]; diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index e6a674fd..68812262 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -69,27 +69,42 @@ public partial class DispatchTests [CombinatorialTestMethod] [AllDevices] - public unsafe void Verify_ThreadIdsNormalized(Device device) + [Data(1, 1, 1)] + [Data(1, 1, 2)] + [Data(1, 2, 1)] + [Data(2, 1, 1)] + [Data(10, 1, 1)] + [Data(10, 1, 20)] + [Data(1, 2, 3)] + [Data(2, 3, 4)] + [Data(3, 2, 1)] + [Data(10, 20, 30)] + [Data(10, 2, 3)] + public unsafe void Verify_ThreadIdsNormalized(Device device, int width, int height, int depth) { - using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(10, 20, 30); + using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(width, height, depth); 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 z = 0; z < depth; z++) { - for (int x = 0; x < 10; x++) + for (int x = 0; x < width; x++) { - for (int y = 0; y < 20; y++) + for (int y = 0; y < height; 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); + float expectedX = width == 1 ? 0 : (x / (float)(buffer.Width - 1)); + float expectedY = height == 1 ? 0 : (y / (float)(buffer.Height - 1)); + float expectedZ = depth == 1 ? 0 : (z / (float)(buffer.Depth - 1)); + + Assert.AreEqual(expectedX, value[0], 0.000001f); + Assert.AreEqual(expectedY, value[1], 0.000001f); + Assert.AreEqual(expectedZ, value[2], 0.000001f); + Assert.AreEqual(expectedX, value[3], 0.000001f); } } }