Skip to content

gfx: the card shader skips the lit-glass work outside the card body - #985

Closed
GLinnik21 wants to merge 4 commits into
mainfrom
gfx/card-pad-early-out
Closed

GLinnik21 wants to merge 4 commits into
mainfrom
gfx/card-pad-early-out

Conversation

@GLinnik21

@GLinnik21 GLinnik21 commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Outcome: closed without merging (TV-measured, 2026-10-24)

This version is pixel-identical, but it saves nothing net, so it does not land.

Pixels. UI-plane capture on the TV, release build, synthetic mock:

  • Home shelves and library grid: 0 differing px against both pre-change captures.
  • Home: 0 px against one pre-change capture. The 991 px against the other (max delta 1) is the known boot-to-boot noise, which two pre-change boots also show.
  • Both the plain and the focus program linked: 0 shader compile FAILED / link failed lines in every run.

GPU. HWCNT GPU_ACTIVE p50 per frame. Base and this head were run alternately on one set in one session (base, head, head, base). Base is origin/main fbafac6, which is this head's own base, so the comparison includes #980, #981 and #983.

scene base this head change
grid-pong 9.80M, 9.80M 9.76M, 9.75M -0.045M (-0.5%)
hero-pong 8.51M, 8.52M 8.55M, 8.55M +0.035M (+0.4%)

Both pairs point the same way on both scenes. With the pad back on the body's shared tail, the plain program only skips the rim's max/abs/mul/mix on pad fragments. That is worth less than what the bigger program costs the hero's other IPROG draws, which is the register-pressure risk named below; the first version's hero row (no change) did not show it. Neither scene gets near the ~8M miss knee on this lever.

The first version (d54cac1, the separate pad exit) saved 0.26M on grid but is NOT picture-identical: red was 1/255 lower on 1,118 shelves px and 20 library px. Whether that trade is acceptable is the owner's decision, so it is not landed either.


What

