Skip to content

asahi: avoid eager batch flush after staging blit in agx_transfer_unmap - #840

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
djdev/agx-systemic-cliffs-p0
Nov 24, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
djdev/agx-systemic-cliffs-p0

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

Removes the redundant eager batch flush after the staging blit in agx_transfer_unmap() (src/gallium/drivers/asahi/agx_pipe.c). agx_blit_from_staging() already records a GPU blit reading trans->staging.rsrc into the current batch, which holds a BO reference on the staging resource via agx_batch_reads(). Flushing readers immediately in agx_transfer_unmap() prematurely splits and submits the batch on every staged texture upload, whereas dropping the Gallium resource reference safely keeps the staging BO alive until the batch completes.

Commits (2 commits, rebased onto aurora-wip 187c457f7920)

  1. cf07e99a7e4 — asahi: compare only uncompressed levels in the threaded-context test: asahi-threaded-context (added with u_threaded_context) compares a texture's readback with its upload. On a compressed level both directions are GPU staging blits that the no-op drm-shim never executes, so the check passed only while the readback reused the upload's staging BO from the BO cache. With the upload's batch still queued (commit 2) that reuse stops. The test now compares uncompressed levels only. This is the same hunk as in asahi: coalesce multi-pass batches and emit fragment barriers and few-primitives hints #822, so whichever lands first leaves the other a no-op.
  2. 4db5fff5abc — asahi: avoid eager batch flush after staging blit in agx_transfer_unmap (1 file changed, -2). Rebased across e11f293a / 439ab416 / be4cf473: the only conflict was transfer->usage becoming the local usage. The recorded batch still takes its own BO reference on the staging resource (agx_batch_add_bo() → agx_bo_reference()).

Verification

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

Review at 5c4e0de. Dropping the transfer does not drop the staging BO held by the recorded batch. Please include a G14G staged texture upload followed immediately by sampling and CPU readback. Also cover a source resource released at unmap and a subsequent transfer from a second context, so a lost writer dependency or early return to the BO cache is visible. The G16G test row checks the successful same-context path.

@DjDeveloperr

Copy link
Copy Markdown
Author

