From 1ccc09eb80c0b9076f39eb75dfa135a04019aca7 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 8 Oct 2026 08:57:09 +0900 Subject: [PATCH] =?UTF-8?q?8=E3=83=93=E3=83=83=E3=83=88=E8=89=B2=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=8F=9B=E3=81=AE=E6=95=B0=E5=80=A4=E3=81=AB=E5=90=8D?= =?UTF-8?q?=E5=89=8D=E3=82=92=E4=BB=98=E3=81=91=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ByteColorTests.cs | 46 +++++++++++++++++++ .../SpectralConvolution/ByteColor.cs | 21 +++++++++ .../SpectralConvolution/CpuTileConvolver.cs | 8 ++-- .../SpectralConvolution/DirectCorrelation.cs | 32 ++++++------- .../SpectralConvolution/GpuLightShaders.cs | 30 ++++++------ .../SpectralConvolution/GpuTileCheck.cs | 4 +- .../SpectralConvolution/GpuTileShaders.cs | 13 ++---- .../SpectralConvolution/LightTransform.cs | 26 +++++------ .../SpectralConvolution/SpectralSelfTest.cs | 8 ++-- .../SpectralConvolution/TileInput.cs | 2 +- 10 files changed, 127 insertions(+), 63 deletions(-) create mode 100644 SpectralConvolution/SpectralConvolution.Tests/ByteColorTests.cs create mode 100644 SpectralConvolution/SpectralConvolution/ByteColor.cs diff --git a/SpectralConvolution/SpectralConvolution.Tests/ByteColorTests.cs b/SpectralConvolution/SpectralConvolution.Tests/ByteColorTests.cs new file mode 100644 index 0000000..037cba9 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution.Tests/ByteColorTests.cs @@ -0,0 +1,46 @@ +using ComputeWeave; + +namespace SpectralConvolution.Tests; + +public sealed class ByteColorTests +{ + [Fact] + public void TheByteRangeIsZeroToTwoHundredFiftyFive() + { + Assert.Equal(256, ByteColor.Levels); + Assert.Equal(255, ByteColor.Maximum); + Assert.Equal(255u, ByteColor.Mask); + Assert.Equal(255f, ByteColor.Scale); + Assert.Equal(255d, ByteColor.ScaleDouble); + } + + [Theory] + [InlineData(0f, 0u)] + [InlineData(1f, 255u)] + [InlineData(0.5f, 128u)] + [InlineData(0.498f, 127u)] + [InlineData(0.002f, 1u)] + [InlineData(0.001f, 0u)] + public void AValueBecomesTheNearestLevel(float value, uint expected) + { + Assert.Equal(expected, ByteColor.ToLevel(value)); + } + + [Fact] + public void ChannelsArePackedFromTheLowestByteFirst() + { + var packed = ByteColor.Pack(new Float4(1f / 255f, 2f / 255f, 3f / 255f, 4f / 255f)); + + Assert.Equal(0x04030201u, packed); + for (var index = 0; index < 4; index++) + Assert.Equal((uint)index + 1u, ByteColor.Channel(packed, index)); + } + + [Fact] + public void PackingMatchesTheDirectCorrelationLayout() + { + var packed = ByteColor.Pack(new Float4(10f / 255f, 20f / 255f, 30f / 255f, 40f / 255f)); + + Assert.Equal(DirectCorrelation.Pack(10, 20, 30, 40), packed); + } +} diff --git a/SpectralConvolution/SpectralConvolution/ByteColor.cs b/SpectralConvolution/SpectralConvolution/ByteColor.cs new file mode 100644 index 0000000..d60ad5d --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/ByteColor.cs @@ -0,0 +1,21 @@ +using ComputeWeave; + +namespace SpectralConvolution; + +internal static class ByteColor +{ + public const int Bits = 8; + public const int Levels = 1 << Bits; + public const int Maximum = Levels - 1; + public const uint Mask = Maximum; + public const float Scale = Maximum; + public const double ScaleDouble = Maximum; + public const float Rounding = 0.5f; + + public static uint ToLevel(float value) => (uint)(value * Scale + Rounding); + + public static uint Channel(uint packed, int index) => packed >> (index * Bits) & Mask; + + public static uint Pack(Float4 value) + => ToLevel(value.X) | (ToLevel(value.Y) << Bits) | (ToLevel(value.Z) << (2 * Bits)) | (ToLevel(value.W) << (3 * Bits)); +} diff --git a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs index 155093d..26053a5 100644 --- a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs +++ b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs @@ -242,9 +242,9 @@ internal sealed class CpuTileConvolver : IDisposable static float[] BuildUnits() { - var units = new float[256]; + var units = new float[ByteColor.Levels]; for (var level = 0; level < units.Length; level++) - units[level] = level / 255f; + units[level] = level / ByteColor.Scale; return units; } @@ -252,7 +252,7 @@ internal sealed class CpuTileConvolver : IDisposable { if (float.IsNaN(value)) return 0; - return (byte)(Math.Clamp(value, 0f, 1f) * 255f + 0.5f); + return (byte)(Math.Clamp(value, 0f, 1f) * ByteColor.Scale + ByteColor.Rounding); } public void Dispose() @@ -539,7 +539,7 @@ internal sealed class CpuTileConvolver : IDisposable 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(255f) + Vector128.Create(0.5f)).AsUInt32(); + 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(); } diff --git a/SpectralConvolution/SpectralConvolution/DirectCorrelation.cs b/SpectralConvolution/SpectralConvolution/DirectCorrelation.cs index 337a38c..f859651 100644 --- a/SpectralConvolution/SpectralConvolution/DirectCorrelation.cs +++ b/SpectralConvolution/SpectralConvolution/DirectCorrelation.cs @@ -5,7 +5,7 @@ internal static class DirectCorrelation public const int Channels = 4; public static uint Pack(byte red, byte green, byte blue, byte alpha) - => red | (uint)green << 8 | (uint)blue << 16 | (uint)alpha << 24; + => red | (uint)green << ByteColor.Bits | (uint)blue << 2 * ByteColor.Bits | (uint)alpha << 3 * ByteColor.Bits; public static void Evaluate(ReadOnlySpan neighborhood, ReadOnlySpan kernel, Span result) { @@ -22,10 +22,10 @@ internal static class DirectCorrelation { var packed = neighborhood[index]; var weight = kernel[index]; - red += (packed & 255u) / 255d * weight; - green += (packed >> 8 & 255u) / 255d * weight; - blue += (packed >> 16 & 255u) / 255d * weight; - alpha += (packed >> 24) / 255d * weight; + red += ByteColor.Channel(packed, 0) / ByteColor.ScaleDouble * weight; + green += ByteColor.Channel(packed, 1) / ByteColor.ScaleDouble * weight; + blue += ByteColor.Channel(packed, 2) / ByteColor.ScaleDouble * weight; + alpha += ByteColor.Channel(packed, 3) / ByteColor.ScaleDouble * weight; } result[0] = red; @@ -56,7 +56,7 @@ internal static class DirectCorrelation var packed = neighborhood[index]; var weight = kernel[index]; var (linearRed, linearGreen, linearBlue, linearAlpha) = LightTransform.FromBytes( - (byte)(packed & 255u), (byte)(packed >> 8 & 255u), (byte)(packed >> 16 & 255u), (byte)(packed >> 24), in light); + (byte)ByteColor.Channel(packed, 0), (byte)ByteColor.Channel(packed, 1), (byte)ByteColor.Channel(packed, 2), (byte)ByteColor.Channel(packed, 3), in light); red += linearRed * weight; green += linearGreen * weight; blue += linearBlue * weight; @@ -95,10 +95,10 @@ internal static class DirectCorrelation continue; var weight = kernel[(offsetY + radius) * kernelSize + offsetX + radius]; var offset = (sourceY * width + sourceX) * Channels; - red += bgra[offset + 2] / 255d * weight; - green += bgra[offset + 1] / 255d * weight; - blue += bgra[offset] / 255d * weight; - alpha += bgra[offset + 3] / 255d * weight; + red += bgra[offset + 2] / ByteColor.ScaleDouble * weight; + green += bgra[offset + 1] / ByteColor.ScaleDouble * weight; + blue += bgra[offset] / ByteColor.ScaleDouble * weight; + alpha += bgra[offset + 3] / ByteColor.ScaleDouble * weight; } } @@ -129,8 +129,8 @@ internal static class DirectCorrelation { var packed = neighborhood[index]; var (linearRed, linearGreen, linearBlue, linearAlpha) = light.Linear - ? LightTransform.FromBytes((byte)(packed & 255u), (byte)(packed >> 8 & 255u), (byte)(packed >> 16 & 255u), (byte)(packed >> 24), in light) - : ((packed & 255u) / 255d, (packed >> 8 & 255u) / 255d, (packed >> 16 & 255u) / 255d, (packed >> 24) / 255d); + ? LightTransform.FromBytes((byte)ByteColor.Channel(packed, 0), (byte)ByteColor.Channel(packed, 1), (byte)ByteColor.Channel(packed, 2), (byte)ByteColor.Channel(packed, 3), in light) + : (ByteColor.Channel(packed, 0) / ByteColor.ScaleDouble, ByteColor.Channel(packed, 1) / ByteColor.ScaleDouble, ByteColor.Channel(packed, 2) / ByteColor.ScaleDouble, ByteColor.Channel(packed, 3) / ByteColor.ScaleDouble); red += linearRed * redKernel[index]; green += linearGreen * greenKernel[index]; blue += linearBlue * blueKernel[index]; @@ -179,10 +179,10 @@ internal static class DirectCorrelation continue; var index = (offsetY + radius) * kernelSize + offsetX + radius; var offset = (sourceY * width + sourceX) * Channels; - red += bgra[offset + 2] / 255d * redKernel[index]; - green += bgra[offset + 1] / 255d * greenKernel[index]; - blue += bgra[offset] / 255d * blueKernel[index]; - alpha += bgra[offset + 3] / 255d * greenKernel[index]; + red += bgra[offset + 2] / ByteColor.ScaleDouble * redKernel[index]; + green += bgra[offset + 1] / ByteColor.ScaleDouble * greenKernel[index]; + blue += bgra[offset] / ByteColor.ScaleDouble * blueKernel[index]; + alpha += bgra[offset + 3] / ByteColor.ScaleDouble * greenKernel[index]; } } diff --git a/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs b/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs index 6e2e43b..dc522b0 100644 --- a/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs +++ b/SpectralConvolution/SpectralConvolution/GpuLightShaders.cs @@ -39,8 +39,8 @@ internal static class LightShaderOutput public static float Quantize(float value, int dither, float threshold) { if (dither == 0) - return Hlsl.Floor(value * 255f + 0.5f); - return Hlsl.Min(Hlsl.Floor(value * 255f + threshold), 255f); + return Hlsl.Floor(value * ByteColor.Scale + ByteColor.Rounding); + return Hlsl.Min(Hlsl.Floor(value * ByteColor.Scale + threshold), ByteColor.Scale); } public static Float4 Convert(Float4 value, float gain, int linear, int dither, int x, int y) @@ -71,10 +71,10 @@ internal static class LightShaderOutput if (dither != 0) threshold = DitherThreshold(x, y); return new Float4( - Quantize(red, dither, threshold) / 255f, - Quantize(green, dither, threshold) / 255f, - Quantize(blue, dither, threshold) / 255f, - Quantize(alpha, dither, threshold) / 255f); + Quantize(red, dither, threshold) / ByteColor.Scale, + Quantize(green, dither, threshold) / ByteColor.Scale, + Quantize(blue, dither, threshold) / ByteColor.Scale, + Quantize(alpha, dither, threshold) / ByteColor.Scale); } } @@ -251,18 +251,18 @@ internal readonly partial struct ForwardRowLightShader( if (linear == 0) return value; - var red = (float)(uint)(value.X * 255f + 0.5f); - var green = (float)(uint)(value.Y * 255f + 0.5f); - var blue = (float)(uint)(value.Z * 255f + 0.5f); - var alpha = (float)(uint)(value.W * 255f + 0.5f); + var red = (float)ByteColor.ToLevel(value.X); + var green = (float)ByteColor.ToLevel(value.Y); + var blue = (float)ByteColor.ToLevel(value.Z); + var alpha = (float)ByteColor.ToLevel(value.W); if (alpha == 0f) return new Float4(0f, 0f, 0f, 0f); - var opacity = alpha / 255f; - var straightRed = red / 255f; - var straightGreen = green / 255f; - var straightBlue = blue / 255f; - if (alpha != 255f) + var opacity = alpha / ByteColor.Scale; + var straightRed = red / ByteColor.Scale; + var straightGreen = green / ByteColor.Scale; + var straightBlue = blue / ByteColor.Scale; + if (alpha != ByteColor.Scale) { straightRed = Hlsl.Min(red / alpha, 1f); straightGreen = Hlsl.Min(green / alpha, 1f); diff --git a/SpectralConvolution/SpectralConvolution/GpuTileCheck.cs b/SpectralConvolution/SpectralConvolution/GpuTileCheck.cs index 1b53283..2164928 100644 --- a/SpectralConvolution/SpectralConvolution/GpuTileCheck.cs +++ b/SpectralConvolution/SpectralConvolution/GpuTileCheck.cs @@ -101,7 +101,7 @@ internal static class GpuTileCheck for (var group = 0; group < plan.GroupCount; group++) measured += BitConverter.UInt32BitsToSingle(report[layout.SumsOffset + group * GpuTileLayout.SumsPerGroup + channel]); - var reference = floating ? floatTotals[channel] : totals[channel] / 255d; + var reference = floating ? floatTotals[channel] : totals[channel] / ByteColor.ScaleDouble; var magnitude = reference * (1d + kernelRounding) + errors[channel]; var tolerance = errors[channel] + groupRounding * magnitude + groupFlushes + accumulation * ((1d + groupRounding) * magnitude + groupFlushes) + reference * kernelRounding; @@ -164,6 +164,6 @@ internal static class GpuTileCheck blueAlpha += report[offset + 5]; } - return (Math.Sqrt(redGreen) / 255d, Math.Sqrt(blueAlpha) / 255d); + return (Math.Sqrt(redGreen) / ByteColor.ScaleDouble, Math.Sqrt(blueAlpha) / ByteColor.ScaleDouble); } } diff --git a/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs b/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs index 5fe26a4..6ba273b 100644 --- a/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs +++ b/SpectralConvolution/SpectralConvolution/GpuTileShaders.cs @@ -86,10 +86,7 @@ internal readonly partial struct GatherShader( if (x >= 0 && x < sourceWidth && y >= 0 && y < sourceHeight) { var value = source[new Int2(x, y)]; - packed = (uint)(value.X * 255f + 0.5f) - | ((uint)(value.Y * 255f + 0.5f) << 8) - | ((uint)(value.Z * 255f + 0.5f) << 16) - | ((uint)(value.W * 255f + 0.5f) << 24); + packed = ByteColor.Pack(value); } report[gatheredOffset + sample * area + index] = packed; @@ -182,10 +179,10 @@ internal readonly partial struct ForwardRowShader( if (column >= 0 && column < sourceWidth && line >= 0 && line < sourceHeight) { value = source[new Int2(column, line)]; - var red = (uint)(value.X * 255f + 0.5f); - var green = (uint)(value.Y * 255f + 0.5f); - var blue = (uint)(value.Z * 255f + 0.5f); - var alpha = (uint)(value.W * 255f + 0.5f); + var red = ByteColor.ToLevel(value.X); + var green = ByteColor.ToLevel(value.Y); + var blue = ByteColor.ToLevel(value.Z); + var alpha = ByteColor.ToLevel(value.W); squareRedGreen += red * red + green * green; squareBlueAlpha += blue * blue + alpha * alpha; if (x >= radius && x < size - radius && y >= radius && y < size - radius diff --git a/SpectralConvolution/SpectralConvolution/LightTransform.cs b/SpectralConvolution/SpectralConvolution/LightTransform.cs index bbbb9cf..0a87242 100644 --- a/SpectralConvolution/SpectralConvolution/LightTransform.cs +++ b/SpectralConvolution/SpectralConvolution/LightTransform.cs @@ -67,22 +67,22 @@ internal static class LightTransform public static (float Red, float Green, float Blue, float Alpha) FromBytes(byte red, byte green, byte blue, byte alpha, in LightOptions options) { if (!options.Linear) - return (red / 255f, green / 255f, blue / 255f, alpha / 255f); + return (red / ByteColor.Scale, green / ByteColor.Scale, blue / ByteColor.Scale, alpha / ByteColor.Scale); if (alpha == 0) return default; - var opacity = alpha / 255f; + var opacity = alpha / ByteColor.Scale; float straightRed; float straightGreen; float straightBlue; float linearRed; float linearGreen; float linearBlue; - if (alpha == 255) + if (alpha == ByteColor.Maximum) { - straightRed = red / 255f; - straightGreen = green / 255f; - straightBlue = blue / 255f; + straightRed = red / ByteColor.Scale; + straightGreen = green / ByteColor.Scale; + straightBlue = blue / ByteColor.Scale; linearRed = OpaqueDecode[red]; linearGreen = OpaqueDecode[green]; linearBlue = OpaqueDecode[blue]; @@ -167,10 +167,10 @@ internal static class LightTransform color = Vector128.ConditionalSelect(AlphaLane, saturated, Vector128.ConditionalSelect(visible, encoded, Vector128.Zero)); } - var scaled = color * Vector128.Create(255f); + var scaled = color * Vector128.Create(ByteColor.Scale); var levels = options.Dither - ? Vector128.ConvertToInt32(Vector128.Min(scaled + Vector128.Create(DitherThreshold(x, y)), Vector128.Create(255f))) - : Vector128.ConvertToInt32(scaled + Vector128.Create(0.5f)); + ? Vector128.ConvertToInt32(Vector128.Min(scaled + Vector128.Create(DitherThreshold(x, y)), Vector128.Create(ByteColor.Scale))) + : Vector128.ConvertToInt32(scaled + Vector128.Create(ByteColor.Rounding)); var ordered = Vector128.Shuffle(levels, Vector128.Create(2, 1, 0, 3)).AsUInt32(); var words = Vector128.Narrow(ordered, ordered); return Vector128.Narrow(words, words).AsUInt32().ToScalar(); @@ -198,17 +198,17 @@ internal static class LightTransform static byte Quantize(float value, bool dither, float threshold) => dither - ? (byte)MathF.Min(value * 255f + threshold, 255f) - : (byte)(value * 255f + 0.5f); + ? (byte)MathF.Min(value * ByteColor.Scale + threshold, ByteColor.Scale) + : (byte)(value * ByteColor.Scale + ByteColor.Rounding); static float Saturate(float value) => float.IsNaN(value) ? 0f : Math.Clamp(value, 0f, 1f); static float[] BuildOpaqueDecode() { - var table = new float[256]; + var table = new float[ByteColor.Levels]; for (var index = 0; index < table.Length; index++) - table[index] = Decode(index / 255f); + table[index] = Decode(index / ByteColor.Scale); return table; } diff --git a/SpectralConvolution/SpectralConvolution/SpectralSelfTest.cs b/SpectralConvolution/SpectralConvolution/SpectralSelfTest.cs index 2594896..ec74d97 100644 --- a/SpectralConvolution/SpectralConvolution/SpectralSelfTest.cs +++ b/SpectralConvolution/SpectralConvolution/SpectralSelfTest.cs @@ -94,11 +94,11 @@ internal static class SpectralSelfTest continue; var hash = Hash((uint)x, (uint)y, (uint)seed); - var alpha = x % 61 < 3 ? 255u : hash & 255u; + var alpha = x % 61 < 3 ? ByteColor.Mask : hash & ByteColor.Mask; var offset = (y * width + x) * 4; - pixels[offset] = (byte)(alpha * (hash >> 8 & 255u) / 255u); - pixels[offset + 1] = (byte)(alpha * (hash >> 16 & 255u) / 255u); - pixels[offset + 2] = (byte)(alpha * (hash >> 24) / 255u); + pixels[offset] = (byte)(alpha * (hash >> 8 & ByteColor.Mask) / ByteColor.Mask); + pixels[offset + 1] = (byte)(alpha * (hash >> 16 & ByteColor.Mask) / ByteColor.Mask); + pixels[offset + 2] = (byte)(alpha * (hash >> 24) / ByteColor.Mask); pixels[offset + 3] = (byte)alpha; } } diff --git a/SpectralConvolution/SpectralConvolution/TileInput.cs b/SpectralConvolution/SpectralConvolution/TileInput.cs index 2289509..cc335f5 100644 --- a/SpectralConvolution/SpectralConvolution/TileInput.cs +++ b/SpectralConvolution/SpectralConvolution/TileInput.cs @@ -36,6 +36,6 @@ internal static class TileInput } } - return (Math.Sqrt(redGreen) / 255d, Math.Sqrt(blueAlpha) / 255d); + return (Math.Sqrt(redGreen) / ByteColor.ScaleDouble, Math.Sqrt(blueAlpha) / ByteColor.ScaleDouble); } }