fs_img.frag (the card composite) no longer runs the lit-glass and rim work for a fragment with d >= 1.0, i.e. outside the rounded card body. This is lever F1 of the grid-pong GPU budget. It applies to the PLX_FOCUS program (the focused or risen card) and to the plain program (a resting card's ring pieces, and a card drawn whole). PLX_STILL_GROUND shares the plain text.

Correction to the first version of this PR. That version said the picture was identical. It was not: its pad exit wrote vec4(0, 0, 0, sh) from its own copy of the body's shadow expression, and on the TV (Mali-T820, release build, 2026-10-24, UI-plane capture, deterministic across boots) a build with it differed from the pre-change build in 1,118 px on the Home shelves screen and 20 px on the library grid. Every differing pixel is the red channel lower by exactly 1/255, and every one sits 1-5 px outside a resting card's edge (the shadow pad, d >= 1.0). Nothing else differs; Home with the hero is 0 px. The simulator evidence below could not see this: it is desktop GL, which ignores mediump.

This version. There is no exit any more. m = 1.0 - smoothstep(-1.0, 1.0, d) is computed once for every fragment, then if (d < 1.0) { ... } wraps only the work that changes tex (the glow, glare and gloss of the focus program, and the rim). The shadow and the closing gl_FragColor write exist once after the gate, so a pad fragment runs the body's own instructions with tex and ta the raw sample, instead of a second transcription of them. The focus program's fully-transparent early exit (d >= 1.0 and the shadow's box proxy past its falloff) is main's again, verbatim. TV re-verification of this version is pending; the device tables below were measured on the first version.

Why (measured on the TV 2026-10-24)

  • Home's grid-pong bench runs at a p50 of 9.93M GPU cycles a frame. The budget is about 11.2M, and misses climb above about 8M.
  • The card draws (Class::Card) cost 3.58M of it. The focus program alone costs about 1.82M, at roughly 13 arithmetic words a pixel on about 258K px a frame.
  • A card is drawn as one quad inflated by its shadow pad. Every fragment of the pad, outside the rounded body, ran the whole card path (glow, glare, gloss, rim, texture mix) before the coverage mask zeroed it. The focus program already had a bare vec4(0.0) exit for the part of the pad the shadow's box proxy had cleared; the rest of the pad, where the shadow still reaches, did not.

What the saving is now, and what it costs

  • Focus program: a pad fragment skips the glow, glare, gloss and rim (the bulk of the lit-glass work), as before.
  • Plain program: the saving is now only the rim and its mix (a handful of arithmetic ops; no malioc count was taken). The first version also skipped smoothstep for these fragments; they pay it again here, and they pay the shadow they always paid.
  • Control flow is not what the first version had. tex now merges from two paths into the tail and smoothstep runs on pad fragments. Sharing the tail's text is what the first version lacked, but it does not by itself guarantee the Mali compiler schedules or rounds the tail the same as the pre-change build, so this is not claimed bit-exact until the TV shows it.
  • Because of that, the device tables below are the first version's. They are kept as the record of what the lever can buy, not as this version's numbers.

Why the picture should be identical

For d >= 1.0, m is 0.0 where smoothstep clamps to its top edge, so tex*m is 0 whatever the glow, glare, gloss and rim made of tex: they reach only tex, never the alpha. Nothing else adds in outside the body (no outer glow halo, focus ring or drop-shadow tint). stillOver(tex*m, ta*m + sh, m, ramp) has scrim ramp*m and keep = (ta*m + sh)*(1 - scrim). The gate writes no fragment, never returns and assigns nothing but tex among the values the tail reads, so a fragment on either side of d = 1 takes the same tail with the same m, ta, d and shadow inputs; only tex differs, and m zeroes it.

Simulator evidence for the FIRST version (cannot show the difference above)

Evidence on the macOS simulator (settled raw 3840x2160 shot.png from tools/screenshots.py --keep, scenes home-shelves, library-grid, home; the simulator is desktop GL and ignores mediump, so it cannot show the device's rounding, which is the manager's device half):

  • The capture reaches the new code. With both new exits temporarily writing solid magenta (reverted, not in this PR), home-shelves shows 843,932 magenta px (10.2% of the frame) and library-grid 811,738 px (9.8%). The magenta rings every resting card (plain program) and the focused card's whole inflated quad (focus program). In library-grid the pixels that changed against the real branch capture are exactly the magenta ones (811,738 of 811,738). In home-shelves 3,310 changed pixels are not magenta; all lie in one 600x150 px box (x 365-964, y 1263-1413) at the "8m left" label under the focused card (its text is drawn over the pad, so the glyph edges blend with magenta there). The binary fingerprints differ as expected: the main binary has no if (d >= 1.0) {, the branch and magenta ones do.
  • library-grid: bit-identical. 7 captures (4 on main, 3 on this branch): every pair 0 differing px, max delta 0.
  • home-shelves: main1 = main(m1) = branch = branch(b1), all four bit-identical (0 differing px). Run-to-run noise exists on main itself: main1 vs main2 341 px (max delta 1), main1 vs m2 1,145 px (max delta 1). One branch run (b2) differs from every other run by 7,807 to 8,952 px, max delta 1; against the identical quartet the difference is confined to x 1317-1543, y 131-231, the top nav pill's Home tab area, far from any card. Counted against the magenta pad mask, the differing pixels that fall ON a pad are 0 for every branch-vs-main pair except those against the main run m2; the same 67 on-pad pixels (max delta 1) separate m2 from main1, from main2 and from every branch run, so they are main's own noise.
  • home: the whole hero frame is not deterministic on main either. 5 main and 4 branch captures: main-vs-main differs by 449 to 1,802 px in the Resume button (x 182-790, y 1425-1559, max delta 2), and one main capture (main3) differs from every other capture, main and branch alike, by 9.9K to 11.7K px over the whole frame (max delta 2). Branch-vs-main (main3 aside) differs by 2 to 2,399 px in the same Resume-button box (max delta 2); branch-vs-branch is 833 to 2,397 px there. With the magenta pad mask of home (221,277 px, all below y 1650), the pad-pixel count is 0 for every pair I counted among main1, main2, main4 and the branch runs (and for the branch against main5); only the pairs with main3 have any (79 px, identical for main1-vs-main3 and branch-vs-main3). The Resume button is outside every pad.

So branch-vs-main shows no difference outside the noise main-vs-main already has, and no difference on a pad pixel at all.

Tests

  • New test the_card_shaders_pad_path_runs_the_bodys_shadow_tail replaces the_card_shaders_pad_exit_is_the_shadow_alone and ..._pad_exit_tail_and_inputs_are_untouched. For the plain, still and focus programs it pins: the coverage, the shadow and the closing write each appear once (one u_shcol.a, one smoothstep); the lit work (glow, gloss, glareTop, glareZone, ease, topDist, chh, rim, u_rimcol, u_rimw) appears only inside the if (d < 1.0) gate; the gate follows the coverage directly, contains no return, gl_FragColor, else or discard, and assigns none of m, ta, d, sh, dShadow; what follows the gate is exactly the shadow (dShadow/dShadowBox/qs as the body has them in the focus program) and the closing write; and the focus program's transparent early exit is main's, once, ahead of the shadow's length().
  • Red against the first version's shader (make test-crate C=plx_gfx T=pad_path_runs, the branch's old fs_img.frag under the new test): assertion `left == right` failed: plain: the shadow's strength is read in more than one place (left: 2, right: 1), then test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 168 filtered out.
  • Green after the change: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 168 filtered out; finished in 0.00s. A temporary ta *= 0.5; inside the gate fails it (the pad gate assigns `ta`, which the tail reads on both paths); reverted.
  • make test-crate C=plx_gfx on the committed tree: test result: ok. 169 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 4.21s.
  • make check-nodef: exit 0.
  • make check on the committed tree (head 01d80ab): exit 0, ==== check: 106 steps — all ok (300s wall, 405s cpu, 4 at once ...), and its plx_gfx line is test result: ok. 169 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 49.33s. A first run failed only on ci/check-rust-inputs.py (No module named tomllib: the non-login shell resolves python3 to 3.9.6); the passing run had a Python 3.12 first on PATH.
  • No host gate compiles GLSL. A scoping or syntax error in the PLX_FOCUS variant would make FOCUS_IMAGE fail to link and fall back silently to the plain program, so the TV run must confirm from the log that both programs linked, not only compare resting-card pixels.

Header and test-doc prose

fs_img.frag's header (and the an_unfocused_program_carries_none_of_the_focus_code doc) said the plain program is byte-for-byte the pre-focus program. It is now that program plus ONE later addition, its pad path, and both say so. This branch merges origin/main (#983, fs_tex.frag); the two comment conflicts take both versions.

Risk to watch on the device

The plain program (IPROG) also runs glyphs, blur reductions and FrameCache quads. They leave through the u_card.z < 0.5 fast path above the gate, so the gate is reached only by rounded cards, but the shader header records a TV A/B where growing IPROG cost every draw. Please check non-card IPROG draws and the program's register use, not only the Class::Card cycles.

To verify on the TV before this leaves draft

  1. Pre-change build vs this head on the Home shelves screen and the library grid, UI-plane capture: 0 differing px (the first version: 1,118 and 20, red -1 in the pad).
  2. The log shows both the plain and the focus program linked.
  3. Re-take the HWCNT numbers below for this version: the plain program saves less than the first version did.

Device measurement (TV, 2026-10-24) - the FIRST version (the exit), not this head

Release build (ARM_PROFILE=release), panel off, sound off, synthetic mock. The control is #980's build on the same base (it changes nothing the GPU draws). HWCNT GPU_ACTIVE per frame (plxnative-hwcnt=frame.ui, ~3,100 frames, the first 60 discarded):

scene control p50 / p95 this PR p50 / p95 change
hero-pong 10.27M / 11.86M 10.26M / 11.91M none: no regression on the hero's photos, glyphs and other IPROG draws
grid-pong 9.93M / 11.54M 9.67M / 11.37M -0.26M (-2.6%)

Missed refreshes on grid-pong, separate runs with no counters armed, cycles 2-60: this PR 12, 15, 11; control 15, 11, 9. Each run also has one cycle-1 miss. This branch predates #980, and in all three runs the frame right after the egl surface (after 120 presents) line is a FRAMEDROP with swap= 13.2, 13.2 and 12.7 ms: the late_probe block #980 removed. At 9.67M the grid is still above the ~8M knee, so the miss count is not expected to move on this lever alone. The saving is measured; whether it shows in misses depends on the other levers.

malioc register counts were not taken (no Mali Offline Compiler on this host). The hero row is the device-side answer to the IPROG register-pressure question above: its draws do not get slower.

No linked issue.

🤖 Generated with Claude Code

GLinnik21 and others added 3 commits October 24, 2026 13:19
Measured on the TV 2026-10-24: Home's grid-pong bench runs at a p50 of 9.93M
GPU cycles a frame against a budget of about 11.2M, and misses climb above
about 8M. The card draws cost 3.58M of it; the focus program alone about
1.82M, at roughly 13 arithmetic words a pixel on about 258K px a frame. A card
is drawn as one quad inflated by its shadow pad, and every fragment of the pad,
outside the rounded body, ran the whole card path (glow, glare, gloss, rim,
texture mix) before the coverage mask zeroed it.

From d >= 1.0, m = 1.0 - smoothstep(-1.0, 1.0, d) is exactly 0 (smoothstep
clamps to exactly 1.0 past its top edge), so the closing vec4(tex*m, ta*m + sh)
is vec4(0, 0, 0, sh) whatever the lit glass made of tex: the glow, glare, gloss
and rim reach only tex, never the alpha. Both programs now compute only the
shadow there, with the body's own shadow text, and return: the PLX_FOCUS one,
which had a bare vec4(0.0) exit for the part of the pad the shadow's box proxy
had already cleared, and the plain one, which every resting card's ring pieces
run. The still-ground specialisation gets the same value (its scrim is ramp*m,
zero). A new source-shape test pins the threshold against the coverage
smoothstep's top edge, the closing expressions, the shadow text shared by the
exit and the body, and that no card work sits in the exit; it also replays the
identity in f32 over d >= 1.

Expected saving: 0.3-0.8M GPU cycles a frame on grid-pong. That is an estimate
from the pad's share of the card's fragments, not a measurement; it is to be
measured on the TV (hwcnt and the miss legs). The simulator renders desktop GL
and ignores mediump, so it cannot show the device's rounding.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…d shader claims

Review of the pad exit:
- fs_img.frag: drop the "Midgard branches per 2x2 quad" sentence (the file and
  gfx.rs say branching is per-thread) and say the fast-path draws "never
  execute" the plain pad exit instead of claiming they "pay nothing" for it.
- the_card_shaders_pad_exit_is_the_shadow_alone: the focus program's
  box-proxy zero-write must be nested inside `if (d >= 1.0)`, once, before
  the exit's `length(qs)` and shadow-alone write (hoisting it out stayed green).
- the_card_shaders_pad_exit_tail_and_inputs_are_untouched: in every variant
  the statements after the body's last `sh = ... (1.0 - m);` are exactly the
  closing gl_FragColor expression, and neither `d` after the exit nor `m`
  after its definition is assigned again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GLinnik21 added a commit that referenced this pull request Oct 24, 2026
)

