Merge pull request #275 from Sergio0694/bugfix/normalized-threadids
Fix ThreadIds.Normalized logic to be in [0, 1] range
検索条件に一致するファイルはありません
@@ -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;
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
変更前 幅: | 高さ: | サイズ: 1.3 MiB 変更後 幅: | 高さ: | サイズ: 1.1 MiB |
|
変更前 幅: | 高さ: | サイズ: 796 KiB 変更後 幅: | 高さ: | サイズ: 708 KiB |
|
変更後 幅: | 高さ: | サイズ: 1.1 MiB |
|
変更前 幅: | 高さ: | サイズ: 645 KiB 変更後 幅: | 高さ: | サイズ: 696 KiB |
|
変更前 幅: | 高さ: | サイズ: 1.2 MiB 変更後 幅: | 高さ: | サイズ: 1.2 MiB |
|
変更前 幅: | 高さ: | サイズ: 23 KiB 変更後 幅: | 高さ: | サイズ: 4.1 KiB |
@@ -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
|
||||
/// <inheritdoc/>
|
||||
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
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,9 @@
|
||||
<None Update="Assets\CityAfter1024x1024SamplingAndDashing.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\CityAfter1024x1024SamplingFrom1920x1080.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\CityAfter1024x1024SamplingFrom1920x1080AndDashing.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||