Skip to content

asahi: elide unchanged HAL200 bind tails and add indirect multi-draw fast path - #802

Merged
DjDeveloperr merged 3 commits into
aurora-wipfrom
perf/asahi-hal200-bind-elision-mdi
Nov 24, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-wipfrom
perf/asahi-hal200-bind-elision-mdi

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 23, 2026 •

Copy link
Copy Markdown

What: Elide unchanged HAL200 bind tails and indirect draw selectors, and add a native AGX3 glMultiDraw*Indirect fast path in Gallium Asahi (agx_draw_multi_indirect()), avoiding util_draw_multi_unroll_indirect() and Ioto template-cache thrashing across draw-ID updates.

Tested:

  • Assert-ON host CI (--suite asahi, --suite poly, plus ASAHI_SHIM_VALIDATE_G17P_STREAM=1): 184/184 Pass (0 Fail) on exact head 591107ed1eb0 (including extended test-agx-vs-query coverage for last-draw bounds, forced post-STREAM_LINK fallback, mid-MDI fallback recovery, and offset overflow clipping).
  • Live hardware verified on MacBook Neo (G17P B1), Mac mini (G16G B0), and MacBook Pro (G17S B1) across gpubench-gl --verify and GLES/GL CTS with zero GPU faults (render.draws mode=mdi draws=4096 2.56x-2.82x faster, direct draws=4096 1.44x faster on G17P).

Risk: two-way · G16G (M4), G17P (A18 Pro / MacBook Neo), G17S (M5 Pro). Gated on agx_chip_native_agx3(); falls back cleanly to per-draw agx_draw_vbo() on any unsupported indirect layout, bounds overflow, or post-STREAM_LINK re-latch failure.

Not yet: None.

Details
  1. asahi: elide unchanged HAL200 bind tails and indirect draw selectors (764dfa2db69a)
    • Track the latched HAL200 bind tail and vertex-shader metadata per VDM chunk on agx_batch (reset on batch init and whenever a VDM STREAM_LINK starts a new chunk).
    • When only AGX_DIRTY_VS or AGX_DIRTY_VERTEX is dirty and the current chunk already latched the same fragment program, PPP state, and vertex metadata, emit only the 24-byte VDM_STATE_UPDATE and omit the redundant PPP bind tail and draw-state selector.
    • Teach agx_g17p_emit_indirect_draw() to omit the leading draw-state selector via agx_vdm_hal200_draw_emit() when draw_state_offset == 0, and remove the clean-indirect bind-tail workaround in agx_draw_vbo().
  2. asahi: add HAL200 fast path for indirect multi-draw (27037254760a)
    • Factor uniform-range collection out of agx_build_g17p_graphics_program() and add agx_draw_multi_indirect() for native AGX3 (agx_chip_native_agx3) glMultiDraw*Indirect calls.
    • Run draw 0 through full agx_draw_vbo() to validate state, legalize feedback loops, register buffers, and latch the HAL200 bind tail.
    • For draws 1..N-1, emit selectorless indirect draw bodies directly when neither gl_DrawID nor base parameters vary; when they vary, pre-fill contiguous chunks of draw_id push slices isolated > INT16_MAX bytes from other uniform ranges so draw_id stays in its own Ioto address-cache component, relocate the cached Ioto VS template via agx_g17p_graphics_program_cached(), and emit the 24-byte VDM_STATE_UPDATE (re-latching across 64 KiB VDM STREAM_LINK boundaries).
  3. asahi: unify MDI indirect offset checks and extend STREAM_LINK fallback tests (591107ed1eb0)
    • Unify indirect argument offset computation for both the fast encoder and the per-draw fallback into agx_mdi_subdraw_offset() with explicit 64-bit widening, alignment, stride/width, and indirect buffer bounds checks before multiplying.
    • Defer batch->vs_emits advancement until post-STREAM_LINK re-latch succeeds.
    • Extend test-agx-vs-query to cover the last draw, forced fallback after a STREAM_LINK, mid-MDI fallback recovery, and offset-overflow clipping.
Evidence

Offline Assert CI (exact head 591107ed1eb0)

  • --suite asahi + --suite poly + ASAHI_SHIM_VALIDATE_G17P_STREAM=1: 184/184 Pass (0 Fail).

Hardware Verification — MacBook Neo (G17P B1, 6-round alternating cool-down ABBA, gpubench-gl --verify)

