Repository navigation
borealis: refit M5 acceleration structures without re-planning them - #1419
Conversation
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>
There was a problem hiding this comment.
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_STATE0,HK_M5_PLAN_ENVELOPE32,HK_M5_PLAN_PARENTS80, 8-byte alignedHK_M5_PLAN_JOBS, 8-byte multipleHK_M5_PLAN_BYTES), push-constant struct ABIs (hk_m5_refit_args120 B,hk_m5_leaf_task16 B,hk_agx3_envelope_job48 B), and GEN1 shaders untouched.src/asahi/vulkan/bvh/agx3_m5_legacy_refit.comp,agx3_m5_legacy_leaf.comp: verifiedSAVE_IR/PREPARE/SAVE_PLAN/REFIT/COPY_PLAN/PREFIXphases,envelope.boundsIR-relative offset round-trip (always non-null peragx3_m5_legacy_plan.comp), device-scope acquire-releaseatomicAddarrival synchronization, and upper-bound binary search overgroups/basesskipping zero-length runs cleanly.src/asahi/vulkan/hk_m5_acceleration_structure.c: verifiedhk_m5_plan_offset()/hk_get_as_size()sizing, empty-AS pre-filtering inhk_m5_backend_CmdBuildAccelerationStructuresKHR()(guaranteeingleaf_node_count >= 1/node_bound >= 2),total <= UINT32_MAX / 64guard inhk_m5_refit_batched(), barrier ordering acrossPREPARE->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-placesrc != 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), sohk_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()runsSAVE_IR+COPY_PLANwheneversrc != dstwithout re-checkingVK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR(valid under VulkanVUID-vkCmdBuildAccelerationStructuresKHR-pInfos-03668since updateflagsmust matchsrc's build flags).
DjDeveloperr
left a comment
There was a problem hiding this comment.
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 at89ddabbfand 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
82c07f762bdpasses 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 onto89ddabbf. - 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.
|
Post-merge base M5 (
|
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:
ray_query.acceleration_structures(traditional, GPU-built) plusray_query.{misc,builtin,watertightness,advanced}(first 630), 11667 cases at this head (82c07f762bd): 7249 pass, 0 fail, 4418 not supported, the same as aurora-wip with borealis: report the M5 refit records in updateScratchSize #1404 and borealis: report the M5 acceleration structure's buffer address #1405 before it.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(): thehk_agx3_plan_state, thehk_agx3_envelope_jobwith its bounds as an offset into the IR, oneuint32_tIR 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):Batched dispatches find their update by a binary search over per-update workgroup (or job) offsets;
HK_M5_REFIT_BATCHEDandHK_M5_LEAF_BATCHEDbuild 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 onevkCmdBuildAccelerationStructuresKHR, timestamps around the call, mean of 20 after 2 warm-ups, default software traversal builds):a532ab05e5bwith #1404 and #140582c07f762bdWhere 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):
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.