Fix ThreadIds.Normalized logic to be in [0, 1] range

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-27 00:01:32 +02:00
1 親 e16b234b94
コミット 219db68545
4ファイル変更
+49 -14

検索条件に一致するファイルはありません

+11 -3
ファイルの表示
@@ -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.<XYZ> / (float)(max(1, __<xyz> - 1));
// 2D: float2(ThreadIds.<XYZ>, ThreadIds.<XYZ>) / float2(max(1, int2(__<xyz>, __<xyz>) -1));
// 3D: float3(ThreadIds.<XYZ>, ThreadIds.<XYZ>, ThreadIds.<XYZ>) / float3(max(1, int3(__<xyz>, __<xyz>, __<xyz>) - 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 __<xyz> 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;
+12
ファイルの表示
@@ -10,6 +10,18 @@ partial class ThreadIds
/// These ids represent equivalent info to those from <see cref="ThreadIds"/>, but normalized in the [0, 1] range.
/// The range used for the normalization is the one given by the target dispatch size (see <see cref="DispatchSize"/>).
/// </summary>
/// <remarks>
/// 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:
/// <list type="number">
/// <item>[0].</item>
/// <item>[0, 1].</item>
/// <item>[0, 0.5, 1].</item>
/// <item>[0, 0.333..., 0.666..., 1].</item>
/// <item>[0, 0.25, 0.5, 0.75, 1].</item>
/// </list>
/// And so on depending on the dispatch size along the requested dispatch axis.
/// </remarks>
public static class Normalized
{
/// <summary>
+2 -2
ファイルの表示
@@ -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];
+24 -9
ファイルの表示
@@ -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<float4> buffer = device.Get().AllocateReadWriteTexture3D<float4>(10, 20, 30);
using ReadWriteTexture3D<float4> buffer = device.Get().AllocateReadWriteTexture3D<float4>(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);
}
}
}