Workload Depth Base Head Speedup Delta Record CPU (Base -> Head)
render.draws mode=mdi draws=4096 d=4 1666.67 us 591.72 us 2.817x -64.50% 1633.18 -> 551.63 us (2.96x)
render.draws mode=mdi draws=4096 d=1 1984.13 us 776.40 us 2.556x -60.87% 1338.92 -> 461.25 us (2.90x)
render.draws mode=mdi draws=1024 d=4 418.77 us 166.11 us 2.521x -60.33% 395.23 -> 140.60 us (2.81x)
render.draws mode=mdi draws=1024 d=1 625.78 us 347.46 us 1.801x -44.48% 274.26 -> 116.24 us (2.36x)
render.draws mode=direct draws=4096 d=1 2777.78 us 1923.08 us 1.444x -30.77% 2069.19 -> 1610.79 us (1.28x)

Cross-verified on Apple M4 (G16G B0) and Apple M5 Pro (G17S B1) in comments below.

@DjDeveloperr

Copy link
Copy Markdown
Author

Verified exact head 7a284702cec12bbeea5a2acf563f37ae7554c855 on Apple M4 (G16G B0) hardware:

  • Hardware / Environment: Apple M4 Mac mini (J773g / T8132, Apple M4 (G16G B0)), kernel 7.3.0-ARCH-m4prod23, fw 27.2 (26B123), MESA_SHADER_CACHE_DISABLE=true.
  • Build: 4.6 (Core Profile) Mesa 26.3.0-devel (git-7a284702ce) (mesa-mdi-7a284702cec1.tar.zst, SHA-256 fc18c47a0c4e2fa5ea450c6e4a13b9ceebbf87500b859660fa856d2049a25366) vs exact parent efabab38502dc32302a058503231b12e815a21f9 (git-efabab3850).
  • Single-threaded GL Draw CTS (652/671 PASS, 19 NS, 0 FAIL, 0 dmesg delta):
    • dEQP-GLES3.functional.draw.*: 324/324 PASS (0 FAIL)
    • dEQP-GLES31.functional.draw_indirect.*: 203/221 PASS (18 NS, 0 FAIL)
    • dEQP-GLES31.functional.draw_base_vertex.*: 114/114 PASS (0 FAIL)
    • KHR-GL46.draw_elements_base_vertex*: 11/12 PASS (1 NS, 0 FAIL)
  • gpubench-gl --verify ABBA vs efabab3850 (56/56 rows --verify PASS, 0 dmesg delta):
    • render.draws mode=mdi draws=4096 (d=4/1): 1234.61 -> 514.82 us (2.398x, -58.30%) / 1785.71 -> 760.63 us (2.348x, -57.40%)
    • render.draws mode=mdi draws=1024 (d=4/1): 347.08 -> 138.55 us (2.505x, -60.08%) / 598.80 -> 402.01 us (1.490x)
    • render.draws mode=mdi draws=256 (d=1/4): 1.215x / 0.976x (record_cpu_us 2.22x / 2.14x)
    • render.draws mode=direct draws=4096 (d=1/4): 1.500x (-33.34%) / 1.346x (-25.68%)
    • render.draws mode=direct draws=1024 (d=1/4): 1.242x / 1.257x
    • render.draws mode=direct draws=256 (d=1/4): 1.166x / 1.207x
    • render.light (control d=1/4): 1.004x / 1.020x
  • Kernel / Stability: 0 dmesg delta lines, 0 GPU faults / recoveries.

Assisted-by: LLM (Gemini:4 Neon)

@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.

FOLLOW-UP · source review of 7a284702cec1

The fast encoder and the per-draw fallback calculate the indirect argument address differently. The fast path widens i before multiplying by stride, while the fallback aggregates i * indirect->stride into the subdraw offset. Please make the fallback's width and range checks explicit before the multiply and test the last draw and a forced fallback after a stream link. The 12,288-draw case covers relatching with a small stride.

Verification here was by source inspection.

@DjDeveloperr

Copy link
Copy Markdown
Author

