Skip to content

asahi: cache the HAL200 resource pointer pair per batch - #619

Merged
DjDeveloperr merged 4 commits into
aurora-wipfrom
djdev/asahi-native-mdi
Dec 2, 2026
Merged

DjDeveloperr merged 4 commits into
aurora-wipfrom
djdev/asahi-native-mdi

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 21, 2026 •

Copy link
Copy Markdown

What: On HAL200, a textured vertex shader uploaded its 16-byte {texture_base, samplers} pointer pair from the batch pool on every draw. That moved the address-cache range, so the graphics-program template cache missed on every sub-draw of a multi-draw. This caches the pair per stage for the batch, reuses it while both addresses are unchanged, and clears it at batch init. Native indirect recording and batched loaders are already on aurora-wip through #802, #1243 and #1308.
Tested: Both Gallium objects compile at this head. The Minecraft A/B in the earlier description below measured the full three-commit series on an older base. It hasn't been rerun for this smaller diff. Merge-result CI on current aurora-wip passed: https://github.com/DjDeveloperr/mesa/tree/ci/head-619-5e991b0e.
Risk: two-way door · Gallium on the native HAL200 chips (M3, M4, A18 Pro): textured draws and multi-draw indirect.
Not yet: rows at this head on J773g and J700: GL textured multi-draw indirect, base against head.

Earlier description (three-commit series)

Problem

