Skip to content

hk/agx3: generalize vertex root-bypass launches and trim draw hot path - #1222

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/borealis-root-bypass-and-draw-hotpath
Nov 27, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/borealis-root-bypass-and-draw-hotpath

Conversation

@DjDeveloperr

Copy link
Copy Markdown

Summary

Generalize the AGX3 direct-draw vertex root-bypass path so vertex shaders with vertex attributes, dynamic uniform buffer offsets, or vertex buffer base/clamp updates can launch over narrow pool slices or patched buffer addresses without uploading a full 1,112 B root descriptor table or invalidating the HAL200 bind tail whenever the fragment shader cannot observe the dirty root categories, and trim remaining per-draw CPU recording overhead on Borealis.

Key changes

  • Categorized root-change tracking & non_dynbuf_generation (hk_cmd_buffer.h, hk_cmd_buffer.c, hk_cmd_meta.c):
    • Track dirty root categories independently (HK_ROOT_DIRTY_PUSH, DYNBUF with dynbuf_dirty_mask, VB_BASE, VB_CLAMP, OTHER) and maintain non_dynbuf_generation so pure dynamic-offset binds do not force full-root invalidation or bind-tail rebuilds when the fragment shader does not read the dirty slots.
    • Tag VS draw.attrib_base and draw.attrib_clamps ranges separately (HK_G17P_RANGE_SOURCE_ATTRIB_BASE / ATTRIB_CLAMP) instead of HK_G17P_RANGE_SOURCE_ROOT, and recognize offset-0 ROOT ranges that only supply root.sets[] (!agx3_main_uses_root_uniform && !has_preamble && !g17p_prolog_uses_root).
  • Generalized bypass vertex launches (hk_cmd_buffer.c, hk_cmd_draw.c):
    • Pack dirty narrow slices (attrib_base, attrib_clamps, push) contiguously into a single hk_pool_alloc() (zero pool bytes when only DYNBUF changed) and cache a bypass loader shape on hk_g17p_launch when the split cluster shape differs from the initial full-root loader.
    • Update HAL200 and M5 fast paths (hk_g17p_flush_dynamic_state_fast, hk_flush_dynamic_state, hk_g17p_emit_native_bind_tail) to elide the bind tail across bypassed push, dynamic-offset, and VB base/clamp updates.
  • Draw hot-path trims & indirect-multi hoisted-preamble fix (hk_cmd_buffer.h, hk_cmd_buffer.c, hk_cmd_draw.c, hk_sync.c, agx_tilebuffer.c):
    • Inline the hot Ioto arena bump path in hk_g17p_gfx_loader_emit() and the active graphics stream fast path in hk_cmd_buffer_get_cs().
    • Defer hk_gfx_republishes_render_state() behind the 65k PPP index-limit guard; guard inactive shader/passthrough-GS/index-robustness/pipeline-stat/attachmentless/G17P-resource checks; inline adjacency topology range check; short-circuit empty sync footprints and already-compact tilebuffer layouts.
    • Fix hk_agx3_multi_draw_ioto_prepare() and hk_agx3_emit_indirect_multi() to account for hoisted VS preambles (flags | 32, hk_hw_vs_vdm_state_update(hw_vs, hoisted_preamble)).
  • Unit tests (test-hk-root-fragment.c, hk_cpu_upload_bench.c):
    • Added tests covering categorized root-bypass dirty transitions, hk_root_reads_ignore_dirty(), and non_dynbuf_generation stability vs invalidation.

Verification (MacBook Neo J700, A18 Pro / G17P B1, kernel 7.3.0-41)

Exact-head ABBA+BAAB (6041c9351ef vs d16e86fce34, rebased cleanly onto origin/aurora-wip 29bdd670922 -> 7fb9b541b729):

Root uploads (ASAHI_MESA_DEBUG=perf, 1200 draws)

  • dx11_draws bind=push: 1200 (1,334,400 B) -> 2 (2,224 B, -99.8%)
  • dx11_draws bind=dynoff: 1200 (1,334,400 B) -> 20 (22,240 B, -98.3%)
  • dx11_draws bind=dynoff_vb: 1200 (1,334,400 B) -> 20 (22,240 B, -98.3%)

