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/Assets/CityAfter1024x1024Sampling.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024Sampling.png index 0430d2c7..caf90bb0 100644 Binary files a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024Sampling.png and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024Sampling.png differ diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png index 3ee0e09d..b334a7e6 100644 Binary files a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingAndDashing.png differ diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080.png new file mode 100644 index 00000000..98047064 Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080.png differ diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png index 32c63715..7f25804c 100644 Binary files a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingFrom1920x1080AndDashing.png differ diff --git a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png index 9d50e30a..38bba1f4 100644 Binary files a/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png and b/tests/ComputeSharp.Tests/Assets/CityAfter1024x1024SamplingInverted.png differ diff --git a/tests/ComputeSharp.Tests/Assets/HelloWorld.png b/tests/ComputeSharp.Tests/Assets/HelloWorld.png index 3595906a..c59b209b 100644 Binary files a/tests/ComputeSharp.Tests/Assets/HelloWorld.png and b/tests/ComputeSharp.Tests/Assets/HelloWorld.png differ diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index cd23b218..039bbe46 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -995,7 +995,7 @@ public partial class ComputeContextTests string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); string originalPath = Path.Combine(assetsPath, "CityWith1920x1280Resizing.png"); - string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"); + string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024SamplingFrom1920x1080.png"); var imageInfo = Image.Identify(originalPath); @@ -1047,7 +1047,7 @@ public partial class ComputeContextTests string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); string originalPath = Path.Combine(assetsPath, "CityWith1920x1280Resizing.png"); - string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024Sampling.png"); + string sampledPath = Path.Combine(assetsPath, "CityAfter1024x1024SamplingFrom1920x1080.png"); var imageInfo = Image.Identify(originalPath); @@ -1347,7 +1347,12 @@ public partial class ComputeContextTests /// public void Execute() { - destination[ThreadIds.XYZ] = source.Sample(ThreadIds.Normalized.XYZ); + float3 xyz = ThreadIds.Normalized.XYZ; + + // The source has a depth of 3, but the destination has a depth of 2 + xyz.Z = Hlsl.Lerp(0, 0.5f, xyz.Z); + + destination[ThreadIds.XYZ] = source.Sample(xyz); } } @@ -1396,7 +1401,12 @@ public partial class ComputeContextTests /// public void Execute() { - destination[ThreadIds.XYZ] = source.Sample(ThreadIds.Normalized.XYZ); + float3 xyz = ThreadIds.Normalized.XYZ; + + // See comment in LinearSampling3DComputeShader + xyz.Z = Hlsl.Lerp(0, 0.5f, xyz.Z); + + destination[ThreadIds.XYZ] = source.Sample(xyz); } } diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index c3bd5cec..c14ba3ee 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -52,6 +52,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest 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); } } }