From b348f039d81dbfb64c31842571b9bc729ae940bd Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 23 Sep 2026 14:48:00 +0900 Subject: [PATCH] =?UTF-8?q?=E6=B5=B8=E9=80=8F=E8=A8=88=E7=AE=97=E3=81=AE?= =?UTF-8?q?=E8=A9=A6=E9=A8=93=E3=82=92=E5=88=86=E3=81=91=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InkDispersionEffectTests.cs | 412 ------------------ .../InkDispersionPipelineTests.cs | 299 +++++++++++++ 2 files changed, 299 insertions(+), 412 deletions(-) create mode 100644 InkDispersion.Tests/InkDispersionPipelineTests.cs diff --git a/InkDispersion.Tests/InkDispersionEffectTests.cs b/InkDispersion.Tests/InkDispersionEffectTests.cs index b716951..b6a5ad1 100644 --- a/InkDispersion.Tests/InkDispersionEffectTests.cs +++ b/InkDispersion.Tests/InkDispersionEffectTests.cs @@ -68,394 +68,6 @@ public sealed class InkDispersionEffectTests Assert.Empty(effect.CreateExoVideoFilters(0, null!)); } - [Fact] - public void TransparentInputYieldsTransparentOutput() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 64; - const int height = 64; - var source = new int[width * height]; - var destination = new int[source.Length]; - Array.Fill(destination, -1); - var parameters = CreateParameters(); - - pipeline.Process(source, destination, width, height, in parameters); - - Assert.All(destination, pixel => Assert.Equal(0, pixel)); - } - - [Fact] - public void SpreadZeroYieldsTransparentOutput() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 96; - const int height = 96; - var source = CreateSquareSource(width, height, 32, 32, 32, 32); - var destination = new int[source.Length]; - Array.Fill(destination, -1); - var parameters = CreateParameters(spread: 0f); - - pipeline.Process(source, destination, width, height, in parameters); - - Assert.All(destination, pixel => Assert.Equal(0, pixel)); - } - - [Fact] - public void FullSpreadProducesInk() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 128; - const int height = 128; - var source = CreateSquareSource(width, height, 48, 48, 32, 32); - var destination = new int[source.Length]; - var parameters = CreateParameters(); - - pipeline.Process(source, destination, width, height, in parameters); - - Assert.True(CountLitPixels(destination) > 0); - } - - [Fact] - public void GpuPipelineIsDeterministic() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 128; - const int height = 128; - var source = CreateSquareSource(width, height, 48, 48, 32, 32); - var first = new int[source.Length]; - var second = new int[source.Length]; - var parameters = CreateParameters(fiber: 0.8f, seed: 42); - - pipeline.Process(source, first, width, height, in parameters); - pipeline.Process(source, second, width, height, in parameters); - - Assert.Equal(first, second); - } - - [Fact] - public void DifferentSeedsProduceDifferentInkMarks() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 128; - const int height = 128; - var source = CreateSquareSource(width, height, 48, 48, 32, 32); - var first = new int[source.Length]; - var second = new int[source.Length]; - - var parametersA = CreateParameters(fiber: 0.8f, seed: 1); - var parametersB = CreateParameters(fiber: 0.8f, seed: 2); - pipeline.Process(source, first, width, height, in parametersA); - pipeline.Process(source, second, width, height, in parametersB); - - Assert.NotEqual(first, second); - } - - [Fact] - public void OutputAlphaStaysPremultipliedAndBounded() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 128; - const int height = 128; - var source = CreateSquareSource(width, height, 48, 48, 32, 32); - var destination = new int[source.Length]; - var parameters = CreateParameters(water: 1f, density: 1f, grain: 1f); - - pipeline.Process(source, destination, width, height, in parameters); - - foreach (var pixel in destination) - { - var alpha = (pixel >> 24) & 255; - Assert.InRange((pixel >> 16) & 255, 0, alpha); - Assert.InRange((pixel >> 8) & 255, 0, alpha); - Assert.InRange(pixel & 255, 0, alpha); - } - } - - [Fact] - public void SpreadIncreasesLitArea() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 128; - const int height = 128; - var source = CreateSquareSource(width, height, 48, 48, 32, 32); - var partial = new int[source.Length]; - var full = new int[source.Length]; - - var partialParameters = CreateParameters(spread: 0.1f); - var fullParameters = CreateParameters(spread: 1f); - pipeline.Process(source, partial, width, height, in partialParameters); - pipeline.Process(source, full, width, height, in fullParameters); - - Assert.True(CountLitPixels(full) > CountLitPixels(partial)); - } - - [Fact] - public void ReachLimitsInkExtent() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 192; - const int height = 192; - var source = CreateSquareSource(width, height, 80, 80, 32, 32); - var shortReach = new int[source.Length]; - var longReach = new int[source.Length]; - - var shortParameters = CreateParameters(reachPixels: 12f, water: 1f); - var longParameters = CreateParameters(reachPixels: 48f, water: 1f); - pipeline.Process(source, shortReach, width, height, in shortParameters); - pipeline.Process(source, longReach, width, height, in longParameters); - - var shortMaxY = MaxLitY(shortReach, width, height); - var longMaxY = MaxLitY(longReach, width, height); - Assert.True(shortMaxY > 0); - Assert.True(longMaxY >= shortMaxY); - Assert.True(shortMaxY <= 80 + 32 + 12 + 8); - } - - [Fact] - public void GpuPipelineDoesNotAllocateManagedMemoryAfterWarmup() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 64; - const int height = 64; - var source = CreateSquareSource(width, height, 24, 24, 16, 16); - var destination = new int[source.Length]; - var parameters = CreateParameters(); - for (var iteration = 0; iteration < 4; iteration++) - pipeline.Process(source, destination, width, height, in parameters); - - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - var minimum = long.MaxValue; - for (var iteration = 0; iteration < 16; iteration++) - { - var before = GC.GetAllocatedBytesForCurrentThread(); - pipeline.Process(source, destination, width, height, in parameters); - minimum = Math.Min(minimum, GC.GetAllocatedBytesForCurrentThread() - before); - } - - Assert.Equal(0, minimum); - } - - [Fact] - public void SharedTexturePipelineMatchesPackedBufferPipeline() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 96; - const int height = 96; - var source = CreateSquareSource(width, height, 32, 32, 32, 32); - var expected = new int[source.Length]; - var parameters = CreateParameters(fiber: 0.7f, seed: 11); - pipeline.Process(source, expected, width, height, in parameters); - - var device = GraphicsDevice.GetDefault(); - using var sourceTexture = InteropServices.AllocateSharedReadWriteTexture2D(device, width, height); - using var outputTexture = InteropServices.AllocateSharedReadWriteTexture2D(device, width, height); - var sourcePixels = new Bgra32[source.Length]; - for (var index = 0; index < source.Length; index++) - sourcePixels[index].PackedValue = unchecked((uint)source[index]); - sourceTexture.CopyFrom(sourcePixels); - pipeline.ProcessSharedAndWait(sourceTexture, outputTexture, width, height, in parameters); - var result = new Bgra32[source.Length]; - outputTexture.CopyTo(result); - - for (var index = 0; index < expected.Length; index++) - Assert.Equal(unchecked((uint)expected[index]), result[index].PackedValue); - } - - [Fact] - public void SubmittedSharedTexturePipelineAllocationsAmortizeToZero() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 64; - const int height = 64; - var device = GraphicsDevice.GetDefault(); - using var source = InteropServices.AllocateSharedReadWriteTexture2D(device, width, height); - using var destination = InteropServices.AllocateSharedReadWriteTexture2D(device, width, height); - var parameters = CreateParameters(); - for (var iteration = 0; iteration < 4; iteration++) - pipeline.Process(source, destination, width, height, in parameters); - pipeline.WaitForCompletion(); - - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - var minimum = long.MaxValue; - for (var iteration = 0; iteration < 16; iteration++) - { - var before = GC.GetAllocatedBytesForCurrentThread(); - pipeline.Process(source, destination, width, height, in parameters); - minimum = Math.Min(minimum, GC.GetAllocatedBytesForCurrentThread() - before); - } - pipeline.WaitForCompletion(); - - Assert.Equal(0, minimum); - } - - [Theory] - [InlineData(0.3f)] - [InlineData(1f)] - public void VisibleBoundsCoverAllLitPixelsAndMatchFullRender(float spread) - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 192; - const int height = 192; - var source = CreateSquareSource(width, height, 80, 80, 32, 24); - var full = new int[source.Length]; - var parameters = CreateParameters(spread: spread, reachPixels: 32f, seed: 5); - pipeline.Process(source, full, width, height, in parameters); - - var device = GraphicsDevice.GetDefault(); - using var sourceTexture = device.AllocateReadWriteTexture2D(width, height); - var sourcePixels = new Bgra32[source.Length]; - for (var index = 0; index < source.Length; index++) - sourcePixels[index].PackedValue = unchecked((uint)source[index]); - sourceTexture.CopyFrom(sourcePixels); - - pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in parameters); - Assert.True(pipeline.TryGetVisibleBounds(width, height, in parameters, out var rect)); - Assert.True(rect.Width > 0 && rect.Height > 0); - Assert.True(rect.X >= 0 && rect.Y >= 0); - Assert.True(rect.X + rect.Width <= width && rect.Y + rect.Height <= height); - - for (var y = 0; y < height; y++) - { - for (var x = 0; x < width; x++) - { - if (full[y * width + x] == 0) - continue; - Assert.InRange(x, rect.X, rect.X + rect.Width - 1); - Assert.InRange(y, rect.Y, rect.Y + rect.Height - 1); - } - } - - using var outputTexture = device.AllocateReadWriteTexture2D(rect.Width, rect.Height); - pipeline.RenderVisible(outputTexture, width, height, rect, in parameters); - var result = new Bgra32[rect.Width * rect.Height]; - outputTexture.CopyTo(result); - - for (var y = 0; y < rect.Height; y++) - { - for (var x = 0; x < rect.Width; x++) - { - var expected = unchecked((uint)full[(rect.Y + y) * width + rect.X + x]); - Assert.Equal(expected, result[y * rect.Width + x].PackedValue); - } - } - } - - [Fact] - public void SimulateCachesStructureUntilInputsChange() - { - using var pipeline = InkDispersionPipeline.TryCreate(); - if (pipeline is null) - { - Assert.Skip("Direct3D 12 is unavailable."); - return; - } - - const int width = 128; - const int height = 128; - var source = CreateSquareSource(width, height, 48, 48, 32, 32); - var device = GraphicsDevice.GetDefault(); - using var sourceTexture = device.AllocateReadWriteTexture2D(width, height); - var sourcePixels = new Bgra32[source.Length]; - for (var index = 0; index < source.Length; index++) - sourcePixels[index].PackedValue = unchecked((uint)source[index]); - sourceTexture.CopyFrom(sourcePixels); - - var parameters = CreateParameters(seed: 3); - Assert.True(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in parameters)); - Assert.False(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in parameters)); - - var spreadChanged = parameters with { Spread = 0.5f }; - Assert.False(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in spreadChanged)); - - var densityChanged = parameters with { Density = 0.5f }; - Assert.False(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in densityChanged)); - - var seedChanged = parameters with { Seed = 4 }; - Assert.True(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in seedChanged)); - - var movedSource = CreateSquareSource(width, height, 32, 32, 32, 32); - for (var index = 0; index < movedSource.Length; index++) - sourcePixels[index].PackedValue = unchecked((uint)movedSource[index]); - sourceTexture.CopyFrom(sourcePixels); - Assert.True(pipeline.Simulate(sourceTexture, width, height, 0, 0, width, height, in seedChanged)); - } - [Fact] public void Direct2DInteropProducesInkAfterGrowingFullHdOutput() { @@ -642,28 +254,4 @@ public sealed class InkDispersionEffectTests } return source; } - - private static int CountLitPixels(int[] pixels) - { - var count = 0; - foreach (var pixel in pixels) - { - if (((pixel >> 24) & 255) > 8) - count++; - } - return count; - } - - private static int MaxLitY(int[] pixels, int width, int height) - { - for (var y = height - 1; y >= 0; y--) - { - for (var x = 0; x < width; x++) - { - if (((pixels[y * width + x] >> 24) & 255) >= 32) - return y; - } - } - return 0; - } } diff --git a/InkDispersion.Tests/InkDispersionPipelineTests.cs b/InkDispersion.Tests/InkDispersionPipelineTests.cs new file mode 100644 index 0000000..ae7d8af --- /dev/null +++ b/InkDispersion.Tests/InkDispersionPipelineTests.cs @@ -0,0 +1,299 @@ +using ComputeWeave; +using ComputeWeave.Interop; + +namespace InkDispersion.Tests; + +[Collection("Direct3D12")] +public sealed class InkDispersionPipelineTests +{ + const int Opaque = unchecked((int)0xFFC0C0C0); + + static InkDispersionPipeline CreatePipeline() + { + var pipeline = InkDispersionPipeline.TryCreate(); + if (pipeline is null) + Assert.Skip("Direct3D 12 is unavailable."); + return pipeline; + } + + static InkDispersionPipeline.Parameters Parameters( + InkDispersionQuality quality = InkDispersionQuality.Balanced, + float spread = 1f, + float water = 0.6f, + float fiber = 0.5f, + float sizing = 0.3f, + float viscosity = 0.3f, + float drying = 0.4f, + float reachPixels = 40f, + float density = 0.8f, + float grain = 0.4f, + int seed = 0) + => new(quality, spread, water, fiber, sizing, viscosity, drying, reachPixels, density, grain, 0.12f, 0.1f, 0.09f, seed); + + static int[] Square(int width, int height, int left, int top, int squareWidth, int squareHeight) + { + var pixels = new int[width * height]; + for (var y = Math.Max(top, 0); y < Math.Min(top + squareHeight, height); y++) + { + for (var x = Math.Max(left, 0); x < Math.Min(left + squareWidth, width); x++) + pixels[y * width + x] = Opaque; + } + + return pixels; + } + + static int Alpha(int pixel) => (pixel >> 24) & 255; + + static int LitPixels(int[] pixels) => pixels.Count(pixel => Alpha(pixel) > 8); + + static int LowestLitRow(int[] pixels, int width) + { + for (var index = pixels.Length - 1; index >= 0; index--) + { + if (Alpha(pixels[index]) >= 32) + return index / width; + } + + return 0; + } + + static void Upload(ReadWriteTexture2D texture, int[] pixels) + => texture.CopyFrom(pixels.Select(pixel => new Bgra32 { PackedValue = unchecked((uint)pixel) }).ToArray()); + + static int[] Render(InkDispersionPipeline pipeline, int[] source, int width, int height, InkDispersionPipeline.Parameters parameters) + { + var destination = new int[source.Length]; + pipeline.Process(source, destination, width, height, in parameters); + return destination; + } + + [Fact] + public void ATransparentSourceProducesNoInk() + { + using var pipeline = CreatePipeline(); + var destination = Enumerable.Repeat(-1, 64 * 64).ToArray(); + var parameters = Parameters(); + + pipeline.Process(new int[64 * 64], destination, 64, 64, in parameters); + + Assert.All(destination, pixel => Assert.Equal(0, pixel)); + } + + [Fact] + public void AZeroSpreadProducesNoInk() + { + using var pipeline = CreatePipeline(); + var source = Square(96, 96, 32, 32, 32, 32); + var destination = Enumerable.Repeat(-1, source.Length).ToArray(); + var parameters = Parameters(spread: 0f); + + pipeline.Process(source, destination, 96, 96, in parameters); + + Assert.All(destination, pixel => Assert.Equal(0, pixel)); + } + + [Fact] + public void AFullSpreadProducesInk() + { + using var pipeline = CreatePipeline(); + + var rendering = Render(pipeline, Square(128, 128, 48, 48, 32, 32), 128, 128, Parameters()); + + Assert.True(LitPixels(rendering) > 0); + } + + [Fact] + public void TheSameSettingsAlwaysProduceTheSameInk() + { + using var pipeline = CreatePipeline(); + var source = Square(128, 128, 48, 48, 32, 32); + + var first = Render(pipeline, source, 128, 128, Parameters(fiber: 0.8f, seed: 42)); + var second = Render(pipeline, source, 128, 128, Parameters(fiber: 0.8f, seed: 42)); + + Assert.Equal(first, second); + } + + [Fact] + public void ADifferentSeedSpreadsTheInkDifferently() + { + using var pipeline = CreatePipeline(); + var source = Square(128, 128, 48, 48, 32, 32); + + var first = Render(pipeline, source, 128, 128, Parameters(fiber: 0.8f, seed: 1)); + var second = Render(pipeline, source, 128, 128, Parameters(fiber: 0.8f, seed: 2)); + + Assert.NotEqual(first, second); + } + + [Fact] + public void TheInkStaysPremultiplied() + { + using var pipeline = CreatePipeline(); + + var rendering = Render(pipeline, Square(128, 128, 48, 48, 32, 32), 128, 128, Parameters(water: 1f, density: 1f, grain: 1f)); + + Assert.All(rendering, pixel => + { + Assert.InRange((pixel >> 16) & 255, 0, Alpha(pixel)); + Assert.InRange((pixel >> 8) & 255, 0, Alpha(pixel)); + Assert.InRange(pixel & 255, 0, Alpha(pixel)); + }); + } + + [Fact] + public void AWiderSpreadCoversMorePixels() + { + using var pipeline = CreatePipeline(); + var source = Square(128, 128, 48, 48, 32, 32); + + var partial = Render(pipeline, source, 128, 128, Parameters(spread: 0.1f)); + var full = Render(pipeline, source, 128, 128, Parameters(spread: 1f)); + + Assert.True(LitPixels(full) > LitPixels(partial)); + } + + [Fact] + public void TheReachLimitsHowFarTheInkTravels() + { + using var pipeline = CreatePipeline(); + var source = Square(192, 192, 80, 80, 32, 32); + + var shortReach = LowestLitRow(Render(pipeline, source, 192, 192, Parameters(reachPixels: 12f, water: 1f)), 192); + var longReach = LowestLitRow(Render(pipeline, source, 192, 192, Parameters(reachPixels: 48f, water: 1f)), 192); + + Assert.True(shortReach > 0); + Assert.True(longReach >= shortReach); + Assert.True(shortReach <= 80 + 32 + 12 + 8); + } + + [Fact] + public void AWarmPipelineAllocatesNoManagedMemory() + { + using var pipeline = CreatePipeline(); + var source = Square(64, 64, 24, 24, 16, 16); + var destination = new int[source.Length]; + var parameters = Parameters(); + for (var iteration = 0; iteration < 4; iteration++) + pipeline.Process(source, destination, 64, 64, in parameters); + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + var minimum = long.MaxValue; + for (var iteration = 0; iteration < 16; iteration++) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + pipeline.Process(source, destination, 64, 64, in parameters); + minimum = Math.Min(minimum, GC.GetAllocatedBytesForCurrentThread() - before); + } + + Assert.Equal(0, minimum); + } + + [Fact] + public void SharedTexturesProduceTheSameInkAsPackedBuffers() + { + using var pipeline = CreatePipeline(); + var source = Square(96, 96, 32, 32, 32, 32); + var parameters = Parameters(fiber: 0.7f, seed: 11); + var expected = Render(pipeline, source, 96, 96, parameters); + var device = GraphicsDevice.GetDefault(); + using var sourceTexture = InteropServices.AllocateSharedReadWriteTexture2D(device, 96, 96); + using var outputTexture = InteropServices.AllocateSharedReadWriteTexture2D(device, 96, 96); + Upload(sourceTexture, source); + + pipeline.ProcessSharedAndWait(sourceTexture, outputTexture, 96, 96, in parameters); + var result = new Bgra32[source.Length]; + outputTexture.CopyTo(result); + + Assert.Equal(expected.Select(pixel => unchecked((uint)pixel)), result.Select(pixel => pixel.PackedValue)); + } + + [Fact] + public void RepeatedSharedTextureSubmissionsAllocateNoManagedMemory() + { + using var pipeline = CreatePipeline(); + var device = GraphicsDevice.GetDefault(); + using var source = InteropServices.AllocateSharedReadWriteTexture2D(device, 64, 64); + using var destination = InteropServices.AllocateSharedReadWriteTexture2D(device, 64, 64); + var parameters = Parameters(); + for (var iteration = 0; iteration < 4; iteration++) + pipeline.Process(source, destination, 64, 64, in parameters); + pipeline.WaitForCompletion(); + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + var minimum = long.MaxValue; + for (var iteration = 0; iteration < 16; iteration++) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + pipeline.Process(source, destination, 64, 64, in parameters); + minimum = Math.Min(minimum, GC.GetAllocatedBytesForCurrentThread() - before); + } + pipeline.WaitForCompletion(); + + Assert.Equal(0, minimum); + } + + [Theory] + [InlineData(0.3f)] + [InlineData(1f)] + public void TheVisibleBoundsHoldEveryInkedPixelAndRenderTheSameInk(float spread) + { + using var pipeline = CreatePipeline(); + var source = Square(192, 192, 80, 80, 32, 24); + var parameters = Parameters(spread: spread, reachPixels: 32f, seed: 5); + var full = Render(pipeline, source, 192, 192, parameters); + var device = GraphicsDevice.GetDefault(); + using var sourceTexture = device.AllocateReadWriteTexture2D(192, 192); + Upload(sourceTexture, source); + + pipeline.Simulate(sourceTexture, 192, 192, 0, 0, 192, 192, in parameters); + Assert.True(pipeline.TryGetVisibleBounds(192, 192, in parameters, out var rect)); + using var outputTexture = device.AllocateReadWriteTexture2D(rect.Width, rect.Height); + pipeline.RenderVisible(outputTexture, 192, 192, rect, in parameters); + var visible = new Bgra32[rect.Width * rect.Height]; + outputTexture.CopyTo(visible); + + Assert.True(rect is { Width: > 0, Height: > 0, X: >= 0, Y: >= 0 }); + Assert.True(rect.X + rect.Width <= 192 && rect.Y + rect.Height <= 192); + for (var y = 0; y < 192; y++) + { + for (var x = 0; x < 192; x++) + { + var inside = x >= rect.X && x < rect.X + rect.Width && y >= rect.Y && y < rect.Y + rect.Height; + if (!inside) + Assert.Equal(0, full[y * 192 + x]); + else + Assert.Equal(unchecked((uint)full[y * 192 + x]), visible[(y - rect.Y) * rect.Width + x - rect.X].PackedValue); + } + } + } + + [Fact] + public void TheFlowIsSimulatedAgainOnlyWhenTheShapeOrTheFlowSettingsChange() + { + using var pipeline = CreatePipeline(); + var device = GraphicsDevice.GetDefault(); + using var sourceTexture = device.AllocateReadWriteTexture2D(128, 128); + Upload(sourceTexture, Square(128, 128, 48, 48, 32, 32)); + var parameters = Parameters(seed: 3); + + Assert.True(pipeline.Simulate(sourceTexture, 128, 128, 0, 0, 128, 128, in parameters)); + Assert.False(pipeline.Simulate(sourceTexture, 128, 128, 0, 0, 128, 128, in parameters)); + + var spreadChanged = parameters with { Spread = 0.5f }; + Assert.False(pipeline.Simulate(sourceTexture, 128, 128, 0, 0, 128, 128, in spreadChanged)); + + var densityChanged = parameters with { Density = 0.5f }; + Assert.False(pipeline.Simulate(sourceTexture, 128, 128, 0, 0, 128, 128, in densityChanged)); + + var seedChanged = parameters with { Seed = 4 }; + Assert.True(pipeline.Simulate(sourceTexture, 128, 128, 0, 0, 128, 128, in seedChanged)); + + Upload(sourceTexture, Square(128, 128, 32, 32, 32, 32)); + Assert.True(pipeline.Simulate(sourceTexture, 128, 128, 0, 0, 128, 128, in seedChanged)); + } +}