Repository navigation
asahi: enable Gallium u_threaded_context with async fence sync - #689
Conversation
|
Thanks, this is a large and useful change. I found a few threading and fence issues against the TC contract that should be fixed before it lands.
Assisted-by: LLM (Claude:Opus 5.5) |
|
@RyanTheTide Thanks for the detailed review — all five code issues are fixed in
Host verification (
We will follow up on this PR with the requested full hardware test/benchmark rows (piglit, dEQP-GLES3/GLES31, glmark2, and multi-context/multi-threaded Assisted-by: LLM (Gemini:4 Neon) |
d2583c6 to
26f6ce1
Compare
satchlj
left a comment
There was a problem hiding this comment.
Gemini 4 Neon on @satchlj's behalf:
Reviewed exact head 26f6ce152e34973a169af8cb767c77e52b585e43 via source audit, isolated meson test --suite asahi (with -Dtools=drm-shim), and live A/B deqp-gles3 runs on J775c (M5 Max / G17C B1, MESA_SHADER_CACHE_DISABLE=1, kernel 7.3.0-lobata-j775c+).
Prior review follow-ups verified
- All 5 prior review items are addressed cleanly in
26f6ce152e34973a169af8cb767c77e52b585e43:PIPE_BUFFERshadowing disabled under TC, faulted pre-allocated fence status semantics restored, lockedquery_state[]replacingthreaded_context_unwrap_sync()inagx_get_query_result(), mutex-guardedvalid_buffer_range, andsubmit_quarantinedteardown inagx_destroy_context(). meson test --suite asahi: 240/240 PASS (includingasahi-g13g-threaded-context,asahi-g16g-threaded-context, andasahi-g17p-threaded-context).- Live J775c A/B
deqp-gles3passes:dEQP-GLES3.functional.fence_sync.*(11/11 PASS),dEQP-GLES3.functional.buffer.map.write.*(176/176 PASS),dEQP-GLES3.functional.occlusion_query.*(66/66 PASS),dmesg: 0 GPU faults/errors.
Blocking bug: pctx->generate_mipmap violates u_threaded_context's contract (SIGABRT on debug/debugoptimized, silent mipmap drop on release)
tc_generate_mipmap() (src/gallium/auxiliary/util/u_threaded_context.c:4829-4860) only checks screen->is_format_supported(..., PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL) and returns true to st_generate_mipmap() (suppressing the state-tracker u_gen_mipmap fallback), then runs pipe->generate_mipmap() asynchronously in tc_call_generate_mipmap() (u_threaded_context.c:4815-4825):
ASSERTED bool result = pipe->generate_mipmap(pipe, p->res, p->format, ...);
assert(result);In src/gallium/drivers/asahi/agx_pipe.c:2487-2490, Asahi hooks pctx->generate_mipmap on both M5 (agx_generate_mipmap_g17g) and native AGX3 (agx_generate_mipmap_agx3), and both return false to request a fallback:
agx_generate_mipmap_g17g()(src/gallium/drivers/asahi/agx_blit.c:1075-1088) returnsfalsefor every renderable 2D texture (if (renderable) return false;), as well as depth/stencil, MSAA, or unsupported pack/unpack formats.agx_generate_mipmap_agx3()(src/gallium/drivers/asahi/agx_blit.c:1260-1266) returnsfalsewhenever!agx_generate_mipmap_agx3_supported(...)(e.g., non-UNORM8x4 formats such asR8/RG8/R16F/RGBA16F/SRGB, 1D/3D/MSAA/linear/compressed targets) or if a precompiled mipmap kernel is missing.
Under u_threaded_context, any glGenerateMipmap() hitting those fallback paths aborts with tc_call_generate_mipmap: Assertion 'result' failed on debug/debugoptimized builds and silently skips mipmap generation on NDEBUG release builds (affecting both M5 and M4/G16G/G17P).
Live J775c A/B reproducer (git-26f6ce152e, MESA_SHADER_CACHE_DISABLE=1)
dEQP-GLES3.functional.pbo.*:GALLIUM_THREAD=0: 56/56 PASS- Default TC (
#689):SIGABRTondEQP-GLES3.functional.pbo.upload.split_upload_last_line:
deqp-gles3: ../src/gallium/auxiliary/util/u_threaded_context.c:4823: tc_call_generate_mipmap: Assertion 'result' failed.
- Standalone A/B:
dEQP-GLES3.functional.pbo.upload.split_upload_last_line: TC =SIGABRT,GALLIUM_THREAD=0= PASS (1/1)dEQP-GLES3.functional.texture.mipmap.2d.generate.a8_fastest: TC =SIGABRT,GALLIUM_THREAD=0= PASS (1/1)
Suggested fix
Handle the util_gen_mipmap() fallback inside agx_generate_mipmap_g17g() and agx_generate_mipmap_agx3() (matching other TC-enabled Gallium drivers like panfrost/v3d that never return false once tc_generate_mipmap() has accepted the call), or avoid returning false from pctx->generate_mipmap for any format/target that tc_generate_mipmap() accepts, and add a test-agx-threaded-context regression case calling pctx->generate_mipmap() on a fallback-eligible 2D texture.
Untested scope
- Live hardware testing was performed on J775c (M5 Max / G17C B1); G13/G14/G15/G16G/G17P hardware paths were verified via
drm-shimunit tests and source inspection only.
Assisted-by: LLM (Google:Gemini 4 Neon)
When no deferred server-wait fence is pending (the common path for glFenceSync()), agx_fence_create() previously exported ctx->syncobj to a sync_file fd, created a new syncobj, imported the sync_file fd into it, and closed the fd (4 syscalls plus kernel sync_file allocation and fd churn per fence). Snapshot ctx->syncobj directly into the new syncobj with drmSyncobjTransfer() (point 0 binary-to-binary transfer) and leave sync_file export lazy in agx_fence_get_fd(). Fall back to the sync_file path if drmSyncobjTransfer() returns EOPNOTSUPP/ENOSYS or when ctx->in_sync_fd >= 0 requires merging a pending server wait. Signed-off-by: Diljit Singh <hey@djdev.me>
Wire Gallium u_threaded_context into the Asahi driver so OpenGL draw recording runs on the application thread while state validation, command encoding, and kernel submission execute asynchronously on the driver worker thread. Handle asynchronous threaded-context flushes cleanly by checking TC_FLUSH_ASYNC when populating pre-allocated threaded-context fences: tc_flush() strips PIPE_FLUSH_ASYNC when forwarding async flushes (such as glClientWaitSync() / glFlush() + fence creation) to the driver callback, so checking only PIPE_FLUSH_ASYNC caused agx_flush() to leave the unflushed fence's util_queue_fence unsignalled and deadlock both the application thread and driver worker. Preserve threaded-context safety invariants across the driver: - Disable driver-side PIPE_BUFFER shadowing when ctx->tc is active so app-thread TC_TRANSFER_MAP_THREADED_UNSYNC maps never race a driver-thread BO swap (buffer renaming under TC goes through tc_invalidate_buffer / replace_buffer_storage; busy DISCARD_RANGE writes stage via stream_uploader). - Keep pre-TC NULL fence semantics for faulted pre-allocated fences: agx_fence_finish() returns true so glClientWaitSync() completes, agx_fence_server_sync() is a no-op without faulting the waiting context, and context reset stays on the faulting context. - Avoid draining the TC queue in agx_get_query_result() for flushed queries by polling/waiting against a query_lock-protected per-batch query_state snapshot (and folding unretired batch timestamps under query_lock). - Always guard rsrc->valid_buffer_range reads and writes with write_mutex so PIPE_MAP_THREAD_SAFE unmaps cannot race driver-thread updates even when screen->num_contexts == 1. - Tear down transfer slabs and context mutexes on the submit_quarantined early-return path in agx_destroy_context(). Include unit tests covering threaded-context fence population, TC_FLUSH_ASYNC, faulted fence semantics, no-shadow under TC, flushed query polling without TC drain, and locked valid_buffer_range updates in src/gallium/drivers/asahi/tests/test-agx-threaded-context.c. Signed-off-by: Diljit Singh <hey@djdev.me>
Under u_threaded_context, tc_generate_mipmap() accepts any format the driver renders to and reports success to the state tracker, which then skips its own util_gen_mipmap() fallback. The driver call runs later in tc_call_generate_mipmap(), which asserts its result. agx_generate_mipmap_g17g() and agx_generate_mipmap_agx3() return false for everything their native paths do not take: every renderable 2D texture on G17G, and every format but UNORM8x4 or any non-2D, MSAA, linear or compressed layout on native AGX3. With TC enabled, such a glGenerateMipmap() aborts debug builds and leaves the levels ungenerated in release builds, for example in dEQP-GLES3.functional.pbo.upload.split_upload_last_line and dEQP-GLES3.functional.texture.mipmap.2d.generate.a8_fastest. Install a single agx_generate_mipmap() hook that tries the native path and otherwise generates the levels with util_gen_mipmap(), as panfrost does. That is what st_generate_mipmap() did without TC, so contexts without TC see no change. util_gen_mipmap() additionally requires sampler-view support, which every format Asahi renders to has. Extend the threaded-context test to generate mipmaps through TC and through the driver hook for formats and targets the native paths decline. Signed-off-by: Diljit Singh <hey@djdev.me>
26f6ce1 to
be4cf47
Compare
|
Thanks @satchlj. You were right about the Fix: new commit
Regression test:
CI on the new head (fork
Stacked #894 is cascade-rebased with no content change, to Not re-run on hardware here. The two dEQP reproducers on J775c would be the confirming run. Assisted-by: LLM (Claude Opus 5.5) |
satchlj
left a comment
There was a problem hiding this comment.
Gemini 4 Neon on @satchlj's behalf:
Re-reviewed exact head be4cf4732be7a7518364504b26d18b0648bf9f05 (asahi: generate mipmaps the native paths decline in the driver). The u_threaded_context generate_mipmap blocker from the prior review is cleanly fixed.
Source Audit
agx_generate_mipmap()(src/gallium/drivers/asahi/agx_blit.c) tries the chip-appropriate native path (agx_generate_mipmap_g17g()on M5 /agx_generate_mipmap_agx3()on native AGX3) and falls back in-driver toutil_gen_mipmap(pctx, ..., PIPE_TEX_FILTER_LINEAR)whenever the native path declines, matchingpanfrostand upholdingtc_call_generate_mipmap()'s non-failure contract.- Verified format-support consistency across
tc_generate_mipmap(),agx_is_format_supported(),ail_pixel_format[], andutil_gen_mipmap(): every format accepted byagx_is_format_supported()forPIPE_BIND_RENDER_TARGETorPIPE_BIND_DEPTH_STENCILalso satisfiesPIPE_BIND_SAMPLER_VIEW, soutil_gen_mipmap()cannot returnfalsefor a format/target thattc_generate_mipmap()accepted. test_generate_mipmap()intest-agx-threaded-context.cexercises both the TC path and the unwrapped driver hook (forNDEBUGcoverage) across 2D, 2D-array, 3D, and non-UNORM8x4 formats (A8_UNORM,R8_UNORM,R8G8_UNORM,R8G8B8A8_SRGB,R16G16B16A16_FLOAT).
Verification (J775c, Apple M5 Max, G17C B1, kernel 7.3.0-lobata-j775c+, isolated build, MESA_SHADER_CACHE_DISABLE=1)
- Host unit tests (
meson test --suite asahi): 243/243 PASS (includingasahi-g13g-threaded-context,asahi-g16g-threaded-context,asahi-g17p-threaded-context, andasahi-g17p-async-fence). - Live GLES3 with default
u_threaded_contextenabled (Apple M5 Max (G17C B1)):dEQP-GLES3.functional.pbo.upload.split_upload_last_line: 1/1 PASS (previously aborted intc_call_generate_mipmap)dEQP-GLES3.functional.texture.mipmap.2d.generate.*: 48/48 PASS (includinga8_fastest)dEQP-GLES3.functional.texture.mipmap.cube.generate.*: 16/16 PASSdEQP-GLES3.functional.pbo.*: 56/56 PASSdEQP-GLES3.functional.fence_sync.*: 11/11 PASS
- Kernel log (
dmesg): 0 GPU faults or errors.
Untested scope
- Live non-M5 (
G13G/G16G/G17P) hardware execution and full GLES3/GLES3.1 CTS / Zink-over-Gallium soak were not run here (non-M5 chips were covered only via the no-op DRM-shimasahi-{g13g,g16g,g17p}-threaded-contextunit tests).
Assisted-by: LLM (Google:Gemini 4 Neon)
|
Merged into
Assisted-by: LLM (Google:Gemini 4 Neon) |
Summary
Enable Gallium
u_threaded_contextfor Asahi graphics contexts so OpenGL command recording runs on the application thread while state validation, descriptor encoding, and kernel submission execute asynchronously on the driver worker thread.Commits
asahi: snapshot syncobj with drmSyncobjTransfer in agx_fence_create(d0959fcb36e): Snapshotctx->syncobjdirectly into a fresh syncobj viaDRM_IOCTL_SYNCOBJ_TRANSFERwhen no deferred server-wait fence is pending, avoiding per-fencesync_fileallocation and fd churn (with fallback when unsupported or whenin_sync_fd >= 0) and keeping fence creation testable underdrm-shim.asahi: enable Gallium u_threaded_context with async fence sync(ae33480a711):threaded_context_create()for graphics contexts requestingPIPE_CONTEXT_PREFER_THREADED(leaving compute-only contexts unwrapped), implementagx_replace_buffer_storage(), slab-backed transfer pools, thread-safe resource/query/streamout/XFB paths, and deferredagx_legalize_compression()inagx_set_sampler_views().tc_flush()forwards asynchronous/deferred flushes (e.g.glFenceSync()+glClientWaitSync()/glFinish()) to the driver callback,u_threaded_contextstripsPIPE_FLUSH_ASYNCand passesTC_FLUSH_ASYNCalongside the pre-allocated unflushed fence. Checking onlyPIPE_FLUSH_ASYNCcausedagx_flush()to replace*fencewithout populating/signalling the pre-allocated fence'sutil_queue_fence, deadlocking the application thread and driver worker. CheckingTC_FLUSH_ASYNCand populating*fenceviaagx_fence_populate()(without unsafef->ctxdereferences) resolves the deadlock cleanly.src/gallium/drivers/asahi/tests/test-agx-threaded-context.ccovering TC wrapping, buffer replacement, unsync/thread-safe transfers, occlusion queries, and async/deferred fence flushes acrossg13g,g16g, andg17p.Verification
Host unit tests (
origin/aurora-wipstandalone build)27/27targeted Asahi unit tests PASS (0 Fail):asahi-g13g-threaded-context,asahi-g16g-threaded-context,asahi-g17p-threaded-contextasahi-priority-{allow,deny,once,shared}asahi-g17p-submit-retirement-*(13/13)asahi-g17p-gallium-{vs,query,submit,xfb-program,vertex-output-shapes,blit,blit-encode}Live hardware verification (M4
G16G B0)gles-smoke: 3086/3109 Pass (0 Fail / 0 Crash), includingdEQP-GLES3.functional.fence_sync.client_wait_sync_finishPass.1080p Fancy RD 12 VSync-off): 174.4 FPS,1%low+9.5% (81.2 -> 88.9 FPS), render-thread CPU (rcpu) -54% to -70%,0deadlocks.Assisted-by: LLM (Gemini:4 Neon)