# P2 package D — `p2/magma` result (v2, rework round 2) Tree: `~/w7/p2-magma`, branch `p2/magma`, from the contract commit `9c6a8a25` (tag `p2/contract`). Not pushed. Build dirs `build-linux` (pull, the G1 build), `build-push`, `build-nolegacy` (`-DMOBILEGL_PIPE_PUSH=ON -DMOBILEGL_PIPE_LEGACY_MEMOS=OFF`). One extra tree exists for verification only: `~/w7/p2-magma-armed`, branch `tmp/magma-armed` = `p2/magma` merged with `p2/tracker` (clean merge, no conflicts — which is itself evidence for C.5's ownership split). **It is not a deliverable**; it exists because m1 cannot be exercised on a tree with no tracker to bind a render-state CSO (review MAJOR 5), and it is where the arming evidence in §4 was taken. No file in any other package's tree was edited. ## 1. Commits | sha | subject | |---|---| | `d66f400f` | `[Refactor] (Magma): key the pipeline memo on the render-state CSO handle and stop recomputing a hash the client already computed` | | `553065dc` | `[Refactor] (Magma): drive the dynamic tail from the pushed dynamic version and make the chunk table check DynamicTailKey's inventory` | | `391e1230` | `[Refactor] (Magma): key the vertex-input cache and the VAO draw memo on {slot, gen} instead of a lifetime id and a heap address` | | `fb410351` | `[Refactor] (State, Magma): take the backend's raw pointers out of the frontend VAO - the hash and state memos become the factory's own per-slot fields` | | **`a0e7c93d`** | **`[Fix] (Magma): make the legacy-memo lever a startup gate for Magma's own bit, bound the {slot, gen} mint, and keep the all-pull arm free of push-only cost`** | | **`1e89fd90`** | **`[Fix] (Magma): assert rather than assume that a handle indexed into a per-slot table is non-null`** | The first four are v1's, unchanged. The last two are this round. Files touched are the same six as v1 — `MG_Backend/DirectVulkan/Renderer/{MagmaPipeArms.h, VulkanRenderer.h, VulkanRenderer.cpp, VertexInputStateFactory.h, VertexInputStateFactory.cpp}` and `MG_State/GLState/VertexArrayState/VertexArrayObject.h` — all this package's by C.5. ## 2. How each major was fixed ### MAJOR 1 — `MOBILEGL_PIPE_LEGACY_MEMOS=0` aborted every draw through a non-Track-H bit `MagmaPipeRequireLegacyArm` (per-draw `MGLOG_F` + `std::abort()`) is **deleted**. In its place `MagmaPipeValidateSubsystemConfiguration()` runs **once, from `VulkanRenderer::Initialize()`**, which is what D14 actually specifies ("a Track-H subsystem whose bit is clear is a **startup** `Fatal{PipeLegacyMemosDisabled}`"). Three boundaries, each of which the per-draw shape got wrong: * it checks **only bit 6** (`kMGPipeSubsystemMagmaVertexInput`), and only when Magma is the backend being brought up, so Espryt's bit 5 cannot kill a DirectVulkan run and vice versa; * **bit 0 is not consulted at all.** It is not Track H (D14 labels only bits 5/6 that) and it is not a memo re-key — it decides where the pipeline memo's *state key* comes from. A clear bit 0 now silently selects the state hash; * the "no CSO bound" case is a **fallback, not an abort**, in every build (see below). Every re-keyed Track-H site now asks one helper, `MagmaPipeTrackHArmIsHandles(bit)`, which is `MagmaPipeSubsystemOn(bit)` where the legacy arm is compiled and a compile-time `true` where it is not (the startup gate has already made a clear bit fatal there). That also removes minor 3: `VertexInputStateFactory::ComputeHash` was the one site deciding for itself. Reproduction of the review's exact commands, on the final commit: | command | v1 | v2 | |---|---|---| | `MOBILEGL_PIPE_PUSH=0x60 MOBILEGL_PIPE_LEGACY_MEMOS=0 ctest -L integration-gpu -j 4 -R DirectVulkan` | 9/9 "Subprocess aborted" on the CrossFrameBuffer subset | **432/432** | | `MOBILEGL_PIPE_LEGACY_MEMOS=0` at the default mask | aborted | **432/432** | | `MOBILEGL_PIPE_PUSH=0x20 MOBILEGL_PIPE_LEGACY_MEMOS=0` (Espryt's bit alone) | aborted mid-draw | startup Fatal naming bit 6, the harness's pre-flight child catches it and the case **SKIPS visibly** with "no usable GPU/display/ICD for backend DirectVulkan" — the D14 semantics, loud, before any frame | **The no-CSO fallback.** `delete_render_state` clears `BoundRenderStateCso` (`MG_Pipe/PipeApply.cpp`), so the null handle is reachable on *any* tree, integrated or not. The fallback is the pre-handle `ComputePipelineStateHash` where one is compiled, and the client's own `MGPipeComputePipelineSubsetHash` over the same 396 pipeline bytes where it is not (`ComputePipelineSubsetStateHashFallback`). That is what makes `-DMOBILEGL_PIPE_LEGACY_MEMOS=OFF` a **runnable** configuration: v1's `build-nolegacy` was 180/432 with `Fatal{PipeLegacyMemosDisabled}` on every draw, v2's is **432/432**. The fallback logs `MGLOG_W_ONCE` rather than `MGLOG_D_ONCE`. W is compiled in at every shipped log level and `_ONCE` costs one static bool test, so a run whose pipeline memo never keys on a CSO handle now says so in its log — which is the thing that made MAJOR 5 invisible, and which §4 turns into the arming evidence. ### MAJOR 2 — the all-pull control arm was contaminated `snap.vaoHandle = ResolveVaoHandle(vao)` at both stamping sites is now inside `if (MagmaPipeTrackHArmIsHandles(kMGPipeSubsystemMagmaVertexInput))`, so `MOBILEGL_PIPE_PUSH=0` mints nothing and compares nothing for a field that arm never reads. D14's "reproduces P1's behaviour exactly" holds again, and D.4.3's T2 is back to meaning "P1's residual fill alone". ### MAJOR 3 — unbounded, never-reclaimed growth of the client slot allocator Magma no longer uses `MG_Impl/Pipe/SlotAllocator` at all. `grep -rn "MGPipeSlots()" MobileGL --include=*.cpp --include=*.h | grep -v MG_Impl/Pipe/SlotAllocator` now returns **one comment line and no code**. The reason the allocator was wrong here is not a defect in the allocator: nothing in P2 can call its `Free`. There is no frontend death notification Magma can hook — `BufferBackendOps::OnDestroy` is handed a `BackendBufferResource`, not the `BufferObject`, and only fires for a buffer that ever had one; `VertexArrayObject` has no hook at all, and adding one is D13's explicit-destroy work, which covers Espryt's six kinds and not `VertexElementsCso`. In its place, `MagmaPipeIdentityTable` (in `MagmaPipeArms.h`): fixed capacity, 2-way set-associative, indexed by lifetime id, LRU victim inside the set, `Gen` incremented whenever a slot changes owner. Two instances — 2048 entries for `VertexElementsCso` (32 KB), 8192 for `Buffer` (128 KB). Bounded, nothing to reclaim, and exactly as ABA-proof as the allocator's `{slot, gen}`: a recycled slot always carries a moved generation, so a stale handle cannot match. It also takes `MG_Backend`'s only `MG_Impl` include back out (minor 4) and turns the per-attribute hash-map probe into an array index (minor 2). ### MAJOR 4 — the density premise was false and the mitigations had been deleted Both per-slot tables are now a **bijection** with the mint rather than a masked direct map: `kVaoDrawMemoSlotCount == kMagmaVaoIdentityEntries` and `VertexInputStateFactory::kVaoMemoSlotCount == kMagmaVaoIdentityEntries` are `static_assert`s, entry *i* is slot *i + kMGPipeFirstAllocatableSlot*, and the index is `MagmaPipeSlotIndex(handle)` with no mask. Two live VAOs cannot land on one entry at all, so the birthday-collision scenario the review constructed cannot happen. The mitigations are not gone, they moved one level down and are now written once: the second candidate is the identity table's second way, and the frame-serial victim choice is its LRU. The comments that argued the false density claim are rewritten. The residual cost this design does carry, stated plainly: with more live objects than the table holds, an LRU eviction changes a live object's handle. For a VAO that costs a memo recompute (both tables are caches; correctness is the exact `{slot, gen}` compare). For a **buffer** it changes the vertex-input *content* hash, which costs a rebuilt `BackendVertexInputState` — but `ComputeHash` is memoised per VAO on its config version, so that is paid per VAO reconfiguration, not per draw, and the buffer table is sized 4× the VAO one for it. It is not measured here; nothing on desktop reaches those working sets. ### MAJOR 5 — m1 was executed by no test Fixed by verification, not by code: `~/w7/p2-magma-armed` = `p2/magma` + `p2/tracker` (D.1's own order), where the tracker emits `create_render_state`/`bind_render_state` and the applier publishes `BoundRenderStateCso`. See §4 for the four runs; the short version is that on that tree **432/432 integration cases and all 40 DirectVulkan retraces run with zero fallback warnings**, i.e. every draw's pipeline memo was keyed on the CSO handle, and a red probe that collapses the handle to a constant turns 4 of 59 render-state-sensitive scenarios red. ## 3. Verification — every command and its actual result All from `~/w7/p2-magma` at `1e89fd90` unless stated. | # | command | result | |---|---|---| | V1 | `cmake --build build-linux -j 12` | rc 0 | | V2 | `cmake --build build-push -j 12` | rc 0 | | V3 | `cmake --build build-nolegacy -j 12` | rc 0 | | V4 | `python3 scripts/symbol_report.py --before ~/w7/p2-before-libMobileGL.so --after build-linux/libMobileGL.so --threshold 0` | `27799 -> 27799 defined symbols: 0 added, 0 removed, 4 resized, 0 renamed`; the four are the **contract's** (`RenderState::RenderState()` +148, `SetCapability` +77, `IsCapabilityEnabled` +29, `_GLOBAL__sub_I_DirectGLES.cpp` −9). **This package adds none.** (G1) | | V5 | `grep -rc 'pGLContext' MobileGL/MG_Backend \| grep -v ':0$'` | empty (G13) | | V6 | `python3 scripts/check_include_closure.py` | `4 probes, 0 skipped, 0 problem(s)` (G13) | | V7 | `python3 scripts/gen_pipe.py --check` / `--self-test` | rc 0 / rc 0 (G13) | | V8 | stdio grep over `MG_Backend/DirectVulkan` + `MG_State/.../VertexArrayState` | none (G13) | | V9 | `awk '/namespace RenderStateImpl {/,/} \/\/ namespace RenderStateImpl/' DirectGLES.cpp \| sha256sum` | `d8fd1c48…0efe27`, equal to `~/w7/p2-before-syncrenderstate.sha` (G5) | | V10 | `ctest -N` names, corrected grep (§6.5), `build-{linux,push,nolegacy}` | 2367 / 2367 / 2367, **all three identical**; vs the baseline: **0 removed**, 4 added and all four the contract's `MG_Test/Pipe` placeholders (G2 name half, G14) | | V11 | `ctest -L unit` in `build-linux` / `build-push` / `build-nolegacy` | **1489/1489** in each | | V12 | `ctest -L integration-gpu -j 4 -R DirectVulkan`, `build-push`, default mask | **432/432** | | V13 | same, `MOBILEGL_PIPE_PUSH=0` | **432/432** (one flake on the first run, see §7; clean on rerun) | | V14 | same, `MOBILEGL_PIPE_PUSH=0x60 MOBILEGL_PIPE_LEGACY_MEMOS=0` — **the review's MAJOR 1 repro** | **432/432** (was 9/9 aborted) | | V15 | same, `MOBILEGL_PIPE_LEGACY_MEMOS=0` at the default mask | **432/432** | | V16 | same, `build-nolegacy` | **432/432** (was 180/432, every failure a per-draw Fatal) | | V17 | same, `build-linux` (pull) | **432/432** (one flake on the first run; clean on rerun) | | V18 | `retrace_gate.py --tree ~/w7/p2-magma --lib build-push/libMobileGL.so --out ~/w7/retrace-out/p2-magma-r2f -j 4 --only 'DirectVulkan$'` | **40/40 PASS**, min SSIM **0.993475** (`minecraft-1.21.11-main-menu`), gate 0.99 (G3, DirectVulkan half) | | V19 | `grep -rn "MGPipeSlots()" MobileGL … \| grep -v MG_Impl/Pipe/SlotAllocator` | one comment line, **no code** (MAJOR 3) | | V20 | `grep -rn "include