Skip to content

borealis: refit M5 acceleration structures without re-planning them - #1419

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/borealis-m5-refit-retained-plan
Nov 30, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/borealis-m5-refit-retained-plan

Conversation

@DjDeveloperr

Copy link
Copy Markdown

What: M5 acceleration-structure updates stop re-planning every structure. A build that allows updates now keeps its plan (plan state, envelope job, each IR node's parent, node jobs) after its retained IR topology. An update regenerates the IR leaves, refits the IR internal nodes from the saved parents, and re-encodes the saved jobs into the same slots. The second commit runs each of those steps once for all updates of a build call, so a call is about seven dispatches however many structures it updates. Before, every update re-ran the whole native encode (five planner phases, a parent-linking pass, node and envelope jobs), one structure after another: GTA V's ~200 bottom-level updates a frame came to ~7000 dispatches and ~5100 barriers. GTA V Enhanced at Very High ray tracing goes from 20.9 to 40.7 FPS on the M5 Pro.

Tested: J714s (M5 Pro, G17S), stock 7.3.0-m5-pr67, SLC on, built natively on the M5 from this head's tree:

Risk: two-way · M5 family only (the M5 native acceleration-structure builder: G17G, G17S, G17C). The GEN1 (G17P) shaders compile to byte-identical SPIR-V before and after, and no GEN1 C code changes. Updatable M5 structures grow by 68 bytes per IR node.

Not yet: a base M5 (G17G) row. A clone or serialization of an updatable structure copied to a different offset from a 1 KiB boundary keeps its retained data at the old relative offset; that was already true of the retained IR.

Details

Retained layout of an updatable structure, after the image: the IR header and internal nodes (as before), then the plan at hk_m5_plan_offset(): the hk_agx3_plan_state, the hk_agx3_envelope_job with its bounds as an offset into the IR, one uint32_t IR parent per IR node (from the plan nodes, so ~0 for the root and for unreferenced leaves), then the node jobs. Their IR and image addresses are stale; the update rewrites them.

Refit phases (agx3_m5_legacy_refit.comp):

  • build: SAVE_IR, then after planning SAVE_PLAN (beside the node jobs, which only read the plan);
  • update: PREPARE (copy the topology into scratch, reset the header bounds, active count and arrival counters) → IR leaves → REFIT (bottom-up refit from the saved parents, restoring the plan into encode scratch) → PREFIX (one thread: job offsets and the node encoding's indirect dispatch) and top-level envelopes → node jobs (and SAVE_IR + COPY_PLAN for an update into another structure) → bottom-level envelopes. This keeps a build's order: a bottom-level structure's nodes before its envelope; a top-level structure's envelope, which places the tables its instance records name, before its nodes.

Batched dispatches find their update by a binary search over per-update workgroup (or job) offsets; HK_M5_REFIT_BATCHED and HK_M5_LEAF_BATCHED build the existing M5 refit and node-encoder sources with that front end.

Updates keep their structure's slot layout, so a refit writes the same records the build wrote (a re-plan assigns slots in atomic order, so it rewrote the whole image differently every update).

Evidence

One update call, GPU time (rqbatch2 --k 207 --time 20: 207 GTA-shaped BLAS, 319102 triangles, updated in place in one vkCmdBuildAccelerationStructuresKHR, timestamps around the call, mean of 20 after 2 warm-ups, default software traversal builds):

Build ms per call
aurora-wip a532ab05e5b with #1404 and #1405 19.51 (19.47 to 19.55)
#1404 + first commit (no re-planning) 11.44 (11.39 to 11.48)
#1404 + both commits 4.60 (4.53 to 4.62)
this head 82c07f762bd 4.59 (4.54 to 4.62)

Where the remaining 4.6 ms goes (private timing build that skips single steps): node encoding about 3.8 ms, prepare 0.47 ms, refit 0.35 ms, IR leaves 0.15 ms, envelopes under 0.02 ms. The node encoder (one job per 8-lane workgroup, with lane 0 fitting every child) is the next lever; it is shared with builds, so it is left for its own change.

GTA V Enhanced (built-in benchmark, 2560x1440, Very High ray tracing, FSR Quality, J714s stock 7.3.0-m5-pr67, SLC on):

Run Build FPS Passes
s2 1 with 41.04 31.6 45.0 40.1 51.9 36.6
s2 2 without 20.76 15.7 22.1 15.9 29.7 20.3
s2 3 without 20.84 16.2 22.2 15.6 29.7 20.5
s2 4 with 40.61 30.5 44.9 40.6 50.8 36.2
s3 1 without 20.97 16.1 22.0 15.7 30.8 20.3
s3 2 with 40.43 31.2 45.0 40.7 48.8 36.4

Every run: 0 GPU faults, 0 s at the 338 MHz clamp, same boot. Screenshot-layer captures of the benchmark with these commits (Vinewood sign, Sandy Shores, the pier at night) show the same geometry and ray-traced shadows as captures without them, and no stray or missing triangles; the headless capture path desaturates frames and adds a top-left inset with and without these commits, and with ray tracing off.

An M5 update re-ran the whole native encode for every structure: the
five planner phases, the node and envelope jobs, and a parent-linking
pass, each behind its own barrier, one structure after another. GTA V
updates about 200 bottom-level structures a frame, which came to about
7000 dispatches and 5100 barriers.

A build that allows updates now also keeps its plan after the retained
IR topology: the plan state, the envelope job, each IR node's parent and
the node jobs. An update regenerates the IR leaves, refits the IR
internal nodes bottom up from the saved parents, restores the saved jobs
with its own IR and image addresses, and re-encodes them into the same
slots, without planning. Every step runs for all updates of a build call
before the next, so a call waits at a handful of barriers instead of
several per structure. An update into another structure also copies the
topology and the plan there.

Updatable structures grow by 68 bytes per IR node for the saved plan.

Signed-off-by: DjDeveloperr <hey@djdev.me>
The refit still dispatched its preparation, its refit and its node and
envelope jobs once per structure, and every M5 internal dispatch waits
for the one before it. With about 200 bottom-level updates a frame in
GTA V that is about 800 dispatches.

Run each step of all the updates of a build call in one dispatch: a
workgroup finds its update by a binary search over per-update workgroup
offsets. After the refit, a single-thread pass sums the restored jobs of
every update into job offsets and the node encoding's indirect dispatch,
and the envelope jobs land in one array, so a call dispatches seven
times however many structures it updates.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 30, 2026 16:35

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at 82c07f76

Blockers:

  • Pending base M5 (G17G) hardware row listed under Not yet before merge (change covers the M5 family: G17G, G17S, G17C).

Checked:

  • src/asahi/vulkan/bvh/hk_build_interface.h, src/asahi/vulkan/bvh/meson.build: verified retained plan layout (HK_M5_PLAN_STATE 0, HK_M5_PLAN_ENVELOPE 32, HK_M5_PLAN_PARENTS 80, 8-byte aligned HK_M5_PLAN_JOBS, 8-byte multiple HK_M5_PLAN_BYTES), push-constant struct ABIs (hk_m5_refit_args 120 B, hk_m5_leaf_task 16 B, hk_agx3_envelope_job 48 B), and GEN1 shaders untouched.
  • src/asahi/vulkan/bvh/agx3_m5_legacy_refit.comp, agx3_m5_legacy_leaf.comp: verified SAVE_IR/PREPARE/SAVE_PLAN/REFIT/COPY_PLAN/PREFIX phases, envelope.bounds IR-relative offset round-trip (always non-null per agx3_m5_legacy_plan.comp), device-scope acquire-release atomicAdd arrival synchronization, and upper-bound binary search over groups/bases skipping zero-length runs cleanly.
  • src/asahi/vulkan/hk_m5_acceleration_structure.c: verified hk_m5_plan_offset() / hk_get_as_size() sizing, empty-AS pre-filtering in hk_m5_backend_CmdBuildAccelerationStructuresKHR() (guaranteeing leaf_node_count >= 1 / node_bound >= 2), total <= UINT32_MAX / 64 guard in hk_m5_refit_batched(), barrier ordering across PREPARE -> hk_build_leaves_batched() -> REFIT -> PREFIX + TLAS envelopes -> SAVE_IR/COPY_PLAN (src != dst) + indirect batched node encode -> BLAS envelopes, and OOM cleanup paths.
  • Exact-head J714s (M5 Pro, G17S) testing at 82c07f762bd: 11,667 CTS cases (7,249 pass, 0 fail), 262,144-ray refit-vs-rebuild equivalence (including 207-BLAS batch, out-of-place src != dst, and TLAS+BLAS), and GTA V Enhanced paired benchmark (20.86 -> 40.69 FPS, +95%, 0 GPU faults).
Evidence
  • hk_m5_updates() partitions updates bottom-level first (0..bottom-1) and top-level second (bottom..count-1), so hk_m5_envelopes() runs TLAS envelopes before batched node encoding and BLAS envelopes after, preserving build order without extra per-structure dispatches.
  • Non-blocking note: hk_update_batched() runs SAVE_IR + COPY_PLAN whenever src != dst without re-checking VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR (valid under Vulkan VUID-vkCmdBuildAccelerationStructuresKHR-pInfos-03668 since update flags must match src's build flags).

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 82c07f762bd4

Blockers: none.

Checked:

  • Updates. A build that allows updates saves its IR topology, then its plan after it: the plan state, the envelope job with its bounds stored as an IR offset, one parent per IR node taken from the planner, and the node jobs. An update copies the topology back from the source, regenerates the leaves, refits bottom up from the saved parents, and re-encodes the saved jobs with its own IR and image addresses and the destination's ID. In-place updates and updates into another structure both read only the source and write only the destination. An update into another structure also gives the destination the topology and the plan. The plan depends only on the topology and on geometry opacity, and an update must keep both. Updatable builds also keep every primitive active. So the saved parents and slots stay valid for every update.
  • Batching. The leaf pass runs once per flag set. It is the same M5 leaf op that the runtime's per-update leaf pass already used, and indirect builds are not exposed on M5. Then come the refit, then the prefix and the top-level envelopes, then all node jobs from one indirect dispatch, then the bottom-level envelopes. There is a barrier after each step, and the one before the node dispatch includes the indirect read. This keeps a build's order: a top-level envelope writes the table offsets before its instance records read them, and bottom-level nodes come before their envelope. A top-level structure can't reference a bottom-level structure that the same call updates, so ordering between dependent updates comes from the application's barrier between calls, as before. The binary searches skip updates with no jobs. The envelope status words are only written, so the uninitialized pool memory is fine. The batched node encoder only reads the jobs, the status and the job count, so the push-constant fields it leaves unset are never read.
  • Sizes. The plan sits after the retained topology (hk_m5_plan_offset(), plus 68 bytes per IR node). The size query, the build and the update all compute its offset from the same leaf count. Update scratch is unchanged from #1404: only the arrival counters live past the scratch layout, and the restored jobs land in the encode scratch after the leaf pass has finished with it.
  • M5 only. Every changed path runs behind the M5 structure format (agx_chip_g17_m5()), and the GEN1 C code is untouched. I compiled every BVH shader at 89ddabbf and at this head with the build's glslang flags. All nine GEN1 variants and the shared encode, copy and write shaders are byte-identical; only the M5 refit changes, and the two new batched variants compile. No G17P row is owed. The base M5 (G17G) row goes through the same code path and can follow the merge.
  • Hardware at this head. On the J714s, a build of 82c07f762bd passes refit-versus-rebuild in all five cases: 207 updates in one call, an update into another structure, a top-level update, and 64 updates into other structures plus a top-level update in one command buffer. Vulkan CTS ray query gives 7249 pass, 0 fail and 4418 not supported, and one 207-structure update call takes 4.59 ms. The GTA V A/B ran these commits before the rebase onto 89ddabbf.
  • CI. The asahi CI configuration (clang, Gallium and Vulkan asahi, Wayland and X11, tests) on x86 at this head: it builds cleanly and all 367 asahi and poly tests pass.
Note on the copy line under Not yet (not a blocker)

A clone, compaction or serialization copies only the native image, sized by the size word in its header. Neither the retained topology nor the saved plan is copied at all, at any offset. Updating from a copied structure already read uninitialized topology before this change. Now that an update trusts the saved slots and parents without the planner's checks, that case would write out of bounds instead of failing, so it is worth closing with the copy alignment follow-up: either copy the retained region or size compaction to keep it.

@DjDeveloperr
DjDeveloperr merged commit 717a7d6 into aurora-wip Nov 30, 2026
@DjDeveloperr
DjDeveloperr deleted the perf/borealis-m5-refit-retained-plan branch November 30, 2026 17:01
@sithglan

Copy link
Copy Markdown

Post-merge base M5 (J813, Apple M5 G17G C0) same-boot userspace Vulkan ICD A/B confirmation (717a7d62e004 vs parent 89ddabbf72c2, kernel 7.3.0-aurora-j813-sgv2+, deqp-runner --jobs 2, regression request 50f51e95): PASS

  • Base 89ddabbf72c2 (libvulkan_asahi.so sha256 49755a5f...):
    • dEQP-VK.ray_query.* (49,360): 14,536 Pass / 0 Fail / 0 Crash / 34,824 Skip (*update* subset: 6,220 Pass / 7,760 Skip) — 0 GPU faults/recoveries/timeouts.
    • dEQP-VK.ray_tracing_pipeline.acceleration_structures.* (10,279): 10 Pass / 0 Fail / 0 Crash / 10,269 Skip — 0 GPU faults/recoveries/timeouts.
  • Head 717a7d62e004 (libvulkan_asahi.so sha256 56334b2a...):
    • dEQP-VK.ray_query.* (49,360): 14,536 Pass / 0 Fail / 0 Crash / 34,824 Skip (*update* subset: 6,220 Pass / 7,760 Skip) — 0 GPU faults/recoveries/timeouts.
    • dEQP-VK.ray_tracing_pipeline.acceleration_structures.* (10,279): 10 Pass / 0 Fail / 0 Crash / 10,269 Skip — 0 GPU faults/recoveries/timeouts.
  • Transitions: 0 across all 59,639 cases (0 Pass -> Fail/Crash/Timeout), 0 GPU faults/recoveries/timeouts in dmesg.

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.

2 participants