From c4fb8d79cf2ae5163dde068b94db3455f536296c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 15:13:00 +0100 Subject: [PATCH] RenderCheck: highlight flicker against a supersampled reference; the 3D draws with MSAA 4x project.godot rendering/anti_aliasing/quality/msaa_3d=2. The oracle frames draw with MSAA off (they check the shader's own pixels); the flicker check restores the project's setting, draws the glaze from 16 sub-pixel camera shifts in the play and close views and bounds the p99 per-pixel luma std at 1.3 times a 8x supersampled reference's and the highlight mean within 1 level of it. lodur:cut_spec:cut-bds-puppet-specaa.r1 Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 +- client/project.godot | 1 + client/tests/RenderCheck.cs | 202 ++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5cb27ac..ecdc9e6 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Synaptic Induction Chamber, the dummy strapped to a stool with its right hand in input only. Godot's physics engine is set to Dummy; each 120 Hz physics frame the client calls `BodyPainBoxSession.Step()` once and draws Ymir's lanes: one pose-carrying node per stable id, and the body drawn as one skinned SDF mesh (a surface-nets isosurface of the shape lanes, limbs smooth-blended into the body they hang from and -joined by a ball at each joint, built once at startup and rebuilt only when the bodies or joints change; skinned on the CPU every frame by `PuppetSkin`, which derives the facets, creases, vertex normals and altitudes from the skinned surface; flat-shaded, each edge softened by a bevel 2 px wide (`Look.BevelScreen`, at least `Look.BevelMin`, question lodur:question:bds-puppet-bevel-look-width): the fragment shader tilts the facet normal to each edge's bisector and blends corners to the vertex normal). The puppet is glazed porcelain: a bisque lit by a studio (a shadowing key and two rims, `Look.Studio`) that the camera carries (lodur:ruling:porcelain-studio-follows-camera), plus an analytic glaze that reflects the same studio (`client/studio.gdshaderinc`, Fresnel times the studio in the reflected direction, added as emission so it draws the same under gl_compatibility), pooled darker in concave creases. +joined by a ball at each joint, built once at startup and rebuilt only when the bodies or joints change; skinned on the CPU every frame by `PuppetSkin`, which derives the facets, creases, vertex normals and altitudes from the skinned surface; flat-shaded, each edge softened by a bevel 2 px wide (`Look.BevelScreen`, at least `Look.BevelMin`, question lodur:question:bds-puppet-bevel-look-width): the fragment shader tilts the facet normal to each edge's bisector and blends corners to the vertex normal). The puppet is glazed porcelain: a bisque lit by a studio (a shadowing key and two rims, `Look.Studio`) that the camera carries (lodur:ruling:porcelain-studio-follows-camera), plus an analytic glaze that reflects the same studio (`client/studio.gdshaderinc`, Fresnel times the studio in the reflected direction, added as emission so it draws the same under gl_compatibility), pooled darker in concave creases. The highlights are prefiltered where the shader decides them (geometric specular antialiasing, Tokuyoshi and Kaplanyan 2021): the spread of the shading normal over a pixel widens the bisque's roughness and the studio the glaze reads, so a highlight does not crawl as the camera moves; the 3D draws with MSAA 4x (`rendering/anti_aliasing/quality/msaa_3d`) for silhouettes and triangle edges. Needs the .NET 8+ SDK, cmake and a C compiler (MinGW gcc on Windows), the sibling Lodur, Ymir (with joint origin lanes, ABI 8) and CultLib checkouts (`..\Lodur`, `..\Ymir`, `..\CultLib`, or `LODUR_ROOT`, `YMIR_ROOT` and `CULTLIB_ROOT`; CultLib must be a clean checkout of 42dce361, the revision Ymir admits, or the build refuses), and Godot 4.7.2 mono. @@ -84,7 +84,7 @@ Exports need export templates (not installed here yet), so the client runs from The smoke has no renderer. `RenderCheck.tscn` is the check that does: it poses the puppet in a bent pose under a moved parent, renders one frame (gl_compatibility, the client's renderer), and compares the drawn silhouette with the mesh skinned on the CPU (intersection over union, at -least 0.999: a correct render measures 1.0000, and a forearm bone 10 degrees off, a bone 2 cm off and hard weights fail), and checks the bisque (the glaze off) is not clipped to white and that the chamber's light still lights it (a glow of 0 fails); with the glaze on at the close view, from the front and from behind, the glaze must change at least 3% of the puppet's pixels and, from the front, clip at most 12% (add `-- --glaze-png=` or `--glaze-behind-png=`). It also draws the shading normal (the soft bevel) on the torso and compares it with a CPU reference of the whole formula, skinned, welded and derived by the check itself, in five views: close up and from 3.7 m at 1280x720, close up in a bend of 90 degrees, and at 1920x1080 from 2.2 m and from 3.7 m (so a width fixed at 720 lines fails). In each: 90th percentile error at most 2 degrees, 0.6 across the bevel band, 1.5 on triangles whose width the smallest altitude caps, at most 0.7 on the pixels the corner blend reaches (the smooth vertex normal, in view space, within 1.5 widths of a corner) and at most 1.5 on the band of edges turning by 115 to 137 degrees (a bevel that is off, a stale crease, a linear falloff, a missing edge strip or width cap, the wrong width, a vertex normal left in model space, a shorter corner reach and a lower crease clamp fail). The drawn normals across triangle boundaries stay continuous: at most 0.5% of adjacent pixel pairs over 30 degrees at a bend of 60 degrees and 3% at 90, where some edges fold past 120 degrees and 2 px cannot soften them. Add `-- --png=`, `-- --normals-png=` or `-- --normals-far-png=` (also `--normals-bend90-png=`, `--normals-mid-png=`, `--normals-far1080-png=`) to keep a frame. It needs a display. +least 0.999: a correct render measures 1.0000, and a forearm bone 10 degrees off, a bone 2 cm off and hard weights fail), and checks the bisque (the glaze off) is not clipped to white and that the chamber's light still lights it (a glow of 0 fails); with the glaze on at the close view, from the front and from behind, the glaze must change at least 3% of the puppet's pixels and, from the front, clip at most 12% (add `-- --glaze-png=` or `--glaze-behind-png=`). It also draws the shading normal (the soft bevel) on the torso and compares it with a CPU reference of the whole formula, skinned, welded and derived by the check itself, in five views: close up and from 3.7 m at 1280x720, close up in a bend of 90 degrees, and at 1920x1080 from 2.2 m and from 3.7 m (so a width fixed at 720 lines fails). In each: 90th percentile error at most 2 degrees, 0.6 across the bevel band, 1.5 on triangles whose width the smallest altitude caps, at most 0.7 on the pixels the corner blend reaches (the smooth vertex normal, in view space, within 1.5 widths of a corner) and at most 1.5 on the band of edges turning by 115 to 137 degrees (a bevel that is off, a stale crease, a linear falloff, a missing edge strip or width cap, the wrong width, a vertex normal left in model space, a shorter corner reach and a lower crease clamp fail). The drawn normals across triangle boundaries stay continuous: at most 0.5% of adjacent pixel pairs over 30 degrees at a bend of 60 degrees and 3% at 90, where some edges fold past 120 degrees and 2 px cannot soften them. The oracle frames above are drawn with MSAA off (they check the shader's own pixels). Last, the flicker check draws the glaze with the project's MSAA from 16 sub-pixel camera shifts in the play view (2.4 m, FOV 55) and the close view (0.7 m), against the same shifts drawn 8 times the window's size, MSAA off, and box-filtered down in linear light: on the pixels the puppet covers fully, the 99th percentile of the per-pixel luma standard deviation over the shifts must stay within 1.3 times the reference's, and the mean luma of the reference's highlights (245 or more) within 1 level of the reference's (a glaze that crawls, or one blurred away or brightened, fails). It adds about 20 s. Add `-- --png=`, `-- --normals-png=` or `-- --normals-far-png=` (also `--normals-bend90-png=`, `--normals-mid-png=`, `--normals-far1080-png=`) to keep a frame. It needs a display. Known gap: the culling box. `Puppet` sets the mesh's `CustomAabb` to the skinned bounds every frame; a 1 cm box at the origin or at the centre culls the puppet in the close view and fails, but a box of half the bounds around the centre passes, since it only drops limbs where the box leaves the screen, which no view samples. diff --git a/client/project.godot b/client/project.godot index 12b1c93..fa9cad5 100644 --- a/client/project.godot +++ b/client/project.godot @@ -31,3 +31,4 @@ common/physics_interpolation=true renderer/rendering_method="gl_compatibility" renderer/rendering_method.mobile="gl_compatibility" +anti_aliasing/quality/msaa_3d=2 diff --git a/client/tests/RenderCheck.cs b/client/tests/RenderCheck.cs index 8f5257b..9cbfcb9 100644 --- a/client/tests/RenderCheck.cs +++ b/client/tests/RenderCheck.cs @@ -17,6 +17,8 @@ namespace BadDay.Client.Tests; /// It also reads the shading normal the shader draws (ShowNormals: the view-space normal as emission) on the torso in the (close, far, /// bent 90 degrees, and at 1920x1080 mid and far) and compares it pixel by pixel with a CPU reference of the whole formula (see ), on all the interior pixels /// and on the pixels of the corner blend and of sharp creases alone, and the drawn normals of adjacent pixels across triangle boundaries. +/// Last it checks that the glaze does not flicker (see ): the puppet in the pose the player sees, drawn with the project's MSAA from 16 sub-pixel camera shifts in the play and close views, against the same shifts drawn 8 times the window's size. +/// The frames above are drawn with MSAA off, since they check the shader's own pixels. /// godot --path client res://tests/RenderCheck.tscn [-- --png=path] [-- --glaze-png=path] [-- --glaze-behind-png=path] [-- --normals-png=path] [-- --normals-far-png=path] [-- --normals-bend90-png=path] [-- --normals-mid-png=path] [-- --normals-far1080-png=path]; exit code 0 passes, 1 fails. It needs a window, so it is /// not part of the headless smoke. /// @@ -93,6 +95,24 @@ public partial class RenderCheck : Node /// Across triangle boundaries: the pairs of adjacent pixels the check must reach (the shares and percentiles each view allows are in ). private const int MinPairs = 20000; + /// The flicker check's reference is this many times the window on a side; each view must reach at least this many interior and highlight pixels (see ) or it proves nothing. + private const int FlickerSupersample = 8, MinFlickerPixels = 20000, MinFlickerHighlights = 200; + + private const float HighlightLuma = 245f; + + /// + /// The 99th percentile of the per-pixel luma standard deviation under sub-pixel camera shifts, as a multiple of the supersampled reference's. The shipped glaze (spec AA and MSAA 4x) measures + /// 1.19 in the play view and 1.16 in the close view; the glaze without the prefilter (kernel 0, or the studio unfiltered) 1.56 and 1.46; without MSAA 1.40 and 1.21 (the interior metric does see + /// triangle edges, in the play view); the studio's smoothstep widened for energy-gaining blur 1.31 and 1.37. + /// + private const float MaxFlickerRatio = 1.3f; + + /// + /// How far the mean luma over the reference's highlights ( or more) may move from the reference's, in levels of 255. The shipped glaze measures 0.2 in the play view and + /// 0.3 in the close view; the pixel filter's variance at the paper's 1/(2 pi) instead of 1/24, which blurs the highlight away, 1.6 and 0.1, so the bound sits between. + /// + private const float MaxHighlightShift = 1f; + private const int BendSeed = 7; private const float BendDegrees = 60f; @@ -123,6 +143,7 @@ public partial class RenderCheck : Node // The puppet alone, on black, with a parent that is neither at the origin nor unturned. main.SetPhysicsProcess(false); + GetViewport().Msaa3D = Viewport.Msaa.Disabled; // the oracle frames below check the shader's own pixels; the flicker check restores the project's setting main.Hud.Visible = false; // The chamber's volume is not the puppet, but its glow lights the puppet: run the box near full glow and keep the light. foreach (var volume in main.Chamber.GetChildren().OfType()) volume.Visible = false; @@ -173,6 +194,7 @@ public partial class RenderCheck : Node if (lift < MinGlowLift) { GD.PrintErr($"RENDER FAIL: the chamber's light lifts the puppet by {lift:F2} levels, under {MinGlowLift}; the box's glow does not light the body"); ok = false; } ok &= await GlazeCheck(main, shape, carriers, world); ok &= await NormalCheck(main, shape, carriers, world); + ok &= await FlickerCheck(main); return ok; } @@ -419,6 +441,186 @@ public partial class RenderCheck : Node return ok; } + /// + /// The glaze must not flicker: a highlight whose pixel value jumps as the camera moves by a fraction of a pixel crawls on screen. The puppet (glaze on, the shipped shader + /// and the project's MSAA) is drawn from 16 camera positions on a sub-pixel grid in the play view and the close view; the same shifts are drawn by a + /// viewport times the window's size, MSAA off, box-filtered down in linear light, which is what a pixel would show if it averaged the + /// highlight over its own area. On the pixels the puppet covers fully in every reference frame (eroded by one pixel, so the silhouette, which MSAA owns, is out), the + /// per-pixel standard deviation of luma over the shifts must have its 99th percentile at most times the reference's in both views, and the mean luma + /// over the pixels the reference lights to or more must be within levels of the reference's (a filter that blurs + /// the highlight away, or adds energy, moves it). + /// + private async Task FlickerCheck(Main main) + { + var window = GetWindow(); + var home = window.Size; + window.Size = Hd; + for (int i = 0; i < 4; i++) await ToSignal(RenderingServer.Singleton, RenderingServer.SignalName.FramePostDraw); + var puppet = main.Bodies.GetNode("Puppet"); + var camera = main.GetNode("Camera"); + camera.Current = true; + puppet.Glazed = true; + puppet.ShowNormals = false; + // The pose the player sees: the session's own, under an unmoved body renderer, not the bent pose of the oracle frames. + main.Bodies.Transform = Transform3D.Identity; + main.Bodies.Sync(main.Session.View, teleport: true); + for (int i = 0; i < 4; i++) await ToSignal(RenderingServer.Singleton, RenderingServer.SignalName.FramePostDraw); + var viewport = GetViewport(); + viewport.Msaa3D = (Viewport.Msaa)(int)ProjectSettings.GetSetting("rendering/anti_aliasing/quality/msaa_3d"); + int w = viewport.GetTexture().GetWidth(), h = viewport.GetTexture().GetHeight(); + + var carriers = puppet.GetChildren().OfType().Where(c => c.Name.ToString().StartsWith("Body")).ToArray(); + var head = carriers.OrderByDescending(c => c.GlobalPosition.Y).First().GlobalPosition; + var torso = puppet.GetNode("Body101").GlobalPosition; + // Both views look at the head and shoulders from the same side: the play view from the distance and field of view OrbitCamera starts at (2.4 m, 55 degrees), the close view from 0.7 m. + var target = head * 0.45f + torso * 0.55f; + var views = new (string Label, float Distance)[] { ("play", 2.4f), ("close", 0.7f) }; + const float yaw = 0.5f, pitch = 0.15f; + + var reference = new SubViewport { Size = new Vector2I(w * FlickerSupersample, h * FlickerSupersample), RenderTargetUpdateMode = SubViewport.UpdateMode.Disabled, Msaa3D = Viewport.Msaa.Disabled }; + var referenceCamera = new Camera3D { Current = true, Fov = 55f }; + reference.AddChild(referenceCamera); + AddChild(reference); + + var shifts = new List(); + for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) shifts.Add(new Vector2((i + 0.5f) / 4f - 0.5f, (j + 0.5f) / 4f - 0.5f)); + + bool ok = true; + foreach (var (label, distance) in views) + { + void Place(Vector2 shift) + { + camera.Fov = 55f; + var direction = new Vector3(Mathf.Sin(yaw) * Mathf.Cos(pitch), Mathf.Sin(pitch), Mathf.Cos(yaw) * Mathf.Cos(pitch)); + camera.GlobalPosition = target + direction * distance; + camera.LookAt(target, Vector3.Up); + float pixel = 2f * distance * Mathf.Tan(Mathf.DegToRad(55f) / 2f) / h; + camera.GlobalPosition += camera.GlobalBasis.X * shift.X * pixel + camera.GlobalBasis.Y * shift.Y * pixel; + referenceCamera.Fov = camera.Fov; + referenceCamera.GlobalTransform = camera.GlobalTransform; + } + + var supersampled = new List(); + var coverage = new List(); + foreach (var shift in shifts) + { + Place(shift); + var (luma, cover) = await Supersample(reference, w, h); + supersampled.Add(luma); + coverage.Add(cover); + } + var drawn = new List(); + foreach (var shift in shifts) + { + Place(shift); + for (int i = 0; i < 3; i++) await ToSignal(RenderingServer.Singleton, RenderingServer.SignalName.FramePostDraw); + var image = viewport.GetTexture().GetImage(); + image.Convert(Image.Format.Rgba8); + drawn.Add(Luma(image.GetData(), w * h)); + } + + var interior = new bool[w * h]; + Array.Fill(interior, true); + foreach (var cover in coverage) for (int i = 0; i < interior.Length; i++) interior[i] &= cover[i] >= 0.999f; + var eroded = new bool[w * h]; + for (int y = 1; y < h - 1; y++) + for (int x = 1; x < w - 1; x++) + { + int i = y * w + x; + eroded[i] = interior[i] && interior[i - 1] && interior[i + 1] && interior[i - w] && interior[i + w]; + } + var highlight = new bool[w * h]; + for (int i = 0; i < highlight.Length; i++) highlight[i] = eroded[i] && supersampled.Any(l => l[i] >= HighlightLuma); + + int pixels = eroded.Count(b => b), highlights = highlight.Count(b => b); + float shownP99 = P99(Deviation(drawn, eroded)), referenceP99 = P99(Deviation(supersampled, eroded)); + float shownMean = Mean(drawn, highlight), referenceMean = Mean(supersampled, highlight); + float ratio = shownP99 / Mathf.Max(referenceP99, 1e-3f), highlightShift = Mathf.Abs(shownMean - referenceMean); + GD.Print($"RENDER flicker ({label}): {pixels} interior pixels, {highlights} highlight; p99 luma std {shownP99:F1} against the {FlickerSupersample}x reference's {referenceP99:F1} (ratio {ratio:F2}); highlight mean {shownMean:F1} against {referenceMean:F1} (shift {highlightShift:F1})"); + if (pixels < MinFlickerPixels || highlights < MinFlickerHighlights) { GD.PrintErr($"RENDER FAIL: the {label} flicker view reaches {pixels} interior and {highlights} highlight pixels, under {MinFlickerPixels} and {MinFlickerHighlights}; it proves nothing"); ok = false; } + if (ratio > MaxFlickerRatio) { GD.PrintErr($"RENDER FAIL: the glaze's highlights flicker in the {label} view: p99 luma std {shownP99:F1} is {ratio:F2} times the supersampled reference's {referenceP99:F1}, over {MaxFlickerRatio}"); ok = false; } + if (highlightShift > MaxHighlightShift) { GD.PrintErr($"RENDER FAIL: the {label} view's highlights average {shownMean:F1}, {highlightShift:F1} levels from the reference's {referenceMean:F1}, over {MaxHighlightShift}; the glaze is blurred away or brighter than the studio"); ok = false; } + } + reference.QueueFree(); + window.Size = home; + return ok; + } + + /// The reference frame of the current camera: the supersampled viewport box-filtered to the window's size in linear light, as luma (Rec. 709 weights on the encoded levels, 0 to 255) and the share of its samples the puppet covers (any channel over 31 of 255, on black). + private async Task<(float[] Luma, float[] Cover)> Supersample(SubViewport reference, int w, int h) + { + reference.RenderTargetUpdateMode = SubViewport.UpdateMode.Once; + for (int i = 0; i < 2; i++) await ToSignal(RenderingServer.Singleton, RenderingServer.SignalName.FramePostDraw); + var big = reference.GetTexture().GetImage(); + big.Convert(Image.Format.Rgba8); + var data = big.GetData(); + big.Dispose(); + int factor = FlickerSupersample, row = w * factor; + var luma = new float[w * h]; + var cover = new float[w * h]; + float inverse = 1f / (factor * factor); + Parallel.For(0, h, y => + { + for (int x = 0; x < w; x++) + { + float r = 0, g = 0, b = 0, covered = 0; + for (int sy = 0; sy < factor; sy++) + for (int sx = 0; sx < factor; sx++) + { + int o = ((y * factor + sy) * row + x * factor + sx) * 4; + r += SrgbToLinear[data[o]]; g += SrgbToLinear[data[o + 1]]; b += SrgbToLinear[data[o + 2]]; + if (Math.Max(data[o], Math.Max(data[o + 1], data[o + 2])) > 31) covered++; + } + luma[y * w + x] = 0.2126f * LinearToSrgb(r * inverse) + 0.7152f * LinearToSrgb(g * inverse) + 0.0722f * LinearToSrgb(b * inverse); + cover[y * w + x] = covered * inverse; + } + }); + return (luma, cover); + } + + private static readonly float[] SrgbToLinear = Enumerable.Range(0, 256).Select(i => { float c = i / 255f; return c <= 0.04045f ? c / 12.92f : Mathf.Pow((c + 0.055f) / 1.055f, 2.4f); }).ToArray(); + + private static float LinearToSrgb(float c) => 255f * (c <= 0.0031308f ? c * 12.92f : 1.055f * Mathf.Pow(c, 1f / 2.4f) - 0.055f); + + private static float[] Luma(byte[] rgba, int count) + { + var luma = new float[count]; + for (int i = 0; i < count; i++) luma[i] = 0.2126f * rgba[4 * i] + 0.7152f * rgba[4 * i + 1] + 0.0722f * rgba[4 * i + 2]; + return luma; + } + + /// The standard deviation over the frames of each masked pixel. + private static List Deviation(List frames, bool[] mask) + { + var result = new List(); + for (int i = 0; i < mask.Length; i++) + { + if (!mask[i]) continue; + float mean = 0; + foreach (var f in frames) mean += f[i]; + mean /= frames.Count; + float sum = 0; + foreach (var f in frames) sum += (f[i] - mean) * (f[i] - mean); + result.Add(Mathf.Sqrt(sum / frames.Count)); + } + return result; + } + + private static float P99(List values) + { + if (values.Count == 0) return 0; + values.Sort(); + return values[(int)(values.Count * 0.99f)]; + } + + private static float Mean(List frames, bool[] mask) + { + double sum = 0; + long count = 0; + foreach (var f in frames) for (int i = 0; i < mask.Length; i++) if (mask[i]) { sum += f[i]; count++; } + return count == 0 ? 0 : (float)(sum / count); + } + private static Surface BuildSurface(PuppetShape shape, Transform3D[] world) { int n = shape.Corners.Length, triangles = n / 3;