From ed8a9c6b8e061fe772fc0050ab83c2dec2db8d1e Mon Sep 17 00:00:00 2001 From: Arthur Hanson Rasmusson Date: Mon, 16 Nov 2026 09:50:31 -0500 Subject: 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. --- tools/guest-diag/gz_pbuf.c | 19 ++- tools/guest-diag/gzdoom_pbuf_run.sh | 15 ++- tools/guest-diag/mission-gz.sh | 122 ++++++++++++------ tools/guest-diag/params-r1624-maps4.txt | 13 ++ tools/guest-diag/params-r1624-maps6.txt | 13 ++ tools/guest-diag/push_gl_tools.sh | 1 + tools/guest-diag/run_cycles_long.sh | 142 +++++++++++++++++++++ tools/guest-diag/sdl_cap.c | 216 +++++++++++++++++++++++++++++--- 8 files changed, 481 insertions(+), 60 deletions(-) create mode 100644 tools/guest-diag/params-r1624-maps4.txt create mode 100644 tools/guest-diag/params-r1624-maps6.txt create mode 100755 tools/guest-diag/run_cycles_long.sh (limited to 'tools/guest-diag') diff --git a/tools/guest-diag/gz_pbuf.c b/tools/guest-diag/gz_pbuf.c index 5c03732..ddd9d00 100644 --- a/tools/guest-diag/gz_pbuf.c +++ b/tools/guest-diag/gz_pbuf.c @@ -42,6 +42,7 @@ static int make_pb(int w, int h) GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_ALPHA_SIZE, 8, + GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, False, None }; @@ -88,7 +89,14 @@ void *SDL_GL_CreateContext(SDL_Window *win) } g_win = win; glXMakeCurrent(g_dpy, g_pb, g_ctx); - fprintf(stderr, "[gzpbuf] pbuffer %dx%d ctx=%p\n", g_w, g_h, (void *)g_ctx); + { + GLint db = -1, sb = -1; + + glGetIntegerv(GL_DEPTH_BITS, &db); + glGetIntegerv(GL_STENCIL_BITS, &sb); + fprintf(stderr, "[gzpbuf] pbuffer %dx%d ctx=%p depth=%d stencil=%d\n", + g_w, g_h, (void *)g_ctx, (int)db, (int)sb); + } return (void *)g_ctx; } @@ -107,7 +115,14 @@ int SDL_GL_MakeCurrent(SDL_Window *win, void *ctx) void SDL_GL_SwapWindow(SDL_Window *win) { (void)win; - glFinish(); + /* + * 2026-11-16 (c899): never glFinish here. The emulated GPU's work does + * not retire, and a blocking finish puts GZDoom in an uninterruptible + * nv ioctl that even SIGKILL cannot reap (the mission then hangs for + * minutes). glFlush only submits; sdl_cap's async PBO+fence harvest + * decides when a frame is actually ready. + */ + glFlush(); } void SDL_GL_GetDrawableSize(SDL_Window *win, int *w, int *h) diff --git a/tools/guest-diag/gzdoom_pbuf_run.sh b/tools/guest-diag/gzdoom_pbuf_run.sh index 47817b7..49c5c96 100755 --- a/tools/guest-diag/gzdoom_pbuf_run.sh +++ b/tools/guest-diag/gzdoom_pbuf_run.sh @@ -25,18 +25,20 @@ set -u GZDOOM_BIN="${GZDOOM_BIN:-/opt/gzdoom/gzdoom}" WAD="${WAD:-/usr/share/games/doom/freedoom2.wad}" -GZCAP="${GZCAP:-/tmp/gzframes.bin}" +GZCAP="${GZCAP:-/dev/shm/gzframes.bin}" GZPBUF_W="${GZPBUF_W:-640}" +GZCAP_MAXF="${GZCAP_MAXF:-1800}" GZPBUF_H="${GZPBUF_H:-480}" GZLOG="${GZLOG:-/tmp/gzdoom-pbuf.log}" TIMEOUT="${TIMEOUT:-300}" GZ_ARGS="${GZ_ARGS:-}" GZ_DETACH="${GZ_DETACH:-0}" +GZ_RCFILE="${GZ_RCFILE:-/tmp/gz-rc}" DISPLAY="${DISPLAY:-:0}" SDL_VIDEODRIVER="${SDL_VIDEODRIVER:-offscreen}" LD_PRELOAD="${LD_PRELOAD:-/tmp/sdl_cap.so /tmp/gz_pbuf.so}" -export DISPLAY SDL_VIDEODRIVER LD_PRELOAD GZCAP GZPBUF_W GZPBUF_H +export DISPLAY SDL_VIDEODRIVER LD_PRELOAD GZCAP GZCAP_MAXF GZPBUF_W GZPBUF_H GZ_RCFILE if [ ! -x "$GZDOOM_BIN" ]; then echo "gzdoom_pbuf_run: GZDOOM_BIN not executable: $GZDOOM_BIN" >&2 @@ -65,6 +67,8 @@ CMD=( -nomusic +vid_activeinbackground 1 +vid_maxfps 60 + +set gl_pipeline_depth 2 + +set gl_finishbeforeswap 0 +map MAP01 ) if [ -n "$GZ_ARGS" ]; then @@ -74,14 +78,17 @@ fi echo "gzdoom_pbuf_run: DISPLAY=$DISPLAY SDL_VIDEODRIVER=$SDL_VIDEODRIVER" echo "gzdoom_pbuf_run: LD_PRELOAD=$LD_PRELOAD" -echo "gzdoom_pbuf_run: GZCAP=$GZCAP GZPBUF_W=$GZPBUF_W GZPBUF_H=$GZPBUF_H" +echo "gzdoom_pbuf_run: GZCAP=$GZCAP GZCAP_MAXF=$GZCAP_MAXF GZPBUF_W=$GZPBUF_W GZPBUF_H=$GZPBUF_H" echo "gzdoom_pbuf_run: LOG=$GZLOG TIMEOUT=$TIMEOUT DETACH=$GZ_DETACH" printf 'CMD:' printf ' %q' "${CMD[@]}" echo if [ "$GZ_DETACH" = "1" ]; then - setsid nohup timeout -k 5 "$TIMEOUT" "${CMD[@]}" > "$GZLOG" 2>&1 < /dev/null & + RCF="${GZ_RCFILE:-/tmp/gz-rc}" + rm -f "$RCF" + setsid nohup bash -c 'timeout -k 5 "$0" "${@:1}"; echo $? > "$GZ_RCFILE"' \ + "$TIMEOUT" "${CMD[@]}" > "$GZLOG" 2>&1 < /dev/null & echo "PID=$!" echo "LOG=$GZLOG" exit 0 diff --git a/tools/guest-diag/mission-gz.sh b/tools/guest-diag/mission-gz.sh index cf58f02..c4665d2 100644 --- a/tools/guest-diag/mission-gz.sh +++ b/tools/guest-diag/mission-gz.sh @@ -1,55 +1,105 @@ #!/bin/bash # SPDX-License-Identifier: BSD-3-Clause # Copyright (c) 2026 Open-IOV contributors -# mission-gz.sh - run INSIDE the guest: bring X up (retry), probe the GPU GL -# path (gl_frames), then run GZDoom offscreen with the GLX-pbuffer shim and the -# swap-capture shim. Everything logs to /tmp/mission-*.log; completion is -# signalled by /tmp/mission-done (content = result line). +# mission-gz.sh - run INSIDE the guest: bring X up (retry), optionally probe the +# GPU GL path (gl_frames), then run GZDoom offscreen with the GLX-pbuffer shim +# and the swap-capture shim. Everything logs to /tmp/mission-*.log; completion +# is signalled by /tmp/mission-done (content = result line). # -# Env knobs: GZ_SECS (default 240) GLF_FRAMES (default 5) GLF_W/GLF_H +# Env knobs: GZ_SECS (default 240) GLF_FRAMES (default 0 = skip probe) +# GLF_W/GLF_H GZCAP GZCAP_MAXF GZ_ATTEMPTS (default 2) +# +# 2026-11-16 (post-reboot c895-c898): the gl_frames probe is now OFF by default +# (GLF_FRAMES=0). It is a diagnostic, not a render source, and in the batch it +# cost 30-40 s/frame and preceded X/GLX failures that made GZDoom abort with +# "Could not create OpenGL window" (rc=255). GZDoom is the mission; probe only +# when explicitly requested. set -u export DISPLAY=:0 +GZCAP="${GZCAP:-/dev/shm/gzframes.bin}" +GZCAP_MAXF="${GZCAP_MAXF:-1800}" +GZ_ATTEMPTS="${GZ_ATTEMPTS:-2}" + +x_up() { + local t r + for t in 1 2 3 4; do + pkill -x Xorg; sleep 1 + systemctl reset-failed xorg0 2>/dev/null + systemd-run --unit=xorg0 --collect --setenv=HOME=/root \ + /usr/bin/Xorg :0 -config /etc/X11/xorg.conf >/dev/null 2>&1 + sleep 7 + if timeout 12 xdpyinfo >/dev/null 2>&1; then echo "$t"; return 0; fi + echo "x try $t: dead" + done + return 1 +} + echo "mission start $(date +%T)" -rm -f /tmp/mission-done /tmp/gzframes.bin +rm -f /tmp/mission-done /tmp/gzframes.bin "$GZCAP" mkdir -p /tmp/gzshots # 1. X up (the DDX ScreenInit is a per-start lottery) XOK=0 -for t in 1 2 3 4; do - pkill -x Xorg; sleep 1 - systemctl reset-failed xorg0 2>/dev/null - systemd-run --unit=xorg0 --collect --setenv=HOME=/root \ - /usr/bin/Xorg :0 -config /etc/X11/xorg.conf >/dev/null 2>&1 - sleep 7 - if timeout 12 xdpyinfo >/dev/null 2>&1; then XOK=$t; break; fi - echo "x try $t: dead" -done -echo "XOK try=$XOK $(date +%T)" +XTRY=$(x_up) && XOK=1 +echo "XOK try=$XTRY $(date +%T)" if [ "$XOK" = 0 ]; then echo "X-FAIL" > /tmp/mission-done; exit 1; fi bash /tmp/guest_mode.sh 2>/dev/null || true -# 2. GPU GL probe: direct pbuffer frames (non-black px0 = path live) -GLF_FRAMES="${GLF_FRAMES:-5}" -( - timeout -k 5 90 /tmp/gl_frames "$GLF_FRAMES" "${GLF_W:-320}" "${GLF_H:-240}" 5 \ - > /tmp/mission-gl.log 2>&1 - echo "glf rc=$? $(date +%T)" >> /tmp/mission-gl.log -) & -GLFPID=$! -sleep 20 -echo "x alive after glf start: $(pgrep -c Xorg)" -wait $GLFPID 2>/dev/null -grep -E "^FRAME seq" /tmp/mission-gl.log | tail -3 +# 2. Optional GPU GL probe (diagnostic only; off by default) +GLF_FRAMES="${GLF_FRAMES:-0}" +if [ "$GLF_FRAMES" != 0 ]; then + ( + timeout -k 5 120 /tmp/gl_frames "$GLF_FRAMES" "${GLF_W:-320}" "${GLF_H:-240}" 5 \ + > /tmp/mission-gl.log 2>&1 + echo "glf rc=$? $(date +%T)" >> /tmp/mission-gl.log + ) & + GLFPID=$! + sleep 10 + echo "x alive after glf start: $(pgrep -c Xorg)" + wait $GLFPID 2>/dev/null + grep -E "^FRAME seq" /tmp/mission-gl.log | tail -3 +else + : > /tmp/mission-gl.log + echo "glf skipped (GLF_FRAMES=0)" +fi -# 3. GZDoom offscreen + pbuffer shim + swap capture (tested recipe wrapper) +# 3. GZDoom offscreen + pbuffer shim + swap capture. The run is DETACHED and +# bounded by GZ_SECS: a GPU wait must never wedge this shell (c899: glFinish +# on non-retiring work put gzdoom in an uninterruptible nv ioctl and even +# SIGKILL could not reap it, so the mission never wrote mission-done). GZ_SECS="${GZ_SECS:-240}" -TIMEOUT="$GZ_SECS" GZLOG=/tmp/mission-gz.log GZCAP=/tmp/gzframes.bin \ - GZPBUF_W="${GZPBUF_W:-640}" GZPBUF_H="${GZPBUF_H:-480}" \ - bash /tmp/gzdoom_pbuf_run.sh -GZRC=$? -echo "gz rc=$GZRC $(date +%T)" >> /tmp/mission-gz.log -ls -la /tmp/gzframes.bin /tmp/gzshots 2>&1 | tail -5 +export GZ_RCFILE=/tmp/gz-rc +GZRC=255 +attempt=1 +while [ "$attempt" -le "$GZ_ATTEMPTS" ]; do + if ! pgrep -x Xorg >/dev/null; then + echo "X dead before GZDoom attempt $attempt - restarting" >> /tmp/mission-gz.log + x_up >/dev/null 2>&1 || { echo "X restart failed" >> /tmp/mission-gz.log; break; } + fi + rm -f "$GZ_RCFILE" + GZ_DETACH=1 TIMEOUT="$GZ_SECS" GZLOG=/tmp/mission-gz.log GZCAP="$GZCAP" \ + GZCAP_MAXF="$GZCAP_MAXF" \ + GZPBUF_W="${GZPBUF_W:-640}" GZPBUF_H="${GZPBUF_H:-480}" \ + bash /tmp/gzdoom_pbuf_run.sh + GZRC="hung" + for i in $(seq 1 $((GZ_SECS + 30))); do + if [ -f "$GZ_RCFILE" ]; then GZRC=$(cat "$GZ_RCFILE"); break; fi + sleep 1 + done + echo "gz attempt=$attempt rc=$GZRC $(date +%T)" >> /tmp/mission-gz.log + # Stop the renderer before fetching/next attempt. A D-state process + # survives SIGKILL; that is fine, the capture file is append-only and the + # cycle teardown removes it. + pkill -x gzdoom 2>/dev/null + sleep 1 + pkill -9 -x gzdoom 2>/dev/null + [ "$GZRC" != 255 ] && break + attempt=$((attempt + 1)) + sleep 2 +done + +ls -la "$GZCAP" /tmp/gzshots 2>&1 | tail -5 echo "XORG-ALIVE=$(pgrep -c Xorg)" | tee -a /tmp/mission-gz.log echo "DONE x=$XOK gzrc=$GZRC $(date +%T)" > /tmp/mission-done -echo "mission end $(date +%T)" +echo "mission end $(date +%T)" \ No newline at end of file diff --git a/tools/guest-diag/params-r1624-maps4.txt b/tools/guest-diag/params-r1624-maps4.txt new file mode 100644 index 0000000..238e5b5 --- /dev/null +++ b/tools/guest-diag/params-r1624-maps4.txt @@ -0,0 +1,13 @@ +gsp_phys_gpu=1 gsp_phys_hazard_ack=1 gsp_phys_boot=1 gsp_phys_nocat=1 gsp_phys_fw=nvidia/tu102/gsp/gsp-570.144.bin gsp_phys_guest_mem=1 gsp_phys_gr_forward=1 cuda_bridge=0 gsp_phys_rmcall_engine=1 gsp_phys_rmcall_golden=1 gsp_phys_rmcall_gr_promote=25 gsp_phys_rmcall_fwclient_mask=0x601 gsp_phys_rmcall_vas_shared=1 gsp_phys_gpu=1 gsp_phys_hazard_ack=1 gsp_phys_boot=1 gsp_phys_nocat=1 gsp_phys_fw=nvidia/tu102/gsp/gsp-570.144.bin gsp_phys_guest_mem=1 gsp_phys_gr_forward=1 cuda_bridge=0 gsp_phys_rmcall_engine=1 gsp_phys_rmcall_golden=1 gsp_phys_rmcall_gr_promote=35 gsp_phys_rmcall_fwclient_mask=0xE3F gsp_phys_rmcall_vas_shared=1 gsp_phys_gr_nowrite_retries=0 gsp_phys_gr_retry_fast=1 gsp_phys_gr_first_poll_ms=2500 gsp_sema_winfill=1 gsp_sema_gr_swrel_gate=1 gsp_phys_rmcall_srv_ctxcommit=0 gsp_phys_rmcall_gr_qmdwin=1 gsp_phys_rmcall_gr_shadow_win0=1 gsp_phys_rmcall_gr_lmem_en=1 gsp_phys_rmcall_gr_skedrec=1 gsp_phys_rmcall_gr_inst_sys=1 gsp_phys_rmcall_gr_r1_first=1 gsp_phys_gr_strict_reloc=0 gsp_phys_fault_relay=1 sema_bigcopy_log=512 sema_gl_xlate=1 gsp_phys_gr_min_data=0 gsp_phys_rmcall_gr_ptr_guest=0 sema_gr_xlate=1 sema_dbg=Y gsp_phys_gr_dec_log=32 gsp_phys_rmcall_gr_rel_mod=1 sema_vas_cand_ms=1000 sema_push_raw=64 binapi07_rep=0x0101 sema_push_raw_small=2000 sema_gr_copy_run_max=2 sema_kick_notify=Y sema_gl_idx_reloc=1 sema_gl_mme_reloc=1 sema_mme_census=1 sema_gr_setup_hold_ms=0 sema_attr_prelude_cap=1 sema_gl_attr_prelude=1 sema_gr_prelude_gate_ms=0 sema_gl_win_drop= sema_gl_attr_zero_fix=1 sema_gl_sdm_drop=1 sema_gl_miss_abort=1 sema_mme_fast_ms=2800 sema_gl_mme_sa_stub_all=N sema_gl_mme_keep_mask=0xffffffffffffffff sema_gl_mme_nop_drop=0x0 sema_gl_mme_hold_calls=1 sema_gl_mme_prelude=1 sema_gl_win_patch=124:0 sema_map_add_max=4096 sema_gl_mme_sa_rearm=0 sema_gl_sph_version=1 sema_gl_sph_bind=1 sema_gr_wedge_recover=1 sema_mme_prelude_cap=1 sema_gl_mme_track=0 sema_gl_mme_version=1 sema_gl_sph_canary=0 sema_gl_shader_exc=1 gsp_phys_rmcall_gr_pte_kind=0 gsp_phys_rmcall_ce_lane=1 sema_gl_mme_prelude_live_skip=1 sema_gl_mme_nop_pair=0 sema_mme_post_ms=20000 sema_gl_mme_seed=Y sema_gl_mme_seed_val=0x1207000 sema_gl_mme_state_seed=Y sema_mme_state_d26=0 sema_mme_state_d27=0 sema_mme_state_d28=0xffffffff sema_mme_state_d29=0 sema_mme_state_d2a=0x80000000 sema_gl_mme_prelude_pack=1 sema_gl_mme_pack_split=1 sema_gl_mme_iram_gate=1 sema_gl_mme_hold_fe_idle=0 sema_gl_mme_iram_gate_break=0 sema_gl_mme_iram_gate_build=0 sema_gl_wit_shown=1024 sema_gl_mme_keep_clamp=1 sema_gl_mme_golden=1 sema_gl_mme_golden_force=1 sema_sph_region_base=0x01500000 sema_gl_sph_8k=1 sema_mme_state_d19=0x1207000 sema_gl_mme_seed_xlat=1 sema_gl_mme_hold_strict=0 sema_gl_mme_hold_window=1 sema_gl_ps_hex=82140a0000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000aa0000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026730400ff0000001f000e0000020e0426730000ff000000a080000000620e0026730100ff000000a180020000a20e0026730200ff000000a280040000e20e0026730300ff000000a380060000220f0008730400040000000010000000641e00208200000400000000c0410000e42f08209201010400000000c0410000e44f0820a202020400000000c0410000c48f0020b203030400000000c0410000e20f014d790000000000000000800300ee0f0047790000f0ffffffffff830300c00f0018790000000000000000000000c00f0018790000000000000000000000c00f0018790000000000000000000000c00f0018790000000000000000000000c00f00 sema_gl_cb_copy_to=0x120700000 sema_gl_cb_src_va=0x0cd70000 sema_gl_sph_gs_img=1 sema_gl_sph_gs_maps=4 sema_gl_sph_gs_mode=1 sema_gl_sph_gs_addr=1 sema_gl_raster_prelude=1 sema_gl_sph_gs_ctx=3 sema_pub_adv=1 bus_gart_mb=4096 disp_caps_seed=1 disp_dbg=1 sema_gr_fb_push=1 sema_gl_mme_pack_carry_client=1 sema_gl_draw_synth=1 +sema_gr_grace_ms=500 sema_gl_mme_keep_done_across_recycle=1 sema_mme_prelude_cap_any=1 +sema_gl_rt0_bind=1 +sema_gl_mme_partial_keep_window=1 sema_gl_mme_pack_keep_gen=1 sema_gl_mme_cb_alias=1 + sema_gl_win_rt0_admit=1 + sema_ddx_tracker_auth=1 +sema_gl_mme_pack_carry=1 sema_gl_mme_pack_carry_ms=100 sema_gl_mme_pack_release=1 sema_gl_mme_pack_release_ms=100 + sema_gl_sph_bind_pair_safe=1 sema_gl_mme_sa_golden_fill=1 sema_gl_mme_release_sa_arm=1 + sema_gl_sph_stage_disable=0x04 + sema_gl_viewport=1 + sema_gl_vs_autobind=2 + sema_gl_wit_shown=40 + sema_gl_surf_alias_pref=1 diff --git a/tools/guest-diag/params-r1624-maps6.txt b/tools/guest-diag/params-r1624-maps6.txt new file mode 100644 index 0000000..973748c --- /dev/null +++ b/tools/guest-diag/params-r1624-maps6.txt @@ -0,0 +1,13 @@ +gsp_phys_gpu=1 gsp_phys_hazard_ack=1 gsp_phys_boot=1 gsp_phys_nocat=1 gsp_phys_fw=nvidia/tu102/gsp/gsp-570.144.bin gsp_phys_guest_mem=1 gsp_phys_gr_forward=1 cuda_bridge=0 gsp_phys_rmcall_engine=1 gsp_phys_rmcall_golden=1 gsp_phys_rmcall_gr_promote=25 gsp_phys_rmcall_fwclient_mask=0x601 gsp_phys_rmcall_vas_shared=1 gsp_phys_gpu=1 gsp_phys_hazard_ack=1 gsp_phys_boot=1 gsp_phys_nocat=1 gsp_phys_fw=nvidia/tu102/gsp/gsp-570.144.bin gsp_phys_guest_mem=1 gsp_phys_gr_forward=1 cuda_bridge=0 gsp_phys_rmcall_engine=1 gsp_phys_rmcall_golden=1 gsp_phys_rmcall_gr_promote=35 gsp_phys_rmcall_fwclient_mask=0xE3F gsp_phys_rmcall_vas_shared=1 gsp_phys_gr_nowrite_retries=0 gsp_phys_gr_retry_fast=1 gsp_phys_gr_first_poll_ms=2500 gsp_sema_winfill=1 gsp_sema_gr_swrel_gate=1 gsp_phys_rmcall_srv_ctxcommit=0 gsp_phys_rmcall_gr_qmdwin=1 gsp_phys_rmcall_gr_shadow_win0=1 gsp_phys_rmcall_gr_lmem_en=1 gsp_phys_rmcall_gr_skedrec=1 gsp_phys_rmcall_gr_inst_sys=1 gsp_phys_rmcall_gr_r1_first=1 gsp_phys_gr_strict_reloc=0 gsp_phys_fault_relay=1 sema_bigcopy_log=512 sema_gl_xlate=1 gsp_phys_gr_min_data=0 gsp_phys_rmcall_gr_ptr_guest=0 sema_gr_xlate=1 sema_dbg=Y gsp_phys_gr_dec_log=32 gsp_phys_rmcall_gr_rel_mod=1 sema_vas_cand_ms=1000 sema_push_raw=64 binapi07_rep=0x0101 sema_push_raw_small=2000 sema_gr_copy_run_max=2 sema_kick_notify=Y sema_gl_idx_reloc=1 sema_gl_mme_reloc=1 sema_mme_census=1 sema_gr_setup_hold_ms=0 sema_attr_prelude_cap=1 sema_gl_attr_prelude=1 sema_gr_prelude_gate_ms=0 sema_gl_win_drop= sema_gl_attr_zero_fix=1 sema_gl_sdm_drop=1 sema_gl_miss_abort=1 sema_mme_fast_ms=2800 sema_gl_mme_sa_stub_all=N sema_gl_mme_keep_mask=0xffffffffffffffff sema_gl_mme_nop_drop=0x0 sema_gl_mme_hold_calls=1 sema_gl_mme_prelude=1 sema_gl_win_patch=124:0 sema_map_add_max=4096 sema_gl_mme_sa_rearm=0 sema_gl_sph_version=1 sema_gl_sph_bind=1 sema_gr_wedge_recover=1 sema_mme_prelude_cap=1 sema_gl_mme_track=0 sema_gl_mme_version=1 sema_gl_sph_canary=0 sema_gl_shader_exc=1 gsp_phys_rmcall_gr_pte_kind=0 gsp_phys_rmcall_ce_lane=1 sema_gl_mme_prelude_live_skip=1 sema_gl_mme_nop_pair=0 sema_mme_post_ms=20000 sema_gl_mme_seed=Y sema_gl_mme_seed_val=0x1207000 sema_gl_mme_state_seed=Y sema_mme_state_d26=0 sema_mme_state_d27=0 sema_mme_state_d28=0xffffffff sema_mme_state_d29=0 sema_mme_state_d2a=0x80000000 sema_gl_mme_prelude_pack=1 sema_gl_mme_pack_split=1 sema_gl_mme_iram_gate=1 sema_gl_mme_hold_fe_idle=0 sema_gl_mme_iram_gate_break=0 sema_gl_mme_iram_gate_build=0 sema_gl_wit_shown=1024 sema_gl_mme_keep_clamp=1 sema_gl_mme_golden=1 sema_gl_mme_golden_force=1 sema_sph_region_base=0x01500000 sema_gl_sph_8k=1 sema_mme_state_d19=0x1207000 sema_gl_mme_seed_xlat=1 sema_gl_mme_hold_strict=0 sema_gl_mme_hold_window=1 sema_gl_ps_hex=82140a0000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000aa0000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026730400ff0000001f000e0000020e0426730000ff000000a080000000620e0026730100ff000000a180020000a20e0026730200ff000000a280040000e20e0026730300ff000000a380060000220f0008730400040000000010000000641e00208200000400000000c0410000e42f08209201010400000000c0410000e44f0820a202020400000000c0410000c48f0020b203030400000000c0410000e20f014d790000000000000000800300ee0f0047790000f0ffffffffff830300c00f0018790000000000000000000000c00f0018790000000000000000000000c00f0018790000000000000000000000c00f0018790000000000000000000000c00f00 sema_gl_cb_copy_to=0x120700000 sema_gl_cb_src_va=0x0cd70000 sema_gl_sph_gs_img=1 sema_gl_sph_gs_maps=6 sema_gl_sph_gs_mode=1 sema_gl_sph_gs_addr=1 sema_gl_raster_prelude=1 sema_gl_sph_gs_ctx=3 sema_pub_adv=1 bus_gart_mb=4096 disp_caps_seed=1 disp_dbg=1 sema_gr_fb_push=1 sema_gl_mme_pack_carry_client=1 sema_gl_draw_synth=1 +sema_gr_grace_ms=500 sema_gl_mme_keep_done_across_recycle=1 sema_mme_prelude_cap_any=1 +sema_gl_rt0_bind=1 +sema_gl_mme_partial_keep_window=1 sema_gl_mme_pack_keep_gen=1 sema_gl_mme_cb_alias=1 + sema_gl_win_rt0_admit=1 + sema_ddx_tracker_auth=1 +sema_gl_mme_pack_carry=1 sema_gl_mme_pack_carry_ms=100 sema_gl_mme_pack_release=1 sema_gl_mme_pack_release_ms=100 + sema_gl_sph_bind_pair_safe=1 sema_gl_mme_sa_golden_fill=1 sema_gl_mme_release_sa_arm=1 + sema_gl_sph_stage_disable=0x04 + sema_gl_viewport=1 + sema_gl_vs_autobind=2 + sema_gl_wit_shown=40 + sema_gl_surf_alias_pref=1 diff --git a/tools/guest-diag/push_gl_tools.sh b/tools/guest-diag/push_gl_tools.sh index 4064d58..986d2d6 100755 --- a/tools/guest-diag/push_gl_tools.sh +++ b/tools/guest-diag/push_gl_tools.sh @@ -33,6 +33,7 @@ FIXED=( "$HERE/guest_mode.sh" "$HERE/gzf2_video.py" "$HERE/gzdoom_pbuf_run.sh" + "$HERE/mission-gz.sh" ) if [ "$#" -gt 0 ]; then diff --git a/tools/guest-diag/run_cycles_long.sh b/tools/guest-diag/run_cycles_long.sh new file mode 100755 index 0000000..bf14ca2 --- /dev/null +++ b/tools/guest-diag/run_cycles_long.sh @@ -0,0 +1,142 @@ +#!/bin/bash +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2026 Open-IOV contributors +# run_cycles_long.sh - durable multi-cycle batch runner for the GZDoom +# GPU-video campaign. For each tag: cycle-r1780 (rmmod/insmod + mdev + +# QEMU + GR-lane arm), push the GL/GZDoom guest tools, run mission-gz.sh +# detached in the guest, wait for /tmp/mission-done, then collect host +# dmesg + guest logs + any swap capture into the evidence dir. +# +# Usage: tools/guest-diag/run_cycles_long.sh c895 c896 c897 +# Env: GZ_SECS (default 120) guest GZDoom seconds +# GLF_FRAMES(default 2) gl_frames probe frames +# MODULE (default kernel/vgsp.ko) +# INSMOD_PARAMS (default params-r1624.txt) +# STOP_ON px|clean247|none (default px: stop at first RT pixel) +# MAX_FETCH_MB (default 512) skip gzframes.bin fetches above this +# +# The runner never reboots and never runs two QEMUs at once. Everything is +# appended to /tmp/opencode/batch-.log and per-tag files under +# backups/evidence-20261002-victory/r1780/. +set -u + +HERE="$(cd "$(dirname "$0")" && pwd)" +REPO="$(cd "$HERE/../.." && pwd)" +BACKUP="$REPO/backups/evidence-20261002-victory/r1780" +VMEXEC="$REPO/scripts/vgpu_vm.py" +MODULE="${MODULE:-$REPO/kernel/vgsp.ko}" +INSMOD_PARAMS="${INSMOD_PARAMS:-$HERE/params-r1624.txt}" +GZ_SECS="${GZ_SECS:-120}" +GLF_FRAMES="${GLF_FRAMES:-2}" +STOP_ON="${STOP_ON:-px}" +MAX_FETCH_MB="${MAX_FETCH_MB:-512}" +STAMP="$(date +%Y%m%d-%H%M%S)" +SUMMARY="/tmp/opencode/batch-$STAMP.log" +mkdir -p "$BACKUP" /tmp/opencode + +say() { echo "[$(date +%T)] $*" | tee -a "$SUMMARY"; } + +gexec() { # gexec + local t="$1"; shift + timeout $((t + 15)) sudo python3 "$VMEXEC" exec --timeout "$t" "$*" 2>/dev/null +} + +guest_done() { + local out + out=$(gexec 20 'cat /tmp/mission-done 2>/dev/null') + case "$out" in + *DONE*|*X-FAIL*) echo "$out" | tail -1; return 0 ;; + esac + return 1 +} + +analyze_tag() { # analyze_tag + local tag="$1" dm="$2" gl="$3" gz="$4" + local m247 fire busy clean ds poison px frames + m247=$(grep -c "MME-CALL-HOLD .*window=247" "$dm" 2>/dev/null) + fire=$(grep -c "MME-CARRY-FIRE" "$dm" 2>/dev/null) + busy=$(grep -c "CANCEL reason=request-busy" "$dm" 2>/dev/null) + poison=$(grep -c "ds_after=badf1201\|->badf1201" "$dm" 2>/dev/null) + clean=$(grep -E "DS-FAST2.*ds_after=8[01]" "$dm" 2>/dev/null | grep -v badf1201 | tail -3) + ds=$(grep -c "DS-FAST" "$dm" 2>/dev/null) + frames=$(grep -c "^FRAME seq" "$gl" 2>/dev/null) + px=$(grep -E "^FRAME seq" "$gl" 2>/dev/null | grep -vc "px0=0* ") + # px0=00000000 means black; any other 8-hex value is non-black. + px=$(grep -E "^FRAME seq" "$gl" 2>/dev/null | grep -Evc "px0=00000000") + { + echo "TAG=$tag" + echo "m247=$m247 fire=$fire busy=$busy poison=$poison ds=$ds frames=$frames px_nonblack=$px" + [ -n "$clean" ] && echo "CLEAN_DS: $clean" + grep -E "MME-CARRY-(ARM|FIRE|CANCEL|REARM)" "$dm" 2>/dev/null | tail -6 + grep -E "DS-FAST2" "$dm" 2>/dev/null | tail -3 + grep -E "^(FRAME seq|XOK|DONE|glf rc|gz rc|XORG-ALIVE)" "$gl" "$gz" 2>/dev/null | tail -8 + } > "$BACKUP/$tag-summary.txt" + say "$tag: m247=$m247 fire=$fire busy=$busy ds=$ds frames=$frames px_nonblack=$px" + grep -E "CLEAN_DS|DS-FAST2" "$BACKUP/$tag-summary.txt" | sed 's/^/ /' | tee -a "$SUMMARY" +} + +RC=0 +for TAG in "$@"; do + say "=== $TAG start (GZ_SECS=$GZ_SECS GLF_FRAMES=$GLF_FRAMES params=$(basename "$INSMOD_PARAMS")) ===" + DM_START=$(sudo dmesg 2>/dev/null | wc -l) + if ! INSMOD_PARAMS="$INSMOD_PARAMS" bash "$HERE/cycle-r1780.sh" "$TAG" "$MODULE" \ + > "$BACKUP/$TAG-cycle.log" 2>&1; then + say "$TAG CYCLE FAILED (see $BACKUP/$TAG-cycle.log)" + tail -5 "$BACKUP/$TAG-cycle.log" | sed 's/^/ /' | tee -a "$SUMMARY" + RC=1 + continue + fi + say "$TAG cycle up" + bash "$HERE/push_gl_tools.sh" > "$BACKUP/$TAG-push.log" 2>&1 + if grep -q "PUSH-FAIL" "$BACKUP/$TAG-push.log"; then + say "$TAG push had failures (guest agent?) - continuing" + fi + + gexec 30 "rm -f /tmp/mission-done; nohup env GZ_SECS=$GZ_SECS GLF_FRAMES=$GLF_FRAMES bash /tmp/mission-gz.sh >/tmp/mission-run.log 2>&1 & echo MISSION_STARTED" >/dev/null + WAITMAX=$(( (GZ_SECS + 300) / 5 )) + DONE="" + for i in $(seq 1 "$WAITMAX"); do + if ! pgrep -f qemu-system-x86_64 >/dev/null; then + say "$TAG QEMU died before mission-done" + break + fi + DONE=$(guest_done) && break + sleep 5 + done + if [ -z "$DONE" ]; then + say "$TAG mission-done timeout after ~$((WAITMAX * 5))s" + else + say "$TAG mission: $DONE" + fi + + sudo dmesg 2>/dev/null | tail -n +$((DM_START + 1)) > "$BACKUP/$TAG-dmesg.log" + gexec 30 'cat /tmp/mission-gl.log 2>/dev/null' > "$BACKUP/$TAG-gl.log" + gexec 30 'cat /tmp/mission-gz.log 2>/dev/null' > "$BACKUP/$TAG-gz.log" + gexec 30 'tail -c 3000 /tmp/mission-run.log 2>/dev/null' > "$BACKUP/$TAG-run.log" + + GZSZ=$(gexec 20 'stat -c%s /dev/shm/gzframes.bin 2>/dev/null' | tr -dc '0-9') + GZSZ="${GZSZ:-0}" + say "$TAG /dev/shm/gzframes.bin=$GZSZ bytes" + if [ "$GZSZ" -gt 4096 ] && [ "$GZSZ" -lt $((MAX_FETCH_MB * 1024 * 1024)) ]; then + if timeout 300 sudo python3 "$HERE/qga_fetch.py" /dev/shm/gzframes.bin \ + "$BACKUP/$TAG-gzframes.bin" > "$BACKUP/$TAG-fetch.log" 2>&1; then + say "$TAG captured $BACKUP/$TAG-gzframes.bin" + else + say "$TAG fetch FAILED (see $BACKUP/$TAG-fetch.log)" + fi + fi + + analyze_tag "$TAG" "$BACKUP/$TAG-dmesg.log" "$BACKUP/$TAG-gl.log" "$BACKUP/$TAG-gz.log" + + PXN=$(grep -E "^FRAME seq" "$BACKUP/$TAG-gl.log" 2>/dev/null | grep -Evc "px0=00000000") + if [ "$STOP_ON" = "px" ] && [ "$PXN" -gt 0 ]; then + say "STOP: $TAG produced non-black RT pixels (px_nonblack=$PXN)" + break + fi + if [ "$STOP_ON" = "clean247" ] && grep -q "CLEAN_DS" "$BACKUP/$TAG-summary.txt" 2>/dev/null; then + say "STOP: $TAG produced a clean maps DS read" + break + fi +done +say "=== batch finished; summary $SUMMARY ===" +exit "$RC" \ No newline at end of file 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); } -- cgit v1.2.3