ABBA+BAAB Performance (us_per_submit & record_cpu_us, 0 CRC mismatches)

Workload Base (us) Cand (us) Speedup record_cpu_us B<A pairs
render.dx11_draws push d1 486.62 358.22 1.358x 202.3 -> 115.6 (1.75x) 4/4
render.dx11_draws push d2 282.22 178.66 1.580x 269.2 -> 141.9 (1.90x) 4/4
render.dx11_draws dynoff d1 484.65 360.58 1.344x 207.1 -> 120.1 (1.72x) 4/4
render.dx11_draws dynoff d2 292.11 186.17 1.569x 275.1 -> 155.4 (1.77x) 4/4
render.dx11_draws dynoff_vb d1 482.70 379.99 1.270x 221.4 -> 133.9 (1.65x) 4/4
render.dx11_draws dynoff_vb d2 316.79 202.92 1.561x 296.3 -> 182.3 (1.63x) 4/4
render.dx11_draws progs=4 color d2 302.76 195.14 1.551x 283.1 -> 169.5 (1.67x) 4/4
render.dx11_draws progs=4 shadow d2 296.46 200.51 1.479x 269.4 -> 151.7 (1.78x) 4/4
render.dx11_draws progs=4 shadow_atlas d2 289.30 202.51 1.429x 269.1 -> 151.3 (1.78x) 4/4
render.bindless_ubo ubo_vec4 d2 306.46 294.15 1.042x 265.6 -> 236.6 (1.12x) 4/4
render.bindless_ubo desc_buf d2 310.08 291.86 1.062x 274.5 -> 250.6 (1.10x) 3/4
Controls (heavy fill, deferred, postfx, cull_mdi, mdi, heavy geom, light) flat flat 0.996x–1.010x — no regressions

Correctness & CI

  • Fedora Asahi CI (fedora-mesaci.sh 7fb9b541b729): 217/0 (Ok 217, Fail 0, Timeout 0, Skipped 0).
  • ASAHI_MESA_DEBUG=verify on J700: 0 errors across all 10 draw/geometry/TVB workloads.
  • Targeted VK CTS on J700: 9,329 passed, 0 failed, 0 warnings (dEQP-VK.draw.renderpass.{linear_interpolation.no_offset_*,basic_draw.draw.*,indirect_draw.indexed.indirect_draw_count.*}, dEQP-VK.draw.dynamic_rendering.primary_cmd_buff.basic_draw.*, dEQP-VK.api.command_buffers.record_many_draws_primary_2, dEQP-VK.pipeline.monolithic.vertex_input.*).
  • Health: 0 kernel faults, 0 AVC denials, stock.sh PRE==POST.

Generalize the AGX3 direct-draw vertex root-bypass path so vertex
shaders with vertex attributes, dynamic uniform buffer offsets, or
vertex buffer base/clamp updates can launch over narrow pool slices or
patched buffer addresses without uploading a full root descriptor table
or invalidating the HAL200 bind tail whenever the fragment shader cannot
observe the dirty root categories.

- Track root mutations by category (push constants, dynamic buffer
  slot mask, VB attrib bases/clamps, other) plus a non-dynbuf
  descriptor generation in hk_descriptor_state, and classify VS
  attrib_base/attrib_clamps ranges independently of general root reads.
- Allow hk_agx3_try_push_only_vs_program() to pack dirty narrow slices
  contiguously and cache a bypass loader shape on hk_g17p_launch when
  the split ranges differ from the full-root cluster plan.
- Inline the hot Ioto arena bump path in hk_g17p_gfx_loader_emit(),
  fast-path hk_cmd_buffer_get_cs() for active graphics streams, defer
  hk_gfx_republishes_render_state() behind the 65k PPP index-limit
  guard, skip redundant per-draw shader/stats/attachmentless/resource
  work when inactive, and short-circuit empty sync footprints and
  already-compact tilebuffer layouts.
