aorecomp

Anarchy Online classic in the browser

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


commit 507b8cf716e6cadec31b8e1e9168e138751642f4
parent 2e401fa3113517c9af8d71c623b273da2ffbd93c
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 00:35:21 +0000

Support live browser viewport resizing

Diffstat:
Mdocs/browser-porting.md | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/Makefile.linux | 4++++
Msrc/Makefile.wasm | 10+++++++++-
Msrc/browser/index.html | 22++++++++++++++++++----
Msrc/game/Clientr.exe/lifted-0000.c | 4++++
Msrc/game/bbr.dll/lifted-0037.c | 36++++++++++++++++++++++++++++++++++++
Msrc/runtime/browser_host.c | 17+++++++++++++++++
Asrc/runtime/browser_resolution.c | 253+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/runtime/linux_graphics.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/runtime/linux_graphics.h | 1+
Msrc/runtime/linux_window.c | 7+++++++
Msrc/runtime/linux_window.h | 1+
Asrc/tests/browser_resolution.c | 220+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/tests/browser_storage.c | 21+++++++++++++++++++++
Msrc/tests/devices.c | 92++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
15 files changed, 799 insertions(+), 6 deletions(-)

diff --git a/docs/browser-porting.md b/docs/browser-porting.md @@ -1126,3 +1126,66 @@ does not provide a browser worker launcher. See [native thread contracts](browse for the ACE entry adapter, lock ordering and shared-WASM requirements. Resource-worker execution must be verified before treating resource loading or world rendering as working. + +## Live browser viewport size + +The browser canvas fills the viewport and renders one pixel per CSS pixel. +The private `Prefs/Main.cfg` receives the canvas dimensions before the guest +starts. The browser build accepts modes below the original 640x480 filter; +native mode selection is unchanged. The original UI keeps its pixel size, +so very small viewports can clip controls rather than scaling them. + +`ResizeObserver` publishes the latest width and height through an atomic +request. `browser_resize_frame` consumes it on the game worker before +Clientr's `sub_28002D20` starts the next frame. It preserves guest registers, +flags and x87 state, coalesces resize events, and defers while a D3D scene is +active. JavaScript never changes the worker-owned backing canvas directly. + +The resize updates the desktop surface, CPU drawing surface, Randy's color +and depth buffers, viewport and camera aspect, sprite clipping, and UI bounds. +Old dirty rectangles are discarded before they can blit outside smaller +buffers. The status bar follows the bottom edge; the equipment panel follows +the right edge and rebuilds its tile grid while preserving the backing +`SpriteInfo_t` address retained by text widgets. Inventory, map and chat retain +their placement rules. Native Direct3D device slot 8 now implements +`SetRenderTarget`, retaining the replacement before releasing the old target +and preserving the viewport. + +The readable layout bridge is `src/runtime/browser_resolution.c`. Its guest +object layout comes from these 14.9.1 symbols (addresses in the lifted tree): + +| Responsibility | Symbol | +| --- | --- | +| Display and CPU drawing surface | BBR `sub_25002770`, `sub_250B0850` | +| Back/depth buffers | BBR `sub_250D6010` | +| Camera viewport | BBR `sub_250A5F40`, `sub_250DF8D0` | +| Dirty rectangle queues | BBR `sub_250041F0`, `sub_250B0B00` | +| Status bar | DDR `sub_2B081C40` | +| Equipment panel | DDR `sub_2B03AAF0`, `sub_2B03B110` | +| Window tile storage | CCR `sub_270181E0` | +| Inventory, map, chat placement | DDR `sub_2B03E740`, `sub_2B07D180`, `sub_2B004560` | + +Focused checks: + +```sh +make -C src -f Makefile.linux build/linux/browser-resolution build/linux/devices +src/build/linux/browser-resolution +src/build/linux/devices /tmp/devices.rgb +make -C src -f Makefile.wasm build/wasm/browser-resolution.js build/wasm/web/devices-test.html build/wasm/web/storage-test.html +toolchains/emsdk/node/24.19.0_64bit/bin/node src/build/wasm/browser-resolution.js +``` + +Run the two HTML tests through a server with the same cross-origin isolation +headers as the game. Full-client validation must also cover odd dimensions, +portrait startup, repeated grow/shrink, resizing during loading, and movement +with inventory/map open. Frame capture reports retain actual backing pixel +dimensions. Wait for the full build to exit successfully before copying a +matching JS/WASM pair into a test server or publishing it. + +The optimized client was checked in Chromium with portrait startup at 391x845 +and repeated live sizes from 319x239 through 2560x1440, across Newland, Lush Fields +and Bronto Burger. Inventory, map and equipment stayed open during resizing; +turning, walking and server updates continued. A captured PNG confirmed the +requested 1373x773 backing size. Native build/device checks and the WASM request, +storage and WebGL device checks passed. Firefox/macOS and Windows were not run +for this change. diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -16,6 +16,10 @@ build/linux/game: $(OBJECTS) objects: $(OBJECTS) +build/linux/browser-resolution: tests/browser_resolution.c runtime/browser_resolution.c runtime/guest_memory.c runtime/recomp_state.c + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $^ -lm -o $@ + build/linux/objects/runtime/linux_gdi.o: CPPFLAGS += $(shell pkg-config --cflags freetype2 fontconfig) build/linux/objects/%.o: %.c diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Igame -Idata -DRECOMP_PORTABLE # Keep exception landing pads in Wasm so JSPI can yield through guest calls. CFLAGS := -O2 -fno-strict-aliasing -pthread -sSUPPORT_LONGJMP=wasm PORT_FLAGS := -sUSE_SDL=2 -sUSE_FREETYPE=1 -RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c runtime/browser_profile.c +RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c runtime/browser_resolution.c runtime/browser_profile.c GAME_SOURCES := $(wildcard game/*/*.c) game/dispatch.c DATA_SOURCES := $(wildcard data/*.c) VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c) @@ -21,6 +21,14 @@ build/wasm/web/resource-dispatch-test.html: tests/browser_resource_queue.c runti @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $^ -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -o $@ +build/wasm/browser-resolution.js: tests/browser_resolution.c runtime/browser_resolution.c runtime/guest_memory.c runtime/recomp_state.c + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $^ -o $@ + +build/wasm/web/devices-test.html: tests/devices.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/browser_profile.c runtime/browser_host.js runtime/browser_profile.js Makefile.wasm + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sUSE_SDL=2 -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -sOFFSCREENCANVAS_SUPPORT -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sJSPI -sEXIT_RUNTIME --js-library runtime/browser_host.js --js-library runtime/browser_profile.js -o $@ + build/wasm/web/game.js build/wasm/web/game.wasm &: $(OBJECTS) runtime/browser_network.js runtime/browser_host.js runtime/browser_fetch.js runtime/browser_audio.js runtime/browser_profile.js ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a Makefile.wasm @mkdir -p $(@D) $(CC) $(CFLAGS) $(PORT_FLAGS) $(OBJECTS) ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a -sUSE_MPG123=1 -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=8 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sOFFSCREENCANVAS_SUPPORT -sJSPI -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sSTACK_SIZE=8388608 --js-library runtime/browser_network.js --js-library runtime/browser_host.js --js-library runtime/browser_fetch.js --js-library runtime/browser_audio.js --js-library runtime/browser_profile.js --emit-symbol-map -o build/wasm/web/game.js diff --git a/src/browser/index.html b/src/browser/index.html @@ -7,12 +7,16 @@ body { margin: 0; background: #111; color: #ddd; font: 16px sans-serif; } header { padding: 12px; } button { padding: 8px 16px; } - canvas { display: block; margin: auto; max-width: 100%; height: auto; } + html, body { width: 100%; height: 100%; overflow: hidden; } + canvas { display: block; width: 100%; height: 100%; } + #tools { position: fixed; z-index: 1; top: 4px; left: 16px; background: #222e; max-width: 90%; max-height: 90%; overflow: auto; } + #tools > summary { padding: 8px; cursor: pointer; } pre { padding: 12px; white-space: pre-wrap; } </style> +<canvas id="canvas" width="1024" height="768" tabindex="0"></canvas> +<details id="tools" open><summary>Tools</summary> <header><button id="start">Enter Rubi-Ka</button> <span id="status">Ready</span></header> <div id="profile-status" style="padding:0 12px"></div> -<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> @@ -27,6 +31,7 @@ </div> </details> <pre id="log"></pre> +</details> <script> const status = document.querySelector('#status'); const log = document.querySelector('#log'); @@ -35,8 +40,14 @@ canvas, print: message => { log.textContent += message + '\n'; }, printErr: message => { log.textContent += message + '\n'; }, - onAbort: message => { status.textContent = 'Stopped: ' + message; }, - onRuntimeInitialized: () => { status.textContent = 'Client running'; document.querySelector('#capture').disabled = false; canvas.focus(); } + onAbort: message => { status.textContent = 'Stopped: ' + message; document.querySelector('#tools').open = true; }, + onRuntimeInitialized: () => { + status.textContent = 'Client running'; + document.querySelector('#capture').disabled = false; + document.querySelector('#tools').open = false; + new ResizeObserver(() => Module._browser_request_resize(canvas.clientWidth, canvas.clientHeight)).observe(canvas); + canvas.focus(); + } }; const captureButton = document.querySelector('#capture'); const captureStatus = document.querySelector('#capture-status'); @@ -144,6 +155,9 @@ show(); const response = await fetch('/browser-launch', {method: 'POST'}); if (!response.ok) { status.textContent = await response.text(); event.target.disabled = false; return; } Module.arguments = (await response.text()).trim().split(' ').slice(1); + // Match CSS pixels; the render worker owns the backing canvas after startup. + canvas.width = canvas.clientWidth; + canvas.height = canvas.clientHeight; const script = document.createElement('script'); script.src = 'game.js'; script.onerror = () => { status.textContent = 'Could not load the client'; }; diff --git a/src/game/Clientr.exe/lifted-0000.c b/src/game/Clientr.exe/lifted-0000.c @@ -102744,6 +102744,10 @@ L_28002C90: } void sub_28002D20(void) { +#ifdef __EMSCRIPTEN__ + extern void browser_resize_frame(void); + browser_resize_frame(); +#endif int _fpu_cmp = 0; uint32_t _cf = 0; int _df = 1; diff --git a/src/game/bbr.dll/lifted-0037.c b/src/game/bbr.dll/lifted-0037.c @@ -6181,6 +6181,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -7037,6 +7041,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -7662,6 +7670,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -8287,6 +8299,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -8912,6 +8928,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -9537,6 +9557,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -10162,6 +10186,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -10787,6 +10815,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); @@ -11412,6 +11444,10 @@ L_250A650A: L_250A651F: esi = edi + 0x1B8; /* 0x250A651F: lea esi, [edi + 0x1b8] */ L_250A6525: +#ifdef __EMSCRIPTEN__ + /* Browser viewports are not constrained to 640x480 monitor modes. */ + goto L_250A6535; +#endif /* cmp MEM16(esi + (-0x4)), 0x280u */ /* 0x250A6525: cmp word ptr [esi - 4], 0x280 */ _flag_a = (uint32_t)(MEM16(esi + (-0x4))) << 16; _flag_b = (uint32_t)(0x280u) << 16; _cf = (uint32_t)CMP_B(_flag_a, _flag_b); diff --git a/src/runtime/browser_host.c b/src/runtime/browser_host.c @@ -8,6 +8,7 @@ #include <pthread.h> #include <stdint.h> #include <emscripten/proxying.h> +#include <emscripten/html5.h> char *ao_asset_manifest(const char *base); char *ao_asset_base_url(void); @@ -95,6 +96,22 @@ void browser_initialize(void) } } free(manifest); + /* Start the guest with the viewport size, including nonstandard aspect ratios. */ + int width, height; + assert(emscripten_get_canvas_element_size("#canvas", &width, &height) == EMSCRIPTEN_RESULT_SUCCESS); + FILE *preferences = fopen("Prefs/Main.cfg", "r"); + FILE *resized = fopen("Prefs/Main.cfg.new", "w"); + assert(preferences && resized); + char line[4096]; + while (fgets(line, sizeof(line), preferences)) { + if (!strncmp(line, "DisplayWidth ", 13)) fprintf(resized, "DisplayWidth %d\n", width); + else if (!strncmp(line, "DisplayHeight ", 14)) fprintf(resized, "DisplayHeight %d\n", height); + else fputs(line, resized); + } + assert(!ferror(preferences)); + fclose(preferences); + assert(fclose(resized) == 0); + assert(rename("Prefs/Main.cfg.new", "Prefs/Main.cfg") == 0); assert(symlink("/remote/Setupf", "Setupf") == 0); assert(symlink("/remote/fonts", "fonts") == 0); assert(symlink("/remote/version.id", "version.id") == 0); diff --git a/src/runtime/browser_resolution.c b/src/runtime/browser_resolution.c @@ -0,0 +1,253 @@ +#include "recomp_types.h" +#include "linux_graphics.h" +#include "guest_heap.h" +#include <assert.h> +#include <stdatomic.h> +#ifdef __EMSCRIPTEN__ +#include <emscripten.h> +#else +#define EMSCRIPTEN_KEEPALIVE +#endif + +static _Atomic uint32_t requested_size; + +/* The browser main thread only publishes a request; the game owns all objects. */ +EMSCRIPTEN_KEEPALIVE void browser_request_resize(unsigned width, unsigned height) +{ + if (!width || !height || width > 65535 || height > 65535) return; + atomic_store(&requested_size, width | (height << 16)); +} + +static void call_method(uint32_t address, uint32_t object, unsigned count, + const uint32_t *arguments) +{ + uint32_t stack = g_esp; + for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]); + PUSH32(g_esp, RECOMP_RETADDR); + g_ecx = object; + recomp_lookup(address)(); + assert(g_esp == stack); +} + +static void move_point(uint32_t method, uint32_t object, int x, int y) +{ + g_esp -= 8; + MEM32(g_esp) = x; + MEM32(g_esp + 4) = y; + call_method(method, object, 1, (uint32_t[]){g_esp}); + g_esp += 8; +} + +/* Layout from src/game/ddr.dll/lifted-0033.c:sub_2B081C40 (status bar). */ +struct status_bar { + uint32_t detector, options, help, inventory, skills, map, chat, menu; + int height; + uint32_t uplink, uplink_text, uplink_value, transfer, transfer_text, transfer_detector; + uint32_t money_text, money, money_detector, load, load_fill; + uint32_t load_state[3], camera[4], camera_detector, camera_mode; + uint32_t distance, distance_fill, distance_detector, distance_value, background; +}; + +static void resize_status_bar(unsigned width, unsigned height) +{ + uint32_t address = MEM32(0x2b0ef09c); + if (!address) return; + struct status_bar *bar = (void *)ADDR(address); + int x = width, y = (int)height - bar->height; + uint32_t buttons[] = {bar->help, bar->options}; + for (unsigned i = 0; i < 2; ++i) { + x -= MEM32(buttons[i] + 0x44); + call_method(0x2700b2d0, buttons[i], 2, (uint32_t[]){x, y}); + } + x -= MEM32(MEM32(bar->uplink + 0x14) + 0xc); + move_point(0x2701cab0, bar->uplink, x, y); + call_method(0x27020dd0, bar->uplink_text, 2, (uint32_t[]){x + 24, y + 10}); + x -= MEM32(MEM32(bar->transfer + 0x14) + 0xc); + move_point(0x2701cab0, bar->transfer, x, y); + move_point(MEM32(MEM32(bar->transfer_detector) + 0x14), bar->transfer_detector, x, y); + call_method(0x27020dd0, bar->transfer_text, 2, (uint32_t[]){x + 130, y + 6}); + + uint32_t menu_buttons[] = {bar->menu, bar->chat, bar->inventory, bar->skills, bar->map}; + for (unsigned i = 0; i < 5; ++i) { + x -= MEM32(menu_buttons[i] + 0x44); + call_method(0x2700b2d0, menu_buttons[i], 2, (uint32_t[]){x, y}); + } + x -= MEM32(MEM32(bar->money + 0x14) + 0xc); + move_point(0x2701cab0, bar->money, x, y); + move_point(MEM32(MEM32(bar->money_detector) + 0x14), bar->money_detector, x, y); + move_point(0x2b076030, bar->money_text, x + 3, y + 3); + x -= 8 + MEM32(MEM32(bar->load + 0x14) + 0xc); + move_point(0x2701cab0, bar->load, x, y); + move_point(0x250ae720, bar->load_fill, x + 4, y + 3); + x -= MEM32(MEM32(bar->camera[0] + 0x14) + 0xc); + for (unsigned i = 0; i < 4; ++i) move_point(0x2701cab0, bar->camera[i], x, y); + move_point(MEM32(MEM32(bar->camera_detector) + 0x14), bar->camera_detector, x, y); + x -= MEM32(MEM32(bar->distance + 0x14) + 0xc); + move_point(0x2701cab0, bar->distance, x, y); + move_point(0x250ae720, bar->distance_fill, x + 4, y + 3); + move_point(MEM32(MEM32(bar->distance_detector) + 0x14), bar->distance_detector, x, y); + + /* Rebuild the tiled strip so growth allocates tiles and shrink releases them. */ + move_point(MEM32(MEM32(bar->detector) + 0x14), bar->detector, 0, y); + move_point(MEM32(MEM32(bar->detector) + 0x18), bar->detector, width, bar->height); + unsigned minimum = MEM32(MEM32(MEM32(bar->background) + 0x14) + 0xc) + + MEM32(MEM32(MEM32(bar->background + 4) + 0x14) + 0xc) + 1; + call_method(0x27026160, bar->background, 0, NULL); + call_method(0x27025ec0, bar->background, 8, + (uint32_t[]){0, y, width > minimum ? width : minimum, 1, 0x62, 0x62, 0x62, 10000}); +} + +static void resize_side_panel(unsigned width, unsigned height) +{ + uint32_t panel = MEM32(0x2b0ee9b8); + if (!panel) return; + unsigned available = height > 20 ? height - 20 : 1; + uint32_t window = MEM32(0x2b0ee9dc); + int active = MEM8(window + 0x24); + MEM32(0x2b0ee9d4) = available; + + /* CCR RenderWindow_t::Resize cannot grow its tile grid; rebuild its area. */ + if (MEM32(window + 0xc) != (available + 63) / 64) { + /* TextOutput_t retains this SpriteInfo_t, so keep its address stable. */ + uint32_t image = MEM32(window + 0x28); + MEM32(window + 0x28) = 0; + g_esp -= 8; + MEM32(g_esp) = 192; + MEM32(g_esp + 4) = available; + call_method(0x270181e0, window, 3, (uint32_t[]){6, 6, g_esp}); + g_esp += 8; + uint32_t replacement = MEM32(window + 0x28); + guest_free(MEM32(image)); + memcpy((void *)ADDR(image), (void *)ADDR(replacement), 0x34); + guest_free(replacement); + MEM32(window + 0x28) = image; + call_method(0x250af340, image, 3, (uint32_t[]){0, 0, 1}); + } + move_point(0x27018620, window, (int)width - 192, 0); + call_method(0x2b03b110, panel, 0, NULL); + if (!active) call_method(0x27018890, window, 0, NULL); + move_point(0x27018620, MEM32(MEM32(panel + 0x70)), (int)width - 192, 0); + + uint32_t border = MEM32(panel + 0x54); + unsigned minimum = MEM32(MEM32(MEM32(border) + 0x14) + 8) + + MEM32(MEM32(MEM32(border + 4) + 0x14) + 8) + 1; + call_method(0x27026160, border, 0, NULL); + call_method(0x27025ec0, border, 8, + (uint32_t[]){width - 197, 0, available > minimum ? available : minimum, + 0, 0x16, 0x16, 0x16, 4000}); + if (MEM8(panel + 0x6c)) call_method(0x270262c0, border, 0, NULL); +} + +static void resize_windows(unsigned width, int height_change) +{ + /* DDR inventory, floating views, shortcut bar, planet map, and chat bounds. */ + const uint32_t guards[] = {0x2b0eea0c, 0x2b0eea04, 0x2b0eea6c, 0x2b0eee7c, + 0x2b0ef028, 0x2b0ef054, 0x2b0ef048, 0x2b0ef0c8}; + for (unsigned i = 0; i < sizeof(guards) / sizeof(guards[0]); ++i) MEM32(guards[i]) = 0; + + /* Preserve user positions, clamping through each window's own layout method. */ + uint32_t inventory = MEM32(0x2b0eea14); + if (inventory) { + uint32_t image = MEM32(MEM32(inventory + 0x14) + 0x14); + int right = (int)width - (int)MEM32(image + 0xc) - 192; + if (right < 0) right = 0; + if (MEM32(inventory + 0x20) == MEM32(inventory + 0x48)) MEM32(inventory + 0x20) = right; + MEM32(inventory + 0x48) = right; + call_method(0x2b03e740, inventory, 1, (uint32_t[]){inventory + 0x20}); + } + uint32_t map = MEM32(0x2b0ef094); + if (map) call_method(0x2b07d180, map, 2, (uint32_t[]){MEM32(map), MEM32(map + 4)}); + + /* Chat output and its input line retain their distance from the bottom edge. */ + uint32_t chat = MEM32(0x2b0ee6a4); + if (chat) { + uint32_t output = MEM32(chat + 0xc), window = MEM32(output + 0x14); + int y = (int)MEM32(window + 0x30) + height_change; + move_point(0x2b004560, output, MEM32(window + 0x2c), y > 0 ? y : 0); + uint32_t input = MEM32(chat + 8); + window = MEM32(input); + y = (int)MEM32(window + 0x30) + height_change; + move_point(0x27015910, input, MEM32(window + 0x2c), y > 0 ? y : 0); + } +} + +/* Called by Clientr.exe::sub_28002D20 before AFCM starts the next frame. */ +void browser_resize_frame(void) +{ + uint32_t size = atomic_load(&requested_size); + if (!size) return; + uint32_t display = MEM32(0x25238568), action = MEM32(0x2b0ee640); + if (!display || !MEM32(display + 0x38) || !MEM32(display + 0x34)) return; + unsigned width = size & 65535, height = size >> 16; + uint32_t info = MEM32(display + 8); + int height_change = (int)height - (int)MEM32(info + 4); + if (MEM32(info) == width && MEM32(info + 4) == height) { + atomic_compare_exchange_strong(&requested_size, &size, 0); + return; + } + if (!linux_graphics_resize(width, height, MEM32(MEM32(display + 0xd4)))) return; + atomic_compare_exchange_strong(&requested_size, &size, 0); + + /* Preserve the interrupted guest CPU, including x87 values and flags. */ + uint32_t registers[] = {g_eax, g_ecx, g_edx, g_ebx, g_esi, g_edi, g_ebp, + g_cur_func, g_flag_k, g_flag_a, g_flag_b, g_flag_cf}; + double fpu[8]; + int64_t integers[8]; + memcpy(fpu, g_st, sizeof(fpu)); + memcpy(integers, g_st_i64, sizeof(integers)); + int top = g_fp_top; + uint16_t control = g_fpu_cw; + + /* BBR DisplayInfo and Randy_t::ResizeBuffers own the full back/depth buffers. */ + MEM32(info) = width; + MEM32(info + 4) = height; + call_method(0x250d6010, MEM32(display + 0x34), 2, (uint32_t[]){width, height}); + uint32_t drawing_area = MEM32(display + 0x20); + MEM32(drawing_area + 0xc) = height; + MEM32(drawing_area + 0x10) = width; + /* Discard old dirty rectangles before any blit can reach the smaller surfaces. */ + for (unsigned i = 0; i < MEM32(drawing_area + 4); ++i) { + uint32_t rectangles = MEM32(MEM32(drawing_area + 0x24) + i * 24); + MEM32(rectangles + 8) = MEM32(rectangles + 4); + } + + /* CCR caches the screen dimensions for sprite clipping and relative layout. */ + MEM32(0x27048be8) = width; + MEM32(0x27048bec) = height; + MEM32(0x27048bc4) = 0; + MEM32(0x27048bd0) = 0; + if (action) { + /* DDR ActionView_t owns the side-panel and bottom-strip camera insets. */ + MEM32(action + 0x18) = width; + MEM32(action + 0x1c) = height; + unsigned view_width = MEM8(0x2b0ee645) ? width : (width > 197 ? width - 197 : 1); + unsigned view_height = height > 20 ? height - 20 : 1; + MEM32(action) = MEM32(action + 4) = 0; + MEM32(action + 8) = view_width; + MEM32(action + 12) = view_height; + call_method(0x250a5f40, display, 2, (uint32_t[]){view_width, view_height}); + call_method(0x250a5ff0, display, 2, (uint32_t[]){0, 0}); + + /* Move the world mouse detector with the viewport, then notify its listeners. */ + uint32_t detector = MEM32(action + 0x10), methods = MEM32(detector); + call_method(MEM32(methods + 0x14), detector, 1, (uint32_t[]){action}); + call_method(MEM32(methods + 0x18), detector, 1, (uint32_t[]){action + 8}); + call_method(0x20003630, MEM32(0x20015cd4), 2, (uint32_t[]){0x12, 0x6e}); + } else { + call_method(0x250a5f40, display, 2, (uint32_t[]){width, height}); + } + resize_status_bar(width, height); + resize_side_panel(width, height); + resize_windows(width, height_change); + if (MEM32(0x27048be4)) call_method(0x2701d270, 0, 0, NULL); + + g_eax = registers[0]; g_ecx = registers[1]; g_edx = registers[2]; + g_ebx = registers[3]; g_esi = registers[4]; g_edi = registers[5]; + g_ebp = registers[6]; g_cur_func = registers[7]; g_flag_k = registers[8]; + g_flag_a = registers[9]; g_flag_b = registers[10]; g_flag_cf = registers[11]; + memcpy(g_st, fpu, sizeof(fpu)); + memcpy(g_st_i64, integers, sizeof(integers)); + g_fp_top = top; + g_fpu_cw = control; + fprintf(stderr, "[browser-resolution] %ux%u\n", width, height); +} diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -14,6 +14,7 @@ #include <errno.h> #include <time.h> #include <math.h> +#include <assert.h> /* Native graphics resources never appear in guest COM objects or vtables. */ static SDL_Window *window; @@ -179,6 +180,40 @@ int linux_graphics_display_mode(SDL_DisplayMode *mode) #endif } +#ifdef __EMSCRIPTEN__ +int linux_graphics_resize(unsigned width, unsigned height, uint32_t drawing_surface) +{ + if (!context || !width || !height || width > (unsigned)maximum_texture_size || + height > (unsigned)maximum_texture_size) return 0; + for (struct device_object *device = devices; device; device = device->next) + if (device->scene) return 0; + + /* Keep the desktop and CPU drawing surface identities while replacing storage. */ + for (struct surface_object *surface = surfaces; surface; surface = surface->next) { + if (!(surface->caps & 0x200) && surface->interfaces[0] != drawing_surface) continue; + assert(!surface->locked && !surface->depth && !surface->attached); + struct gl_target *target = gl_target_create(width, height, 1); + if (!target) abort(); + gl_target_destroy(surface->target); + surface->target = target; + surface->width = width; + surface->height = height; + surface->pitch = width * 4; + if (surface->pixels) { + guest_free(surface->pixels); + surface->pixels = guest_malloc(surface->pitch * height); + if (!surface->pixels) abort(); + memset((void *)ADDR(surface->pixels), 0, surface->pitch * height); + gl_target_write(surface->target, (void *)ADDR(surface->pixels)); + } + } + browser_display.w = width; + browser_display.h = height; + linux_window_resize(width, height); + return 1; +} +#endif + static struct draw_object *find_object(uint32_t address) { for (struct draw_object *object = objects; object; object = object->next) @@ -1592,6 +1627,24 @@ static void device_get_target(void) g_esp += 12; } +static void device_set_target(void) +{ + struct device_object *device = find_device(MEM32(g_esp + 4)); + uint32_t target = MEM32(g_esp + 8), flags = MEM32(g_esp + 12); + g_esp += 16; + g_eax = 0x80070057u; + if (!target || flags) return; + struct surface_object *surface = find_surface(target); + if (!surface->target || !(surface->caps & 0x2000) || surface->owner != device->owner) return; + + /* Retain before releasing, including when the device selects its current target. */ + ++surface->references[0]; + release_surface(device->target, 0); + device->target = surface; + gl_target_set_depth(surface->target, surface->attached ? surface->attached->depth : NULL); + g_eax = 0; +} + static void device_viewport(void) { struct device_object *device = find_device(MEM32(g_esp + 4)); @@ -2460,6 +2513,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 5) return device_begin_scene; if (current_slot == 6) return device_end_scene; if (current_slot == 7) return device_parent; + if (current_slot == 8) return device_set_target; if (current_slot == 9) return device_get_target; if (current_slot == 10) return device_clear; if (current_slot == 11 || current_slot == 12) return device_transform; diff --git a/src/runtime/linux_graphics.h b/src/runtime/linux_graphics.h @@ -10,5 +10,6 @@ struct SDL_Window; struct SDL_Window *linux_graphics_window(void); struct SDL_DisplayMode; int linux_graphics_display_mode(struct SDL_DisplayMode *mode); +int linux_graphics_resize(unsigned width, unsigned height, uint32_t drawing_surface); #endif diff --git a/src/runtime/linux_window.c b/src/runtime/linux_window.c @@ -1044,6 +1044,13 @@ static int read_message(uint32_t output, uint32_t window, uint32_t first, uint32 return found; } +void linux_window_resize(unsigned width, unsigned height) +{ + client.width = width; + client.height = height; + SDL_SetWindowSize(linux_graphics_window(), width, height); +} + static void retrieve_message(int wait) { uint32_t output = MEM32(g_esp + 4), window = MEM32(g_esp + 8); diff --git a/src/runtime/linux_window.h b/src/runtime/linux_window.h @@ -6,4 +6,5 @@ recomp_func_t linux_window_lookup(const char *dll, const char *name); struct SDL_Window; struct SDL_Window *linux_window_native(uint32_t handle); +void linux_window_resize(unsigned width, unsigned height); #endif diff --git a/src/tests/browser_resolution.c b/src/tests/browser_resolution.c @@ -0,0 +1,220 @@ +#include "recomp_types.h" +#include <assert.h> + +void browser_request_resize(unsigned width, unsigned height); +void browser_resize_frame(void); +static unsigned calls, width, height, resize_calls; +static int graphics_ready = 1, request_during_resize; +static unsigned freed_images, rebuilt_panels; + +void guest_free(uint32_t address) +{ + assert(address == (freed_images ? 0x50001300 : 0x50001400)); + ++freed_images; +} + +static void rebuild_panel(void) +{ + assert(g_ecx == 0x50001100); + assert(MEM32(g_ecx + 0x28) == 0); + MEM32(g_ecx + 0x28) = 0x50001300; + MEM32(g_ecx + 0xc) = 12; + MEM32(0x50001300) = 0x50001500; + MEM32(0x50001308) = 768; + MEM32(0x5000130c) = 192; + g_esp += 16; + ++rebuilt_panels; +} + +static void clear_panel(void) +{ + assert(g_ecx == 0x50001200); + assert(MEM32(g_ecx) == 0x50001500); + assert(freed_images == 2); + g_esp += 16; +} + +static void panel_no_arguments(void) { g_esp += 4; } +static void panel_one_argument(void) { g_esp += 8; } +static void panel_eight_arguments(void) { g_esp += 36; } + +int linux_graphics_resize(unsigned new_width, unsigned new_height, uint32_t drawing_surface) +{ + assert(drawing_surface == 0x50000780); + if (!graphics_ready) return 0; + width = new_width; + height = new_height; + ++resize_calls; + if (request_during_resize) browser_request_resize(913, 617); + return 1; +} + +static void resize_buffers(void) +{ + assert(calls++ == 0); + assert(g_ecx == 0x50000200); + assert(MEM32(g_esp + 4) == width && MEM32(g_esp + 8) == height); + assert(MEM32(0x50000100) == width && MEM32(0x50000104) == height); + g_esp += 12; + g_eax = 1; g_ecx = 2; g_edx = 3; g_ebx = 4; g_esi = 5; g_edi = 6; g_ebp = 7; + g_cur_func = 8; g_flag_k = 9; g_flag_a = 10; g_flag_b = 11; g_flag_cf = 12; + g_st[0] = -1; g_st_i64[0] = -2; g_fp_top = 3; g_fpu_cw = 4; +} + +static void reposition_viewport(void) +{ + assert(calls++ == 2); + assert(g_ecx == 0x50000000); + assert(MEM32(g_esp + 4) == 0 && MEM32(g_esp + 8) == 0); + g_esp += 12; +} + +static void resize_viewport(void) +{ + assert(calls++ == 1); + assert(g_ecx == 0x50000000); + assert(MEM32(g_esp + 4) == (width > 197 ? width - 197 : 1)); + assert(MEM32(g_esp + 8) == (height > 20 ? height - 20 : 1)); + assert(MEM32(0x27048be8) == width && MEM32(0x27048bec) == height); + assert(MEM32(0x27048bc4) == 0 && MEM32(0x27048bd0) == 0); + g_esp += 12; +} + +static void reposition_detector(void) +{ + assert(calls++ == 3); + assert(g_ecx == 0x50000400 && MEM32(g_esp + 4) == 0x50000300); + g_esp += 8; +} + +static void resize_detector(void) +{ + assert(calls++ == 4); + assert(g_ecx == 0x50000400 && MEM32(g_esp + 4) == 0x50000308); + g_esp += 8; +} + +static void notify_layout(void) +{ + assert(calls++ == 5); + assert(g_ecx == 0x50000600); + assert(MEM32(g_esp + 4) == 0x12 && MEM32(g_esp + 8) == 0x6e); + g_esp += 12; +} + +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x250d6010: return resize_buffers; + case 0x250a5ff0: return reposition_viewport; + case 0x250a5f40: return resize_viewport; + case 1: return reposition_detector; + case 2: return resize_detector; + case 0x20003630: return notify_layout; + case 0x270181e0: return rebuild_panel; + case 0x250af340: return clear_panel; + case 0x27018620: return panel_one_argument; + case 0x2b03b110: case 0x27018890: case 0x27026160: case 0x270262c0: + return panel_no_arguments; + case 0x27025ec0: return panel_eight_arguments; + default: abort(); + } +} + +int main(void) +{ + assert(guest_map("display globals", 0x25230000, 0x10000)); + assert(guest_map("sprite globals", 0x27040000, 0x10000)); + assert(guest_map("action globals", 0x2b0e0000, 0x10000)); + assert(guest_map("message globals", 0x20010000, 0x10000)); + assert(guest_map("resize objects", 0x50000000, 0x10000)); + MEM32(0x25238568) = 0x50000000; + MEM32(0x50000008) = 0x50000100; + MEM32(0x50000020) = 0x50000800; + MEM32(0x50000804) = 1; + MEM32(0x50000824) = 0x50000900; + MEM32(0x50000900) = 0x50000a00; + MEM32(0x50000a04) = 0x50000b00; + MEM32(0x50000a08) = 0x50000b10; + MEM32(0x50000034) = 0x50000200; + MEM32(0x50000038) = 0x50000280; + MEM32(0x500000d4) = 0x50000700; + MEM32(0x50000700) = 0x50000780; + MEM32(0x2b0ee640) = 0x50000300; + MEM32(0x50000310) = 0x50000400; + MEM32(0x50000400) = 0x50000500; + MEM32(0x50000514) = 1; + MEM32(0x50000518) = 2; + MEM32(0x20015cd4) = 0x50000600; + MEM32(0x27048bc4) = MEM32(0x27048bd0) = 3; + g_esp = 0x5000ff00; + g_eax = 101; g_ecx = 102; g_edx = 103; g_ebx = 104; g_esi = 105; g_edi = 106; g_ebp = 107; + g_cur_func = 108; g_flag_k = 109; g_flag_a = 110; g_flag_b = 111; g_flag_cf = 112; + g_st[0] = 123.5; g_st_i64[0] = 456; g_fp_top = 7; g_fpu_cw = 0x37f; + + browser_request_resize(1600, 900); + browser_request_resize(1001, 733); + browser_resize_frame(); + + assert(calls == 6 && resize_calls == 1 && width == 1001 && height == 733); + assert(MEM32(0x50000308) == 804 && MEM32(0x5000030c) == 713); + assert(MEM32(0x5000080c) == 733 && MEM32(0x50000810) == 1001); + assert(MEM32(0x50000a08) == 0x50000b00); + assert(g_esp == 0x5000ff00); + assert(g_eax == 101 && g_ecx == 102 && g_edx == 103 && g_ebx == 104); + assert(g_esi == 105 && g_edi == 106 && g_ebp == 107 && g_cur_func == 108); + assert(g_flag_k == 109 && g_flag_a == 110 && g_flag_b == 111 && g_flag_cf == 112); + assert(g_st[0] == 123.5 && g_st_i64[0] == 456 && g_fp_top == 7 && g_fpu_cw == 0x37f); + + calls = 0; + graphics_ready = 0; + browser_request_resize(801, 599); + + browser_resize_frame(); + assert(!calls && resize_calls == 1); + graphics_ready = 1; + request_during_resize = 1; + browser_resize_frame(); + calls = 0; + request_during_resize = 0; + browser_resize_frame(); + + assert(calls == 6 && resize_calls == 3 && width == 913 && height == 617); + browser_resize_frame(); + browser_request_resize(913, 617); + browser_resize_frame(); + browser_request_resize(0, 0); + browser_resize_frame(); + assert(calls == 6 && resize_calls == 3); + + calls = 0; + browser_request_resize(1, 1); + browser_resize_frame(); + + assert(calls == 6 && resize_calls == 4); + assert(MEM32(0x50000308) == 1 && MEM32(0x5000030c) == 1); + + calls = 0; + MEM32(0x2b0ee9b8) = 0x50001000; + MEM32(0x2b0ee9dc) = 0x50001100; + MEM32(0x50001128) = 0x50001200; + MEM32(0x50001200) = 0x50001400; + MEM32(0x50001070) = 0x50001600; + MEM32(0x50001600) = 0x50001700; + MEM32(0x50001054) = 0x50001800; + MEM32(0x50001800) = MEM32(0x50001804) = 0x50001900; + MEM32(0x50001914) = 0x50001a00; + MEM32(0x50001a08) = 4; + uint32_t text_destination = MEM32(0x50001128); + + browser_request_resize(1001, 733); + browser_resize_frame(); + + assert(rebuilt_panels == 1 && freed_images == 2); + assert(MEM32(0x50001128) == text_destination); + assert(MEM32(text_destination) == 0x50001500); + assert(MEM32(text_destination + 8) == 768 && MEM32(text_destination + 12) == 192); + assert(g_esp == 0x5000ff00); + puts("Browser resize request, frame boundary, and guest state checks passed"); + return 0; +} diff --git a/src/tests/browser_storage.c b/src/tests/browser_storage.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <string.h> #include <sys/stat.h> +#include <emscripten/html5.h> void browser_initialize(void); @@ -66,12 +67,32 @@ static void read_across_asset_chunk(void) assert(!memcmp(actual, expected, sizeof(expected))); } +static void preferences_use_canvas_dimensions(void) +{ + int width, height, configured_width = 0, configured_height = 0; + assert(emscripten_get_canvas_element_size("#canvas", &width, &height) == EMSCRIPTEN_RESULT_SUCCESS); + FILE *file = fopen("Prefs/Main.cfg", "r"); + assert(file); + char line[4096]; + + while (fgets(line, sizeof(line), file)) { + if (!strncmp(line, "DisplayWidth ", 13)) assert(sscanf(line + 13, "%d", &configured_width) == 1); + if (!strncmp(line, "DisplayHeight ", 14)) assert(sscanf(line + 14, "%d", &configured_height) == 1); + } + assert(!ferror(file)); + fclose(file); + + assert(configured_width == width && configured_height == height); +} + int main(void) { browser_initialize(); + preferences_use_canvas_dimensions(); replace_occlusion_cache(); create_occlusion_cache(); read_across_asset_chunk(); puts("PASS: browser occlusion caches preserve assets and support writes"); puts("PASS: asset reads preserve bytes across the 1 MiB boundary"); + puts("PASS: private preferences use the canvas dimensions"); } diff --git a/src/tests/devices.c b/src/tests/devices.c @@ -1510,6 +1510,88 @@ static void alpha_tests_preserve_rejected_color_and_depth(uint32_t draw, uint32_ assert(!invoke(device, 20, 3, 25u, 8u)); } +static void changing_render_targets_preserves_viewport_and_ownership(uint32_t draw, uint32_t device, uint32_t original) +{ + uint32_t desc = workspace() + 128, viewport = workspace() + 256; + const uint32_t initial_viewport[] = {0, 0, 32, 32, 0, 0x3f800000}; + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 7; + *memory(desc + 8) = 48; *memory(desc + 12) = 64; *memory(desc + 104) = 0x6040; + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t replacement = *memory(workspace()); + *memory(desc + 4) = 0x1007; *memory(desc + 104) = 0x24000; + const uint32_t format[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0}; + memcpy(memory(desc + 72), format, 32); + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t depth = *memory(workspace()); + assert(!invoke(replacement, 3, 2, depth)); + assert(invoke(depth, 2, 1) == 1); + + assert(!invoke(device, 8, 3, replacement, 0u)); + assert(!invoke(device, 8, 3, replacement, 0u)); + assert(!invoke(device, 15, 2, viewport)); + assert(!invoke(device, 9, 2, workspace())); + + assert(*memory(workspace()) == replacement); + assert(!memcmp(memory(viewport), initial_viewport, sizeof(initial_viewport))); + assert(invoke(replacement, 2, 1) == 2); + assert(invoke(replacement, 2, 1) == 1); + assert(invoke(original, 1, 1) == 2); + assert(invoke(original, 2, 1) == 1); + + *memory(viewport + 8) = 64; *memory(viewport + 12) = 48; + assert(!invoke(device, 13, 2, viewport)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff123456u, 0x3f800000u, 0u)); + *memory(desc) = 124; + assert(!invoke(replacement, 25, 5, 0u, desc, 0x11u, 0u)); + + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert((*memory(pixels + 47 * pitch + 63 * 4) & 0xffffff) == 0x123456); + assert(!invoke(replacement, 32, 2, 0u)); + assert(!invoke(device, 8, 3, original, 0u)); + memcpy(memory(viewport), initial_viewport, sizeof(initial_viewport)); + assert(!invoke(device, 13, 2, viewport)); + assert(!invoke(device, 9, 2, workspace()) && *memory(workspace()) == original); + assert(invoke(original, 2, 1) == 2); +} + +#ifdef __EMSCRIPTEN__ +static void browser_resize_reallocates_the_cpu_drawing_surface(uint32_t draw, uint32_t device) +{ + uint32_t desc = workspace() + 128, rectangle = workspace() + 256; + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 7; + *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x840; + assert(!invoke(draw, 6, 4, desc, workspace(), 0u)); + uint32_t surface = *memory(workspace()); + const uint32_t bounds[] = {0, 0, 101, 73}; + memcpy(memory(rectangle), bounds, sizeof(bounds)); + + assert(!invoke(device, 5, 1)); + assert(!linux_graphics_resize(101, 73, surface)); + assert(!invoke(device, 6, 1)); + assert(linux_graphics_resize(101, 73, surface)); + + assert(!invoke(surface, 25, 5, rectangle, desc, 0x801u, 0u)); + assert(*memory(desc + 8) == 73 && *memory(desc + 12) == 101); + assert(*memory(desc + 16) == 404); + uint32_t pixels = *memory(desc + 36); + *memory(pixels + 72 * 404 + 100 * 4) = 0xff123456; + assert(!invoke(surface, 32, 2, rectangle)); + assert(!invoke(surface, 25, 5, rectangle, desc, 0x11u, 0u)); + assert(*memory(*memory(desc + 36) + 72 * 404 + 100 * 4) == 0xff123456); + assert(!invoke(surface, 32, 2, rectangle)); + + assert(linux_graphics_resize(17, 19, surface)); + + assert(!invoke(surface, 25, 5, 0u, desc, 0x11u, 0u)); + assert(*memory(desc + 8) == 19 && *memory(desc + 12) == 17); + assert(*memory(desc + 16) == 68); + assert(!invoke(surface, 32, 2, 0u)); + assert(!invoke(surface, 2, 1)); +} +#endif + static void device_lifetime_and_rendering(const char *pixels_path) { const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; @@ -1552,6 +1634,10 @@ static void device_lifetime_and_rendering(const char *pixels_path) material_state_is_copied(device); transforms_copy_geometry_and_texture_matrices(device); light_definitions_and_enable_state_are_owned(device); + changing_render_targets_preserves_viewport_and_ownership(draw, device, color); +#ifdef __EMSCRIPTEN__ + browser_resize_reallocates_the_cpu_drawing_surface(draw, device); +#endif uint32_t identity = query(device, unknown); assert(!invoke(device, 9, 2, workspace())); @@ -1682,14 +1768,18 @@ static void device_lifetime_and_rendering(const char *pixels_path) int main(int argc, char **argv) { +#ifndef __EMSCRIPTEN__ assert(argc == 2); +#endif #ifndef _WIN32 assert(guest_map("device test", 0x50000000, 0x10000)); g_esp = 0x5000f000; #endif - device_lifetime_and_rendering(argv[1]); #ifdef __EMSCRIPTEN__ + device_lifetime_and_rendering("/devices.rgb"); linux_graphics_shutdown(); +#else + device_lifetime_and_rendering(argv[1]); #endif puts("device: ownership, viewport, scenes, clear, triangles, line lists, depth, textures, filtering and blending passed"); return 0;