## What and why

`fs_img.frag` (the image program, `IPROG`) has an early exit for any draw that is not a rounded card: `u_card.z < 0.5` writes `vec4(tex*u_tint.rgb, ta*u_tint.a)`, about 2 arithmetic words and 1 texture word a fragment. Midgard sizes registers and occupancy for the whole shader, so that exit was charged the card chain's footprint it never runs. The two Home hero photographs (radius 0) take it over about 2.1M px a frame, and so does every resting card's interior piece (`card_cut` splits it off with the card flag off). Measured on the device 2026-10-24.

This adds `fs_tex.frag`, that exit as a whole program, linked with the unchanged `vs_img.vert`.

- Body: the same declarations at the same precision and the same three statements as the exit. It reads `u_tex`, `v_cuv` and `u_tint` and nothing else (no premultiply, scrim, crop clamp or colour-space flag stands behind that exit), so nothing had to be carried over or excluded.
- Routing, in `draw_tex_core_in`, by the pure `plain_texture_program(plain, overridden, radius)`: a draw goes to the new program when it carries no focus/still program override and `radius < 0.5` (the packed `u_card.z`). A rim, shadow pad or tint do not matter: the shader never reads them past that exit, so a radius-0 `Class::Card` hero with a shadow pad routes there too. A split card's interior piece (drawn last, radius 0) routes there as well; its four ring pieces stay on `IPROG`. A still-program card's interior stays on the still program (its exit is `stillOver`, not the plain write).
- The program is optional like `FOCUS_IMAGE`: if it does not link, `IPROG` draws the same picture as before and a line is logged.
- Every uniform it reads (`u_trect`, `u_uvrect`, `u_tint`) is sent on every bind from one helper; `u_tscreen` and the sampler unit are set once at link.
- The overdraw/drawmask ledger is untouched: `gate()` still runs first, `note_card_path` still books the same paths. The program is chosen after both.