The Gallium Asahi OpenGL driver advertises caps->multi_draw_indirect = true, so OpenGL 4.3+ applications (such as Minecraft 26.3's indirect chunk renderer) submit large batches of chunk draws via glMultiDrawElementsIndirect. However, agx_draw_vbo() previously unconditionally delegated every indirect->draw_count > 1 call to util_draw_multi_unroll_indirect(), running full per-draw validation (agx_draw_vbo), rebuilding the G17P/HAL200 graphics program from scratch, re-packing VDM state updates and indirect records, and growing the batch's scissor/depth-bias arrays on every sub-draw.

On an Apple M4 Mac mini, profiling showed GL43C.nglMultiDrawElementsIndirect consuming 37.7–51.5% of render-thread wall time (~3.5–4.1 ms/frame) in user CPU even when all sub-draws shared the same pipeline, textures, samplers, and render state. Additionally, for textured vertex shaders (resource_pair == true, e.g. lightmap sampling), agx_build_g17p_graphics_program() allocated a fresh 16-byte {texture_base, samplers} pointer pair from batch->pool on every draw, shifting ranges[0] inside the batch->pool address-cache component and causing graphics_program_shape_equal() to miss the 8-entry template cache on every sub-draw.

Change

Three commits cleanly targeting aurora-wip (fa9a6766c01), gated solely by the native HAL200 graphics capability (agx_native_graphics(dev), with clean fallback to util_draw_multi_unroll_indirect() on non-native chips, count_from_stream_output, indirect draw counts, and user index pointers — no chip, board, or firmware gates):

  1. asahi: add clean-batch fast path for native multi-draw indirect (src/gallium/drivers/asahi/{agx_batch.c,agx_state.{c,h}})
    • Adds agx_draw_multi_indirect() for indirect->draw_count > 1 without an indirect draw count on native HAL200 chips.
    • Runs draw 0 (and the first draw after any mid-loop batch flush) through agx_draw_vbo() to establish shaders, descriptors, feedback loops, BO residency, and the HAL200 bind tail.
    • For subsequent sub-draws on the clean batch, emits only the indirect draw body when neither uses_base_param nor uses_draw_id is active; when active, updates only the per-draw VS parameters and reuses draw 0's cached bind tail and draw-state selector without growing scissor/depth-bias tables. Normalizes stride == 0 to tightly packed command sizes (20 B indexed, 16 B non-indexed).
  2. asahi: fast-patch VS Ioto program and VDM records in native multi-draw indirect (src/gallium/drivers/asahi/agx_state.c)
    • Pre-validates and caches reusable templates across sub-draws on the common uses_base_param && !uses_draw_id && !root_reads_params_ptr path:
      • VS Ioto program template (patching only the AGX_SYSVAL_TABLE_PARAMS VA immediate per sub-draw),
      • 28-byte VDM vertex state update (patching only the 8-byte pipeline pair per sub-draw),
      • packed HAL200 indirect draw record (patching only the indirect buffer VA words per sub-draw).
    • Falls back cleanly to agx_build_pipeline() / agx_g17p_emit_indirect_draw() on any edge case (short-LDIMM overflow, preamble, unaligned indirect VA).
  3. asahi: cache G17P resource pointer pair per batch and guarantee MDI fast-patch (src/gallium/drivers/asahi/{agx_batch.c,agx_state.{c,h}})
    • Caches each stage's 16-byte {texture_base, samplers} pointer pair upload in batch->g17p_resource_pair[] (zeroed on batch init) and reuses it across agx_build_g17p_graphics_program() and agx_multidraw_init_vs_patch(), eliminating per-draw template cache thrashing on textured vertex shaders.
    • Narrows root_reads_params_ptr overlap checks specifically to [offsetof(struct agx_draw_uniforms, tables[AGX_SYSVAL_TABLE_PARAMS]), +8), and falls back to generating one address-independent .patchable = true template per multi-draw if the MRU template shares an address-cache cluster with PARAMS.

Testing

Tested on Apple M4 Mac mini (Apple M4 (G16G B0)), Arch Linux ARM, kernel 7.3.0-ARCH-m4drv8, headless Wayland (kwin_wayland --virtual --width 1920 --height 1080), Minecraft 26.3 (RD 12, Fancy, VSync off, maxFps 260, fixed 50s still:15,spin:15,fly:20 benchmark scene). All timed rows met thermal and load gates (hotspot <= 70 C, load1 < 0.5), with 0 GPU kernel error/fault lines and 0 failed units.

Same-Machine A/B Performance (Gallium Asahi Native OpenGL 4.6)

Arm Mesa revision Still FPS Spin FPS Fly FPS All FPS p50 / p99 (ms) Render CPU (user) ms/frame (still) Proc CPU ms/frame (still)
Baseline d4ad4a4dce 111.8 91.4 128.7 112.4 8.63 / 15.90 5.31 (4.85) 7.44
Commit 1 (clean-batch) e222fe5e62 124.8 101.3 145.4 126.0 (+12.1%) 7.59 / 14.00 4.52 (4.25) (-14.9%) 8.16
Commits 1–2 (fast-patch pre-G2) ed1ffa40c4 125.4–127.0 102.6–102.8 148.1–148.6 127.6–128.4 (+13.5–14.2%) 7.54 / 13.71–13.94 4.57–4.58 (4.28) 7.82–7.83
Proposed (Commits 1–3) 5411b98d71 (e1ba7c727a / branch head 2c4f7c49405) 127.4 103.6 151.4 129.9 (+15.6% vs baseline) 7.39 / 13.93 3.61 (3.09) (-32.0% total / -36.3% user vs baseline; -21.2% vs Commits 1–2) 6.61 (-11.2% vs baseline, -15.4% vs Commits 1–2)
  • JFR + kstacks.txt verification (render thread, 50s steady window):
    • GL43C.nglMultiDrawElementsIndirect collapsed ~10x: from 51.2% (~4.09 ms/frame; 10,122/19,764 steady samples) before Commit 3 to 5.2% (~0.40 ms/frame; 1,059/20,506 steady samples) with Commits 1–3.
    • Render-thread CPU dropped -20.6% to -23.5% across still/spin/fly vs Commits 1–2 (4.58 -> 3.61, 5.27 -> 4.19, 3.97 -> 3.04 ms/frame).
    • GL is now GPU-bound: GL32C.nglClientWaitSync (GlCommandEncoder.awaitSubmit) is the agx: introduce agx3b support & HAL200 #1 wall limiter at 49.4% (~3.79 ms/frame; 10,126/20,506 samples), corroborated by kstacks.txt (52.0% sleeping, 504/989 = 51.0% in drm_syncobj_array_wait_timeout).

Conformance Gates (VK-GL-CTS 1.4.7.1, 0 GPU faults)

Suite Driver Result
gles31-mdi-gate + gl46-mdi-gate (458 cases: draw indirect, multi-draw indirect, base-vertex, base-instance, draw-id) Gallium Asahi OpenGL (5411b98d71 & e1ba7c727a) 439 Pass / 19 NotSupported / 0 Fail / 0 Crash / 0 Timeout
Driver identities, benchmark settings, and build options
  • Build configuration: Release (-Dbuildtype=release -Db_ndebug=true), gallium-drivers=asahi,zink, vulkan-drivers=asahi, llvm=disabled, spirv-tools=disabled, glvnd=enabled, clang 23.1.5 (aarch64).
  • Measured binaries:
    • 5411b98d719845ada3db0815b1cc402a72c54103 (libgallium-26.3.0-devel.so SHA256: 6aa6f5263ca210ac68bc2fc8ab6c7509b2476d457347d5cd8f8e11c14a3f9282)
    • Rebased identical Gallium tree e1ba7c727ae449f818510dad36476177f115bf38 (libgallium-26.3.0-devel.so SHA256: 49128984b68ef82971dede735ecf67c8a19ad5d0715529f800dddc8010991917)
  • Workload: Vanilla Minecraft 26.3 offline client (Temurin 25.0.5, -Xms1G -Xmx2560M -XX:+UseZGC -XX:+UseCompactObjectHeaders -XX:+UseStringDeduplication), renderDistance:12, graphicsPreset:"fancy", enableVsync:false, maxFps:260, 1920x1080 fullscreen on headless KWin Wayland.
  • No regressions: Non-native chips bypass agx_draw_multi_indirect() and continue through util_draw_multi_unroll_indirect(); edge cases (preamble, cluster-shared PARAMS, short-LDIMM overflow, stream-output count, indirect draw count, user index pointers) fall back cleanly.

Still to run

  • Cross-generation A/B and GL/GLES CTS verification on G13 / G14 (M1/M2, verifying zero regression on the fallback path), G17P (M4 Pro / A18 Pro, native HAL200 path), and M5 before merge.

Not yet

  • M4 mini J773g and Neo J700: verify native multi-draw indirect and the G17P pointer-cache path at the current head, including the Minecraft game row.

Handle glMultiDraw{Elements,Arrays}Indirect (indirect->draw_count > 1
without an indirect draw count) on native HAL200 chips via a dedicated
agx_draw_multi_indirect() fast path instead of unrolling every sub-draw
through full agx_draw_vbo() validation.

Draw 0 (and the first draw after any mid-loop batch flush) runs through
agx_draw_vbo() to validate shaders, descriptors, feedback loops, BO
residency, and the HAL200 bind tail. Subsequent sub-draws on the clean
batch skip per-draw validation:
- When neither uses_base_param nor uses_draw_id is active, emit only the
  indirect draw body without a bind tail or draw-state selector.
- When uses_base_param (including instanced vertex attributes) or
  uses_draw_id is active, update only the per-draw VS parameters, rebuild
  the VS program, and emit the VDM vertex state update followed by the
  cached bind tail and draw-state selector from draw 0, avoiding redundant
  bind-state uploads and scissor/depth-bias array growth.

Normalize stride == 0 to the tightly packed command size (20 bytes indexed,
16 bytes non-indexed) and fall back to util_draw_multi_unroll_indirect()
for non-native chips, count_from_stream_output, and user index pointers.

Signed-off-by: Diljit Singh <hey@djdev.me>
…w indirect

In agx_draw_multi_indirect(), after draw 0 (or any mid-loop batch flush)
establishes a clean native HAL200 batch, memoize:
- the pre-relocated G17P VS Ioto program and its AGX_SYSVAL_TABLE_PARAMS
  relocation sites (using hk_g17p_patch_plan_init/hk_g17p_patch_ldimm) for
  the common uses_base_param && !uses_draw_id && !root_reads_params_ptr path,
- the 28-byte VDM vertex state update (patching only the 8-byte pipeline
  pair per sub-draw), and
- the packed HAL200 indirect draw record (patching only the indirect buffer
  VA words per sub-draw).

Any edge case (cluster-shared PARAMS ranges, short-LDIMM overflow, preamble,
or unaligned indirect VA) falls back cleanly to agx_build_pipeline() and
agx_g17p_emit_indirect_draw().

Signed-off-by: Diljit Singh <hey@djdev.me>
…ast-patch

Cache each stage's 16-byte {texture_base, samplers} pointer pair upload
in batch->g17p_resource_pair[] (reset on batch init) and reuse it in
agx_build_g17p_graphics_program() and agx_multidraw_init_vs_patch().

Previously, every textured VS draw allocated a fresh pointer pair from
batch->pool, shifting ranges[0] by +16 bytes relative to ROOT/VS ranges
in the same batch->pool address-cache component. That caused
graphics_program_shape_equal() to fail on every textured sub-draw, both
disabling the multi-draw indirect VS fast-patch and thrashing the
8-entry G17P graphics program template cache.

Also narrow root_reads_params_ptr in agx_draw_multi_indirect() to check
overlap specifically with tables[AGX_SYSVAL_TABLE_PARAMS], and fall back
to generating one address-independent patchable template per multi-draw
if the MRU template shares a cluster with PARAMS.

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

@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: REQUEST CHANGES at 2c4f7c49

Blockers:

  1. Branch is CONFLICTING with aurora-wip. Rebase/merge current aurora-wip across the 3 touched files (+716/-13) so the PR is MERGEABLE.
  2. Missing exact-head build/host test and hardware verification. After rebasing, post host test results and exact-head HAL200/G17P multi-draw indirect hardware verification (plus a second-generation regression row for shared Gallium paths).

Checked:

  • Commit series (d8439b38, b24a6d02, 2c4f7c49), CONFLICTING against aurora-wip.
Evidence
  • Resolve CONFLICTING against aurora-wip before full code/hardware sign-off.

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>

@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 844e9384

Blockers:

  • PR title and description out of sync with narrowed diff (+34/-6): After 844e9384 merged aurora-wip, the multi-draw indirect fast-path commits are no longer in the diff against aurora-wip — only the per-batch agx_g17p_resource_pair_va() {texture_base, samplers} pointer-pair cache remains. Please update the PR title and body to match the actual +34/-6 change.
  • Exact-head build, host tests, and hardware verification at 844e9384 missing: Please post a fresh build + meson test --suite asahi pass plus exact-head 844e9384 Gallium GL hardware evidence across two native AGX3 generations (e.g., G16G/G17P and M5) covering textured vertex/fragment draws with ASAHI_MESA_DEBUG=verify and batch reuse.

Checked:

  • Audited full +34/-6 diff (src/gallium/drivers/asahi/agx_batch.c, agx_state.c, agx_state.h):
    • agx_batch_init() cleanly zeroes batch->g17p_resource_pair for every new batch.
    • agx_g17p_resource_pair_va() caches {texture_base, samplers} -> va per stage within the batch, matching the caller's !texture_base || !samplers precondition in agx_g17p_collect_graphics_uniform_ranges() and handling agx_pool_upload_aligned() failure (!va) cleanly.
Evidence
  • Head commit: 844e9384b7c330c828adb67e22bb5c6f67f740a6 (Merge aurora-wip into the HAL200 indirect-draw change, MERGEABLE into aurora-wip).
  • Diff vs aurora-wip: +34/-6 across agx_batch.c, agx_state.c, agx_state.h.

@github-actions github-actions Bot added area:gallium Area from CODEOWNERS (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Dec 1, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Merged aurora-wip at 844e9384b7c3. #802, #1243 and #1308 already supply native indirect recording and batched loaders. This integration adds the remaining per-stage texture/sampler pointer pair to the shared range collector and resets it on batch recycle. Both Gallium objects compiled; J773g and J700 textured indirect draws remain due.

@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: MERGE at 844e9384b7c3

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: - Audited full +34/-6 diff (src/gallium/drivers/asahi/agx_batch.c, agx_state.c, agx_state.h):

  • agx_batch_init() cleanly zeroes batch->g17p_resource_pair for every new batch.
  • agx_g17p_resource_pair_va() caches {texture_base, samplers} -> va per stage within the batch, matching the caller's !texture_base || !samplers precondition in agx_g17p_collect_graphics_uniform_ranges() and handling agx_pool_upload_aligned() failure (!va) cleanly.

Follow-ups (not blocking): update title/body to narrowed diff; head build/meson tests + GL row (one gen enough)

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Merge readiness at 844e9384: not ready yet

Check Detail
CI at head ok no CI for this change (the hardware row is the gate)
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA 844e9384, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text adopt when you next edit add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:gallium

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr

Copy link
Copy Markdown
Author

J700 (G17P) row: mesa#619 · J700 MacBook Neo (A18 Pro, G17P B1), stock Fedora 44 7.3.0-41.fc44.aurora.aarch64, same boot, base 8b59f2b vs head 844e938: PASS — mditex (gl43, 4 textured MDI calls/frame with VS texelFetch and texture/sampler rebinds): exact 256×256 image at both grids on both arms; ABBA 1024 draws/call 0.297 → 0.300 ms/frame (+0.9%), 64 draws/call 0.153 → 0.148 ms/frame (-2.8%); 619-gles31 321/321 compared, base 307 Pass / 14 NotSupported → head 307 Pass / 14 NotSupported, 0 status changes; 619-gles3 541/541 compared, base 541 Pass → head 541 Pass, 0 status changes; 0 GPU faults, 0 kernel WARNINGs; stock handback verified.

Kit B payload 5c0239c4471e71ad…, target evidence 91796765eaa899d7… (Mini: work/j700-mesa-batch-b-20261202/evidence/receipt-WfPHHaNB). The run started about 08:44 UTC and spent 347 s on target under the Neo lease. The stock fingerprint and both Apple partners were identical before and after.

@DjDeveloperr

Copy link
Copy Markdown
Author

Row: 844e9384b7c3 + J773g (Apple M4 Mac mini, T8132, G16G B0 / HAL200 / AGX3B, kernel 7.3.0-ARCH-m4main-6c83a2a6, boot_id=21564087-69fe-448d-be51-ec60300e9bc4) + PASS: same-boot native Asahi Gallium A/B vs base 8b59f2b0a70e, 221 dEQP-GLES31.functional.draw_indirect.* cases with 0 diffs + textured glMultiDrawElementsIndirect VS-sampling verification PASS, 0 GPU faults.

  • Textured glMultiDrawElementsIndirect with VS texture sampling (test_mdi_vstex, GL 4.6 Core, texelFetch + texture() in VS and FS across 4 indirect quads with baseVertex/baseInstance, 200 iterations + RGBA readback):
    • Base git-8b59f2b0a7: PASS (errs=0).
    • Head git-844e9384b7: PASS (errs=0).
  • GLES 3.1 CTS (dEQP-GLES31.functional.draw_indirect.*, surfaceless FBO 256x256, MESA_SHADER_CACHE_DISABLE=true):
    • Base 8b59f2b0a70e: 203 Pass, 18 NotSupported, 0 Fail.
    • Head 844e9384b7c3: 203 Pass, 18 NotSupported, 0 Fail (0 status diffs).
  • GPU health: health: failed 0 errno 0 degraded 0, recoveries 0, hang_kills 0, 0 dmesg faults.

@DjDeveloperr DjDeveloperr changed the title asahi: fast-path native HAL200 multi-draw indirect and cache G17P resource pointer pair asahi: cache the HAL200 resource pointer pair per batch Dec 2, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

Local check on the merge tree (pc, x86-64 clang, asserts on, Asahi Gallium + Vulkan + drm-shim, every test input rebuilt and a second ninja pass clean): aurora-wip 893e648e + #1546 + #1541 + this head 844e9384 → tree 4a34627b. meson test --suite asahi --suite poly --suite vulkan: 461 OK, 0 fail, 1 skip (hk-g17p-cross-queue, needs two queues).

With the review at this head and the J700 (G17P) and J773g (G16G) rows above (0 status changes, 0 GPU faults), this meets the merge bar, so I'm merging it. I retitled it to match the narrowed diff. M3 (G15) is the remaining HAL200 generation and is post-merge verification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gallium Area from CODEOWNERS (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant