Skip to content

asahi: replay native AGX3 direct draws that change only VS constants - #1285

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/asahi-agx3-repeat-direct-draws
Nov 28, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/asahi-agx3-repeat-direct-draws

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 28, 2026 •

Copy link
Copy Markdown

What

On native AGX3 GPUs (M3, M4, Neo), Gallium asahi now replays a direct draw that follows the previous draw of the batch with only vertex constant buffers rebound, or with nothing changed. This is the loop of a UBO range rebind or a glUniform* before each glDraw*. Before this change each such draw ran the whole of agx_draw_vbo(): shader and legality checks, descriptor updates, the root table comparison, uniform range collection and the patch-plan lookup for the vertex program. All of that only to emit a vertex state update and a draw body.

  • At the end of a full-path draw, the driver records the draw together with the uniform ranges and patch plan of the vertex program it latched. A range read from a system value table is kept as (table, offset), tagged by the push range's table when the ranges are collected.
  • A replay uploads the vertex stage table and patches the same plan with the ranges moved to the current tables. It emits the vertex state update and the draw body the full path would emit, or only the draw body when nothing changed.
  • Anything else takes the full path, which records the draw again. That covers other dirty state, other shaders, primitive or index format, bound queries, transform feedback, conditional rendering, stream output, a VDM stream that moved on, and a template list that changed since the record.
  • A vertex shader that reads draw parameters, or its stage table pointer from the root table, is never recorded.

This is a different approach from the direct-draw fast path dropped from mesa#1271. On J700 that one never fired: agx_g17p_try_fast_direct_draw is absent from the profile. It refused every vertex program with a root-table range, i.e. every VS that fetches attributes, so it only added per-draw bookkeeping.

New drm-shim test asahi-{g13g,g15g,g16g,g17p}-repeat-draw checks:

  • replayed draws read the new binding;
  • they emit the same commands as the full path apart from the vertex program address;
  • a draw with nothing changed emits only the draw body;
  • query-counted draws and fragment state changes take the full path;
  • G13G never records.

Tested