- Fix hk_agx3_multi_draw_ioto_prepare() and
  hk_agx3_emit_indirect_multi() to account for hoisted VS preambles.
- Add unit tests covering generalized root-bypass dirty tracking and
  non_dynbuf_generation behavior.

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

Copy link
Copy Markdown
Author

Verdict at 7fb9b541b729: ready

Blockers

None.

Checked

  • Root dirty categories.
    • hk_bind_descriptor_sets() marks dynamic-offset-only changes with hk_root_mark_dynbuf_dirty() for the changed slots. Any change to set addresses, set starts or the dynamic buffer end, a binding change, an updated set or a root that can't be kept still takes hk_root_mark_dirty() and bumps non_dynbuf_generation.
    • The vertex-buffer update marks bases and clamps narrowly only when no stride changed; a stride change still dirties the whole root.
    • Descriptor buffers, embedded samplers and push descriptors keep the full mark.
  • When the bypass is taken.
    • hk_root_reads_ignore_dirty() refuses whenever OTHER is dirty, the shader reads the root address, or it reads a dirty category: push constants, vertex input for VB, or the general dynamic-buffer flag or a dirty slot for dynbuf. It is checked against the fragment shader before the vertex launch may bypass.
    • A bypassed launch leaves the root dirty (hk_root_mark_bypass_launched()), so every later path that reads the uploaded copy uploads first, as with the old push-only launch.
    • A root range at offset 0 counts as general unless the shader info proves only root.sets[] is read, and none of the bypassable categories touch those.
  • Other changes.
    • The hoisted-preamble flag (32) is now part of the multi-draw launch match in hk_agx3_multi_draw_ioto_prepare() and hk_agx3_emit_indirect_multi().
    • The inlined Ioto bump in hk_g17p_gfx_loader_emit() keeps the bounds and alignment checks and falls back to the slow reservation.
    • The hk_sync_pass_footprints_done() early return yields the same all-zero wait.
    • agx_tilebuffer_compact()'s fast path returns the same layout for a layout whose targets are already contiguous from RT0. Unlike the slow path, it keeps whatever spilled/_offset_B the unused tail slots hold, which matters only if a layout ever carries data there.
  • Merge and build.
  • Mutations.
    • Ignoring dirty dynamic-buffer reads, ignoring dirty vertex-input reads, and treating OTHER as bypassable each fail hk_root_fragment_test.
    • Dropping hk_root_mark_dynbuf_dirty() from hk_bind_descriptor_sets() is not caught: no unit test binds new dynamic offsets and then checks the dirty state.
  • Policy. agent-policy.sh pr HEAD: PR is clean. Signed off, no attribution lines, no new environment options. The subject uses hk/agx3: where Borealis commits use borealis:.

Merge order with #1144. This conflicts with #1144 in hk_sync_pass_footprints_done(): both edit its opening lines. Whichever lands second should keep #1144's frag_ok setup and make the new early return also clear st->pass_tile_wait_frag, so the fragment-only wait can't go stale.

Still owed before merge: the J773g (G16G) second-generation row, which has been requested. The J700 row and CTS (9,329/0) are in the body.

@DjDeveloperr

Copy link
Copy Markdown
Author

J773g (M4 Mac mini, G16G B0, HAL200, second generation) at 7fb9b541b729, against its parent 3edbd1c46211: PASS, no regression. The DX11-style draw stream is 1.10-1.39× faster on G16G; bindless_ubo is about 4 % faster at 2048 draws.

Both arms are release Borealis Vulkan overlays built natively on the Mac and loaded from private prefixes through VK_DRIVER_FILES, on the stock kernel 7.3.0-ARCH-m4mainefed1ae3. System Mesa wasn't touched. Each arm's vulkaninfo shows its git SHA.

gpubench-vk ABBA (us_per_submit, head/base; lower is better; CRCs equal on all four runs of every row). render.dx11_draws comes from gpubench PR #32 at 226bc71baf35, built natively on the Mac as a separate install, because it isn't in gpubench main yet. The other rows come from the usual suite build.

