From 9d70d1b2b3a742d198aff945e28501f631431bf6 Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Thu, 23 Apr 2026 16:28:05 +0200 Subject: [PATCH] mesa/main: Add trace dispatch plumbing for MESA_VERBOSE=api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reintroduce VERBOSE_API as a bit that will cause a parallel trace dispatch table to be installed at context create. The table itself is populated in a follow-up commit. This commit only wires the TLS-publish indirection via _mesa_set_dispatch(..). With Trace NULL (the common case), the helper is equivalent to a plain _mesa_glapi_set_dispatch — no behaviour change yet. Signed-off-by: Christian Gmeiner Assisted-by: Claude --- src/mesa/main/context.c | 21 +++++++++++++++++++-- src/mesa/main/context.h | 3 +++ src/mesa/main/debug.c | 1 + src/mesa/main/dlist.c | 4 ++-- src/mesa/main/glthread.c | 4 ++-- src/mesa/main/mtypes.h | 4 ++++ src/mesa/main/robustness.c | 2 +- src/mesa/vbo/vbo_exec_api.c | 4 ++-- 8 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/mesa/main/context.c b/src/mesa/main/context.c index 9c0d7bd6a6e..5df84fdd37e 100644 --- a/src/mesa/main/context.c +++ b/src/mesa/main/context.c @@ -77,6 +77,7 @@ #include "util/glheader.h" +#include "util/u_thread.h" #include "accum.h" #include "arrayobj.h" @@ -860,7 +861,7 @@ _mesa_alloc_dispatch_tables(gl_api api, struct gl_dispatch *d, bool glthread) return false; } - d->Current = d->Exec = d->OutsideBeginEnd; + d->RealPublished = d->Current = d->Exec = d->OutsideBeginEnd; return true; } @@ -874,6 +875,22 @@ _mesa_free_dispatch_tables(struct gl_dispatch *d) free(d->ContextLost); } +void +_mesa_set_dispatch(struct gl_context *ctx, struct _glapi_table *t) +{ + /* On the glthread worker, the user-thread wrapper already logged the + * call; bypass Trace and don't touch RealPublished (main-thread state). + */ + if (ctx->GLThread.enabled && + u_thread_is_self(ctx->GLThread.queue.threads[0])) { + _mesa_glapi_set_dispatch(t); + return; + } + + ctx->Dispatch.RealPublished = t; + _mesa_glapi_set_dispatch(ctx->Dispatch.Trace ? ctx->Dispatch.Trace : t); +} + bool _mesa_initialize_dispatch_tables(struct gl_context *ctx) { @@ -1475,7 +1492,7 @@ _mesa_make_current( struct gl_context *newCtx, else { _mesa_glapi_set_context((void *) newCtx); assert(_mesa_get_current_context() == newCtx); - _mesa_glapi_set_dispatch(newCtx->GLApi); + _mesa_set_dispatch(newCtx, newCtx->GLApi); if (drawBuffer && readBuffer) { assert(_mesa_is_winsys_fbo(drawBuffer)); diff --git a/src/mesa/main/context.h b/src/mesa/main/context.h index e845146b66b..f51533615a7 100644 --- a/src/mesa/main/context.h +++ b/src/mesa/main/context.h @@ -91,6 +91,9 @@ _mesa_alloc_dispatch_tables(gl_api api, struct gl_dispatch *d, bool glthread); extern bool _mesa_initialize_dispatch_tables(struct gl_context *ctx); +extern void +_mesa_set_dispatch(struct gl_context *ctx, struct _glapi_table *t); + extern struct _glapi_table * _mesa_new_nop_table(bool glthread); diff --git a/src/mesa/main/debug.c b/src/mesa/main/debug.c index 83733a1879f..7df19b49928 100644 --- a/src/mesa/main/debug.c +++ b/src/mesa/main/debug.c @@ -103,6 +103,7 @@ set_verbose_flags(const char *str) static const struct option opts[] = { { "state", VERBOSE_STATE }, { "list", VERBOSE_DISPLAY_LIST }, + { "api", VERBOSE_API }, }; GLuint i; diff --git a/src/mesa/main/dlist.c b/src/mesa/main/dlist.c index b530767e0a1..8886df0a564 100644 --- a/src/mesa/main/dlist.c +++ b/src/mesa/main/dlist.c @@ -13204,7 +13204,7 @@ _mesa_NewList(GLuint name, GLenum mode) vbo_save_NewList(ctx, name, mode); ctx->Dispatch.Current = ctx->Dispatch.Save; - _mesa_glapi_set_dispatch(ctx->Dispatch.Current); + _mesa_set_dispatch(ctx, ctx->Dispatch.Current); if (!ctx->GLThread.enabled) { ctx->GLApi = ctx->Dispatch.Current; } @@ -13423,7 +13423,7 @@ _mesa_EndList(void) ctx->CompileFlag = GL_FALSE; ctx->Dispatch.Current = ctx->Dispatch.Exec; - _mesa_glapi_set_dispatch(ctx->Dispatch.Current); + _mesa_set_dispatch(ctx, ctx->Dispatch.Current); if (!ctx->GLThread.enabled) { ctx->GLApi = ctx->Dispatch.Current; } diff --git a/src/mesa/main/glthread.c b/src/mesa/main/glthread.c index a03af6736e5..95d102c6d2e 100644 --- a/src/mesa/main/glthread.c +++ b/src/mesa/main/glthread.c @@ -303,7 +303,7 @@ void _mesa_glthread_enable(struct gl_context *ctx) /* Update the dispatch only if the dispatch is current. */ if (GET_DISPATCH() == ctx->Dispatch.Current) { - _mesa_glapi_set_dispatch(ctx->GLApi); + _mesa_set_dispatch(ctx, ctx->GLApi); } } @@ -323,7 +323,7 @@ void _mesa_glthread_disable(struct gl_context *ctx) /* Update the dispatch only if the dispatch is current. */ if (GET_DISPATCH() == ctx->MarshalExec) { - _mesa_glapi_set_dispatch(ctx->GLApi); + _mesa_set_dispatch(ctx, ctx->GLApi); } /* Unbind VBOs in all VAOs that glthread bound for non-VBO vertex uploads diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h index 575603b6fcb..4a4d4da0017 100644 --- a/src/mesa/main/mtypes.h +++ b/src/mesa/main/mtypes.h @@ -3247,6 +3247,9 @@ struct gl_dispatch * - ContextLost */ struct _glapi_table *Current; + + struct _glapi_table *Trace; + struct _glapi_table *RealPublished; }; /** @@ -3672,6 +3675,7 @@ enum _verbose { VERBOSE_STATE = 0x0001, VERBOSE_DISPLAY_LIST = 0x0002, + VERBOSE_API = 0x0004, }; diff --git a/src/mesa/main/robustness.c b/src/mesa/main/robustness.c index 81745d4fc0a..01b3d7e8a6c 100644 --- a/src/mesa/main/robustness.c +++ b/src/mesa/main/robustness.c @@ -104,7 +104,7 @@ _mesa_set_context_lost_dispatch(struct gl_context *ctx) } ctx->Dispatch.Current = ctx->Dispatch.ContextLost; - _mesa_glapi_set_dispatch(ctx->Dispatch.Current); + _mesa_set_dispatch(ctx, ctx->Dispatch.Current); } /** diff --git a/src/mesa/vbo/vbo_exec_api.c b/src/mesa/vbo/vbo_exec_api.c index a5b6476be39..7791051ebe2 100644 --- a/src/mesa/vbo/vbo_exec_api.c +++ b/src/mesa/vbo/vbo_exec_api.c @@ -862,7 +862,7 @@ _mesa_Begin(GLenum mode) ctx->Dispatch.Current = ctx->Dispatch.Exec; } else if (ctx->GLApi == ctx->Dispatch.OutsideBeginEnd) { ctx->GLApi = ctx->Dispatch.Current = ctx->Dispatch.Exec; - _mesa_glapi_set_dispatch(ctx->GLApi); + _mesa_set_dispatch(ctx, ctx->GLApi); } else { assert(ctx->GLApi == ctx->Dispatch.Save); } @@ -925,7 +925,7 @@ _mesa_End(void) } else if (ctx->GLApi == ctx->Dispatch.BeginEnd || ctx->GLApi == ctx->Dispatch.HWSelectModeBeginEnd) { ctx->GLApi = ctx->Dispatch.Current = ctx->Dispatch.Exec; - _mesa_glapi_set_dispatch(ctx->GLApi); + _mesa_set_dispatch(ctx, ctx->GLApi); } if (exec->vtx.prim_count > 0) {