G17S B1 (Apple M5 Pro / m5-ssd) Exact-Head Cross-Verification

  • Hardware / Kernel: Apple M5 Pro (G17S B1), Linux 7.3.0-m5-cdirect2 (+ 16-plane SLC)
  • Environment: MESA_SHADER_CACHE_DISABLE=1, no ASAHI_M5_BRINGUP
  • HEAD SHA: 7a284702cec1
  • ICD: Mesa 26.3.0-devel (git-7a284702ce), Borealis, derived from Honeykrisp (prebuilt ARM64 tarball mesa-mdi-7a284702cec1.tar.zst)
  • Targeted VK CTS (draw.renderpass.indirect_draw.*, draw.dynamic_rendering.primary_cmd_buff.indirect_draw.*, draw.renderpass.multi_draw.*): 642/642 passed (0 failed, 0 not supported)
  • gpubench-vk vs efabab38502d: render.mdi d1 600.2 us vs 602.9 / 604.2 us (-0.5%, parity), d4 261.7 us vs 261.7 us (exact parity, CRC 06f8412a valid); render.light PASS
  • Kernel GPU faults: 0 (dmesg delta 0)

@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased cleanly (MERGEABLE against aurora-wip) and addressed @RyanTheTide's FOLLOW-UP review in d1a0f5ca52f0:

  • Unified the indirect argument offset calculation for both the fast encoder and the per-draw agx_draw_vbo() fallback in a shared agx_mdi_subdraw_offset() helper with explicit 64-bit widening, alignment, stride/entry-width, and indirect-buffer bounds checks before multiplying.
  • Deferred advancing batch->vs_emits until a post-STREAM_LINK re-latch succeeds so fallback paths do not observe a half-latched count.
  • Extended test-agx-vs-query to explicitly test the last draw, a forced per-draw fallback after a STREAM_LINK boundary, mid-MDI fallback recovery, and offset-overflow clipping under ASAHI_SHIM_VALIDATE_G17P_STREAM=1.

Assert-ON host CI on exact head d1a0f5ca52f0: 179/179 Pass (0 Fail). Ready for re-review.

On native AGX3 (HAL200), agx_encode_state() previously emitted a full
PPP bind tail and draw-state selector on every dirty draw, even when
only vertex uniforms or vertex buffer bindings changed. Clean indirect
draws also emitted a redundant bind tail and selector.

Track the latched HAL200 bind tail and vertex-shader metadata per VDM
chunk on agx_batch, resetting them on batch init and whenever a VDM
STREAM_LINK starts a new chunk. When only AGX_DIRTY_VS or
AGX_DIRTY_VERTEX is dirty and the current chunk already latched the
same fragment program, PPP state, and vertex metadata, emit only the
VDM_STATE_UPDATE and omit the bind tail and draw-state selector.

Also teach agx_g17p_emit_indirect_draw() to omit the leading selector
via agx_vdm_hal200_draw_emit() when draw_state_offset is zero, matching
Borealis and asahi_g17p_stream_validator, and drop the clean-indirect
bind-tail workaround in agx_draw_vbo().

Extend test-agx-vs-query to cover VS-only bind-tail elision, VDM
STREAM_LINK re-latching, and selectorless clean indirect draws under
ASAHI_SHIM_VALIDATE_G17P_STREAM=1.

Signed-off-by: Diljit Singh <hey@djdev.me>
On native AGX3 (HAL200), glMultiDraw*Indirect previously fell back to
util_draw_multi_unroll_indirect(), running full agx_draw_vbo() state
validation, BO registration, and 560-byte root uniform uploads for
every sub-draw.

Factor uniform-range collection out of agx_build_g17p_graphics_program()
and add agx_draw_multi_indirect() for native AGX3 indirect multi-draws:

- Run draw 0 through full agx_draw_vbo() to validate state, legalize
  feedback loops, register the indirect and index buffers, and latch the
  HAL200 bind tail.
- For draws 1..N-1 when neither gl_DrawID nor base parameters vary, emit
  only the selectorless indirect draw body.
- When gl_DrawID or base parameters vary, pre-fill contiguous chunks of
  draw_id push slices isolated more than INT16_MAX bytes from other
  uniform ranges so draw_id stays in its own Ioto address-cache
  component (preventing template cache thrashing against static pool
  tables), relocate the cached Ioto VS template via
  agx_g17p_graphics_program_cached(), emit the 24-byte VDM_STATE_UPDATE
  without a bind tail (re-latching only after a 64 KiB VDM STREAM_LINK),
  and emit the indirect draw.

Extend test-agx-vs-query with state-clean and gl_DrawID + base-param +
vertex-attribute indirect multi-draw tests crossing a VDM STREAM_LINK
boundary under ASAHI_SHIM_VALIDATE_G17P_STREAM=1 and verifying Ioto
template cache reuse.

Signed-off-by: Diljit Singh <hey@djdev.me>
…ck tests

Unify indirect sub-draw offset calculation between the HAL200 fast path
and per-draw fallback in agx_draw_multi_indirect() via a shared
agx_mdi_subdraw_offset() helper that widens to uint64_t and validates
alignment, uint32_t width (pipe_draw_indirect_info::offset), and buffer
bounds (indirect->buffer->width0) before multiplying draw_index * stride.

Bound the valid draw prefix and base-parameter address span by the same
in-range limit, and defer ctx->perf.vs_emits accounting until bind-tail
re-latching succeeds so an aborted fast-path emit cannot double-count.

Extend test-agx-vs-query to cover:
- agx_mdi_subdraw_offset() boundary, alignment, 32-bit multiply wraparound,
  and 32-bit addition overflow guards;
- unpacking and verifying the last draw's indirect buffer VA, draw_id, and
  params table on both small-stride and large-stride non-zero-offset paths;
- a forced per-draw fallback starting in an unlatched post-STREAM_LINK
  chunk and crossing another VDM STREAM_LINK under
  ASAHI_SHIM_VALIDATE_G17P_STREAM=1;
- a mid-MDI fast-path -> STREAM_LINK -> flush fallback -> fast-path
  recovery through the last draw; and
- runtime overflow and out-of-range clipping on both fast and fallback
  paths.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the perf/asahi-hal200-bind-elision-mdi branch from 7a28470 to 591107e Compare November 24, 2026 16:58
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 24, 2026 16:58
@DjDeveloperr

Copy link
Copy Markdown
Author

Pushed exact head 591107ed1eb0 (3 commits rebased cleanly onto aurora-wip 054c36c5d30e), addressing @RyanTheTide's FOLLOW-UP review with shared agx_mdi_subdraw_offset() 64-bit bounds/alignment/width checks, deferred vs_emits advancement on post-STREAM_LINK re-latch, and extended test-agx-vs-query coverage. Assert-ON host CI on exact head 591107ed1eb0: 184/184 Pass (0 Fail).

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict at 591107ed1eb0

