CPUの画素の8ビットとの変換を別の型へ分ける

このコミットが含まれているのは:
routersys committed 2026-10-08 09:22:42 +09:00
1 親 61a23e6ada
コミット f9d34ba224
6ファイル変更
+139 -130

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

+1
ファイルの表示
@@ -131,6 +131,7 @@ SpectralConvolution/ 外側
| `KernelSpectrum` | 和を 1 にそろえたカーネルと、タイルの大きさに合わせた回転因子とスペクトル |
| `CpuTileConvolver` | CPU の経路の畳み込み |
| `CpuFft` | CPU の経路の高速フーリエ変換。4 つの float を 1 つのベクトルに置いて計算する |
| `CpuPixels` | CPU の経路で、8 ビットの素材を float の行に置く変換と、float の出力を 8 ビットにする変換 |
| `GpuTileConvolver` | GPU の経路の資源と、ComputeWeave のコンテキストへの記録 |
| `GpuTileJob` | GPU の経路の 1 回分の畳み込みの設定。`GpuTileConvolver.Prepare` が返す |
| `GpuTileCheck` | GPU が書き出した値から作る検査 |
+11 -11
ファイルの表示
@@ -63,10 +63,10 @@ public sealed class CpuTileConvolverTests
for (var index = 0; index < output.Length; index += 4)
{
Assert.Equal(CpuTileConvolver.ToUnorm(convolved[index + 2] * 2.5f), output[index]);
Assert.Equal(CpuTileConvolver.ToUnorm(convolved[index + 1] * 2.5f), output[index + 1]);
Assert.Equal(CpuTileConvolver.ToUnorm(convolved[index] * 2.5f), output[index + 2]);
Assert.Equal(CpuTileConvolver.ToUnorm(convolved[index + 3] * 2.5f), output[index + 3]);
Assert.Equal(CpuPixels.ToUnorm(convolved[index + 2] * 2.5f), output[index]);
Assert.Equal(CpuPixels.ToUnorm(convolved[index + 1] * 2.5f), output[index + 1]);
Assert.Equal(CpuPixels.ToUnorm(convolved[index] * 2.5f), output[index + 2]);
Assert.Equal(CpuPixels.ToUnorm(convolved[index + 3] * 2.5f), output[index + 3]);
}
}
@@ -136,8 +136,8 @@ public sealed class CpuTileConvolverTests
Assert.Equal(single, several);
for (var pixel = 0; pixel < pixels; pixel++)
{
Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 2] * 1.3f), single[pixel * 4]);
Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4] * 1.3f), single[pixel * 4 + 2]);
Assert.Equal(CpuPixels.ToUnorm(stored[pixel * 4 + 2] * 1.3f), single[pixel * 4]);
Assert.Equal(CpuPixels.ToUnorm(stored[pixel * 4] * 1.3f), single[pixel * 4 + 2]);
}
}
@@ -178,7 +178,7 @@ public sealed class CpuTileConvolverTests
[InlineData(float.PositiveInfinity, 255)]
public void ConversionFollowsTheDirect3DRule(float value, byte expected)
{
Assert.Equal(expected, CpuTileConvolver.ToUnorm(value));
Assert.Equal(expected, CpuPixels.ToUnorm(value));
}
[Fact]
@@ -455,10 +455,10 @@ public sealed class CpuTileConvolverTests
for (var pixel = 0; pixel < pixels; pixel++)
{
Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 2] * gain), output[pixel * 4]);
Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 1] * gain), output[pixel * 4 + 1]);
Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4] * gain), output[pixel * 4 + 2]);
Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 3] * gain), output[pixel * 4 + 3]);
Assert.Equal(CpuPixels.ToUnorm(stored[pixel * 4 + 2] * gain), output[pixel * 4]);
Assert.Equal(CpuPixels.ToUnorm(stored[pixel * 4 + 1] * gain), output[pixel * 4 + 1]);
Assert.Equal(CpuPixels.ToUnorm(stored[pixel * 4] * gain), output[pixel * 4 + 2]);
Assert.Equal(CpuPixels.ToUnorm(stored[pixel * 4 + 3] * gain), output[pixel * 4 + 3]);
}
}
+4 -4
ファイルの表示
@@ -139,10 +139,10 @@ public sealed class GpuTileConvolverTests
for (var index = 0; index < run.Output.Length; index += 4)
{
Assert.InRange(run.Output[index] - CpuTileConvolver.ToUnorm(run.Store[index + 2] * 2.5f), -1, 1);
Assert.InRange(run.Output[index + 1] - CpuTileConvolver.ToUnorm(run.Store[index + 1] * 2.5f), -1, 1);
Assert.InRange(run.Output[index + 2] - CpuTileConvolver.ToUnorm(run.Store[index] * 2.5f), -1, 1);
Assert.InRange(run.Output[index + 3] - CpuTileConvolver.ToUnorm(run.Store[index + 3] * 2.5f), -1, 1);
Assert.InRange(run.Output[index] - CpuPixels.ToUnorm(run.Store[index + 2] * 2.5f), -1, 1);
Assert.InRange(run.Output[index + 1] - CpuPixels.ToUnorm(run.Store[index + 1] * 2.5f), -1, 1);
Assert.InRange(run.Output[index + 2] - CpuPixels.ToUnorm(run.Store[index] * 2.5f), -1, 1);
Assert.InRange(run.Output[index + 3] - CpuPixels.ToUnorm(run.Store[index + 3] * 2.5f), -1, 1);
}
}
+1 -1
ファイルの表示
@@ -229,7 +229,7 @@ public sealed class LightTransformTests
{
var (blue, green, red, alpha) = LightTransform.ToBytes(value, value, value, value, 1.5f, default, 3, 4);
var expected = CpuTileConvolver.ToUnorm(value * 1.5f);
var expected = CpuPixels.ToUnorm(value * 1.5f);
Assert.Equal(expected, red);
Assert.Equal(expected, green);
Assert.Equal(expected, blue);
+116
ファイルの表示
@@ -0,0 +1,116 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
namespace SpectralConvolution;
internal static class CpuPixels
{
const int Channels = 4;
static readonly float[] Units = BuildUnits();
public static byte ToUnorm(float value)
{
if (float.IsNaN(value))
return 0;
return (byte)(Math.Clamp(value, 0f, 1f) * ByteColor.Scale + ByteColor.Rounding);
}
public static bool LoadRow(Span<float> row, ReadOnlySpan<byte> pixels, ReadOnlySpan<int> order, int sourceX, int left, int first, int last, in LightOptions light)
{
row.Clear();
var lit = false;
var units = Units;
for (var x = first; x < last; x++)
{
var offset = (left + x - sourceX) * Channels;
var blue = pixels[offset];
var green = pixels[offset + 1];
var red = pixels[offset + 2];
var alpha = pixels[offset + 3];
if ((blue | green | red | alpha) == 0)
continue;
var target = order[x] * Channels;
if (light.Linear)
{
var (linearRed, linearGreen, linearBlue, linearAlpha) = LightTransform.FromBytes(red, green, blue, alpha, in light);
row[target] = linearRed;
row[target + 1] = linearGreen;
row[target + 2] = linearBlue;
row[target + 3] = linearAlpha;
}
else
{
row[target] = units[red];
row[target + 1] = units[green];
row[target + 2] = units[blue];
row[target + 3] = units[alpha];
}
lit = true;
}
return lit;
}
public static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, Span<byte> bytes, Span<float> store)
{
var count = pixels.Length / Channels;
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
throw new ArgumentException(null, nameof(bytes));
ref var from = ref MemoryMarshal.GetReference(pixels);
ref var to = ref MemoryMarshal.GetReference(bytes);
ref var kept = ref MemoryMarshal.GetReference(store);
var scaleVector = Vector128.Create(scale);
var gainVector = Vector128.Create(gain);
var keep = !store.IsEmpty;
for (var pixel = 0; pixel < count; pixel++)
{
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
if (keep)
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), PackBgra(value * gainVector));
}
}
public static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, in LightOptions light, int x, int y, Span<byte> bytes, Span<float> store)
{
var count = pixels.Length / Channels;
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
throw new ArgumentException(null, nameof(bytes));
ref var from = ref MemoryMarshal.GetReference(pixels);
ref var to = ref MemoryMarshal.GetReference(bytes);
ref var kept = ref MemoryMarshal.GetReference(store);
var scaleVector = Vector128.Create(scale);
var keep = !store.IsEmpty;
for (var pixel = 0; pixel < count; pixel++)
{
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
if (keep)
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), LightTransform.ToPacked(value, gain, in light, x + pixel, y));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static uint PackBgra(Vector128<float> rgba)
{
var ordered = Vector128.Shuffle(rgba, Vector128.Create(2, 1, 0, 3));
ordered = Vector128.ConditionalSelect(Vector128.Equals(ordered, ordered), ordered, Vector128<float>.Zero);
var clamped = Vector128.Min(Vector128.Max(ordered, Vector128<float>.Zero), Vector128<float>.One);
var levels = Vector128.ConvertToInt32(clamped * Vector128.Create(ByteColor.Scale) + Vector128.Create(ByteColor.Rounding)).AsUInt32();
var words = Vector128.Narrow(levels, levels);
return Vector128.Narrow(words, words).AsUInt32().ToScalar();
}
static float[] BuildUnits()
{
var units = new float[ByteColor.Levels];
for (var level = 0; level < units.Length; level++)
units[level] = level / ByteColor.Scale;
return units;
}
}
+6 -114
ファイルの表示
@@ -11,8 +11,6 @@ internal sealed class CpuTileConvolver : IDisposable
public const int StoredChunkPixels = 16384;
const int Channels = 4;
static readonly float[] Units = BuildUnits();
readonly Worker[] workers;
readonly WorkerPool pool;
readonly ConvolveJob convolveJob;
@@ -217,21 +215,6 @@ internal sealed class CpuTileConvolver : IDisposable
}
}
static float[] BuildUnits()
{
var units = new float[ByteColor.Levels];
for (var level = 0; level < units.Length; level++)
units[level] = level / ByteColor.Scale;
return units;
}
public static byte ToUnorm(float value)
{
if (float.IsNaN(value))
return 0;
return (byte)(Math.Clamp(value, 0f, 1f) * ByteColor.Scale + ByteColor.Rounding);
}
public void Dispose()
{
if (disposed)
@@ -273,6 +256,7 @@ internal sealed class CpuTileConvolver : IDisposable
var order = reversal.AsSpan(0, size);
var twiddles = chromatic is { } multi ? multi.Twiddles : spectrum!.Twiddles;
var values = spectrum is { } single ? single.Spectrum : default;
var light = this.light;
var lit = false;
var first = Math.Max(0, sourceX - left);
@@ -281,7 +265,7 @@ internal sealed class CpuTileConvolver : IDisposable
{
var row = work.Slice(y * rowLength, rowLength);
var sourceRow = top + y - sourceY;
if (sourceRow < 0 || sourceRow >= sourceHeight || first >= last || !LoadRow(row, order, sourceRow, left, first, last))
if (sourceRow < 0 || sourceRow >= sourceHeight || first >= last || !CpuPixels.LoadRow(row, source.AsSpan(sourceRow * sourceWidth * Channels, sourceWidth * Channels), order, sourceX, left, first, last, in light))
{
row.Clear();
continue;
@@ -322,7 +306,6 @@ internal sealed class CpuTileConvolver : IDisposable
}
var scale = 1f / (size * size);
var light = this.light;
var outputOrigin = plan.RegionX + regionLeft;
for (var y = radius; y < radius + validHeight; y++)
{
@@ -338,9 +321,9 @@ internal sealed class CpuTileConvolver : IDisposable
var bytes = output.AsSpan(index, validWidth * Channels);
var store = convolved is { } convolvedValues ? convolvedValues.AsSpan(index, validWidth * Channels) : default;
if (light.IsDefault)
Emit(pixels, scale, gain, bytes, store);
CpuPixels.Emit(pixels, scale, gain, bytes, store);
else
Emit(pixels, scale, gain, in light, outputOrigin, plan.RegionY + outputRow, bytes, store);
CpuPixels.Emit(pixels, scale, gain, in light, outputOrigin, plan.RegionY + outputRow, bytes, store);
}
}
@@ -421,58 +404,6 @@ internal sealed class CpuTileConvolver : IDisposable
}
}
static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, Span<byte> bytes, Span<float> store)
{
var count = pixels.Length / Channels;
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
throw new ArgumentException(null, nameof(bytes));
ref var from = ref MemoryMarshal.GetReference(pixels);
ref var to = ref MemoryMarshal.GetReference(bytes);
ref var kept = ref MemoryMarshal.GetReference(store);
var scaleVector = Vector128.Create(scale);
var gainVector = Vector128.Create(gain);
var keep = !store.IsEmpty;
for (var pixel = 0; pixel < count; pixel++)
{
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
if (keep)
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), PackBgra(value * gainVector));
}
}
static void Emit(ReadOnlySpan<float> pixels, float scale, float gain, in LightOptions light, int x, int y, Span<byte> bytes, Span<float> store)
{
var count = pixels.Length / Channels;
if (bytes.Length < count * Channels || (!store.IsEmpty && store.Length < count * Channels))
throw new ArgumentException(null, nameof(bytes));
ref var from = ref MemoryMarshal.GetReference(pixels);
ref var to = ref MemoryMarshal.GetReference(bytes);
ref var kept = ref MemoryMarshal.GetReference(store);
var scaleVector = Vector128.Create(scale);
var keep = !store.IsEmpty;
for (var pixel = 0; pixel < count; pixel++)
{
var value = Vector128.LoadUnsafe(ref from, (nuint)(pixel * Channels)) * scaleVector;
if (keep)
value.StoreUnsafe(ref kept, (nuint)(pixel * Channels));
Unsafe.WriteUnaligned(ref Unsafe.Add(ref to, pixel * Channels), LightTransform.ToPacked(value, gain, in light, x + pixel, y));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static uint PackBgra(Vector128<float> rgba)
{
var ordered = Vector128.Shuffle(rgba, Vector128.Create(2, 1, 0, 3));
ordered = Vector128.ConditionalSelect(Vector128.Equals(ordered, ordered), ordered, Vector128<float>.Zero);
var clamped = Vector128.Min(Vector128.Max(ordered, Vector128<float>.Zero), Vector128<float>.One);
var levels = Vector128.ConvertToInt32(clamped * Vector128.Create(ByteColor.Scale) + Vector128.Create(ByteColor.Rounding)).AsUInt32();
var words = Vector128.Narrow(levels, levels);
return Vector128.Narrow(words, words).AsUInt32().ToScalar();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Copy(ref float from, int fromIndex, ref float to, int toIndex)
=> Vector128.LoadUnsafe(ref from, (nuint)fromIndex).StoreUnsafe(ref to, (nuint)toIndex);
@@ -503,7 +434,7 @@ internal sealed class CpuTileConvolver : IDisposable
var bytes = output.AsSpan(first * Channels, count * Channels);
if (light.IsDefault)
{
Emit(pixels, 1f, gain, bytes, default);
CpuPixels.Emit(pixels, 1f, gain, bytes, default);
return;
}
@@ -512,7 +443,7 @@ internal sealed class CpuTileConvolver : IDisposable
for (var done = 0; done < count;)
{
var run = storedWidth > 0 ? Math.Min(count - done, storedWidth - column) : count - done;
Emit(pixels.Slice(done * Channels, run * Channels), 1f, gain, in light, storedOriginX + column, storedOriginY + row,
CpuPixels.Emit(pixels.Slice(done * Channels, run * Channels), 1f, gain, in light, storedOriginX + column, storedOriginY + row,
bytes.Slice(done * Channels, run * Channels), default);
done += run;
column = 0;
@@ -520,45 +451,6 @@ internal sealed class CpuTileConvolver : IDisposable
}
}
bool LoadRow(Span<float> row, ReadOnlySpan<int> order, int sourceRow, int left, int first, int last)
{
row.Clear();
var lit = false;
var pixels = source.AsSpan(sourceRow * sourceWidth * Channels, sourceWidth * Channels);
var light = this.light;
var units = Units;
for (var x = first; x < last; x++)
{
var offset = (left + x - sourceX) * Channels;
var blue = pixels[offset];
var green = pixels[offset + 1];
var red = pixels[offset + 2];
var alpha = pixels[offset + 3];
if ((blue | green | red | alpha) == 0)
continue;
var target = order[x] * Channels;
if (light.Linear)
{
var (linearRed, linearGreen, linearBlue, linearAlpha) = LightTransform.FromBytes(red, green, blue, alpha, in light);
row[target] = linearRed;
row[target + 1] = linearGreen;
row[target + 2] = linearBlue;
row[target + 3] = linearAlpha;
}
else
{
row[target] = units[red];
row[target + 1] = units[green];
row[target + 2] = units[blue];
row[target + 3] = units[alpha];
}
lit = true;
}
return lit;
}
void ClearValid(int regionLeft, int regionTop, int validWidth, int validHeight)
{
for (var y = 0; y < validHeight; y++)