Tested: J700 MacBook Neo (G17P B1), kit built from 12baca553c7. The driver code of head 40dc32a is the same in release builds; the head adds a debug-build ASAHI_MESA_DEBUG=dirty guard, the unit test and the commit message. gpubench-gl render.draws mode=direct: 1024 draws +21.3 % (depth 1) / +37.7 % (depth 4), 4096 draws +43.8 % (depth 4). 4096 draws at depth 1 is −39.6 %, from CPU placement (see Not yet): with the process pinned to the P-cores or with uclampset -m 512, the same row is +30 % / +32 %. mode=mdi and the control rows are within ±0.5 %, except copy_b2i d1 at −3.5 % (inside the arm spread). Conformance is unchanged. 0 GPU faults, 0 SELinux denials, 0 failed units.

  • Benchmark: gpubench gpubench-gl c228eec (+dirty; the same binary for both arms), --verify, warmup 250 ms, 5 runs × 300 ms per row. Same-boot interleaved A1 B1 B2 A2 B3 A3 A4 B4 (ABBA + BAAB), 4 arms per build. Each arm ran from an isolated tmpfs kit with MESA_LOADER_DRIVER_OVERRIDE=asahi EGL_PLATFORM=surfaceless MESA_SHADER_CACHE_DISABLE=true, with a CPU cool-down between arms.
  • Correctness: a GL test of seven direct-draw loops gave the same CRC on both builds, with and without GALLIUM_THREAD=0. The loops cover UBO rebinds, default-block uniforms, DrawArrays, a mix of VBO, fragment-uniform, restart, instancing, base-vertex and misaligned-start changes, UBO rewrites mid-loop, identical consecutive draws with blending, and two UBOs with flushes. gpubench --verify CRCs were identical on every row that has one.
  • dEQP on J700: identical on both builds; see the table below.
  • Merge with aurora-wip (7918a2e, after mesa#1271): the only conflict was the drm-shim test list in meson.build; both test sets are kept. mesa#1271's compute pixel-unpack upload only binds compute-stage state, which the replay already ignores, so no driver change was needed. Fedora Mesa CI at 7918a2e: 220/0. drm-shim builds, full --suite asahi, release and assert (-Db_ndebug=false): 341/344 each. That includes repeat-draw on G13G/G15G/G16G/G17P and mesa#1271's image-copy-buffer (unit, G13G, G17P). The 3 failures are asahi-egl-priority-*, which fail the same way on aurora-wip: EGL does not initialize in the headless CI container. No new warnings in src/gallium/drivers/asahi (the assert build's unused batch_idx in agx_batch.c is already on aurora-wip). The newer aurora-wip commits after 55a8378 do not touch this driver.
  • Fedora Mesa CI (./rig/fedora-mesaci.sh, the repo's asahi workflow) at 40dc32a: 219/0. The drm-shim build (-Dtools=drm-shim) passed 6/6 (the 4 new tests + reload-elision) with 0 new warnings. The assert build (-Db_ndebug=false, full --suite asahi) at 40dc32a: 218/0. hk_pack hit the 30 s timeout while the CI host was busy and passed on rerun (21.6 s).
  • Kernel and boot: kernel 7.3.0-41.fc44.aurora.aarch64 (stock), AC power, SELinux enforcing. The stock.sh state was identical before and after every run.
  • Builds: base 7a0a055 (aurora-wip; the only later commit, 8ba943e, does not touch this driver), candidate 12baca553c7. Both are Fedora 44 cross builds (rig/fedora-mbuild.sh) with the same meson options.
  • Profile: base perf on draws=4096 depth 4: the threaded-context driver thread has 66 % of the samples and the GPU is busy 165 µs of each ~840 µs submit. On the candidate, the replay replaces the full path for these draws.

Risk

Risk: two-way · M3, M4, Neo GL (native AGX3 only; M1/M2 and M5 never record a draw)

Not yet

  • 4096 draws at depth 1 regresses unpinned (−39.6 %). On J700, the candidate's lighter driver thread is placed on an E-core (cpu_capacity 426 vs 1024), and the app thread then waits on the threaded-context queue. Pinned to the P-cores (taskset -c 0,5) the row is 926.0 → 649.4 µs (+30 %); with uclampset -m 512 it is 960.0 → 650.1 µs (+32 %). Base already loses the same way at 1024 draws: 757 µs unpinned vs 393 µs pinned. Mesa's thread-placement policy for this (util_thread_sched, the threaded-context thread following the app thread's cache domain) is x86-only. A fix belongs in its own change with its own A/B. This PR stays a draft until then.
  • No M3 Pro (G15) or M4 mini (G16G) hardware run yet; both take the same code path and pass the drm-shim test.
Evidence

J700 per-arm medians, render.draws, µs per submit (lower is better):

row A1 B1 B2 A2 B3 A3 A4 B4
direct 1024 d1 757.6 597.6 596.4 753.8 595.2 755.7 759.5 595.2
direct 1024 d4 203.0 125.1 126.0 204.4 128.0 203.5 220.8 128.8
direct 4096 d1 977.2 1351.4 1310.0 937.5 1351.4 990.1 958.5 1357.5
direct 4096 d4 849.9 460.1 476.2 833.3 464.4 826.4 840.3 490.2
mdi 1024 d1 215.7 217.7 216.9 217.2 217.2 217.5 217.2 217.7
mdi 1024 d4 47.9 48.2 48.2 48.4 47.8 47.6 47.5 47.7
mdi 4096 d1 362.3 363.6 363.2 364.1 363.2 363.2 362.8 363.2
mdi 4096 d4 105.0 106.3 106.8 109.6 105.2 106.3 105.9 107.1

Median of 4 arm medians. Positive delta = faster. crc same = identical --verify CRC on every arm of both builds; n/a = the row has no image check.

row                                                                unit       base       cand    delta     n crc
render.draws draws=1024,mode=direct d1                         us_per_s     756.62     595.83   +21.3%  4/4  n/a
render.draws draws=1024,mode=direct d4                         us_per_s     203.94     126.99   +37.7%  4/4  n/a
render.draws draws=4096,mode=direct d1                         us_per_s     967.83    1351.35   -39.6%  4/4  n/a
render.draws draws=4096,mode=direct d4                         us_per_s     836.83     470.29   +43.8%  4/4  n/a
render.draws draws=1024,mode=mdi d1                            us_per_s     217.23     217.47    -0.1%  4/4  n/a
render.draws draws=1024,mode=mdi d4                            us_per_s      47.73      47.98    -0.5%  4/4  n/a
render.draws draws=4096,mode=mdi d1                            us_per_s     362.98     363.20    -0.1%  4/4  n/a
render.draws draws=4096,mode=mdi d4                            us_per_s     106.12     106.55    -0.4%  4/4  n/a
render.heavy kind=opaque,res=1080p d1                          us_per_s     222.80     222.88    -0.0%  4/4  n/a
render.heavy kind=opaque,res=1080p d4                          us_per_s      73.57      73.63    -0.1%  4/4  n/a
render.light kind=tri d1                                       us_per_s     125.76     126.18    -0.3%  4/4  n/a
render.light kind=tri d4                                       us_per_s      34.35      34.36    -0.0%  4/4  n/a
transfer.ops kind=copy_b2i d1                                      gbps      36.29      35.01    -3.5%  4/4  same
transfer.ops kind=copy_b2i d4                                      gbps      50.75      49.83    -1.8%  4/4  same
transfer.ops kind=clear_4m d1                                      gbps      39.17      39.19    +0.1%  4/4  same
transfer.ops kind=clear_4m d4                                      gbps     204.35     204.50    +0.1%  4/4  same
compute.light  d1                                                  us_per_s 101.78     101.88    -0.1%  4/4  n/a
compute.light  d4                                                  us_per_s  19.70      19.68    +0.1%  4/4  n/a

copy_b2i d1 per arm, GB/s: base 36.18 / 36.41 / 36.41 / 34.78, candidate 34.62 / 35.18 / 34.84 / 35.18.

Depth-1 placement check (same boot, 2 arms per build, µs per submit):

arm 1024 d1 base 1024 d1 cand 4096 d1 base 4096 d1 cand
unpinned (ABBA above) 756.6 595.8 967.8 1351.4
taskset -c 0,5 392.9 310.2 926.0 649.4
uclampset -m 512 393.0 312.7 960.0 650.1

Unpinned, perf --sample-cpu puts the base driver thread on CPU 5 (a P-core) and the candidate's on CPU 4 (an E-core).

dEQP on J700, same counts on both builds:

group passed
GLES3 draw 324/324
GLES3 ubo 2357/2357
GLES3 vertex_arrays 1005/1005
GLES3 instanced 45/45
GLES3 uniform_api.value 1422/1422
GLES3 primitive_restart 504/504
GLES3 occlusion_query 66/66
GLES31 draw_base_vertex 114/114
GLES31 draw_indirect 203/221 (18 not supported, 0 failed)

Fedora CI on 40dc32a: FEDORA-CI 40dc32ad69bb 219/0 after-rc=0; drm-shim asahi-{g13g,g15g,g16g,g17p}-repeat-draw and asahi-{g13g,g17p}-reload-elision 6/6, 0 warnings in agx_state.c and the new test. Assert build (-Db_ndebug=false): 218/0 + hk_pack timeout, hk_pack and agx_tests OK on rerun.

Exact-head J700 pair verification (#1285 @ 7918a2e19e46 + #1290 @ 6e42e7ada894)

Same-boot ABBA+BAAB (n=4 arms/build, 5 timed samples/arm) on MacBook Neo (J700 / G17P B1), stock 7.3.0-41 (apple-mcc 4-plane SLC enabled, DT okay, stock.sh PRE==POST, 0 GPU faults, 0 AVCs), base 702dd0165bd0 vs combined 6a2466d5ac44:

API Workload Depth Base Combined Change Pairs won
GL render.draws direct 1024 1 806.45 us 311.53 us -61.37% 4/4
GL render.draws direct 1024 4 210.91 us 132.08 us -37.37% 4/4
GL render.draws direct 4096 1 948.77 us 676.60 us -28.69% 4/4
GL render.draws direct 4096 4 849.19 us 513.38 us -39.54% 4/4
GL transfer.ops copy_b2i 1 46.20 GB/s 46.74 GB/s +1.16% 2/4
GL transfer.ops copy_b2i 4 57.18 GB/s 56.41 GB/s -1.35% (recheck -1.41%) 0/4 (1/4 recheck)

All 14 other GL/VK control rows (mdi 1024/4096 d1/d4, render.heavy, render.light, compute.light) stayed within -0.19% to +0.96% (0/16 controls exceeded the regression threshold). All 20 row CRCs, 28 draw-state CRC snapshots, and 16 in-arm copy CRCs matched; dEQP GLES3 draw 324/324 and UBO 2,357/2,357 passed on both builds; gdrv0 moved uclamp.min 0 -> 512 with Seccomp: 0.

A loop of direct draws that rebinds a uniform block range or updates a
default-block uniform before each draw changes nothing on the native
AGX3 path (M3, M4 and Neo GPUs) but the vertex stage table. Each draw
still went through the whole of agx_draw_vbo(): shader, feedback-loop
and transform-feedback checks, descriptor updates, the root table
comparison, uniform range collection and the patch-plan lookup for the
vertex program, only to emit a vertex state update and a draw body.

Record the last direct draw of a batch, with the uniform ranges of the
vertex program it latched, and replay a following direct draw that
changed only vertex constant buffers (or nothing): upload the vertex
stage table, patch the same plan with the ranges moved to the current
tables, and emit the vertex state update and the draw body the full
path would emit. The replay needs the recorded draw to end the VDM
stream, the same shaders, primitive and index format, no dirty state
but vertex constants, no bound queries, no transform feedback or
conditional rendering, and a template list unchanged since the record.
Anything else takes the full path, which records the draw again.

A vertex shader that reads draw parameters or its stage table pointer
from the root table is never recorded: a replay changes that pointer
without uploading a new root table.

A drm-shim test checks on G13G, G15G, G16G and G17P that replayed draws
read the new binding, emit the same commands as the full path apart
from the vertex program address, emit only the draw body when nothing
changed, and that query-counted draws and fragment state changes take
the full path.

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

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

Reviewed at 40dc32ad69bb: clean native-AGX3 repeat-draw fast path in agx_draw_vbo_repeat() / agx_draw_repeat_record(). Guards cover num_draws > 1 (handled earlier), vdm_end == batch->vdm.current, non-VS-const dirty bits, query/XFB/tess/GS/patch/memory-write/unaligned-index/user-index/base-param/draw-id exclusions, template eviction (g17p_graphics_templates_generated), root-table stage-pointer reads (agx_vs_reads_stage_table_pointer), and safe fallback if patching/emission fails before batch->vdm.current or dirty bits are touched. Solid unit test (test-agx-repeat-draw.c), GLES3/3.1 dEQP coverage, and J700 ABBA numbers.

Bring in mesa#1271 (compute pixel-unpack uploads to AGX3 images). The
only conflict is the asahi drm-shim test list in meson.build: keep both
the repeat-draw tests and the image-copy-buffer tests.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr marked this pull request as ready for review November 28, 2026 13:46
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 28, 2026 13:46

@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 7918a2e1 (land together with or after mesa#1290)

Blockers: none (pairing note: keep merge order with or after mesa#1290 so unpinned render.draws direct 4096 d1 keeps the P-core placement win instead of the standalone -39.6% E-core placement regression).

Checked:

  • Verified forward-merge 7918a2e1 onto aurora-wip (post mesa#1271): PR diff remains strictly scoped to the same 5 files (agx_batch.c, agx_state.c, agx_state.h, src/gallium/drivers/asahi/meson.build, tests/test-agx-repeat-draw.c). The only conflict resolution in meson.build cleanly preserves both asahi-*-repeat-draw and asahi-*-image-copy-buffer drm-shim tests.
  • Core repeat-draw fast path (40dc32ad) was already reviewed and approved, and mesa#1271's compute pixel-unpack path touches only compute-stage state (ignored by the vertex-only replay guard).
  • Verified exact-head J700 ABBA+BAAB pair run (#1285 @ 7918a2e1 + #1290 @ 6e42e7ad, n=4 arms): all four direct-draw rows win 4/4 pairs (-61.37% / -37.37% / -28.69% / -39.54%), 0/16 control regressions, all 20 row CRCs + 28 draw-state CRCs + 16 copy CRCs match, dEQP GLES3 draw 324/324 and UBO 2,357/2,357 pass, 0 GPU faults, 0 AVCs.
  • Verified exact-head Fedora Mesa CI 220/0 and full drm-shim --suite asahi (341/344 in both release and -Db_ndebug=false, including repeat-draw on g13g/g15g/g16g/g17p).
Evidence
  • Risk: two-way · M3, M4, Neo GL (native AGX3 only; M1/M2 and M5 never record a draw).
  • Minor body nit (non-blocking): the ## Not yet bullet still reads "This PR stays a draft until then" even though the PR is now ready and verified paired with #1290.

@DjDeveloperr

Copy link
Copy Markdown
Author

J516s second-generation confirmation — PASS for GL and stock-kernel compatibility

MacBook Pro (M3 Pro / T6030), GL_RENDERER=Apple M3 Pro (G15S B1), stock Linux 7.3.0, AC connected. Same-boot ABBA + BAAB, three timing samples per arm and row. SLC was on before and after at 0x2100 on all six MCC planes (16 data and 16 tag ways each).

GL row Depth Base Pair Change Faster pairs CRC32
Direct 1024 1 672.04 µs 548.85 µs -18.33% 4/4 3d1384b2
Direct 1024 4 174.70 µs 174.76 µs +0.03% 2/4 3d1384b2
Direct 4096 1 1234.58 µs 1184.71 µs -4.04% 3/4 de8865c4
Direct 4096 4 621.89 µs 501.50 µs -19.36% 4/4 de8865c4
Indexed MDI 1024 4 181.62 µs 180.31 µs -0.72% 4/4 3d1384b2
Copy buffer → image 4 51.88 GB/s 51.37 GB/s -0.97% 1/4 0883f778
Render clear 4 54.81 µs 54.88 µs +0.13% 1/4 2d2b34c3
Compute light 4 41.26 µs 40.97 µs -0.70% 2/4 d6efe1fe

64/64 CRC observations matched across all eight rows. Direct 1024 d4 was flat and direct 4096 d1 was variable (3/4 pairs faster). Copy throughput was 0.97% lower (3/4 pairs slower, worst pair −1.16%). The other control medians were within 0.72%. There were 0 GPU faults, recoveries, hang kills, quarantines or AVCs.

Both arms mapped their own libgallium and libEGL_mesa through the stock GLVND frontends. The matching asahi_dri.so and libGLX_mesa were verified separately because surfaceless EGL links the Gallium driver directly.

gdrv0 completed on both builds with Seccomp: 0, policy 0 and priority 120. This stock kernel has CONFIG_UCLAMP_TASK disabled. The stock compatibility check passed, and this run leaves the effective 0 → 512 transition unverified on J516s.

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