diff --git a/SpectralConvolution/SpectralConvolution.Tests/ChromaticGpuConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/ChromaticGpuConvolverTests.cs index c9aa1b1..8610b9f 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/ChromaticGpuConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/ChromaticGpuConvolverTests.cs @@ -59,6 +59,18 @@ public sealed class ChromaticGpuConvolverTests static ChromaticConvolutionScene Scene(int size, int radius, int width, int height, int margin, int seed, LightOptions light = default) => ChromaticConvolutionScene.Create(ConvolutionScene.RandomSource(width, height, seed, 0.45), width, height, margin, radius, size, light); + [Fact] + public void AWarmChromaticUploadAllocatesNoManagedMemory() + { + var device = HardwareOrDefault(); + var scene = Scene(128, 23, 140, 90, 24, 5); + using var convolver = new GpuTileConvolver(device); + convolver.Upload(scene.Spectrum); + convolver.Upload(scene.Spectrum); + + Assert.Equal(0L, AllocationProbe.MinimumAllocatedBytes(() => convolver.Upload(scene.Spectrum), 16)); + } + [Theory] [InlineData(64, 5, 150, 97, 11)] [InlineData(128, 23, 140, 90, 24)] diff --git a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs index 48143b8..9d58f23 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs @@ -276,6 +276,8 @@ public sealed class CpuTileConvolverTests Convolve(first); var bytes = first.WorkingBytes; + Assert.Equal(Environment.ProcessorCount, first.Threads); + Assert.True(second.WorkingBytes >= bytes); Convolve(second); var active = CpuTileConvolver.ActiveThreads(first.Threads, 512); diff --git a/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs b/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs index e16f5b2..15dd134 100644 --- a/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs +++ b/WaveOptics.Tests/FraunhoferKernelSamplerTests.cs @@ -144,4 +144,19 @@ public sealed class FraunhoferKernelSamplerTests Assert.Equal(0, AllocationProbe.MinimumAllocatedBytes(SampleEveryDefocusStep, 16)); } + + [Fact] + public void ASamplerHoldsOnlyTheRowsTheApertureReaches() + { + const int GridSize = 512; + var specification = PsfSpecification.Of(Descriptor(gridSize: GridSize, diameter: 16)); + Assert.True(new FraunhoferKernelSampler().TrySample(in specification, kernel.AsSpan(0, 31 * 31))); + var fresh = new FraunhoferKernelSampler(); + + var before = GC.GetAllocatedBytesForCurrentThread(); + Assert.True(fresh.TrySample(in specification, kernel.AsSpan(0, 31 * 31))); + var allocated = GC.GetAllocatedBytesForCurrentThread() - before; + + Assert.True(allocated < (long)GridSize * GridSize * sizeof(double) * 2, allocated.ToString()); + } } diff --git a/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs b/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs index 6143e63..8ea407b 100644 --- a/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs +++ b/WaveOptics.Tests/WaveOpticsCpuPipelineTests.cs @@ -1,4 +1,6 @@ +using System.Reflection; using System.Runtime.InteropServices; +using SpectralConvolution; using WaveOptics.Effects; using WaveOptics.Rendering; @@ -34,6 +36,25 @@ public sealed class WaveOpticsCpuPipelineTests return pipeline.RenderVisible(rect, in parameters).ToArray(); } + static CpuTileConvolver Convolver(WaveOpticsCpuPipeline pipeline) + => (CpuTileConvolver)typeof(WaveOpticsCpuPipeline).GetField("_convolver", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(pipeline)!; + + [Fact] + public void PipelinesShareTheThreadsAndTheWorkingArea() + { + using var first = new WaveOpticsCpuPipeline(); + using var second = new WaveOpticsCpuPipeline(); + var source = Bytes(Square(150, 120, 30, 20, 70)); + var parameters = Parameters(gain: 1.5f, defocus: 1f); + + RenderFrame(first, source, 150, 120, 16, in parameters, out _); + var bytes = Convolver(first).WorkingBytes; + + Assert.True(bytes > 0); + Assert.Equal(Environment.ProcessorCount, Convolver(second).Threads); + Assert.True(Convolver(second).WorkingBytes >= bytes); + } + [Fact] public void TheCpuAndTheGpuDrawTheSameImageWithinTwoLevels() { diff --git a/WaveOptics.Tests/WaveOpticsEffectProcessorAllocationTests.cs b/WaveOptics.Tests/WaveOpticsEffectProcessorAllocationTests.cs index c0fdc75..b42ef6d 100644 --- a/WaveOptics.Tests/WaveOpticsEffectProcessorAllocationTests.cs +++ b/WaveOptics.Tests/WaveOpticsEffectProcessorAllocationTests.cs @@ -10,13 +10,13 @@ namespace WaveOptics.Tests; [Collection("Direct2D")] public sealed class WaveOpticsEffectProcessorAllocationTests { - const int Size = 128; - const int Length = 60; + internal const int Size = 128; + internal const int Length = 60; const int Rounds = 24; - static Bgra Shape(int x, int y) => x is >= 32 and < 96 && y is >= 32 and < 96 && (x + 2 * y) % 9 != 0 ? Bgra.Opaque(220, 160, 90) : Bgra.Transparent; + internal static Bgra Shape(int x, int y) => x is >= 32 and < 96 && y is >= 32 and < 96 && (x + 2 * y) % 9 != 0 ? Bgra.Opaque(220, 160, 90) : Bgra.Transparent; - static void RequireInterop(IGraphicsDevicesAndContext devices) + internal static void RequireInterop(IGraphicsDevicesAndContext devices) { using var scheduler = ComputeExternalQueueScheduler.Create(); using var provider = WaveOpticsInteropProvider.TryCreate(devices, scheduler, out var device); @@ -24,7 +24,7 @@ public sealed class WaveOpticsEffectProcessorAllocationTests Assert.Skip("Direct3D 11 and Direct3D 12 sharing is unavailable."); } - static WaveOpticsEffectProcessor Processor(IGraphicsDevicesAndContext devices, WaveOpticsEffect effect, bool allowGpu) + internal static WaveOpticsEffectProcessor Processor(IGraphicsDevicesAndContext devices, WaveOpticsEffect effect, bool allowGpu) => new(devices, effect, new ComputeGuardian(new ComputeGuardianOptions { Report = _ => { } }), null, allowGpu); static long MinimumAllocatedPerUpdate(bool allowGpu, Action configure) diff --git a/WaveOptics.Tests/WaveOpticsEffectProcessorBitmapTests.cs b/WaveOptics.Tests/WaveOpticsEffectProcessorBitmapTests.cs new file mode 100644 index 0000000..c45b7df --- /dev/null +++ b/WaveOptics.Tests/WaveOpticsEffectProcessorBitmapTests.cs @@ -0,0 +1,59 @@ +using System.Reflection; +using System.Runtime.InteropServices; +using Vortice.Direct2D1; +using WaveOptics.Effects; +using WaveOptics.Rendering; +using YukkuriMovieMaker.Commons; +using YukkuriMovieMaker.Player.Video; + +namespace WaveOptics.Tests; + +[Collection("Direct2D")] +public sealed class WaveOpticsEffectProcessorBitmapTests +{ + static object Field(WaveOpticsEffectProcessor processor, string name) + => typeof(WaveOpticsEffectProcessor).GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(processor)!; + + static int SourceBitmapReferences(WaveOpticsEffectProcessor processor) + { + var resources = (WaveOpticsResourceSet)Field(processor, "_resourceSet"); + using var borrow = resources.BeginSourceExternalOperation(); + return Marshal.Release(borrow.DangerousGetView().AddRefBitmap()); + } + + [Fact] + public void EveryFrameLeavesTheReferencesOfTheSourceBitmapBalanced() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + WaveOpticsEffectProcessorAllocationTests.RequireInterop(context); + using var source = new SourceImage(context, WaveOpticsEffectProcessorAllocationTests.Size, WaveOpticsEffectProcessorAllocationTests.Size, WaveOpticsEffectProcessorAllocationTests.Shape); + using var processor = WaveOpticsEffectProcessorAllocationTests.Processor(context, new WaveOpticsEffect(), true); + processor.SetInput(source.Bitmap); + var description = EffectDescriptions.At(7, WaveOpticsEffectProcessorAllocationTests.Length); + processor.Update(description); + var baseline = SourceBitmapReferences(processor); + + for (var frame = 0; frame < 10; frame++) + processor.Update(description); + + Assert.Equal(baseline, SourceBitmapReferences(processor)); + } + + [Fact] + public void TheSourceBitmapWrapperHoldsNoPointerBetweenFrames() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + WaveOpticsEffectProcessorAllocationTests.RequireInterop(context); + using var source = new SourceImage(context, WaveOpticsEffectProcessorAllocationTests.Size, WaveOpticsEffectProcessorAllocationTests.Size, WaveOpticsEffectProcessorAllocationTests.Shape); + using var processor = WaveOpticsEffectProcessorAllocationTests.Processor(context, new WaveOpticsEffect(), true); + processor.SetInput(source.Bitmap); + var description = EffectDescriptions.At(7, WaveOpticsEffectProcessorAllocationTests.Length); + + processor.Update(description); + processor.Update(description); + + Assert.Equal(IntPtr.Zero, ((ID2D1Bitmap1)Field(processor, "_sourceBitmap")).NativePointer); + } +}