From 9c36fba49f1ed5af0f40fc7a3d1fbb9ffadd4d97 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Rasmusson Date: Mon, 16 Nov 2026 11:30:08 -0500 Subject: sdl_cap: capture-mode and file-ownership fixes for the GZDoom video run - GZCAP_SYNC=1: synchronous readback path for software GL (the async PBO+fence path never signals under Mesa llvmpipe, so 0 frames were recorded; the GPU path keeps the async path). - GZCAP_READBUF=back: read GL_BACK on a double-buffered X11 window (Xvfb); the default GL_FRONT is correct only for the single-buffered GPU pbuffer. Without it every Xvfb frame was black. - File gate: only an SDL renderer process opens the capture, and an existing non-empty capture is appended, never truncated. The old 'wb' open in every LD_PRELOAD child turned a 288-frame capture into a 4108-byte file; a helper without SDL also opened it via the glReadPixels fallback. - Log the read buffer in the async arm line. Validated by the 2026-11-16 GZDoom video runs: 2200-frame captures, clean_end=1, corrupt=0, 30 fps encodes, delivered via files.vgpu.io. --- tools/guest-diag/sdl_cap.c | 92 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 10 deletions(-) (limited to 'tools/guest-diag') diff --git a/tools/guest-diag/sdl_cap.c b/tools/guest-diag/sdl_cap.c index 84aa35b..4c034f0 100644 --- a/tools/guest-diag/sdl_cap.c +++ b/tools/guest-diag/sdl_cap.c @@ -76,6 +76,7 @@ static void (*real_glDeleteSync)(void *); static FILE *cap_fp; static unsigned long long cap_count, cap_max = 0; static int cap_stop; +static unsigned cap_readbuf = 0x0404; /* GL_FRONT by default (single-buffered pbuffer) */ __attribute__((constructor)) static void cap_init(void) { @@ -87,6 +88,12 @@ __attribute__((constructor)) static void cap_init(void) cap_max = strtoull(m, NULL, 0); if (!p) p = "/dev/shm/gzframes.bin"; + { + const char *rb = getenv("GZCAP_READBUF"); + + if (rb && (rb[0] == 'b' || rb[0] == 'B')) + cap_readbuf = 0x0405; /* GL_BACK: double-buffered window (Xvfb) */ + } real_RenderReadPixels = dlsym(RTLD_NEXT, "SDL_RenderReadPixels"); real_RenderPresent = dlsym(RTLD_NEXT, "SDL_RenderPresent"); real_GetWindowSurface = dlsym(RTLD_NEXT, "SDL_GetWindowSurface"); @@ -139,14 +146,39 @@ __attribute__((constructor)) static void cap_init(void) (int)getpid(), p, (void *)real_RenderPresent, (void *)real_UpdateWindowSurface, (void *)real_GL_SwapWindow, (void *)real_RenderReadPixels, (void *)real_GetRendererOutputSize, (void *)real_glReadPixels); - if (!real_glReadPixels && !real_RenderReadPixels) - return; - cap_fp = fopen(p, "wb"); - if (!cap_fp) + /* + * Only the renderer process may own the capture file. Every process + * that inherits LD_PRELOAD runs this constructor; a helper without SDL + * (gdb, a crash handler, pipewire) used to fall through the + * glReadPixels-only gate and truncate the capture with "wb" at exit + * (the 288-frame run ended as a 4108-byte file). Require an SDL + * presentation symbol before touching the path, and never truncate an + * existing non-empty capture: append instead. + */ + if (!real_GL_SwapWindow && !real_RenderPresent && !real_UpdateWindowSurface) return; - memset(hdr, 0, sizeof(hdr)); - memcpy(hdr, "GZCAPV01", 8); - fwrite(hdr, 1, sizeof(hdr), cap_fp); + cap_fp = fopen(p, "r+b"); + if (cap_fp) { + long sz; + + fseek(cap_fp, 0, SEEK_END); + sz = ftell(cap_fp); + if (sz < (long)sizeof(hdr)) { + rewind(cap_fp); + memset(hdr, 0, sizeof(hdr)); + memcpy(hdr, "GZCAPV01", 8); + fwrite(hdr, 1, sizeof(hdr), cap_fp); + } else { + fseek(cap_fp, 0, SEEK_END); + } + } else { + cap_fp = fopen(p, "wb"); + if (!cap_fp) + return; + memset(hdr, 0, sizeof(hdr)); + memcpy(hdr, "GZCAPV01", 8); + fwrite(hdr, 1, sizeof(hdr), cap_fp); + } fflush(cap_fp); } @@ -343,6 +375,46 @@ void SDL_GL_SwapWindow(SDL_Window *win) 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, real_glGenBuffers != NULL); + { + static int force_sync = -1; + + if (force_sync < 0) + force_sync = getenv("GZCAP_SYNC") != NULL; + if (force_sync && 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 && w <= 4096 && h <= 4096) { + unsigned char *buf = malloc((size_t)w * 4 * h); + unsigned char *tmp = malloc((size_t)w * 4 * h); + + if (buf && tmp) { + int y; + + if (real_glBindFramebuffer) + real_glBindFramebuffer(0x8CA8, 0); + if (real_glReadBuffer) + real_glReadBuffer(cap_readbuf); + while (real_glGetError && real_glGetError() != 0) + ; + if (real_glFinish) + real_glFinish(); + real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, buf); + for (y = 0; y < h; y++) + memcpy(tmp + (size_t)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(buf); + free(tmp); + } + if (real_GL_SwapWindow) + real_GL_SwapWindow(win); + return; + } + } if (cap_fp && !cap_stop && real_glReadPixels && real_GL_GetDrawableSize) { int w = 0, h = 0; @@ -358,14 +430,14 @@ void SDL_GL_SwapWindow(SDL_Window *win) if (!cap_pbo) { real_glGenBuffers(1, &cap_pbo); - fprintf(stderr, "[gzcap] async capture armed %dx%d pbo=%u\n", - w, h, cap_pbo); + fprintf(stderr, "[gzcap] async capture armed %dx%d pbo=%u readbuf=%#x\n", + w, h, cap_pbo, cap_readbuf); } if (cap_pbo) { if (real_glBindFramebuffer) real_glBindFramebuffer(0x8CA8, 0); if (real_glReadBuffer) - real_glReadBuffer(0x0404); + real_glReadBuffer(cap_readbuf); while (real_glGetError && real_glGetError() != 0) ; real_glBindBuffer(GL_PIXEL_PACK_BUFFER, cap_pbo); -- cgit v1.2.3