Expected saving (an ESTIMATE, not a result): 0.5-1.0M GPU cycles a frame on hero-pong and 0.2-0.4M on grid-pong.

## Identity evidence (macOS simulator, raw 3840x2160 PNGs)

Captured with `tools/screenshots.py --keep` on `home`, `home-shelves`, `library-grid` (the demo library), comparing the raw `shot.png` the sim writes, before JPEG encoding. A canary build (the new program halving its colour) changed 7.6M of `home`'s 8.3M pixels, so these scenes do run through the new program for most of the page. Desktop GL ignores precision qualifiers, so this proves the routing and the arithmetic, not Mali's fp16 rounding; that rests on the identical declarations the new test pins.

Run-to-run noise on `origin/main` (20 `home` captures of `origin/main`, one of them the reference): `home` differs by 0 to 2880 px (max channel delta 1, or 2 in 98 px), `home-shelves` by 0 to 2 px, `library-grid` by 0.

Branch vs main:

- `library-grid`: 0 differing pixels in every run.
- `home-shelves`: 0 to 2 px in all runs but one (below).
- `home`: in 17 of 19 branch captures, 0 to 3445 px against the main reference, max delta 1 or 2, in the same bottom-left region the main-vs-main noise sits in (identical bounding box (222,1425)-(790,1559) for branch-vs-main and main-vs-main). Three of them were pixel-identical to the main reference or within 4 px.
- Two of the 19 branch `home` captures (the first one after building, and one more) showed a different pattern: about 10-11k px, max delta 1-2, scattered over the hero photograph region. Neither reproduced in 17 further branch captures (home-only and full runs), and none of the 20 `origin/main` captures showed it. I could not tie it to the change: the new program's output is a pure function of its inputs and 17 captures match, which points at a differing input (an animation caught at a slightly different value under load, the failure mode `tools/screenshots.py` itself documents) rather than the program, but that is an inference, not a measurement. It is the one thing the reviewer should weigh.

