diff --git a/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs b/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs index 865a8c1..3a25e0a 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/ConvolutionScene.cs @@ -3,9 +3,11 @@ namespace SpectralConvolution.Tests; internal sealed class ConvolutionScene { (double RedGreen, double BlueAlpha)[]? tileNorms; + float[]? transformed; - ConvolutionScene(byte[] source, int sourceX, int sourceY, int sourceWidth, int sourceHeight, KernelSpectrum spectrum, TilePlan plan) + ConvolutionScene(byte[] source, int sourceX, int sourceY, int sourceWidth, int sourceHeight, KernelSpectrum spectrum, TilePlan plan, LightOptions light) { + Light = light; Source = source; SourceX = sourceX; SourceY = sourceY; @@ -15,6 +17,8 @@ internal sealed class ConvolutionScene Plan = plan; } + public LightOptions Light { get; } + public byte[] Source { get; } public int SourceX { get; } @@ -73,23 +77,96 @@ internal sealed class ConvolutionScene return pixels; } - public static ConvolutionScene Create(byte[] source, int sourceWidth, int sourceHeight, int margin, int radius, int size) + public static ConvolutionScene Create(byte[] source, int sourceWidth, int sourceHeight, int margin, int radius, int size, LightOptions light = default) { var spectrum = new KernelSpectrum(); spectrum.Update(AsymmetricKernel(radius), radius, size); var plan = TilePlan.Create(size, radius, 0, 0, sourceWidth + margin * 2, sourceHeight + margin * 2); - return new ConvolutionScene(source, margin, margin, sourceWidth, sourceHeight, spectrum, plan); + return new ConvolutionScene(source, margin, margin, sourceWidth, sourceHeight, spectrum, plan, light); } public void Reference(int x, int y, Span result) - => DirectCorrelation.Evaluate(Source, SourceWidth, SourceHeight, Spectrum.Kernel, Spectrum.Radius, x - SourceX, y - SourceY, result); + { + if (!Light.Linear) + { + DirectCorrelation.Evaluate(Source, SourceWidth, SourceHeight, Spectrum.Kernel, Spectrum.Radius, x - SourceX, y - SourceY, result); + return; + } + + var values = Transformed(); + var radius = Spectrum.Radius; + var kernelSize = radius * 2 + 1; + var kernel = Spectrum.Kernel; + result[..4].Clear(); + for (var offsetY = -radius; offsetY <= radius; offsetY++) + { + var sourceY = y - SourceY + offsetY; + if (sourceY < 0 || sourceY >= SourceHeight) + continue; + for (var offsetX = -radius; offsetX <= radius; offsetX++) + { + var sourceX = x - SourceX + offsetX; + if (sourceX < 0 || sourceX >= SourceWidth) + continue; + var weight = kernel[(offsetY + radius) * kernelSize + offsetX + radius]; + var offset = (sourceY * SourceWidth + sourceX) * 4; + for (var channel = 0; channel < 4; channel++) + result[channel] += values[offset + channel] * weight; + } + } + } public (double RedGreen, double BlueAlpha) Norms(int x, int y) { - tileNorms ??= [.. Enumerable.Range(0, Plan.TileCount).Select(tile => TileInput.Norms(Source, SourceX, SourceY, SourceWidth, SourceHeight, Plan, tile))]; + tileNorms ??= [.. Enumerable.Range(0, Plan.TileCount).Select(TileNorms)]; return tileNorms[Plan.TileAt(x, y)]; } + public float[] Transformed() + { + if (transformed is null) + { + var values = new float[SourceWidth * SourceHeight * 4]; + for (var index = 0; index < SourceWidth * SourceHeight; index++) + { + var (red, green, blue, alpha) = LightTransform.FromBytes(Source[index * 4 + 2], Source[index * 4 + 1], Source[index * 4], Source[index * 4 + 3], Light); + values[index * 4] = red; + values[index * 4 + 1] = green; + values[index * 4 + 2] = blue; + values[index * 4 + 3] = alpha; + } + + transformed = values; + } + + return transformed; + } + + (double RedGreen, double BlueAlpha) TileNorms(int tile) + { + if (!Light.Linear) + return TileInput.Norms(Source, SourceX, SourceY, SourceWidth, SourceHeight, Plan, tile); + + var values = Transformed(); + var left = Plan.OriginX(tile) - Plan.Radius; + var top = Plan.OriginY(tile) - Plan.Radius; + var redGreen = 0d; + var blueAlpha = 0d; + var lastY = Math.Min(top + Plan.Size, SourceY + SourceHeight); + var lastX = Math.Min(left + Plan.Size, SourceX + SourceWidth); + for (var y = Math.Max(top, SourceY); y < lastY; y++) + { + for (var x = Math.Max(left, SourceX); x < lastX; x++) + { + var offset = ((y - SourceY) * SourceWidth + x - SourceX) * 4; + redGreen += (double)values[offset] * values[offset] + (double)values[offset + 1] * values[offset + 1]; + blueAlpha += (double)values[offset + 2] * values[offset + 2] + (double)values[offset + 3] * values[offset + 3]; + } + } + + return (Math.Sqrt(redGreen), Math.Sqrt(blueAlpha)); + } + public double WorstRatio(ReadOnlySpan convolved, double operationError) { var reference = new double[4]; diff --git a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs index b75b978..020f040 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs @@ -262,4 +262,142 @@ public sealed class CpuTileConvolverTests Convolve(convolver, 128, 3, 610); Assert.Equal(24 * PerThread(128), convolver.WorkingBytes); } + + static (byte[] Output, float[] Convolved) RunLight(ConvolutionScene scene, int threads, float gain) + { + var output = new byte[scene.RegionLength]; + var convolved = new float[scene.RegionLength]; + using var convolver = new CpuTileConvolver(threads); + convolver.Convolve(scene.Source, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, scene.Spectrum, scene.Plan, output, gain, convolved, scene.Light); + return (output, convolved); + } + + [Theory] + [InlineData(64, 5, 150, 97, 11, 0f, 0f)] + [InlineData(128, 23, 140, 90, 24, 0.9f, 20f)] + [InlineData(64, 0, 70, 70, 3, 0.5f, 1000f)] + public void ALinearLightConvolutionStaysWithinTheBoundOfTheExactCorrelation(int size, int radius, int width, int height, int margin, float threshold, float boost) + { + var light = new LightOptions(true, false, threshold, boost); + var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(width, height, size + radius, 0.4), width, height, margin, radius, size, light); + + var (_, convolved) = RunLight(scene, 3, 1f); + + Assert.True(scene.Plan.TileCount > 1); + Assert.InRange(scene.WorstRatio(convolved, ConvolutionBound.CpuOperationError), 0d, 1d); + } + + [Theory] + [InlineData(false, 1f)] + [InlineData(true, 1f)] + [InlineData(true, 2.5f)] + public void TheLinearOutputIsTheTransformOfTheConvolution(bool dither, float gain) + { + var light = new LightOptions(true, dither, 0.8f, 8f); + var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(90, 60, 9, 0.7), 90, 60, 8, 4, 64, light); + + var (output, convolved) = RunLight(scene, 2, gain); + + for (var y = 0; y < scene.Plan.RegionHeight; y++) + { + for (var x = 0; x < scene.Plan.RegionWidth; x++) + { + var index = (y * scene.Plan.RegionWidth + x) * 4; + var expected = LightTransform.ToBytes(convolved[index], convolved[index + 1], convolved[index + 2], convolved[index + 3], gain, light, scene.Plan.RegionX + x, scene.Plan.RegionY + y); + Assert.Equal((expected.Blue, expected.Green, expected.Red, expected.Alpha), (output[index], output[index + 1], output[index + 2], output[index + 3])); + } + } + } + + [Theory] + [InlineData(false, false, 1f)] + [InlineData(true, false, 2.75f)] + [InlineData(true, true, 0.3f)] + [InlineData(false, true, 1.5f)] + public void RenderingTheStoreMatchesTheLightConvolutionBitForBit(bool linear, bool dither, float gain) + { + var light = new LightOptions(linear, dither, 0.85f, linear ? 12f : 0f); + var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(130, 90, 13, 0.6), 130, 90, 8, 4, 64, light); + var (expected, _) = RunLight(scene, 3, gain); + var (_, stored) = RunLight(scene, 3, 1f); + var output = new byte[scene.RegionLength]; + using var convolver = new CpuTileConvolver(3); + + convolver.RenderStored(stored, scene.Plan.RegionWidth * scene.Plan.RegionHeight, gain, output, light, scene.Plan.RegionWidth, scene.Plan.RegionX, scene.Plan.RegionY); + + Assert.Equal(expected, output); + } + + [Fact] + public void TheLinearResultDoesNotDependOnTheNumberOfThreads() + { + var light = new LightOptions(true, true, 0.9f, 30f); + var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(300, 170, 5, 0.6), 300, 170, 16, 9, 64, light); + + var single = RunLight(scene, 1, 1.75f); + var many = RunLight(scene, 16, 1.75f); + + Assert.Equal(single.Output, many.Output); + Assert.Equal(single.Convolved, many.Convolved); + } + + [Fact] + public void AnOpaqueWhiteSceneStaysWhiteInLinearLight() + { + var light = new LightOptions(true, true, 0f, 0f); + var scene = ConvolutionScene.Create(ConvolutionScene.Uniform(120, 120, 255), 120, 120, 8, 7, 64, light); + + var (output, convolved) = RunLight(scene, 2, 1f); + + Assert.InRange(scene.WorstRatio(convolved, ConvolutionBound.CpuOperationError), 0d, 1d); + var center = ((scene.Plan.RegionHeight / 2) * scene.Plan.RegionWidth + scene.Plan.RegionWidth / 2) * 4; + Assert.Equal([255, 255, 255, 255], output.AsSpan(center, 4).ToArray()); + } + + [Fact] + public void ABrightPointSpreadsFartherWithHighlights() + { + var pixels = new byte[61 * 61 * 4]; + var center = (30 * 61 + 30) * 4; + pixels[center] = 255; + pixels[center + 1] = 255; + pixels[center + 2] = 255; + pixels[center + 3] = 255; + var plain = new LightOptions(true, false, 0f, 0f); + var boosted = new LightOptions(true, false, 0.9f, 400f); + var spectrum = new KernelSpectrum(); + spectrum.Update(ConvolutionScene.AsymmetricKernel(20), 20, 128); + var plan = TilePlan.Create(128, 20, 0, 0, 101, 101); + using var convolver = new CpuTileConvolver(2); + var plainOutput = new byte[101 * 101 * 4]; + var boostedOutput = new byte[101 * 101 * 4]; + + convolver.Convolve(pixels, 20, 20, 61, 61, spectrum, plan, plainOutput, 1f, null, plain); + convolver.Convolve(pixels, 20, 20, 61, 61, spectrum, plan, boostedOutput, 1f, null, boosted); + + var plainLit = plainOutput.Where((value, index) => index % 4 == 1 && value > 0).Count(); + var boostedLit = boostedOutput.Where((value, index) => index % 4 == 1 && value > 0).Count(); + Assert.True(boostedLit > plainLit * 2, $"{plainLit} {boostedLit}"); + } + + [Fact] + public void ARegionWithDitheringNeedsAWidthToRenderTheStore() + { + using var convolver = new CpuTileConvolver(1); + + Assert.Throws(() => convolver.RenderStored(new float[8], 2, 1f, new byte[8], new LightOptions(false, true, 0f, 0f))); + convolver.RenderStored(new float[8], 2, 1f, new byte[8], new LightOptions(false, true, 0f, 0f), 2); + } + + [Fact] + public void InvalidLightOptionsAreRejected() + { + var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(40, 40, 1, 0.5), 40, 40, 4, 2, 64); + using var convolver = new CpuTileConvolver(1); + var output = new byte[scene.RegionLength]; + + Assert.Throws(() => convolver.Convolve( + scene.Source, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, scene.Spectrum, scene.Plan, output, 1f, null, new LightOptions(true, false, 2f, 5f))); + Assert.Throws(() => convolver.RenderStored(new float[8], 2, 1f, output, new LightOptions(true, false, 0.5f, 5000f))); + } }