diff options
| author | Arthur Hanson Rasmusson <arthur@vgpu.io> | 2026-11-16 09:50:31 -0500 |
|---|---|---|
| committer | Arthur Hanson Rasmusson <arthur@vgpu.io> | 2026-11-16 09:50:31 -0500 |
| commit | ed8a9c6b8e061fe772fc0050ab83c2dec2db8d1e (patch) | |
| tree | 458d2ea063d8a8a6996cd15891e3242cfe93ece5 /tools/guest-diag/sdl_cap.c | |
| parent | f2c73f4e52d24188a9edb0e8190c50f87f19181f (diff) | |
| download | open-iov-ed8a9c6b8e061fe772fc0050ab83c2dec2db8d1e.tar.gz open-iov-ed8a9c6b8e061fe772fc0050ab83c2dec2db8d1e.zip | |
vgsp: eng89 - live-derived GS map, draw-move anchor, raster enables, async capture
Kernel (deterministic fixes, each with an A/B knob):
- sema_gl_sph_gs_maps=6: derive the whole 216-bit GS IMAP/OMAP blocks
from the bound VTG image's OMAP and the PS image's IMAP at authoring
time (SPH-GS-MAP receipt; SPH-GS-SKIP names ps-invalid/gs-invalid/
vtg-overlap/vtg-short). maps=4 stays as the fallback.
- DS-MAP census line: dump stage-4/5 w5/w10/w13/w17 next to DS-IMAGE so
the fetched maps are visible without a new probe.
- MME pack carrier: reject maxa > 4096 (the IR pack array) instead of
64K, with MME-PACK-REJECT reason=bound.
- MME-DRAW-MOVE: anchor the slot-34 move after a 0x3810 VERTEX_STREAM
run. c848 moved CALL(34) to 0x204, ahead of the prepended stream bind,
so the 8x3 fetch used stale stream state.
- sema_gl_raster_enable=1: emit SET_RASTER_ENABLE=1 and
SET_GLOBAL_RENDER_ENABLE MODE_TRUE in the raster prelude (fresh replay
contexts may carry 0 for both, discarding every fragment).
Guest tooling (unblocks the render loop and capture):
- sdl_cap.c: asynchronous PBO+fence capture with a 5 s abandonment
bound; no glFinish/blocking readback (c899 wedged gzdoom in an
uninterruptible nv ioctl); clean sentinel from a destructor.
- gz_pbuf.c: request depth 24/stencil 8, log GL_DEPTH_BITS, and swap
with glFlush only.
- mission-gz.sh: gl_frames probe off by default; GZDoom launched
detached with a bounded wait, per-attempt X restart and retry;
capture in /dev/shm with GZCAP_MAXF.
- gzdoom_pbuf_run.sh: gl_pipeline_depth=2, gl_finishbeforeswap=0,
GZCAP_MAXF default, rc-file support for the detached run.
- run_cycles_long.sh: durable multi-cycle runner (the previous one lived
in /tmp and was lost on reboot); pushes tools, runs the mission, grabs
dmesg + guest logs + capture per cycle, stops on pixels.
Test matrix: params-r1624-maps{4,6}.txt.
Diffstat (limited to 'tools/guest-diag/sdl_cap.c')
| -rw-r--r-- | tools/guest-diag/sdl_cap.c | 216 |
1 files changed, 198 insertions, 18 deletions
diff --git a/tools/guest-diag/sdl_cap.c b/tools/guest-diag/sdl_cap.c index f875ed0..84aa35b 100644 --- a/tools/guest-diag/sdl_cap.c +++ b/tools/guest-diag/sdl_cap.c @@ -58,6 +58,20 @@ static int (*real_GetRendererOutputSize)(SDL_Renderer *, int *, int *); static void (*real_GL_SwapWindow)(SDL_Window *); static int (*real_GL_GetDrawableSize)(SDL_Window *, int *, int *); static void (*real_glReadPixels)(int, int, int, int, unsigned, unsigned, void *); +static void (*real_glFinish)(void); +static void (*real_glFlush)(void); +static void (*real_glBindFramebuffer)(unsigned, unsigned); +static void (*real_glReadBuffer)(unsigned); +static unsigned (*real_glGetError)(void); +static void (*real_glGenBuffers)(int, unsigned *); +static void (*real_glBindBuffer)(unsigned, unsigned); +static void (*real_glBufferData)(unsigned, long, const void *, unsigned); +static void *(*real_glMapBufferRange)(unsigned, long, long, unsigned); +static unsigned char (*real_glUnmapBuffer)(unsigned); +static void (*real_glDeleteBuffers)(int, const unsigned *); +static void *(*real_glFenceSync)(unsigned, unsigned); +static unsigned (*real_glClientWaitSync)(void *, unsigned, unsigned long long); +static void (*real_glDeleteSync)(void *); static FILE *cap_fp; static unsigned long long cap_count, cap_max = 0; @@ -82,6 +96,20 @@ __attribute__((constructor)) static void cap_init(void) real_GL_SwapWindow = dlsym(RTLD_NEXT, "SDL_GL_SwapWindow"); real_GL_GetDrawableSize = dlsym(RTLD_NEXT, "SDL_GL_GetDrawableSize"); real_glReadPixels = dlsym(RTLD_NEXT, "glReadPixels"); + real_glFinish = dlsym(RTLD_NEXT, "glFinish"); + real_glBindFramebuffer = dlsym(RTLD_NEXT, "glBindFramebuffer"); + real_glReadBuffer = dlsym(RTLD_NEXT, "glReadBuffer"); + real_glGetError = dlsym(RTLD_NEXT, "glGetError"); + real_glFlush = dlsym(RTLD_NEXT, "glFlush"); + real_glGenBuffers = dlsym(RTLD_NEXT, "glGenBuffers"); + real_glBindBuffer = dlsym(RTLD_NEXT, "glBindBuffer"); + real_glBufferData = dlsym(RTLD_NEXT, "glBufferData"); + real_glMapBufferRange = dlsym(RTLD_NEXT, "glMapBufferRange"); + real_glUnmapBuffer = dlsym(RTLD_NEXT, "glUnmapBuffer"); + real_glDeleteBuffers = dlsym(RTLD_NEXT, "glDeleteBuffers"); + real_glFenceSync = dlsym(RTLD_NEXT, "glFenceSync"); + real_glClientWaitSync = dlsym(RTLD_NEXT, "glClientWaitSync"); + real_glDeleteSync = dlsym(RTLD_NEXT, "glDeleteSync"); if (!real_glReadPixels) real_glReadPixels = dlsym(RTLD_DEFAULT, "glReadPixels"); if (!real_glReadPixels) { @@ -89,8 +117,23 @@ __attribute__((constructor)) static void cap_init(void) if (!h) h = dlopen("libGLESv2.so.2", RTLD_NOW | RTLD_GLOBAL); - if (h) + if (h) { real_glReadPixels = dlsym(h, "glReadPixels"); + real_glFinish = dlsym(h, "glFinish"); + real_glBindFramebuffer = dlsym(h, "glBindFramebuffer"); + real_glReadBuffer = dlsym(h, "glReadBuffer"); + real_glGetError = dlsym(h, "glGetError"); + real_glFlush = dlsym(h, "glFlush"); + real_glGenBuffers = dlsym(h, "glGenBuffers"); + real_glBindBuffer = dlsym(h, "glBindBuffer"); + real_glBufferData = dlsym(h, "glBufferData"); + real_glMapBufferRange = dlsym(h, "glMapBufferRange"); + real_glUnmapBuffer = dlsym(h, "glUnmapBuffer"); + real_glDeleteBuffers = dlsym(h, "glDeleteBuffers"); + real_glFenceSync = dlsym(h, "glFenceSync"); + real_glClientWaitSync = dlsym(h, "glClientWaitSync"); + real_glDeleteSync = dlsym(h, "glDeleteSync"); + } } fprintf(stderr, "[gzcap] init pid=%d path=%s present=%p updwin=%p swap=%p read=%p gros=%p readpix=%p\n", (int)getpid(), p, (void *)real_RenderPresent, (void *)real_UpdateWindowSurface, @@ -107,6 +150,26 @@ __attribute__((constructor)) static void cap_init(void) fflush(cap_fp); } +__attribute__((destructor)) static void cap_fini(void) +{ + uint64_t endseq = 0xFFFFFFFFFFFFFFFFull; + + if (cap_fp && !cap_stop) { + char m[4] = { 'G', 'Z', 'F', '2' }; + + fwrite(m, 1, 4, cap_fp); + fwrite(&endseq, 8, 1, cap_fp); + fflush(cap_fp); + cap_stop = 1; + fprintf(stderr, "[gzcap] final frames=%llu (clean sentinel)\n", + cap_count); + } + if (cap_fp) { + fclose(cap_fp); + cap_fp = NULL; + } +} + static void cap_record(const void *px, int w, int h, int stride, int fmt) { struct timespec ts; @@ -192,47 +255,164 @@ int SDL_UpdateWindowSurface(SDL_Window *win) return 0; } +/* + * 2026-11-16 (c899): the synchronous glFinish + glReadPixels path is what + * wedged GZDoom: the emulated GPU's work does not retire, the guest process + * sits in an uninterruptible nv ioctl and even SIGKILL cannot reap it, so the + * mission bash never finishes. Capture is now fully asynchronous: each swap + * queues a glReadPixels into a PBO and a fence; the NEXT swap polls the fence + * with a zero timeout and only maps/records when it has signalled. A fence + * that never signals is abandoned after CAP_FENCE_MS and the PBO is retired + * (GL defers deletion until pending work completes), so GZDoom's render loop + * keeps running and frames are captured as soon as the host GPU path starts + * retiring work. + */ +static unsigned cap_pbo; +static void *cap_sync; +static int cap_pw, cap_ph; +static unsigned long long cap_sync_ns; + +#define CAP_FENCE_MS 5000 +#define GL_PIXEL_PACK_BUFFER 0x88EB +#define GL_PIXEL_PACK_BUFFER_BINDING 0x88EF +#define GL_STREAM_READ 0x88E1 +#define GL_MAP_READ_BIT 0x0001 +#define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117 +#define GL_ALREADY_SIGNALED 0x911A +#define GL_CONDITION_SATISFIED 0x911C +#define GL_SYNC_FLUSH_COMMANDS_BIT 0x0001 + +static unsigned long long now_ms(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return (unsigned long long)ts.tv_sec * 1000ull + ts.tv_nsec / 1000000ull; +} + +static void cap_try_harvest(void) +{ + unsigned st; + int prev = 0; + void *p; + + if (!cap_sync) + return; + st = real_glClientWaitSync(cap_sync, 0, 0); + if (st == GL_ALREADY_SIGNALED || st == GL_CONDITION_SATISFIED) { + real_glBindBuffer(GL_PIXEL_PACK_BUFFER, cap_pbo); + p = real_glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, + (long)cap_pw * 4 * cap_ph, + GL_MAP_READ_BIT); + if (p) { + unsigned char *tmp = malloc((size_t)cap_pw * 4 * cap_ph); + + if (tmp) { + int y; + + for (y = 0; y < cap_ph; y++) + memcpy(tmp + (size_t)y * cap_pw * 4, + (unsigned char *)p + + (size_t)(cap_ph - 1 - y) * cap_pw * 4, + (size_t)cap_pw * 4); + cap_record(tmp, cap_pw, cap_ph, cap_pw * 4, GZF2_FMT_RGBA); + free(tmp); + } + real_glUnmapBuffer(GL_PIXEL_PACK_BUFFER); + } + real_glBindBuffer(GL_PIXEL_PACK_BUFFER, prev); + real_glDeleteSync(cap_sync); + cap_sync = NULL; + } else if (now_ms() - cap_sync_ns > CAP_FENCE_MS) { + static int given; + + real_glDeleteSync(cap_sync); + cap_sync = NULL; + if (!given++) + fprintf(stderr, "[gzcap] fence abandoned after %d ms (GPU work not retiring); capture continues asynchronously\n", + CAP_FENCE_MS); + } +} + void SDL_GL_SwapWindow(SDL_Window *win) { static int hit; - static unsigned char *gbuf; - static size_t gbufsz; + static unsigned long long hook_ms; if (!hit++) - fprintf(stderr, "[gzcap] hook GLSwapWindow pid=%d fp=%p read=%p gros=%p\n", + fprintf(stderr, "[gzcap] hook GLSwapWindow pid=%d fp=%p read=%p gros=%p pbo=%d\n", (int)getpid(), (void *)cap_fp, (void *)real_glReadPixels, - (void *)real_GL_GetDrawableSize); + (void *)real_GL_GetDrawableSize, real_glGenBuffers != NULL); if (cap_fp && !cap_stop && real_glReadPixels && real_GL_GetDrawableSize) { int w = 0, h = 0; - if (real_GL_GetDrawableSize(win, &w, &h) == 0 && w > 0 && h > 0 && + cap_try_harvest(); + if (!cap_sync && + real_GL_GetDrawableSize(win, &w, &h) == 0 && w > 0 && h > 0 && w <= 4096 && h <= 4096) { - size_t need = (size_t)w * 4 * h; + if (!hook_ms) + hook_ms = now_ms(); + if (real_glGenBuffers && real_glBindBuffer && real_glBufferData && + real_glFenceSync && real_glClientWaitSync && real_glMapBufferRange) { + int prev = 0; - if (gbufsz < need) { - free(gbuf); - gbuf = malloc(need); - gbufsz = gbuf ? need : 0; - } - if (gbuf) { - real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, gbuf); - /* GL rows are bottom-up: flip into a scratch record */ - { - unsigned char *tmp = malloc(need); + if (!cap_pbo) { + real_glGenBuffers(1, &cap_pbo); + fprintf(stderr, "[gzcap] async capture armed %dx%d pbo=%u\n", + w, h, cap_pbo); + } + if (cap_pbo) { + if (real_glBindFramebuffer) + real_glBindFramebuffer(0x8CA8, 0); + if (real_glReadBuffer) + real_glReadBuffer(0x0404); + while (real_glGetError && real_glGetError() != 0) + ; + real_glBindBuffer(GL_PIXEL_PACK_BUFFER, cap_pbo); + real_glBufferData(GL_PIXEL_PACK_BUFFER, + (long)w * 4 * h, NULL, GL_STREAM_READ); + real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, NULL); + cap_pw = w; + cap_ph = h; + cap_sync = real_glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, + GL_SYNC_FLUSH_COMMANDS_BIT); + cap_sync_ns = now_ms(); + if (!cap_sync) { + if (real_glDeleteBuffers) { + unsigned b = cap_pbo; + + real_glDeleteBuffers(1, &b); + } + cap_pbo = 0; + } + } + real_glBindBuffer(GL_PIXEL_PACK_BUFFER, prev); + if (real_glFlush) + real_glFlush(); + } else if (real_glReadPixels) { + /* No PBO support: keep a bounded synchronous fallback. */ + unsigned char *buf = malloc((size_t)w * 4 * h); + + if (buf) { + unsigned char *tmp = malloc((size_t)w * 4 * h); int y; if (tmp) { + real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, buf); for (y = 0; y < h; y++) memcpy(tmp + (size_t)y * w * 4, - gbuf + (size_t)(h - 1 - y) * w * 4, + buf + (size_t)(h - 1 - y) * w * 4, (size_t)w * 4); cap_record(tmp, w, h, w * 4, GZF2_FMT_RGBA); free(tmp); } + free(buf); } } } } + if (real_glFlush && !real_GL_SwapWindow) + real_glFlush(); if (real_GL_SwapWindow) real_GL_SwapWindow(win); } |
