From 04b46d740683e0b08f0b827e8f7e54892054cf2e Mon Sep 17 00:00:00 2001 From: soul Date: Tue, 1 Dec 2026 03:57:29 +0100 Subject: [PATCH] Isoline smokes: chain junction fixtures and frame-reuse check through the real kernels; flow p5 gate dropped, flow p25 and p50 from real-shader readings; capacity guard matches ChainClear's group limit Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/Editor/IsolineExtractSmoke.cs | 122 +++++++++++++++++- Assets/Scripts/Editor/IsolineRenderSmoke.cs | 14 +- .../Scripts/Zone Display/IsolineExtractor.cs | 14 +- 3 files changed, 143 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/Editor/IsolineExtractSmoke.cs b/Assets/Scripts/Editor/IsolineExtractSmoke.cs index d425fa77f..5a8278a1b 100644 --- a/Assets/Scripts/Editor/IsolineExtractSmoke.cs +++ b/Assets/Scripts/Editor/IsolineExtractSmoke.cs @@ -246,7 +246,7 @@ namespace Aetheria.EditorTools // segment's s the arclength from its chain's tail, taken from the CPU in double: sA = -(length from this segment's start to // the tail's start), sB = sA + |b - a|. A chain that BuildChains walks b to a throughout is reversed first; one that mixes // directions holds an orientation conflict and is skipped here (Orientation fails it). - static void ChainsAreArclength(string what, Result result, int rayConflicts) + static void ChainsAreArclength(string what, Result result, int rayConflicts, int minCompared = 100) { var segs = result.Segments; var chains = IsolineRenderSmoke.BuildChains(segs, 1u); @@ -333,7 +333,7 @@ namespace Aetheria.EditorTools var stats = result.ChainStats; Info($"chains {what}: {segs.Count(s => IsRay(s.key))} ray segments, {chains.Count} chains (longest {longest} segments), {loops} closed loops of {loopSegments} segments, {mixed} mixed-direction; GPU stats heads {stats[0]} conflicts {stats[1]} unterminated {stats[2]} overflow {stats[3]}; " + $"s error / chain length: position worst {positionShipped:G3} (mutants first-found {positionFirst:G3} on {firstWrong} segments, J = 8 {positionJ8:G3}), joint worst {jointShipped:G3} (mutants {jointFirst:G3}, {jointJ8Worst:G3}); |sB - sA| = |b - a| worst relative {edgeWorst:G3}"); - if (compared < 100) Fail("chains", $"{what}: only {compared} ray segments compared, the check is vacuous"); + if (compared < minCompared) Fail("chains", $"{what}: only {compared} ray segments compared, the check is vacuous"); if (mixed != 0) Fail("chains", $"{what}: {mixed} chains run both ways"); if (stats[0] != chains.Count - loops) Fail("chains", $"{what}: the GPU counted {stats[0]} chain heads, the CPU {chains.Count - loops} open chains"); if (stats[1] != rayConflicts) Fail("chains", $"{what}: the GPU counted {stats[1]} orientation conflicts, the CPU {rayConflicts}"); @@ -388,6 +388,7 @@ namespace Aetheria.EditorTools var terms = GravityLowering.Lower(zone, 0f, ref wells); field = GravityLowering.CreateField(material); Guarded("real zone", () => RealZone(zone, orbits, terms)); + Guarded("chain junctions", ChainJunctions); Guarded("chains are arclength", () => ChainCases(zone, orbits, terms)); Guarded("captures and cost", () => CapturesAndCost(zone, orbits, terms)); Guarded("chain cost", () => ChainCostCases(zone, orbits, terms)); @@ -855,6 +856,7 @@ namespace Aetheria.EditorTools ContoursAreNotRanked("stress 60 wells", result.Segments); } finally { Free(stress); } + ChainFramesReuse(zone, orbits, terms); ChainBounds(); } @@ -865,6 +867,122 @@ namespace Aetheria.EditorTools if (ranked != 0) Fail("contours", $"{what}: {ranked} contour segments carry an s other than 0"); } + // ---- chain junctions and frame reuse (verdict cut-isoline-chain-arclength.s1) ---------------------------------------------- + + static IsolineExtractor.IsoSegment RaySegment(Vector2 a, Vector2 b) => new IsolineExtractor.IsoSegment + { + a = a, b = b, key = 1u << 8, cell = 0, slopeA = 1f, slopeB = 1f, depthA = 0f, depthB = 0f + }; + + // An arm of n segments of vector step meeting the junction P: arriving (the last segment ends at P) or leaving (the first + // starts at P). Coordinates are small integers, so every shared point is bit-equal. + static void Arm(List into, Vector2 p, Vector2 step, int n, bool arriving) + { + for (var k = 0; k < n; k++) + { + var a = arriving ? p - step * (n - k) : p + step * k; + into.Add(RaySegment(a, a + step)); + } + } + + // Hand-made ray segments through the real chain kernels (RecordFinishAndChains over a buffer the fixture wrote), read back. + static Result ChainsOver(IsolineExtractor.IsoSegment[] segments) + { + var extractor = new IsolineExtractor(Shader, Math.Max(segments.Length, 16)); + var command = new CommandBuffer { name = "IsolineChainFixture" }; + try + { + var padded = new IsolineExtractor.IsoSegment[extractor.Segments.count]; + Array.Copy(segments, padded, segments.Length); + extractor.Segments.SetData(padded); + extractor.Count.SetData(new[] { (uint)segments.Length, (uint)segments.Length }); + extractor.ChainStats.SetData(new uint[4]); + extractor.RecordFinishAndChains(command, new Vector2Int(64, 64)); + Graphics.ExecuteCommandBuffer(command); + var all = new IsolineExtractor.IsoSegment[extractor.Segments.count]; + extractor.Segments.GetData(all); + var stats = new uint[4]; + extractor.ChainStats.GetData(stats); + return new Result { Segments = all.Take(segments.Length).ToArray(), ChainStats = stats, Written = (uint)segments.Length, Requested = (uint)segments.Length }; + } + finally + { + command.Release(); + extractor.Release(); + } + } + + // The junction rule through the real kernels: a point shared by three or more ray endpoints ends every chain touching it, so + // each arm is its own chain; a point of exactly one end and one start joins its two arms. Arms of different lengths, so a + // wrong link changes a rank. Every case is compared with the CPU rule (BuildChains) by ChainsAreArclength; linking at a + // junction through the first start found reads far off here (Soul: 8, 4 and 8 segments of 12, 12 and 16 off). + static void ChainJunctions() + { + var p = Vector2.zero; + var cases = new (string name, int[] arriving, int[] leaving)[] + { + ("Y two ends one start", new[] { 4, 3 }, new[] { 4 }), + ("Y one end two starts", new[] { 4 }, new[] { 4, 5 }), + ("X two ends two starts", new[] { 4, 3 }, new[] { 4, 5 }), + ("control one end one start", new[] { 4 }, new[] { 4 }), + }; + var arrivingSteps = new[] { new Vector2(1f, 0f), new Vector2(0f, -1f) }; + var leavingSteps = new[] { new Vector2(1f, 1f), new Vector2(1f, -1f) }; + foreach (var (name, arriving, leaving) in cases) + { + var list = new List(); + for (var i = 0; i < arriving.Length; i++) Arm(list, p, arrivingSteps[i], arriving[i], true); + for (var i = 0; i < leaving.Length; i++) Arm(list, p, leavingSteps[i], leaving[i], false); + ChainsAreArclength("junction " + name, ChainsOver(list.ToArray()), 0, list.Count); + } + } + + // One extractor over several frames with changing fields, as the renderer drives it: the table, the ping-pong links and the + // stats are per-frame scratch the kernels must clear or overwrite every frame. Every frame is judged like a fresh one (heads, + // conflicts, unterminated against the CPU, s against the CPU arclength). A table left uncleared after the first frame reads + // 63546 of the stress frame's 64573 ray segments off (Soul's probe). + static void ChainFramesReuse(Zone zone, Orbit[] orbits, GravityLowering.GravityTerms terms) + { + var range = Math.Max(1f, orbits.Max(o => -zone.GetHeight(new float2(o.Position.x, o.Position.y)))); + var zoneLevels = new IsolineExtractor.IsolineLevels(0f, range, SlopeEpsilon); + var stress = WellsFrom(IsolineRenderSmoke.StressWells(out var stressRange)); + var stressLevels = new IsolineExtractor.IsolineLevels(0f, stressRange, SlopeEpsilon); + var extractor = new IsolineExtractor(Shader, ChainCapacity); + try + { + TopFrame(ZoneCentre, 1100f, 1920, 1080, out var zoneOrigin, out var zonePerTexel); + var frames = new (string what, GravityLowering.GravityTerms terms, Vector2 origin, Vector2 perTexel, Vector2Int size, IsolineExtractor.IsolineLevels levels)[] + { + ("stress", stress, new Vector2(.5f, .5f), Vector2.one, new Vector2Int(1920, 1080), stressLevels), + ("stress moved by (13.3, 7.7)", stress, new Vector2(13.8f, 8.2f), Vector2.one, new Vector2Int(1920, 1080), stressLevels), + ("real zone 1920 x 1080", terms, zoneOrigin, zonePerTexel, new Vector2Int(1920, 1080), zoneLevels), + ("stress again", stress, new Vector2(.5f, .5f), Vector2.one, new Vector2Int(1920, 1080), stressLevels), + }; + for (var i = 0; i < frames.Length; i++) + { + var (what, t, origin, perTexel, size, levels) = frames[i]; + var command = new CommandBuffer { name = "IsolineExtractReuse" }; + extractor.Extract(command, t, origin, perTexel, size, levels); + Graphics.ExecuteCommandBuffer(command); + command.Release(); + var count = new uint[2]; + extractor.Count.GetData(count); + var all = new IsolineExtractor.IsoSegment[extractor.Segments.count]; + extractor.Segments.GetData(all); + var stats = new uint[4]; + extractor.ChainStats.GetData(stats); + var result = new Result { Segments = all.Take((int)count[0]).ToArray(), Written = count[0], Requested = count[1], ChainStats = stats }; + var (conflicts, _) = Orientation($"reused extractor, frame {i + 1} ({what})", result.Segments); + ChainsAreArclength($"reused extractor, frame {i + 1} ({what})", result, conflicts); + } + } + finally + { + extractor.Release(); + Free(stress); + } + } + // ---- chain cost (reported, not gated) --------------------------------------------------------------------------------- // The chain kernels' cost (ChainClear through ChainEmit, J passes): per iteration, Extract recorded 8 times through Finish and diff --git a/Assets/Scripts/Editor/IsolineRenderSmoke.cs b/Assets/Scripts/Editor/IsolineRenderSmoke.cs index ed3cb9fbd..a19f120b4 100644 --- a/Assets/Scripts/Editor/IsolineRenderSmoke.cs +++ b/Assets/Scripts/Editor/IsolineRenderSmoke.cs @@ -681,8 +681,13 @@ namespace Aetheria.EditorTools // is |ln(mutant / reference)| 1.10, 1.48 and 0.103. The shipped frames equal the reference frames to the pixel at both poses // (|ln| 0 at every percentile), so the shipped reading is floored at one pixel of run length: ln(7/6) 0.154, ln(45/44) 0.0225, // ln(52/51) 0.0194. Bounds at the geometric means: p5 0.412 (separation 7.1), p25 0.182 (66), p50 0.0447 (5.3). The flow - // frame's mutant readings were not taken at 3d8a0d1d3; the same bounds apply to it. - const double DashP5Bound = 0.412, DashP25Bound = 0.182, DashP50Bound = 0.0447; + // Flow pose (verdict cut-isoline-chain-arclength.s1, finding flow-p5-bound-below-its-floor): p5 is 1 px there, so one pixel of + // quantisation is |ln| 0.693, above any bound a mutant could fall under: the p5 gate is dropped. p25 and p50 take Soul's + // real-shader mutants (mutA and mutB soul.txt) at the flow pose, those the one-pixel floor separates by 1.2: p25 floor + // ln(13/12) 0.080 against the least claimed, M8 (the tail has a rank) 0.405, bound 0.180, separation 5.1 (M3 0.734, M5 1.39; + // M2 J = 8 reads 0.080, unclaimed); p50 floor ln(31/30) 0.0328 against M3 (rank counts segments) 0.105, bound 0.0587, + // separation 3.2 (M5 2.01; M2 0.0328, M8 0, M4 0 unclaimed). + const double DashP5Bound = 0.412, DashP25Bound = 0.182, DashP50Bound = 0.0447, FlowP25Bound = 0.180, FlowP50Bound = 0.0587; // The map pose (Period 160 u, 78.5 px) and Soul's flow pose (Period 60 u at 1.17 u/px), rays only: the dash run lengths of // the frame drawn from the extractor's segments against the frame drawn from the same segments with s replaced by the CPU @@ -711,7 +716,10 @@ namespace Aetheria.EditorTools Info($"dashes {name} pose, loops included: run length px shipped {Pcts(DashRuns(segs, chains, view, shippedFrame, ppx, true))}; reference {Pcts(DashRuns(segs, chains, view, referenceFrame, ppx, true))}"); if (shippedRuns.Count < 40 || referenceRuns.Count < 40) { Fail("dashes", $"{name}: {shippedRuns.Count} and {referenceRuns.Count} dash runs, the check would be vacuous"); continue; } Info($"dashes {name} pose: {segs.Count(s => ((s.key >> 8) & 1u) != 0)} ray segments, {chains.Count} chains of which {chains.Count(c => IsClosed(EdgesOf(segs, c)))} closed loops of {chains.Where(c => IsClosed(EdgesOf(segs, c))).Sum(c => c.Seg.Count)} segments (not measured); run length px shipped {Pcts(shippedRuns)} ({shippedRuns.Count} runs); reference (CPU arclength) {Pcts(referenceRuns)} ({referenceRuns.Count} runs)"); - foreach (var (label, q, bound) in new[] { ("p5", .05, DashP5Bound), ("p25", .25, DashP25Bound), ("p50", .5, DashP50Bound) }) + var gates = name == "map" + ? new[] { ("p5", .05, DashP5Bound), ("p25", .25, DashP25Bound), ("p50", .5, DashP50Bound) } + : new[] { ("p25", .25, FlowP25Bound), ("p50", .5, FlowP50Bound) }; + foreach (var (label, q, bound) in gates) { var gap = Math.Abs(Math.Log(Pct(shippedRuns, q) / Pct(referenceRuns, q))); Info($"dashes {name} {label}: shipped {Pct(shippedRuns, q):G3} reference {Pct(referenceRuns, q):G3} |ln(shipped/reference)| {gap:G3} (bound {bound:G3})"); diff --git a/Assets/Scripts/Zone Display/IsolineExtractor.cs b/Assets/Scripts/Zone Display/IsolineExtractor.cs index 4f78a9b2e..2b5515349 100644 --- a/Assets/Scripts/Zone Display/IsolineExtractor.cs +++ b/Assets/Scripts/Zone Display/IsolineExtractor.cs @@ -85,8 +85,10 @@ public sealed class IsolineExtractor /// Larger only where a check needs room past the guard. public IsolineExtractor(ComputeShader shader, int capacity, int backing = 0) { - // the chain kernels dispatch one thread per segment indirectly: 64 to a group, at most 65535 groups - if (capacity > 65535 * 64) throw new System.ArgumentOutOfRangeException(nameof(capacity), capacity, "more segments than one indirect dispatch covers"); + // Finish sizes the table at the next power of two of 4 x count and ChainClear runs 256 slots to a group, at most 65535 groups: + // the table may not pass 2^23 slots, so the count (at most Capacity) may not pass 2^21. Per-segment dispatches, 64 to a group, + // then stay under 32768 groups. + if (capacity > 1 << 21) throw new System.ArgumentOutOfRangeException(nameof(capacity), capacity, "ChainClear would need more than 65535 groups"); _shader = shader; Capacity = capacity; _sample = shader.FindKernel("Sample"); @@ -149,6 +151,14 @@ public sealed class IsolineExtractor cmd.SetComputeBufferParam(_shader, _march, CountId, Count); cmd.DispatchCompute(_shader, _march, groupsX, groupsY, 1); + RecordFinishAndChains(cmd, size, upTo); + } + + /// Records Finish and, unless upTo stops short, the chain kernels over the segments and Count already in the buffers: what + /// Extract appends after March. Fixtures call it over segments they wrote themselves (Count[0] and [1] set). + public void RecordFinishAndChains(CommandBuffer cmd, Vector2Int size, Stage upTo = Stage.Chains) + { + cmd.SetComputeIntParam(_shader, "_Capacity", Capacity); cmd.SetComputeBufferParam(_shader, _finish, CountId, Count); cmd.SetComputeBufferParam(_shader, _finish, ArgsId, _args); cmd.DispatchCompute(_shader, _finish, 1, 1, 1);