APPROVE — Clean HAL200 bind-tail elision and indirect multi-draw fast path, resolving both issues from the earlier split (#701): the separate OOM-prone patch-plan helper is replaced by direct agx_g17p_graphics_program_cached() calls with isolated draw_id chunk placement (agx_mdi_isolated_va() / agx_mdi_alloc_draw_id_chunk()) and safe per-draw fallback on failure, and agx_g17p_emit_indirect_draw() documents and strips the unused draw-state selector cleanly when no fresh bind tail is emitted.

Blockers

None.

Checked

  • Reviewed all 5 changed files (src/asahi/lib/agx_linker.h, src/gallium/drivers/asahi/agx_batch.c, src/gallium/drivers/asahi/agx_state.c, src/gallium/drivers/asahi/agx_state.h, src/gallium/drivers/asahi/tests/test-agx-vs-query.c):
    • Verified HAL200 bind-tail elision (need_bind_tail in agx_encode_state() and g17p_bind_tail_latched / g17p_have_latched_vs resets across AGX_VDM_RECORD_STREAM_LINK in agx_ensure_cmdbuf_has_space() and batch init) against asahi_g17p_stream_validator.c.
    • Verified agx_mdi_subdraw_offset() overflow/alignment/bounds checks, agx_g17p_collect_graphics_uniform_ranges() tracking (single fixed-ABI AGX_SYSVAL_TABLE_PARAMS range at offset 0 with fail-closed fallback on multiple ranges, AGX_SYSVAL_TABLE_ROOT indirect-root detection), agx_mdi_isolated_va() / agx_mdi_alloc_draw_id_chunk() fallback on allocation failure, and agx_draw_multi_indirect() re-latching on STREAM_LINK when needs_vs_update emits a fresh VDM_STATE_UPDATE.
    • Verified test-agx-vs-query.c coverage for bind-tail elision, agx_mdi_subdraw_offset() bounds/overflow, and 1,600-draw MDI with STREAM_LINK re-latching and template reuse.
  • Verified isolated Gallium + EGL + drm-shim build and full non-layout asahi unit/drm-shim suite (167/167 PASS, including asahi-g17p-gallium-vs, asahi-t6041-gallium-vs, asahi-g17p-gallium-blit, asahi-g17p-gallium-submit, and agx_tests).
  • Verified live Apple M5 Max (G17C B1, git-591107ed1e, Linux 7.3, MESA_SHADER_CACHE_DISABLE=1, 0 GPU faults) Gallium GLES31 draw_indirect CTS (204/204 supported PASS).
  • Untested scope: asahi_layout_tests / agx3_layout (excluded due to pre-existing GCC 16 designated-initializer build issue in src/asahi/layout/tests/test-layout.cpp); G17P hardware execution beyond asahi-g17p-gallium-vs / asahi-t6041-gallium-vs drm-shim stream validation and live M5 Max Gallium GLES31 draw_indirect CTS.
Evidence
  • Unit & drm-shim tests (meson test --no-rebuild): 167/167 non-layout asahi tests PASS (asahi-g17p-gallium-vs OK, asahi-t6041-gallium-vs OK, agx_tests OK).
  • Live Apple M5 Max Gallium GLES31 CTS (OpenGL ES 3.2 Mesa 26.3.0-devel (git-591107ed1e), MESA_SHADER_CACHE_DISABLE=1, 0 GPU faults):
    • dEQP-GLES31.info.version: 1/1 PASS
    • dEQP-GLES31.functional.draw_indirect.*: 203/203 PASS (18 NotSupported)

@DjDeveloperr
DjDeveloperr merged commit 2465587 into aurora-wip Nov 24, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merged into aurora-wip at 2465587eb8098a6a7f78a0a3c06cbe686af29a01 (tree 50fe53f491bd571668608f3ff0b036b95d06c1c8).

Gates verified at exact head 591107ed1eb05084965c1e9e1e914bad79016fb5:

  • Independent exact-head approval from @satchlj (167/167 non-layout asahi unit/drm-shim tests, 204/204 live M5 Max Gallium GLES31 draw_indirect CTS, 0 GPU faults) plus live A18 Pro (G17P B1), M4 (G16G B0 652/652 GL Draw CTS & 56/56 gpubench-gl --verify), and M5 Pro (G17S B1 642/642 VK CTS) verification, with all @RyanTheTide follow-up items addressed.
  • Exact-head mirror CI green: asahi run 45443242799 (PASS) and aurora-agent-policy run 45443242843 (PASS).
  • Clean commits (3/3 Signed-off-by, policy check PASS).

DjDeveloperr added a commit that referenced this pull request Nov 24, 2026
The revert of #822 kept #896 (inter-submission barrier elision for
disjoint AGX3 batches, merge commit ba9bcc7), but #896 is built on
#822's written-BO list and per-batch submission state, so the driver no
longer compiled. Revert #896 as well, in the order a clean revert takes:
#896 first, then #822.

Two later users of #822 are adapted rather than reverted:

- #802 (HAL200 bind elision for multi-draw indirect) noted the indirect
  buffer as a vertex-stage read for #822's render barrier tracking. That
  tracking is gone; the indirect buffer is still recorded as a batch
  read.
- #745 (fast-clear batches) also checked #822's primitive counters. Its
  draw checks remain.

#754's agx_resource_set_valid() and #745's change to always write the
compute end timestamp are kept.

Two test changes keep later tests meaningful without #822:

- #822's guard in the threaded-context test that skips comparing a
  compressed level is kept. That level uploads and reads back through
  GPU blits, which the no-op drm-shim never runs.
- The #802 multi-draw test checked that a forced flush started a new
  batch by comparing batch pointers. Without #822 the flushed batch is
  submitted at once and its slot can be reused, so it compares the batch
  sequence number instead.

Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
@RyanTheTide
RyanTheTide deleted the perf/asahi-hal200-bind-elision-mdi branch November 27, 2026 13:20
DjDeveloperr added a commit that referenced this pull request Nov 30, 2026
Retain the native indirect recorder and batched loaders already merged in #802, #1243 and #1308. Reuse the texture/sampler pointer pair in the shared range collector for regular and multi-draw launches. Invalidate it when its batch is recycled.

Signed-off-by: DjDeveloperr <hey@djdev.me>
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.

3 participants