From 0f26dccd49c3fb32776ae174fdb4c4f973fc03c2 Mon Sep 17 00:00:00 2001 From: routersys Date: Mon, 12 Oct 2026 11:28:14 +0900 Subject: [PATCH] =?UTF-8?q?=E5=89=B2=E3=82=8A=E5=BD=93=E3=81=A6=E3=81=8C?= =?UTF-8?q?=E7=84=A1=E3=81=84=E3=81=93=E3=81=A8=E3=82=92=E8=A9=A6=E9=A8=93?= =?UTF-8?q?=E3=81=A7=E5=AE=88=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpectrumColorShift.Tests/Allocation.cs | 31 ++++++++ SpectrumColorShift.Tests/AllocationTests.cs | 70 +++++++++++++++++++ .../SpectrumColorShiftEffectProcessorTests.cs | 67 ++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 SpectrumColorShift.Tests/Allocation.cs create mode 100644 SpectrumColorShift.Tests/AllocationTests.cs diff --git a/SpectrumColorShift.Tests/Allocation.cs b/SpectrumColorShift.Tests/Allocation.cs new file mode 100644 index 0000000..0f421b1 --- /dev/null +++ b/SpectrumColorShift.Tests/Allocation.cs @@ -0,0 +1,31 @@ +namespace SpectrumColorShift.Tests; + +internal static class Allocation +{ + const int Rounds = 1000; + const int Attempts = 16; + + public static long Of(Action work) + { + var previous = Window(work); + for (var attempt = 1; attempt < Attempts; attempt++) + { + var current = Window(work); + if (current == previous) + return current; + + previous = current; + } + + return previous; + } + + static long Window(Action work) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var round = 0; round < Rounds; round++) + work(); + + return GC.GetAllocatedBytesForCurrentThread() - before; + } +} diff --git a/SpectrumColorShift.Tests/AllocationTests.cs b/SpectrumColorShift.Tests/AllocationTests.cs new file mode 100644 index 0000000..b339716 --- /dev/null +++ b/SpectrumColorShift.Tests/AllocationTests.cs @@ -0,0 +1,70 @@ +using System.Numerics; +using Vortice; +using static SpectrumColorShift.SpectrumColorShiftCustomEffect; + +namespace SpectrumColorShift.Tests; + +public sealed class AllocationTests +{ + [Theory] + [InlineData(ShiftMode.Linear)] + [InlineData(ShiftMode.Radial)] + [InlineData(ShiftMode.Rotational)] + public void MappingTheRectsAllocatesNothing(ShiftMode mode) + { + var impl = new EffectImpl { Mode = (int)mode, Distance = 24f, Angle = 30f }; + RawRect[] inputs = [new RawRect(0, 0, 640, 480)]; + RawRect[] opaque = [new RawRect(0, 0, 640, 480)]; + RawRect[] requested = new RawRect[1]; + var tile = new RawRect(100, 100, 200, 200); + + var allocated = Allocation.Of(() => + { + impl.MapInputRectsToOutputRect(inputs, opaque, out _, out _); + impl.MapOutputRectToInputRects(tile, requested); + impl.MapInvalidRect(0, tile); + }); + + Assert.Equal(0, allocated); + } + + [Fact] + public void ChangingTheSettingsAllocatesNothing() + { + var impl = new EffectImpl(); + var step = 0; + + var allocated = Allocation.Of(() => + { + step++; + impl.Mode = 1 << (step % 3); + impl.Distance = step % 50; + impl.Angle = step % 360; + impl.CenterX = step % 100; + impl.CenterY = step % 100; + impl.Strength = step % 100 / 100f; + impl.Dispersion = step & 1; + impl.ReferenceWavelength = 400f + step % 300; + impl.Steps = Spectrum.MinSteps + step % (Spectrum.MaxSteps - Spectrum.MinSteps + 1); + }); + + Assert.Equal(0, allocated); + } + + [Theory] + [InlineData(DispersionLaw.Grating)] + [InlineData(DispersionLaw.Prism)] + public void FillingTheTableAllocatesNothing(DispersionLaw law) + { + var table = new Vector4[Spectrum.MaxSteps]; + var step = 0; + + var allocated = Allocation.Of(() => + { + step++; + Spectrum.Fill(table, Spectrum.MinSteps + step % (Spectrum.MaxSteps - Spectrum.MinSteps + 1), 400f + step % 300, law); + }); + + Assert.Equal(0, allocated); + } +} diff --git a/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs b/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs index f8842f1..f479e28 100644 --- a/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs +++ b/SpectrumColorShift.Tests/SpectrumColorShiftEffectProcessorTests.cs @@ -11,6 +11,7 @@ public sealed class SpectrumColorShiftEffectProcessorTests const int Width = 64; const int Height = 48; const int Length = 30; + const long HostSlack = 1024; static Bgra Textured(int x, int y) { @@ -52,6 +53,72 @@ public sealed class SpectrumColorShiftEffectProcessorTests Assert.Same(description.DrawDescription, draw); } + [Fact] + public void UpdatingTheProcessorWithFixedSettingsAllocatesNothing() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = new SourceImage(context, Width, Height, Textured); + using var processor = Shifted(24d, shifted => shifted.Mode = ShiftMode.Radial).CreateVideoEffect(context); + processor.SetInput(source.Bitmap); + _ = RenderFrame(context, processor, 0); + var descriptions = Enumerable.Range(0, Length).Select(frame => EffectDescriptions.At(frame, Length)).ToArray(); + var round = 0; + + var allocated = Allocation.Of(() => processor.Update(descriptions[round++ % Length])); + + Assert.Equal(0, allocated); + } + + [Fact] + public void UpdatingTheProcessorAllocatesOnlyWhatTheHostAllocatesForTheAnimations() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = new SourceImage(context, Width, Height, Textured); + var effect = Shifted(0d, shifted => + { + shifted.Distance.CopyFrom(Linear(0d, 24d)); + shifted.Angle.CopyFrom(Linear(0d, 90d)); + shifted.CenterX.CopyFrom(Linear(0d, 20d)); + shifted.CenterY.CopyFrom(Linear(0d, -20d)); + shifted.Strength.CopyFrom(Linear(100d, 40d)); + shifted.ReferenceWavelength.CopyFrom(Linear(450d, 650d)); + }); + using var processor = effect.CreateVideoEffect(context); + using var bare = new SpectrumColorShiftCustomEffect(context); + processor.SetInput(source.Bitmap); + _ = RenderFrame(context, processor, 0); + var descriptions = Enumerable.Range(0, Length).Select(frame => EffectDescriptions.At(frame, Length)).ToArray(); + var reads = 0; + var sets = 0; + var updates = 0; + + var reading = Allocation.Of(() => + { + var frame = reads++ % Length; + _ = effect.Distance.GetValue(frame, Length, EffectDescriptions.Fps); + _ = effect.Angle.GetValue(frame, Length, EffectDescriptions.Fps); + _ = effect.CenterX.GetValue(frame, Length, EffectDescriptions.Fps); + _ = effect.CenterY.GetValue(frame, Length, EffectDescriptions.Fps); + _ = effect.Strength.GetValue(frame, Length, EffectDescriptions.Fps); + _ = effect.ReferenceWavelength.GetValue(frame, Length, EffectDescriptions.Fps); + }); + var setting = Allocation.Of(() => + { + var value = ++sets % 50; + bare.Distance = value; + bare.Angle = value; + bare.CenterX = value; + bare.CenterY = value; + bare.Strength = value / 100f; + bare.ReferenceWavelength = 400f + value; + }); + var updating = Allocation.Of(() => processor.Update(descriptions[updates++ % Length])); + + Assert.InRange(updating, reading + setting - HostSlack, reading + setting + HostSlack); + } + [Fact] public void WithoutADistanceTheProcessorPassesTheImageThrough() {