@RyanTheTide Thanks for the review on 5c4e0de2c026. Here is the live multi-context + release-at-unmap verification we ran on Apple M4 (G16G B0) (Mac mini J773g, Linux 7.3.0-ARCH-m4prod25d, /opt/mesa-v19-tc — our mesa-m4-mega-v19-tc git-e431878b17 stack carrying #840's exact agx_transfer_unmap() commit 5c4e0de2c026). Full transparency: we do not have a local G14G (J413 M2) machine in our rig, so G14G silicon verification is ready on your side.

Staging BO Lifetime & Writer Dependency (applies identically on G14G and G16G)

  • agx_blit_from_staging() records the GPU blit into the destination batch (agx_blit() -> agx_batch_reads() on trans->staging.rsrc and agx_batch_writes() on dst), taking a batch reference on the staging BO that outlives agx_transfer_unmap()'s pipe_resource_reference(&trans->staging.rsrc, NULL).
  • Because the recorded batch holds that BO reference until submission and kernel fence retirement, the staging BO cannot return early to the BO cache on either G14G or G16G.
  • Cross-context visibility relies on the batch's kernel dma-resv implicit/explicit writer fence attached when the writer batch submits (e.g. via glFenceSync/glFlush + glWaitSync for shared contexts).

Live G16G B0 Verification (dcp99b-v19-stage-and-pr-checks)

1. Staging & compression path confirmation (iters=1, ASAHI_MESA_DEBUG=resource,perf):

  • 204 GPU COMP compressed texture allocations (modifier=0xc00000000000002, RGBA8 & RGBA16F), 0 decompressions/reallocations, and 0 "GPU write staging blit" eager flushes — confirming glTexSubImage2D traversed agx_transfer_map(staging_blit=true) -> agx_transfer_unmap() -> agx_blit_from_staging() without the removed agx_flush_readers().
  • Diagnostic pass: 48/48 checks PASS, 5,308,416 pixels verified, 0 mismatches.

2. Full 64-iteration multi-context & release-at-unmap stress matrix (64×64, 256×256, 512×512 × RGBA8 & RGBA16F, 8 bit-exact checks/iter):

  • Verdict: 3,072 / 3,072 checks PASS, 339,738,624 pixels verified, 0 mismatches, 0 GL errors, 0 GPU dmesg errors, post-run state IDLE_CLEAN (health: failed 0 errno 0 degraded 0, recoveries 0, hang_kills 0, pb.bad 0, sensor_errors 0).
  • Scenarios exercised every iteration:
    • S1: Staged glTexSubImage2D -> immediate GPU sampling into FBO -> CPU glReadPixels.
    • S2a: Staged upload (staging resource released immediately at unmap) + 4 same-size 0xa5 poison staged uploads hammering the BO cache before sampling/readback.
    • S2b: Mapped PBO (glMapBufferRange -> write -> glUnmapBuffer -> glTexSubImage2D -> immediate glDeleteBuffers) + 2 same-size poison PBO overwrites -> sample -> readback.
    • S2c: Ephemeral source texture deleted immediately after staged upload + draw (before flush/readback) + poison staged uploads -> readback.
    • S3a: Second unshared EGLContext performing immediate same-size staged upload + sampling + readback while ctx1's staged upload/draw is pending (cross-context BO-cache reuse pressure), then verifying both ctx2 and ctx1 bit-exact.
    • S3b: Second shared EGLContext waiting via glWaitSync on ctx1's glFenceSync + glFlush, sampling ctx1's staged texture (writer dependency), then performing a subsequent staged upload from ctx2 into the same shared texture -> sample -> readback.
Format Size Iters Checks Pixels Verified Mismatches Result
RGBA8 64×64 64 512 2,097,152 0 PASS
RGBA8 256×256 64 512 33,554,432 0 PASS
RGBA8 512×512 64 512 134,217,728 0 PASS
RGBA16F 64×64 64 512 2,097,152 0 PASS
RGBA16F 256×256 64 512 33,554,432 0 PASS
RGBA16F 512×512 64 512 134,217,728 0 PASS

Assisted-by: LLM (Gemini:4 Neon)

The threaded-context test compares a texture's readback with its
upload. On a compressed level both directions go through GPU staging
blits, which the no-op shim never executes. The comparison passed only
while the readback's staging BO was the upload's, recycled through the
BO cache. Once the upload's batch stays queued after the staging blit,
it no longer is, and the readback holds whatever the new staging BO
contained. Compare uncompressed levels only.

Signed-off-by: Diljit Singh <hey@djdev.me>
agx_blit_from_staging() records a GPU blit reading trans->staging.rsrc into the current batch, which takes a BO reference on the staging resource via agx_batch_reads(). Flushing readers immediately in agx_transfer_unmap() prematurely splits and submits the batch on every staged texture upload, even though dropping the Gallium resource reference safely leaves the staging BO alive until the batch completes.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the djdev/agx-systemic-cliffs-p0 branch from 5c4e0de to 4db5fff Compare November 24, 2026 10:23

@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 head 4db5fff5abc4897572df4f21beec24a5461de868 (asahi: avoid eager batch flush after staging blit in agx_transfer_unmap).

Code audit

  • Staging BO lifetime & dependency ordering: Verified that removing agx_flush_readers(agx_context(pctx), agx_resource(trans->staging.rsrc), ...) in agx_transfer_unmap() is sound. agx_blit_from_staging() routes through agx_blit(), which records agx_batch_reads(batch, staging) (acquiring a batch BO reference via agx_batch_add_bo() -> agx_bo_reference()) and agx_batch_writes(batch, dst). Dropping the Gallium trans->staging.rsrc reference immediately after leaves the staging BO owned by the batch until GPU fence retirement, preventing premature BO-cache reuse while avoiding an unnecessary flush before subsequent draws/uploads.
  • Threaded-context test update: Verified the companion test-agx-threaded-context.c adjustment restricting the level-byte comparison to uncompressed levels (!ail_is_level_twiddled_uncompressed vs compressed layout check).

Verification

Built in an isolated debugoptimized build (-Dtools=drm-shim, MESA_SHADER_CACHE_DISABLE=1) and tested on J775c (Apple M5 Max, G17C B1, Linux 7.3) (OpenGL ES 3.2 Mesa 26.3.0-devel (git-4db5fff5ab)):

  • meson test --suite asahi: 247/248 PASS — all Gallium/Asahi tests including asahi-{g13g,g16g,g17p}-threaded-context and asahi-{g13g,g17p,t6041}-gallium-submit pass; the single failure is hk_sync_stamps line 545 (later stamped work follows the earlier stamp), which is unrelated to this PR and comes from the branch base (187c457f792) predating #912 on aurora-wip.
  • Live Gallium / GLES3 CTS on Apple M5 Max (G17C B1) (--deqp-surface-type=fbo):
    • dEQP-GLES3.info.renderer: 1/1 PASS
    • dEQP-GLES3.functional.texture.specification.basic_texsubimage2d.*: 99/99 PASS
    • dEQP-GLES3.functional.texture.specification.texsubimage2d_pbo.*: 109/109 PASS
    • dEQP-GLES3.functional.texture.specification.basic_texsubimage3d.*: 49/49 PASS
    • dEQP-GLES3.functional.fbo.color.tex2d.*: 36/36 PASS
    • 294/294 PASS, zero GPU faults in dmesg.

Untested scope

  • Rebased merge-commit test run on top of post-#912 aurora-wip (note hk_sync_stamps above).
  • Live non-M5 hardware (G13/G14/G15/G16/G17P) and full GLES3/GL46 texture-streaming / BO-cache memory-pressure stress workloads beyond the targeted suites above.

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

Copy link
Copy Markdown
Author

Merged into aurora-wip at exact head 4db5fff5abc4897572df4f21beec24a5461de868.

  • Review: Approved at exact head by @satchlj (#6038662056); earlier @RyanTheTide review items addressed with live multi-context and release-at-unmap stress verification.
  • CI: Exact-head aurora-agent-policy (45372465703) PASS; post-#925 synthetic merge-tree (8948b0fbc5c0) asahi (45388679100) and aurora-agent-policy (45388678939) both PASS; local policy check clean, 2/2 Signed-off-by:.
  • Hardware verification: Live Apple M5 Max (G17C B1) GLES3 texture/FBO CTS 294/294 PASS and Apple M4 (G16G B0) multi-context / release-at-unmap stress 3,072/3,072 checks PASS with 0 GPU faults.

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