aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit f64d86c494609adb0b3774ecb1becf22d06dd501
parent 46c0100f179a34ef1d63274cd1d92501ddf3a312
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 06:41:48 +0000

Add consecutive browser frame capture for transient rendering faults

Diffstat:
Msrc/Makefile.wasm | 7+++++++
Msrc/browser/index.html | 90++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/runtime/gl_renderer.c | 23+++++++++++++++++++++++
Asrc/tests/browser_capture.c | 40++++++++++++++++++++++++++++++++++++++++
Asrc/tests/browser_capture.html | 22++++++++++++++++++++++
5 files changed, 181 insertions(+), 1 deletion(-)

diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -78,3 +78,10 @@ build/wasm/skinning.js: tests/skinning.c game/bbr.dll/lifted-0061.c runtime/gues build/wasm/ground-height.js: tests/ground_height.c game/bbr.dll/lifted-0013.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + +build/wasm/web/capture-test.html: tests/browser_capture.html build/wasm/web/capture-test.js + cp $< $@ + +build/wasm/web/capture-test.js: tests/browser_capture.c runtime/gl_renderer.c runtime/gl_renderer.h Makefile.wasm + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=1 -sOFFSCREENCANVAS_SUPPORT -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -o $@ diff --git a/src/browser/index.html b/src/browser/index.html @@ -12,6 +12,14 @@ </style> <header><button id="start">Enter Rubi-Ka</button> <span id="status">Ready</span></header> <canvas id="canvas" width="1024" height="768" tabindex="0"></canvas> +<details style="padding:12px"> +<summary>Frame capture</summary> +<p>Records every game frame. This slows rendering; use it to inspect glitches, not measure FPS.</p> +<label>Frames <input id="capture-count" type="number" min="1" max="120" value="60" required></label> +<button id="capture" disabled>Record frames</button> +<span id="capture-status"></span> +<a id="capture-download" hidden download="ao-frames.html">Download frame report</a> +</details> <pre id="log"></pre> <script> const status = document.querySelector('#status'); @@ -22,7 +30,87 @@ print: message => { log.textContent += message + '\n'; }, printErr: message => { log.textContent += message + '\n'; }, onAbort: message => { status.textContent = 'Stopped: ' + message; }, - onRuntimeInitialized: () => { status.textContent = 'Client running'; canvas.focus(); } + onRuntimeInitialized: () => { status.textContent = 'Client running'; document.querySelector('#capture').disabled = false; canvas.focus(); } + }; + const captureButton = document.querySelector('#capture'); + const captureStatus = document.querySelector('#capture-status'); + const captureDownload = document.querySelector('#capture-download'); + let recording = null, reportURL = null; + + // Keep input times beside the frame sequence so turns can be replayed and inspected. + for (const type of ['keydown', 'keyup', 'mousedown', 'mouseup', 'mousemove']) { + canvas.addEventListener(type, event => { + if (recording) recording.inputs.push({time: performance.timeOrigin + performance.now(), type, + key: event.key, button: event.button, x: event.clientX, y: event.clientY}); + }); + } + function finishCapture() { + Module._browser_capture_frames(0); + const report = recording; + recording = null; + captureButton.textContent = 'Record frames'; + captureStatus.textContent = `Saved ${report.frames.length} frames.`; + if (!report.frames.length) return; + report.log = log.textContent; + // A standalone HTML report retains every PNG and works without the game server. + const data = JSON.stringify(report).replaceAll('<', '\\u003c'); + const viewer = `<!doctype html><meta charset="utf-8"><title>AO frame capture</title> +<style>body{background:#111;color:#ddd;font:16px sans-serif}img{max-width:100%}pre{white-space:pre-wrap}</style> +<button id="previous">Previous</button> <button id="next">Next</button> +<input id="frame" aria-label="Frame" type="range" min="0" max="${report.frames.length - 1}" value="0"> +<span id="label"></span><br><img id="image"><details><summary>Capture details</summary><pre id="details"></pre></details> +<script type="application/json" id="data">${data}<\/script> +<script> +const report=JSON.parse(document.querySelector('#data').textContent); +const slider=document.querySelector('#frame'); +function show() { + const index = Number(slider.value), frame = report.frames[index]; + document.querySelector('#image').src = frame.png; + document.querySelector('#label').textContent = (index+1)+' / '+report.frames.length+ + ' at '+(frame.time-report.frames[0].time).toFixed(1)+' ms'; +} +slider.oninput=show; +document.querySelector('#previous').onclick=()=>{slider.stepDown();show();}; +document.querySelector('#next').onclick=()=>{slider.stepUp();show();}; +document.onkeydown = event => { + if (event.key === 'ArrowLeft') slider.stepDown(); + else if (event.key === 'ArrowRight') slider.stepUp(); + else return; + event.preventDefault(); + show(); +}; +const {frames, ...metadata} = report; +document.querySelector('#details').textContent = JSON.stringify(metadata, null, 2); +show(); +<\/script>`; + reportURL = URL.createObjectURL(new Blob([viewer], {type: 'text/html'})); + captureDownload.href = reportURL; + captureDownload.hidden = false; + } + captureButton.onclick = () => { + if (recording) { finishCapture(); return; } + const count = document.querySelector('#capture-count'); + if (!count.reportValidity()) return; + if (reportURL) URL.revokeObjectURL(reportURL); + captureDownload.hidden = true; + recording = {requested: Number(count.value), started: new Date().toISOString(), + userAgent: navigator.userAgent, inputs: [], frames: []}; + captureButton.textContent = 'Stop recording'; + captureStatus.textContent = 'Waiting for game frames'; + Module._browser_capture_frames(recording.requested); + canvas.focus(); + }; + Module.captureFrame = (pixels, width, height, time) => { + if (!recording) return; + const image = new ImageData(width, height); + for (let row = 0; row < height; ++row) + image.data.set(pixels.subarray(row * width * 4, (row + 1) * width * 4), (height - row - 1) * width * 4); + const frame = document.createElement('canvas'); + frame.width = width; frame.height = height; + frame.getContext('2d').putImageData(image, 0, 0); + recording.frames.push({time, png: frame.toDataURL('image/png')}); + captureStatus.textContent = `Recorded ${recording.frames.length} / ${recording.requested}`; + if (recording.frames.length === recording.requested) finishCapture(); }; document.querySelector('#start').onclick = async event => { event.target.disabled = true; diff --git a/src/runtime/gl_renderer.c b/src/runtime/gl_renderer.c @@ -3,7 +3,15 @@ #ifdef __EMSCRIPTEN__ #include <GLES3/gl3.h> #include <emscripten.h> +#include <stdatomic.h> static double next_presentation_check; +static atomic_int capture_remaining; + +EMSCRIPTEN_KEEPALIVE void browser_capture_frames(int count) +{ + /* Bound diagnostic readback memory, and allow an unfinished capture to stop. */ + atomic_store(&capture_remaining, count > 0 && count <= 120 ? count : 0); +} #define SHADER_VERSION "#version 300 es\nprecision highp float;\nprecision highp int;\n" #else #include <SDL2/SDL_opengl.h> @@ -416,6 +424,21 @@ void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int #ifdef __EMSCRIPTEN__ /* WebGL error queries synchronize with the GPU. Check presentation once a * second; allocation and readback errors are still checked immediately. */ + /* Capture each actual presentation on the GL worker, before the browser + * can discard its default framebuffer. Readback is opt-in and synchronous. */ + int remaining = atomic_load(&capture_remaining); + if (remaining > 0) { + unsigned char *pixels = malloc((size_t)width * height * 4); + assert(pixels); + glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + check_graphics("frame capture"); + MAIN_THREAD_EM_ASM({ + Module.captureFrame(HEAPU8.slice($0, $0 + $1 * $2 * 4), $1, $2, $3); + }, pixels, width, height, EM_ASM_DOUBLE({ return performance.timeOrigin + performance.now(); })); + free(pixels); + /* A stop request from the UI must not be overwritten by this frame. */ + atomic_compare_exchange_strong(&capture_remaining, &remaining, remaining - 1); + } double now = emscripten_get_now(); if (now >= next_presentation_check) { check_graphics("frame rendering and presentation"); diff --git a/src/tests/browser_capture.c b/src/tests/browser_capture.c @@ -0,0 +1,40 @@ +#include "gl_renderer.h" +#include <assert.h> +#include <emscripten.h> +#include <emscripten/html5.h> + +void browser_capture_frames(int count); + +int main(void) +{ + EmscriptenWebGLContextAttributes attributes; + emscripten_webgl_init_context_attributes(&attributes); + attributes.majorVersion = 2; + attributes.antialias = 0; + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#canvas", &attributes); + assert(context > 0); + assert(emscripten_webgl_make_context_current(context) == EMSCRIPTEN_RESULT_SUCCESS); + assert(gl_renderer_initialize()); + struct gl_target *target = gl_target_create(2, 2, 1); + const int32_t rectangle[] = {0, 0, 2, 2}; + const unsigned char pixels[] = {255, 0, 0, 255, 0, 255, 0, 255, + 0, 0, 255, 255, 255, 255, 255, 255}; + gl_target_write(target, pixels); + + browser_capture_frames(2); + gl_target_present(target, rectangle, 2, 2); + gl_target_clear(target, 0xffffff00, 1); + gl_target_present(target, rectangle, 2, 2); + gl_target_clear(target, 0xff000000, 1); + gl_target_present(target, rectangle, 2, 2); + browser_capture_frames(2); + browser_capture_frames(0); + gl_target_present(target, rectangle, 2, 2); + browser_capture_frames(121); + gl_target_present(target, rectangle, 2, 2); + + MAIN_THREAD_EM_ASM({ Module.assertCapture(); }); + gl_target_destroy(target); + gl_renderer_shutdown(); + emscripten_webgl_destroy_context(context); +} diff --git a/src/tests/browser_capture.html b/src/tests/browser_capture.html @@ -0,0 +1,22 @@ +<!doctype html> +<meta charset="utf-8"> +<title>Browser frame capture test</title> +<canvas id="canvas" width="2" height="2"></canvas> +<pre id="result">Running</pre> +<script> +const frames = []; +var Module = { + canvas: document.querySelector('#canvas'), + onAbort: message => { document.querySelector('#result').textContent = 'FAIL: ' + message; }, + captureFrame: (pixels, width, height, time) => { frames.push({pixels: Array.from(pixels), width, height, time}); }, + assertCapture: () => { + const expected = [[0, 0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 0, 255, 0, 255], + [255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255]]; + if (JSON.stringify(frames.map(frame => frame.pixels)) !== JSON.stringify(expected) || + frames.some(frame => frame.width !== 2 || frame.height !== 2) || frames[1].time < frames[0].time) + throw new Error('Capture omitted, reordered, inverted or added frames'); + document.querySelector('#result').textContent = 'PASS: exact consecutive frames, orientation, count, cancellation and bound'; + } +}; +</script> +<script src="capture-test.js"></script>