From f9d34ba2244df5032cbd0daeffdb5b11e7d355b5 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 8 Oct 2026 09:22:42 +0900 Subject: [PATCH] =?UTF-8?q?CPU=E3=81=AE=E7=94=BB=E7=B4=A0=E3=81=AE8?= =?UTF-8?q?=E3=83=93=E3=83=83=E3=83=88=E3=81=A8=E3=81=AE=E5=A4=89=E6=8F=9B?= =?UTF-8?q?=E3=82=92=E5=88=A5=E3=81=AE=E5=9E=8B=E3=81=B8=E5=88=86=E3=81=91?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpectralConvolution/README.md | 1 + .../CpuTileConvolverTests.cs | 22 ++-- .../GpuTileConvolverTests.cs | 8 +- .../LightTransformTests.cs | 2 +- .../SpectralConvolution/CpuPixels.cs | 116 +++++++++++++++++ .../SpectralConvolution/CpuTileConvolver.cs | 120 +----------------- 6 files changed, 139 insertions(+), 130 deletions(-) create mode 100644 SpectralConvolution/SpectralConvolution/CpuPixels.cs diff --git a/SpectralConvolution/README.md b/SpectralConvolution/README.md index 7dde091..62b6b4e 100644 --- a/SpectralConvolution/README.md +++ b/SpectralConvolution/README.md @@ -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 が書き出した値から作る検査 | diff --git a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs index 50eb7bd..6c56216 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs @@ -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]); } } diff --git a/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs index 21e9bd0..da3ad65 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/GpuTileConvolverTests.cs @@ -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); } } diff --git a/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs b/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs index 50c5ac9..cefe5a4 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/LightTransformTests.cs @@ -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); diff --git a/SpectralConvolution/SpectralConvolution/CpuPixels.cs b/SpectralConvolution/SpectralConvolution/CpuPixels.cs new file mode 100644 index 0000000..839aea1 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/CpuPixels.cs @@ -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 row, ReadOnlySpan pixels, ReadOnlySpan 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 pixels, float scale, float gain, Span bytes, Span 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 pixels, float scale, float gain, in LightOptions light, int x, int y, Span bytes, Span 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 rgba) + { + var ordered = Vector128.Shuffle(rgba, Vector128.Create(2, 1, 0, 3)); + ordered = Vector128.ConditionalSelect(Vector128.Equals(ordered, ordered), ordered, Vector128.Zero); + var clamped = Vector128.Min(Vector128.Max(ordered, Vector128.Zero), Vector128.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; + } +} diff --git a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs index e1f66ab..c0ed172 100644 --- a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs +++ b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs @@ -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 pixels, float scale, float gain, Span bytes, Span 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 pixels, float scale, float gain, in LightOptions light, int x, int y, Span bytes, Span 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 rgba) - { - var ordered = Vector128.Shuffle(rgba, Vector128.Create(2, 1, 0, 3)); - ordered = Vector128.ConditionalSelect(Vector128.Equals(ordered, ordered), ordered, Vector128.Zero); - var clamped = Vector128.Min(Vector128.Max(ordered, Vector128.Zero), Vector128.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 row, ReadOnlySpan 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++)