row A1 / B1 / B2 / A2 head/base
render.dx11_draws bind=dynoff draws=1200 progs=1 res=native target=color, d1 538.60 / 438.60 / 437.32 / 541.52 0.811
render.dx11_draws bind=dynoff_vb draws=1200 progs=1 res=native target=color, d1 557.62 / 449.78 / 450.45 / 555.56 0.809
render.dx11_draws bind=dynoff_vb draws=1200 progs=4 target=shadow, d1 403.23 / 344.04 / 346.42 / 405.95 0.853
render.dx11_draws bind=dynoff_vb draws=1200 progs=4 target=shadow_atlas, d1 402.68 / 344.83 / 344.04 / 403.77 0.854
render.dx11_draws bind=push draws=1200 progs=1 res=native target=color, d1 529.10 / 427.35 / 423.13 / 525.39 0.807
render.dx11_draws bind=dynoff draws=1200 progs=1 res=native target=color, d2 318.81 / 229.18 / 230.06 / 318.13 0.721
render.dx11_draws bind=dynoff_vb draws=1200 progs=1 res=native target=color, d2 325.38 / 237.15 / 242.52 / 322.93 0.740
render.dx11_draws bind=dynoff_vb draws=1200 progs=4 target=shadow, d2 219.62 / 199.34 / 194.05 / 216.14 0.903
render.dx11_draws bind=dynoff_vb draws=1200 progs=4 target=shadow_atlas, d2 222.06 / 205.20 / 198.81 / 223.21 0.907
render.dx11_draws bind=push draws=1200 progs=1 res=native target=color, d2 294.70 / 210.82 / 212.92 / 287.36 0.728
render.bindless_ubo draws=2048 mode=desc_buf, d1 423.73 / 410.40 / 413.22 / 431.65 0.963
render.bindless_ubo draws=2048 mode=ubo_vec4, d1 423.13 / 409.28 / 410.40 / 427.35 0.964
render.bindless_ubo draws=512 mode=ubo_vec4, d1 287.63 / 274.22 / 274.47 / 286.53 0.956
render.bindless_ubo draws=2048 mode=desc_buf, d2 236.97 / 227.62 / 226.42 / 237.91 0.956
render.bindless_ubo draws=2048 mode=ubo_vec4, d2 237.91 / 227.79 / 225.73 / 236.97 0.955
render.bindless_ubo draws=512 mode=ubo_vec4, d2 164.29 / 164.93 / 164.65 / 164.56 1.002
render.mdi , d1 364.96 / 359.28 / 358.42 / 358.00 0.993
render.mdi , d4 160.77 / 159.32 / 158.65 / 159.91 0.992
render.light , d16 57.66 / 57.87 / 58.55 / 58.15 1.005

VK CTS at 1 worker, 67,446 cases: dEQP-VK.draw.* (29,547) and every pipeline.* case with vertex_input in its name (37,971). Both arms give 56,204 Pass and 11,242 NotSupported, with 0 cases that differ.

GPU health: 0 faults, 0 recoveries and failed 0 errno 0 degraded 0 at start and end of both parts.

…d-draw-hotpath

Resolve hk_sync_pass_footprints_done() conflict against #1144 by keeping
#1144's frag_ok setup and clearing st->pass_tile_wait_frag = NULL in the
empty sync_pass_records early return.

Signed-off-by: DjDeveloperr <hey@djdev.me>

@RyanTheTide RyanTheTide 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: FINISH, then merge at 07910f2fdb96ffccdf243537d874caa1a0f541dc (code approved. The remaining item is a hardware row at this head, see below.)

Blockers: none in the code.

Merge-policy item, not a code finding: both hardware rows (J700 in the body, J773g in the comment) are at 7fb9b541. The head is now the merge 07910f2f. It brings aurora-wip 5ab97d0f into files this PR touches (hk_sync.c +399, hk_cmd_draw.c +235, hk_cmd_buffer.{c,h}) and hand-resolves a conflict in hk_sync_pass_footprints_done(). That is not the clean-rebase exception, so a row at 07910f2f is still owed. A J700 or J773g rerun of the dx11_draws plus draw CTS subset is enough.