## Local tests

- `make test-crate C=plx_gfx T=plain`: `test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 157 filtered out`
- `make test-crate C=plx_gfx`: `test result: ok. 163 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out`
- Mutation check: changing `fs_tex.frag`'s alpha term, or the rule to `radius < 0.6`, fails 3 of the new tests.
- The release-config hook's `cargo check --lib --no-default-features` stayed silent after the last edit. `make check` was not run locally (congested machine); CI gates it.

## Device measurement

Measured on the dev TV (Mali-T820, webOS 4.5) on 2026-10-24:
- **Build:** `make ARM_PROFILE=release deploy` of 6bea562.
- **Runner:** `tests/run.py --fps --mock`, panel off, no sampler.
- **Control:** the same base plus #980.
- **GPU counters:** `plxnative-hwcnt=frame.ui`, GPU_ACTIVE per frame, first 60 frames discarded.
- **Misses:** from separate runs with no counters.

| scene | build | GPU_ACTIVE p50 | p95 | missed, cycles 2-60 |
|---|---|---|---|---|
| hero-pong | control | 10.27M | 11.86M | 14, 7, 9 |
| hero-pong | this PR | **9.16M (-1.11M)** | 10.96M | 12, 5, 5 |
| grid-pong | control | 9.93M | 11.54M | 15, 11, 9 |
| grid-pong | this PR | 9.80M (-0.13M) | 11.41M | 14, 14, 12 |

- **Hero** gained more than the estimate.
- **Grid** gained less: its resting card interiors are a small share of the frame.
- **Grid misses** are unchanged within run-to-run noise, since 9.8M is still well above the ~8M knee where misses fall off.
- **Not yet done:** a pixel diff of this build on the device.

No linked issue.

TV pixel check (2026-10-24, release build of main + this + #985, UI-plane GRAPHIC capture on the mock catalog, the TV's own OSD masked): Home with the hero is 0 px different from the pre-work build (3e6665a); on the Home shelves and the library grid the only differing pixels (1118 and 20, each 1/255 in red) sit in the card shadow pad, which only #985 touches. Nothing this change draws differs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n/main

The first version of the pad exit (#985) wrote `vec4(0, 0, 0, sh)` from its own
copy of the body's shadow expression. On the TV (Mali-T820, release build,
2026-10-24) that was not the same picture: against the pre-change build the
Home shelves differed in 1,118 px and the library grid in 20 px, every one the
red channel lower by 1/255, 1-5 px outside a resting card's edge.

fs_img.frag now has no exit there. `m` is computed once, before a gate
`if (d < 1.0) { ... }` that wraps only the work that changes `tex` (the lit
glass in the focus program, and the rim); the shadow and the closing write are
written once after it, so a pad fragment runs the body's own instructions with
`tex`/`ta` the raw sample. The focus program's fully-transparent early exit is
main's again, verbatim.

the_card_shaders_pad_exit_is_the_shadow_alone and
..._pad_exit_tail_and_inputs_are_untouched are replaced by
the_card_shaders_pad_path_runs_the_bodys_shadow_tail: the coverage, shadow and
closing write each appear once per program, the lit work only inside the gate,
the gate neither returns, writes a fragment nor assigns anything but `tex` among
the values the tail reads, and what follows it is exactly the shadow and the
closing write.

Merges origin/main (#983): the two comment conflicts (the FOCUS_IMAGE test doc
and fs_img.frag's header) take both versions.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@GLinnik21 GLinnik21 changed the title gfx: the card shader returns the shadow alone outside the card body gfx: the card shader skips the lit-glass work outside the card body Oct 24, 2026
@GLinnik21 GLinnik21 closed this Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant