diff --git a/VolumetricLight.Harness/HarnessCases.cs b/VolumetricLight.Harness/HarnessCases.cs index 75027df..42f04bf 100644 --- a/VolumetricLight.Harness/HarnessCases.cs +++ b/VolumetricLight.Harness/HarnessCases.cs @@ -95,6 +95,8 @@ internal static class HarnessCases yield return ("shadow-figure-half-strength", Shadowed(effect => effect.Direction.Values[0].Value = -60), [0], HarnessPose.Flat, [Figure(new Vector3(-120f, 110f, 240f), configure: effect => effect.Strength.Values[0].Value = 50)]); yield return ("shadow-figure-strength-animated-frames", Shadowed(effect => effect.Direction.Values[0].Value = -60), [0, 150, 299], HarnessPose.Flat, [Figure(new Vector3(-120f, 110f, 240f), configure: effect => effect.Strength.CopyFrom(Linear(100d, 10d)))]); yield return ("shadow-figure-rotated-light", Shadowed(), [0], RotatedY, [Figure(new Vector3(0f, 120f, 200f))]); + yield return ("shadow-figure-other-scene", Shadowed(effect => { effect.Direction.Values[0].Value = -60; effect.Elevation.Values[0].Value = 45; }), [0], HarnessPose.Flat, [Figure(new Vector3(40f, 120f, 220f)) with { Scene = new Guid("5b0a8d3e-2c1f-4e7a-9a52-7d4f1c6b8e90") }]); + yield return ("shadow-figure-other-frame", Shadowed(effect => { effect.Direction.Values[0].Value = -60; effect.Elevation.Values[0].Value = 45; }), [0], HarnessPose.Flat, [Figure(new Vector3(-220f, 130f, 160f)) with { FrameOffset = 7 }]); yield return ("shadow-figure-other-number", Shadowed(effect => { effect.Direction.Values[0].Value = -60; effect.Elevation.Values[0].Value = 45; }), [0], HarnessPose.Flat, [Figure(new Vector3(-120f, 110f, 240f), configure: effect => effect.Channel = 2)]); } diff --git a/VolumetricLight.Harness/HarnessOccluders.cs b/VolumetricLight.Harness/HarnessOccluders.cs index fc3692e..a262bb9 100644 --- a/VolumetricLight.Harness/HarnessOccluders.cs +++ b/VolumetricLight.Harness/HarnessOccluders.cs @@ -6,7 +6,7 @@ using YukkuriMovieMaker.Player.Video; namespace VolumetricLight.Harness; -internal sealed record HarnessOccluder(VolumetricLightOccluderEffect Effect, HarnessPose Pose, HarnessImage Image); +internal sealed record HarnessOccluder(VolumetricLightOccluderEffect Effect, HarnessPose Pose, HarnessImage Image, Guid Scene = default, int FrameOffset = 0); internal sealed class HarnessOccluderScene : IDisposable { @@ -57,11 +57,11 @@ internal sealed class HarnessOccluderScene : IDisposable public IEnumerable Layers => entries.Select(entry => entry.Display.Output); - public void Update(Func describe, int frame) + public void Update(Func describe, int frame) { foreach (var entry in entries) { - var description = describe(frame, entry.Occluder.Pose); + var description = describe(frame + entry.Occluder.FrameOffset, entry.Occluder.Pose, entry.Occluder.Scene); entry.Blocker.Update(description); entry.Display.Update(description); } diff --git a/VolumetricLight.Harness/HarnessRenderer.cs b/VolumetricLight.Harness/HarnessRenderer.cs index af9147d..b4f9bfd 100644 --- a/VolumetricLight.Harness/HarnessRenderer.cs +++ b/VolumetricLight.Harness/HarnessRenderer.cs @@ -228,7 +228,7 @@ internal sealed class HarnessRenderer : IDisposable sync.Unmap(); } - internal EffectDescription Describe(int frame, HarnessPose? pose) + internal EffectDescription Describe(int frame, HarnessPose? pose, Guid scene = default) { var timeline = new TimelineSourceDescription( new System.Drawing.Size(CanvasWidth, CanvasHeight), @@ -236,7 +236,7 @@ internal sealed class HarnessRenderer : IDisposable new YukkuriMovieMaker.Player.Video.FrameTime(Length, Fps), Fps, TimelineSourceUsage.Playing, - Guid.Empty, + scene, []); var item = new TimelineItemSourceDescription(timeline, frame, Length, 0); var placement = pose ?? HarnessPose.Flat; diff --git a/VolumetricLight.Harness/golden.json b/VolumetricLight.Harness/golden.json index f800032..8926924 100644 --- a/VolumetricLight.Harness/golden.json +++ b/VolumetricLight.Harness/golden.json @@ -340,6 +340,20 @@ ], "hash": "6885C2736451491A0EC78156B9813B642238350992D3F33BF52435E1ADC9DE83" }, + { + "name": "shadow-figure-other-scene", + "frames": [ + 0 + ], + "hash": "44996CD0E824BAE692A7BFF8063E42BFEA76A8E1997B93A86584B92956835454" + }, + { + "name": "shadow-figure-other-frame", + "frames": [ + 0 + ], + "hash": "91CC1D8F8948BE95E71C93CFE47816849FDF2A03D1DA5E7752059F401CB0774F" + }, { "name": "shadow-figure-other-number", "frames": [ diff --git a/VolumetricLight.Tests/EffectDescriptions.cs b/VolumetricLight.Tests/EffectDescriptions.cs index 7d2ab3c..bf8f480 100644 --- a/VolumetricLight.Tests/EffectDescriptions.cs +++ b/VolumetricLight.Tests/EffectDescriptions.cs @@ -19,7 +19,7 @@ internal static class EffectDescriptions public static EffectDescription At(int frame, int length, DrawDescription draw) => At(frame, length, draw, ScreenSize); - public static EffectDescription At(int frame, int length, DrawDescription draw, Size screenSize) + public static EffectDescription At(int frame, int length, DrawDescription draw, Size screenSize, Guid scene = default) { var timeline = new TimelineSourceDescription( screenSize, @@ -27,7 +27,7 @@ internal static class EffectDescriptions new FrameTime(length, Fps), Fps, TimelineSourceUsage.Playing, - Guid.Empty, + scene, []); var item = new TimelineItemSourceDescription(timeline, frame, length, 0); return new EffectDescription(item, draw, 0, 1, 0, 1); diff --git a/VolumetricLight.Tests/OccluderRegistryTests.cs b/VolumetricLight.Tests/OccluderRegistryTests.cs index 92dbb29..3b7375b 100644 --- a/VolumetricLight.Tests/OccluderRegistryTests.cs +++ b/VolumetricLight.Tests/OccluderRegistryTests.cs @@ -28,14 +28,54 @@ public sealed class OccluderRegistryTests using var first = devices.CreateContext(); using var second = devices.CreateContext(); - using var a = OccluderRegistry.Acquire(first, 1); - using var b = OccluderRegistry.Acquire(second, 1); - using var c = OccluderRegistry.Acquire(first, 2); + using var a = OccluderRegistry.Acquire(first, Guid.Empty, 1); + using var b = OccluderRegistry.Acquire(second, Guid.Empty, 1); + using var c = OccluderRegistry.Acquire(first, Guid.Empty, 2); Assert.Same(a.Group, b.Group); Assert.NotSame(a.Group, c.Group); } + [Fact] + public void TheSameNumberInAnotherSceneHasItsOwnGroup() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + var home = Guid.NewGuid(); + + using var a = OccluderRegistry.Acquire(context, home, 1); + using var b = OccluderRegistry.Acquire(context, home, 1); + using var c = OccluderRegistry.Acquire(context, Guid.NewGuid(), 1); + + Assert.Same(a.Group, b.Group); + Assert.NotSame(a.Group, c.Group); + } + + [Fact] + public void ASnapshotKeepsOnlyTheOccludersPublishedForItsOwnFrame() + { + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + using var source = new SourceImage(context, Width, Height, Marker); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); + using var now = lease.Group.Claim()!; + using var later = lease.Group.Claim()!; + now.Publish(source.Bitmap, Sample(0.25f), 4); + later.Publish(source.Bitmap, Sample(0.75f), 9); + var snapshot = new Occluder[VolumetricLightSettings.OccluderCapacity]; + + lease.Group.Snapshot(snapshot, 4); + Assert.Equal(0.25f, snapshot[0].Strength); + Assert.Equal(0f, snapshot[1].Strength); + + lease.Group.Snapshot(snapshot, 9); + Assert.Equal(0f, snapshot[0].Strength); + Assert.Equal(0.75f, snapshot[1].Strength); + + lease.Group.Snapshot(snapshot, 5); + Assert.All(snapshot, occluder => Assert.Equal(0f, occluder.Strength)); + } + [Fact] public void DifferentDevicesNeverShareAGroup() { @@ -44,8 +84,8 @@ public sealed class OccluderRegistryTests using var first = firstDevices.CreateContext(); using var second = secondDevices.CreateContext(); - using var a = OccluderRegistry.Acquire(first, 1); - using var b = OccluderRegistry.Acquire(second, 1); + using var a = OccluderRegistry.Acquire(first, Guid.Empty, 1); + using var b = OccluderRegistry.Acquire(second, Guid.Empty, 1); Assert.NotSame(a.Group, b.Group); } @@ -55,8 +95,8 @@ public sealed class OccluderRegistryTests { using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); - var a = OccluderRegistry.Acquire(context, 3); - var b = OccluderRegistry.Acquire(context, 3); + var a = OccluderRegistry.Acquire(context, Guid.Empty, 3); + var b = OccluderRegistry.Acquire(context, Guid.Empty, 3); var group = a.Group; var slot = group.Claim()!; @@ -64,7 +104,7 @@ public sealed class OccluderRegistryTests a.Dispose(); Assert.NotNull(group.Claim()); b.Dispose(); - using var again = OccluderRegistry.Acquire(context, 3); + using var again = OccluderRegistry.Acquire(context, Guid.Empty, 3); Assert.NotSame(group, again.Group); Assert.Null(group.Claim()); @@ -76,7 +116,7 @@ public sealed class OccluderRegistryTests { using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); var slots = Enumerable.Range(0, VolumetricLightSettings.OccluderCapacity).Select(_ => lease.Group.Claim()).ToArray(); Assert.All(slots, Assert.NotNull); @@ -93,7 +133,7 @@ public sealed class OccluderRegistryTests { using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); Assert.False(lease.Group.IsOccupied); var first = lease.Group.Claim()!; @@ -111,18 +151,18 @@ public sealed class OccluderRegistryTests using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); using var source = new SourceImage(context, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); using var first = lease.Group.Claim()!; var second = lease.Group.Claim()!; var snapshot = new Occluder[VolumetricLightSettings.OccluderCapacity]; - first.Publish(source.Bitmap, Sample(0.25f)); - second.Publish(source.Bitmap, Sample(0.75f)); - lease.Group.Snapshot(snapshot); + first.Publish(source.Bitmap, Sample(0.25f), 0); + second.Publish(source.Bitmap, Sample(0.75f), 0); + lease.Group.Snapshot(snapshot, 0); Assert.Equal([0.25f, 0.75f, 0f, 0f], snapshot.Select(occluder => occluder.Strength)); second.Dispose(); - lease.Group.Snapshot(snapshot); + lease.Group.Snapshot(snapshot, 0); Assert.Equal([0.25f, 0f, 0f, 0f], snapshot.Select(occluder => occluder.Strength)); } @@ -132,13 +172,13 @@ public sealed class OccluderRegistryTests using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); using var source = new SourceImage(context, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); var slot = lease.Group.Claim()!; slot.Dispose(); var snapshot = new Occluder[VolumetricLightSettings.OccluderCapacity]; - slot.Publish(source.Bitmap, Sample()); - lease.Group.Snapshot(snapshot); + slot.Publish(source.Bitmap, Sample(), 0); + lease.Group.Snapshot(snapshot, 0); Assert.All(snapshot, occluder => Assert.Equal(0f, occluder.Strength)); } @@ -149,12 +189,12 @@ public sealed class OccluderRegistryTests using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); using var source = new SourceImage(context, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); var slot = lease.Group.Claim()!; var relay = lease.Group.GetOutput(0); var empty = context.DeviceContext.GetImageLocalBounds(relay); - slot.Publish(source.Bitmap, Sample()); + slot.Publish(source.Bitmap, Sample(), 0); var shown = Render(context, relay); slot.Dispose(); var gone = context.DeviceContext.GetImageLocalBounds(relay); @@ -172,10 +212,10 @@ public sealed class OccluderRegistryTests using var reader = devices.CreateContext(); using var writer = devices.CreateContext(); using var source = new SourceImage(writer, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(reader, 1); + using var lease = OccluderRegistry.Acquire(reader, Guid.Empty, 1); using var slot = lease.Group.Claim()!; - slot.Publish(source.Bitmap, Sample()); + slot.Publish(source.Bitmap, Sample(), 0); var shown = Render(reader, lease.Group.GetOutput(0)); Assert.Equal(source[3, 2], shown[3, 2]); @@ -188,11 +228,11 @@ public sealed class OccluderRegistryTests using var reader = devices.CreateContext(); var creator = devices.CreateContext(); using var source = new SourceImage(reader, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(creator, 1); + using var lease = OccluderRegistry.Acquire(creator, Guid.Empty, 1); using var slot = lease.Group.Claim()!; creator.Dispose(); - slot.Publish(source.Bitmap, Sample()); + slot.Publish(source.Bitmap, Sample(), 0); var shown = Render(reader, lease.Group.GetOutput(0)); Assert.Equal(source[5, 1], shown[5, 1]); @@ -204,7 +244,7 @@ public sealed class OccluderRegistryTests using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); using var source = new SourceImage(context, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); var claimed = 0; Parallel.For(0, 64, index => @@ -214,10 +254,10 @@ public sealed class OccluderRegistryTests return; Interlocked.Increment(ref claimed); - slot.Publish(source.Bitmap, Sample((index + 1) / 100f)); + slot.Publish(source.Bitmap, Sample((index + 1) / 100f), 0); }); var snapshot = new Occluder[VolumetricLightSettings.OccluderCapacity]; - lease.Group.Snapshot(snapshot); + lease.Group.Snapshot(snapshot, 0); Assert.Equal(VolumetricLightSettings.OccluderCapacity, claimed); Assert.All(snapshot, occluder => Assert.True(occluder.Strength > 0f)); @@ -229,7 +269,7 @@ public sealed class OccluderRegistryTests using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); using var source = new SourceImage(context, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); using var slot = lease.Group.Claim()!; using var blur = new Vortice.Direct2D1.Effects.GaussianBlur(context.DeviceContext); blur.SetInput(0, source.Bitmap, true); @@ -241,12 +281,12 @@ public sealed class OccluderRegistryTests var freed = new Occluder[VolumetricLightSettings.OccluderCapacity]; lease.Group.AddConsumer(blur.NativePointer); - slot.Publish(image, Sample()); - lease.Group.Snapshot(blocked); + slot.Publish(image, Sample(), 0); + lease.Group.Snapshot(blocked, 0); var kept = context.DeviceContext.GetImageLocalBounds(lease.Group.GetOutput(0)); lease.Group.RemoveConsumer(blur.NativePointer); - slot.Publish(image, Sample()); - lease.Group.Snapshot(freed); + slot.Publish(image, Sample(), 0); + lease.Group.Snapshot(freed, 0); var relayed = context.DeviceContext.GetImageLocalBounds(lease.Group.GetOutput(0)); Assert.Equal(0f, blocked[0].Strength); @@ -261,18 +301,18 @@ public sealed class OccluderRegistryTests using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); using var source = new SourceImage(context, Width, Height, Marker); - using var lease = OccluderRegistry.Acquire(context, 1); + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); using var slot = lease.Group.Claim()!; using var blur = new Vortice.Direct2D1.Effects.GaussianBlur(context.DeviceContext); blur.SetInput(0, source.Bitmap, true); using var image = blur.Output; var snapshot = new Occluder[VolumetricLightSettings.OccluderCapacity]; - slot.Publish(image, Sample()); - lease.Group.Snapshot(snapshot); + slot.Publish(image, Sample(), 0); + lease.Group.Snapshot(snapshot, 0); Assert.Equal(1f, snapshot[0].Strength); lease.Group.AddConsumer(blur.NativePointer); - lease.Group.Snapshot(snapshot); + lease.Group.Snapshot(snapshot, 0); Assert.Equal(0f, snapshot[0].Strength); Assert.Equal((0f, 0f, 0f, 0f), (context.DeviceContext.GetImageLocalBounds(lease.Group.GetOutput(0)) is var bounds ? (bounds.Left, bounds.Top, bounds.Right, bounds.Bottom) : default)); @@ -283,8 +323,8 @@ public sealed class OccluderRegistryTests { using var devices = new GraphicsDevices(); using var context = devices.CreateContext(); - using var lease = OccluderRegistry.Acquire(context, 1); - var link = new OccluderLink { Group = lease.Group }; + using var lease = OccluderRegistry.Acquire(context, Guid.Empty, 1); + var link = new OccluderLink { Group = lease.Group, Frame = 0 }; var id = OccluderLinks.Register(link); diff --git a/VolumetricLight.Tests/ShaftCustomEffectTests.cs b/VolumetricLight.Tests/ShaftCustomEffectTests.cs index b507855..40c11c8 100644 --- a/VolumetricLight.Tests/ShaftCustomEffectTests.cs +++ b/VolumetricLight.Tests/ShaftCustomEffectTests.cs @@ -53,11 +53,11 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output) readonly OccluderLease lease; readonly OccluderSlot[] slots; readonly ID2D1Bitmap1[] bitmaps; - readonly OccluderLink link = new(); + readonly OccluderLink link = new() { Frame = 0 }; public OccluderRig(IGraphicsDevicesAndContext devices, IReadOnlyList<(DrawDescription Draw, GoboPyramid Image, float Strength)> occluders) { - lease = OccluderRegistry.Acquire(devices, 1); + lease = OccluderRegistry.Acquire(devices, Guid.Empty, 1); link.Group = lease.Group; LinkId = OccluderLinks.Register(link); slots = new OccluderSlot[occluders.Count]; @@ -68,7 +68,7 @@ public sealed class ShaftCustomEffectTests(ITestOutputHelper output) Assert.True(ViewTransform.TryCreate(draw, out var view)); slots[index] = lease.Group.Claim()!; bitmaps[index] = image.CreateBitmaps(devices)[0]; - slots[index].Publish(bitmaps[index], new Occluder(view, GoboMapping.Create(draw.Zoom, draw.Invert, draw.CenterPoint), strength)); + slots[index].Publish(bitmaps[index], new Occluder(view, GoboMapping.Create(draw.Zoom, draw.Invert, draw.CenterPoint), strength), 0); } } diff --git a/VolumetricLight.Tests/VolumetricLightOccluderEffectProcessorTests.cs b/VolumetricLight.Tests/VolumetricLightOccluderEffectProcessorTests.cs index f76a435..757fada 100644 --- a/VolumetricLight.Tests/VolumetricLightOccluderEffectProcessorTests.cs +++ b/VolumetricLight.Tests/VolumetricLightOccluderEffectProcessorTests.cs @@ -49,9 +49,11 @@ public sealed class VolumetricLightOccluderEffectProcessorTests { readonly IGraphicsDevicesAndContext context; readonly SourceImage source; + readonly Guid scene; - public Item(GraphicsDevices devices, YukkuriMovieMaker.Plugin.Effects.VideoEffectBase effect, Func pixels) + public Item(GraphicsDevices devices, YukkuriMovieMaker.Plugin.Effects.VideoEffectBase effect, Func pixels, Guid scene = default) { + this.scene = scene; context = devices.CreateContext(); source = new SourceImage(context, Width, Height, pixels); Processor = effect.CreateVideoEffect(context); @@ -62,12 +64,12 @@ public sealed class VolumetricLightOccluderEffectProcessorTests public SourceImage Source => source; - public DrawDescription Update(int frame, DrawDescription draw) - => Processor.Update(EffectDescriptions.At(frame, Length, draw, Canvas)); + public DrawDescription Update(int frame, DrawDescription draw, Guid? moveTo = null) + => Processor.Update(EffectDescriptions.At(frame, Length, draw, Canvas, moveTo ?? scene)); - public Rendering Render(int frame, DrawDescription draw) + public Rendering Render(int frame, DrawDescription draw, Guid? moveTo = null) { - Update(frame, draw); + Update(frame, draw, moveTo); return Rendering.Capture(context, Processor.Output); } @@ -92,11 +94,11 @@ public sealed class VolumetricLightOccluderEffectProcessorTests .Where(point => point.X < 0 || point.Y < 0 || point.X >= Width || point.Y >= Height) .Sum(point => (long)rendering[point.X, point.Y].Red + rendering[point.X, point.Y].Green + rendering[point.X, point.Y].Blue); - static Occluder[] Snapshot(IGraphicsDevicesAndContext context, int channel) + static Occluder[] Snapshot(IGraphicsDevicesAndContext context, int channel, int frame = 0, Guid scene = default) { - using var lease = OccluderRegistry.Acquire(context, channel); + using var lease = OccluderRegistry.Acquire(context, scene, channel); var snapshot = new Occluder[VolumetricLightSettings.OccluderCapacity]; - lease.Group.Snapshot(snapshot); + lease.Group.Snapshot(snapshot, frame); return snapshot; } @@ -158,8 +160,8 @@ public sealed class VolumetricLightOccluderEffectProcessorTests effect.Channel = 3; item.Update(1, InFront); - Assert.All(Snapshot(context, 1), occluder => Assert.Equal(0f, occluder.Strength)); - Assert.True(Snapshot(context, 3)[0].Strength > 0f); + Assert.All(Snapshot(context, 1, 1), occluder => Assert.Equal(0f, occluder.Strength)); + Assert.True(Snapshot(context, 3, 1)[0].Strength > 0f); } [Fact] @@ -185,13 +187,13 @@ public sealed class VolumetricLightOccluderEffectProcessorTests var light = Light(1).CreateVideoEffect(context); light.SetInput(source.Bitmap); light.Update(EffectDescriptions.At(0, Length, EffectDescriptions.Flat, Canvas)); - var held = OccluderRegistry.Acquire(context, 1); + var held = OccluderRegistry.Acquire(context, Guid.Empty, 1); var group = held.Group; light.Dispose(); held.Dispose(); - using var again = OccluderRegistry.Acquire(context, 1); + using var again = OccluderRegistry.Acquire(context, Guid.Empty, 1); Assert.NotSame(group, again.Group); } @@ -209,8 +211,9 @@ public sealed class VolumetricLightOccluderEffectProcessorTests Assert.All(snapshot, occluder => Assert.True(occluder.Strength > 0f)); items[0].Dispose(); - items[^1].Update(1, InFront); - Assert.All(Snapshot(context, 1), occluder => Assert.True(occluder.Strength > 0f)); + foreach (var item in items.Skip(1)) + item.Update(1, InFront); + Assert.All(Snapshot(context, 1, 1), occluder => Assert.True(occluder.Strength > 0f)); } finally { @@ -275,6 +278,96 @@ public sealed class VolumetricLightOccluderEffectProcessorTests Assert.True(darker > Width * Height / 4, $"{darker}"); } + [Fact] + public void AnOccluderPublishedForAnotherFrameCastsNoShadowUntilThatFrame() + { + using var devices = new GraphicsDevices(); + using var light = new Item(devices, Light(1), Glass); + using var occluder = new Item(devices, Blocker(), Bars); + var clearNow = light.Render(0, EffectDescriptions.Flat); + var clearLater = light.Render(15, EffectDescriptions.Flat); + + occluder.Update(15, InFront); + + Assert.True(clearNow.SamePixelsAs(light.Render(0, EffectDescriptions.Flat))); + Assert.True(OutsideLight(light.Render(15, EffectDescriptions.Flat)) < OutsideLight(clearLater) * 0.9); + } + + [Fact] + public void AnOccluderInAnotherSceneNeverReachesTheLight() + { + var home = Guid.NewGuid(); + var away = Guid.NewGuid(); + using var devices = new GraphicsDevices(); + using var light = new Item(devices, Light(1), Glass, home); + using var foreign = new Item(devices, Blocker(), Bars, away); + var clear = light.Render(0, EffectDescriptions.Flat); + + foreign.Update(0, InFront); + Assert.True(clear.SamePixelsAs(light.Render(0, EffectDescriptions.Flat))); + + using var native = new Item(devices, Blocker(), Bars, home); + native.Update(0, InFront); + Assert.True(OutsideLight(light.Render(0, EffectDescriptions.Flat)) < OutsideLight(clear) * 0.9); + } + + [Fact] + public void ALightThatMovesToAnotherSceneFollowsTheOccludersThereAndLeavesTheOldOnes() + { + var home = Guid.NewGuid(); + var away = Guid.NewGuid(); + using var devices = new GraphicsDevices(); + using var light = new Item(devices, Light(1), Glass, home); + using var occluder = new Item(devices, Blocker(), Bars, away); + var clear = light.Render(0, EffectDescriptions.Flat); + occluder.Update(0, InFront); + + Assert.True(OutsideLight(light.Render(0, EffectDescriptions.Flat, away)) < OutsideLight(clear) * 0.9); + Assert.True(clear.SamePixelsAs(light.Render(0, EffectDescriptions.Flat, home))); + } + + [Fact] + public void AnOccluderThatMovesToAnotherSceneLeavesTheLightsOfTheOldOne() + { + var home = Guid.NewGuid(); + var away = Guid.NewGuid(); + using var devices = new GraphicsDevices(); + using var light = new Item(devices, Light(1), Glass, home); + using var occluder = new Item(devices, Blocker(), Bars, home); + var clear = light.Render(0, EffectDescriptions.Flat); + occluder.Update(0, InFront); + Assert.True(OutsideLight(light.Render(0, EffectDescriptions.Flat)) < OutsideLight(clear) * 0.9); + + occluder.Update(0, InFront, away); + + Assert.True(clear.SamePixelsAs(light.Render(0, EffectDescriptions.Flat))); + } + + [Fact] + public void EachSceneKeepsItsOwnFourOccluderSlotsOnTheSameNumber() + { + var home = Guid.NewGuid(); + var away = Guid.NewGuid(); + using var devices = new GraphicsDevices(); + using var context = devices.CreateContext(); + var items = Enumerable.Range(0, VolumetricLightSettings.OccluderCapacity + 1) + .Select(index => new Item(devices, Blocker(), Bars, index < VolumetricLightSettings.OccluderCapacity ? home : away)) + .ToArray(); + try + { + foreach (var item in items) + item.Update(0, InFront); + + Assert.All(Snapshot(context, 1, 0, home), occluder => Assert.True(occluder.Strength > 0f)); + Assert.True(Snapshot(context, 1, 0, away)[0].Strength > 0f); + } + finally + { + foreach (var item in items) + item.Dispose(); + } + } + [Fact] public void ALightIgnoresOccludersOfAnotherNumberAndWhenItsOwnNumberIsZero() { diff --git a/VolumetricLight/OccluderRegistry.cs b/VolumetricLight/OccluderRegistry.cs index 9eccad7..f22cf90 100644 --- a/VolumetricLight/OccluderRegistry.cs +++ b/VolumetricLight/OccluderRegistry.cs @@ -17,6 +17,7 @@ internal sealed class OccluderGroup : IDisposable private readonly ID2D1Image[] _outputs = new ID2D1Image[VolumetricLightSettings.OccluderCapacity]; private readonly ID2D1Image?[] _published = new ID2D1Image?[VolumetricLightSettings.OccluderCapacity]; private readonly Occluder[] _requested = new Occluder[VolumetricLightSettings.OccluderCapacity]; + private readonly int[] _frames = new int[VolumetricLightSettings.OccluderCapacity]; private readonly bool[] _blocked = new bool[VolumetricLightSettings.OccluderCapacity]; private readonly int[] _checked = new int[VolumetricLightSettings.OccluderCapacity]; private readonly HashSet _consumers = []; @@ -78,12 +79,12 @@ internal sealed class OccluderGroup : IDisposable } } - public void Snapshot(Span destination) + public void Snapshot(Span destination, int frame) { lock (_gate) { for (var index = 0; index < destination.Length; index++) - destination[index] = !_disposed && index < _occluders.Length ? _occluders[index] : default; + destination[index] = !_disposed && index < _occluders.Length && _frames[index] == frame ? _occluders[index] : default; } } @@ -124,7 +125,7 @@ internal sealed class OccluderGroup : IDisposable } } - internal void Publish(int index, ID2D1Image image, in Occluder occluder) + internal void Publish(int index, ID2D1Image image, in Occluder occluder, int frame) { lock (_gate) { @@ -132,6 +133,7 @@ internal sealed class OccluderGroup : IDisposable return; _requested[index] = occluder; + _frames[index] = frame; if (!ReferenceEquals(_published[index], image) || _checked[index] != _version) { _published[index] = image; @@ -217,7 +219,7 @@ internal sealed class OccluderSlot(OccluderGroup group, int index) : IDisposable { private int _retired; - public void Publish(ID2D1Image image, in Occluder occluder) => group.Publish(index, image, occluder); + public void Publish(ID2D1Image image, in Occluder occluder, int frame) => group.Publish(index, image, occluder, frame); public void Dispose() { @@ -229,13 +231,13 @@ internal sealed class OccluderSlot(OccluderGroup group, int index) : IDisposable internal sealed class OccluderLease : IDisposable { private readonly OccluderBoard _board; - private readonly int _channel; + private readonly (Guid Scene, int Channel) _key; private int _released; - internal OccluderLease(OccluderBoard board, int channel, OccluderGroup group) + internal OccluderLease(OccluderBoard board, (Guid Scene, int Channel) key, OccluderGroup group) { _board = board; - _channel = channel; + _key = key; Group = group; } @@ -244,7 +246,7 @@ internal sealed class OccluderLease : IDisposable public void Dispose() { if (Interlocked.Exchange(ref _released, 1) == 0) - _board.Release(_channel); + _board.Release(_key); } } @@ -258,31 +260,32 @@ internal sealed class OccluderBoard } private readonly object _gate = new(); - private readonly Dictionary _entries = []; + private readonly Dictionary<(Guid Scene, int Channel), Entry> _entries = []; - public OccluderLease Acquire(IGraphicsDevicesAndContext devices, int channel) + public OccluderLease Acquire(IGraphicsDevicesAndContext devices, Guid scene, int channel) { + var key = (scene, channel); lock (_gate) { - if (!_entries.TryGetValue(channel, out var entry)) + if (!_entries.TryGetValue(key, out var entry)) { entry = new Entry(new OccluderGroup(devices.DeviceContext)); - _entries.Add(channel, entry); + _entries.Add(key, entry); } entry.References++; - return new OccluderLease(this, channel, entry.Group); + return new OccluderLease(this, key, entry.Group); } } - internal void Release(int channel) + internal void Release((Guid Scene, int Channel) key) { lock (_gate) { - if (!_entries.TryGetValue(channel, out var entry) || --entry.References > 0) + if (!_entries.TryGetValue(key, out var entry) || --entry.References > 0) return; - _entries.Remove(channel); + _entries.Remove(key); entry.Group.Dispose(); } } @@ -292,19 +295,26 @@ internal static class OccluderRegistry { private static readonly ConditionalWeakTable Boards = []; - public static OccluderLease Acquire(IGraphicsDevicesAndContext devices, int channel) - => Boards.GetValue(devices.D2D, _ => new OccluderBoard()).Acquire(devices, channel); + public static OccluderLease Acquire(IGraphicsDevicesAndContext devices, Guid scene, int channel) + => Boards.GetValue(devices.D2D, _ => new OccluderBoard()).Acquire(devices, scene, channel); } internal sealed class OccluderLink { private OccluderGroup? _group; + private int _frame; public OccluderGroup? Group { get => Volatile.Read(ref _group); set => Volatile.Write(ref _group, value); } + + public int Frame + { + get => Volatile.Read(ref _frame); + set => Volatile.Write(ref _frame, value); + } } internal static class OccluderLinks diff --git a/VolumetricLight/ShaftCustomEffect.cs b/VolumetricLight/ShaftCustomEffect.cs index 5b63bb8..f2b66d0 100644 --- a/VolumetricLight/ShaftCustomEffect.cs +++ b/VolumetricLight/ShaftCustomEffect.cs @@ -229,7 +229,7 @@ internal sealed class ShaftCustomEffect : D2D1CustomShaderEffectBase Span occluders = stackalloc Occluder[VolumetricLightSettings.OccluderCapacity]; Span strengths = stackalloc float[VolumetricLightSettings.OccluderCapacity]; - group.Snapshot(occluders); + group.Snapshot(occluders, link.Frame); for (var index = 0; index < occluders.Length; index++) { var plane = OccluderPlane.Create(_cameraFromLocal, occluders[index]); diff --git a/VolumetricLight/VolumetricLightEffectProcessor.cs b/VolumetricLight/VolumetricLightEffectProcessor.cs index cc22c44..1aac931 100644 --- a/VolumetricLight/VolumetricLightEffectProcessor.cs +++ b/VolumetricLight/VolumetricLightEffectProcessor.cs @@ -54,6 +54,7 @@ internal sealed class VolumetricLightEffectProcessor(IGraphicsDevicesAndContext private int _linkId; private OccluderLease? _lease; private int _occlusionChannel = -1; + private Guid _occlusionScene; private int _revision; public override DrawDescription Update(EffectDescription effectDescription) @@ -100,7 +101,8 @@ internal sealed class VolumetricLightEffectProcessor(IGraphicsDevicesAndContext { SetScale(scene.Scale); Apply(scene.Constants); - SetOcclusion(Math.Clamp(_item.OcclusionChannel, 0, VolumetricLightSettings.MaximumOcclusionChannel)); + _link.Frame = effectDescription.TimelinePosition.Frame; + SetOcclusion(effectDescription.SceneId, Math.Clamp(_item.OcclusionChannel, 0, VolumetricLightSettings.MaximumOcclusionChannel)); SetShaft(ShaftState.Shaft); } @@ -213,18 +215,19 @@ internal sealed class VolumetricLightEffectProcessor(IGraphicsDevicesAndContext _constants = next; } - private void SetOcclusion(int channel) + private void SetOcclusion(Guid scene, int channel) { - if (_occlusionChannel != channel) + if (_occlusionChannel != channel || _occlusionScene != scene) { ReleaseOcclusion(); - _lease = channel > 0 ? OccluderRegistry.Acquire(_devices, channel) : null; + _lease = channel > 0 ? OccluderRegistry.Acquire(_devices, scene, channel) : null; _lease?.Group.AddConsumer(_shaft!.NativePointer); for (var slot = 0; slot < VolumetricLightSettings.OccluderCapacity; slot++) _shaft!.SetInput(VolumetricLightSettings.GoboLevelCount + slot, _lease?.Group.GetOutput(slot) ?? _emptyOutput, true); _link.Group = _lease?.Group; _occlusionChannel = channel; + _occlusionScene = scene; } if (_lease?.Group.IsOccupied == true) diff --git a/VolumetricLight/VolumetricLightOccluderEffectProcessor.cs b/VolumetricLight/VolumetricLightOccluderEffectProcessor.cs index d46d3bd..7576f98 100644 --- a/VolumetricLight/VolumetricLightOccluderEffectProcessor.cs +++ b/VolumetricLight/VolumetricLightOccluderEffectProcessor.cs @@ -12,6 +12,7 @@ internal sealed class VolumetricLightOccluderEffectProcessor(IGraphicsDevicesAnd private OccluderLease? _lease; private OccluderSlot? _slot; private int _channel; + private Guid _scene; public override DrawDescription Update(EffectDescription effectDescription) { @@ -34,11 +35,13 @@ internal sealed class VolumetricLightOccluderEffectProcessor(IGraphicsDevicesAnd var draw = effectDescription.DrawDescription; var channel = Math.Clamp(_item.Channel, 1, VolumetricLightSettings.MaximumOcclusionChannel); - if (_lease is null || _channel != channel) + var scene = effectDescription.SceneId; + if (_lease is null || _channel != channel || _scene != scene) { Release(); - _lease = OccluderRegistry.Acquire(_devices, channel); + _lease = OccluderRegistry.Acquire(_devices, scene, channel); _channel = channel; + _scene = scene; } _slot ??= _lease.Group.Claim(); @@ -51,7 +54,7 @@ internal sealed class VolumetricLightOccluderEffectProcessor(IGraphicsDevicesAnd var occluder = draw.Zoom.X != 0f && draw.Zoom.Y != 0f && ViewTransform.TryCreate(draw, out var view) ? new Occluder(view, GoboMapping.Create(draw.Zoom, draw.Invert, draw.CenterPoint), strength) : default; - _slot.Publish(input, occluder); + _slot.Publish(input, occluder, effectDescription.TimelinePosition.Frame); } private void Release()