Checked:

  • Dirty tracking.
    • Pure dynamic-offset binds mark only the changed slots (hk_root_mark_dynbuf_dirty()).
    • Set address, set start, buffer end, binding changes, set updates, descriptor buffers, embedded samplers, push descriptors and hk_meta_end() all take hk_root_mark_dirty() and bump non_dynbuf_generation.
    • In VB updates, a stride change takes the full mark, and base or clamp alone take the narrow one.
    • A bypassed launch leaves the root dirty (bypass_launched), so every full-root reader still uploads.
  • Vertex side of the bypass. Ranges re-derive from current state: SET from desc->root.sets[], BUFFER through hk_g17p_dma_buffer() (current dynamic-buffer descriptors, so a dynamic-offset change reaches a VS reading through a DMA span), ATTRIB/PUSH from fresh narrow slices. Only ROOT ranges read the stale gfx->root, and those either pass hk_root_reads_ignore_dirty() or are the offset-0 header range from hk_agx3_g17p_root_range(), which covers root_desc_addr + sets[set_count] only.
    • Non-blocking hardening: hk_g17p_launch_record() exempts an offset-0 ROOT range by offset alone. A root-relative DMA span that starts at 0 is also tagged ROOT. Bounding the exemption to size <= hk_root_descriptor_offset(draw) would keep a future longer span at offset 0 from slipping through.
  • Fragment side and the bind tail. Elision needs hk_root_bypass_dirty() plus hk_root_reads_ignore_dirty() against the FS, the same non_dynbuf_generation, and for VB-generation skew an FS that reads neither the root address nor vertex input. The debug-build cross-check hk_g17p_prepared_bind_tail_matches_impl() follows the same rules.
  • Pipeline switches, indirect draws and secondaries.
    • hk_g17p_launch_matches() still gates on shader, linked object and flags.
    • Indirect draws, a VS with a preamble, a hoisted preamble and root growth all fall back to a full upload.
    • Secondaries start from fresh descriptor state.
    • The multi-draw paths now include the hoisted-preamble flag (32) and hk_hw_vs_vdm_state_update(hw_vs, hoisted_preamble).
  • Hot-path trims, checked for equivalence. hk_handle_passthrough_gs() already returned unless a VS, GS or TCS/TES dirty bit was set. hk_flush_shaders() does nothing unless shaders_dirty or a dynamic dirty bit is set. hk_needs_index_robustness() returns false for non-indexed draws. hk_pipeline_stat_addr() returns the scratch page when pipeline_stats_flags is 0. The adjacency range check matches the four *_WITH_ADJACENCY enums, which are contiguous (6..9). The tilebuffer fast path returns the same occupied-slot layout (only unused tail metadata differs, as already noted).
  • Merge resolution. The 07910f2f early return clears both pass_tile_wait[] and pass_tile_wait_frag, which matches #1144's full path for an empty record list.
  • Tests fail without the fix. I removed the DYNBUF clause from hk_root_reads_ignore_dirty() on the merged tree, and hk_root_fragment fails. With it restored, it passes. Non-blocking: as already noted in the thread, dropping hk_root_mark_dynbuf_dirty() from hk_bind_descriptor_sets() is still not caught by any test.
  • NO-GATES. No new chip gates. The agx_chip_native_hal200 || agx_chip_g17_m5 and AGX_CHIP_G17P checks are pre-existing code that was moved or inlined.
  • Merge and local gate. The head merges cleanly onto aurora-wip a97571c6b2839. The drm-shim build of the merge (x86, asahi + poly) passes 338/338, the same as the tip.
Evidence
  • Merged tree fbdde42f = a97571c6 + 07910f2f: Ok 338, Fail 0.
  • Mutation (DYNBUF clause removed from hk_root_reads_ignore_dirty): asahi - mesa:hk_root_fragment FAIL exit status 1. Restored: Ok 1.
  • git show --cc 07910f2f: the only combined hunk is hk_sync.c (st->pass_tile_wait_frag = NULL; in the early return). git diff --stat 7fb9b541^ 5ab97d0f over the PR's files: 5 files, +590/-119.

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