commit 8077952bd1b156c9baf7bed180467fea1b869b3a
parent ce5e3680eeea6cf8630b7e0111df4b66b9467208
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 08:42:10 +0000
Optimize terrain tile height rejection while preserving guest state
Diffstat:
4 files changed, 197 insertions(+), 62 deletions(-)
diff --git a/src/Makefile.linux b/src/Makefile.linux
@@ -226,6 +226,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/ground-height
./build/linux/tile-corners
./build/linux/terrain-line
+ ./build/linux/terrain-tile
./build/linux/network-queue
./build/linux/vertex-upload
./build/linux/vertex-upload-paged
@@ -491,3 +492,9 @@ build/linux/vertex-initialization: tests/vertex_initialization.c game/bbr.dll/li
$(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@
test: build/linux/vertex-initialization
+
+build/linux/terrain-tile: tests/terrain_tile.c game/llr.dll/lifted-0023.c game/mr.dll/lifted-0000.c game/mr.dll/lifted-0001.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 -pthread -lm -o $@
+
+test: build/linux/terrain-tile
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -93,3 +93,7 @@ build/wasm/vertex-initialization.js: tests/vertex_initialization.c game/bbr.dll/
build/wasm/tile-corners.js: tests/tile_corners.c game/llr.dll/lifted-0021.c game/llr.dll/lifted-0022.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/terrain-tile.js: tests/terrain_tile.c game/llr.dll/lifted-0023.c game/mr.dll/lifted-0000.c game/mr.dll/lifted-0001.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 $@
diff --git a/src/game/llr.dll/lifted-0023.c b/src/game/llr.dll/lifted-0023.c
@@ -5950,66 +5950,26 @@ L_3803C220:
MEM32(ebp + (-0x28)) = 0; /* 0x3803C294: mov dword ptr [ebp - 0x28], 0 */
RECOMP_ICALL(0x38038370u); /* 0x3803C29B: call 0x38038370 */ /* not lifted */
RECOMP_FLAGS_IN();
- fp_push(*(float*)&MEM32(ebp + (-0x14))); /* 0x3803C2A0: fld dword ptr [ebp - 0x14] */
- _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(ebp + (-0x2C))); /* 0x3803C2A3: fcomp dword ptr [ebp - 0x2c] */
- fp_pop();
- SET_LO8(ebx, LO8(eax)); /* 0x3803C2A6: mov bl, al */
- edx = ebp + (-0x14); /* 0x3803C2A8: lea edx, [ebp - 0x14] */
- eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803C2AB: fnstsw ax */
- /* test HI8(eax), 0x41u */ /* 0x3803C2AD: test ah, 0x41 */
- _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(0x41u) << 24;
- _cf = 0;
- _flag_k = FK_NARROW(FK_TEST, 24);
- if (TEST_Z(_flag_a, _flag_b)) goto L_3803C2B5; /* 0x3803C2B0: je 0x3803c2b5 */
-L_3803C2B2:
- edx = ebp + (-0x2C); /* 0x3803C2B2: lea edx, [ebp - 0x2c] */
-L_3803C2B5:
- fp_push(*(float*)&MEM32(ebp + (-0x8))); /* 0x3803C2B5: fld dword ptr [ebp - 8] */
- ecx = ebp + (-0x8); /* 0x3803C2B8: lea ecx, [ebp - 8] */
- _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(ebp + (-0x20))); /* 0x3803C2BB: fcomp dword ptr [ebp - 0x20] */
- fp_pop();
- eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803C2BE: fnstsw ax */
- /* test HI8(eax), 0x41u */ /* 0x3803C2C0: test ah, 0x41 */
- _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(0x41u) << 24;
- _cf = 0;
- _flag_k = FK_NARROW(FK_TEST, 24);
- if (TEST_Z(_flag_a, _flag_b)) goto L_3803C2C8; /* 0x3803C2C3: je 0x3803c2c8 */
-L_3803C2C5:
- ecx = ebp + (-0x20); /* 0x3803C2C5: lea ecx, [ebp - 0x20] */
-L_3803C2C8:
- fp_push(*(float*)&MEM32(ecx)); /* 0x3803C2C8: fld dword ptr [ecx] */
- _fpu_cmp = FPU_CMP(_st[0], (double)*(float*)ADDR(edx)); /* 0x3803C2CA: fcomp dword ptr [edx] */
- fp_pop();
- eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803C2CC: fnstsw ax */
- /* test HI8(eax), 0x41u */ /* 0x3803C2CE: test ah, 0x41 */
- _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(0x41u) << 24;
- _cf = 0;
- _flag_k = FK_NARROW(FK_TEST, 24);
- if (TEST_Z(_flag_a, _flag_b)) goto L_3803C2D5; /* 0x3803C2D1: je 0x3803c2d5 */
-L_3803C2D3:
- ecx = edx; /* 0x3803C2D3: mov ecx, edx */
-L_3803C2D5:
- fp_push(*(float*)&MEM32(ecx)); /* 0x3803C2D5: fld dword ptr [ecx] */
- fp_push(*(float*)&MEM32(0x3806F02C)); /* 0x3803C2D7: fld dword ptr [0x3806f02c] */
- _fpu_cmp = FPU_CMP(_st[0], _st[1]); /* 0x3803C2DD: fcomp st(1) */
- fp_pop();
- eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803C2DF: fnstsw ax */
- /* test HI8(eax), 0x41u */ /* 0x3803C2E1: test ah, 0x41 */
- _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(0x41u) << 24;
- _cf = 0;
- _flag_k = FK_NARROW(FK_TEST, 24);
- if (TEST_NZ(_flag_a, _flag_b)) goto L_3803C302; /* 0x3803C2E4: jne 0x3803c302 */
-L_3803C2E6:
- fp_push(*(float*)&MEM32(0x3806F00C)); /* 0x3803C2E6: fld dword ptr [0x3806f00c] */
- _fpu_cmp = FPU_CMP(_st[0], _st[1]); /* 0x3803C2EC: fcomp st(1) */
- fp_pop();
- eax = (eax & 0xFFFF0000u) | ((_fpu_cmp >= 0x10000) ? (uint32_t)(_fpu_cmp & 0x4700) : _fpu_cmp == 2 ? 0x4500u : _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0x0000u); /* 0x3803C2EE: fnstsw ax */
- { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x3803C2F0: fstp st(0) */
- /* test HI8(eax), 0x41u */ /* 0x3803C2F2: test ah, 0x41 */
- _flag_a = (uint32_t)(HI8(eax)) << 24; _flag_b = (uint32_t)(0x41u) << 24;
- _cf = 0;
- _flag_k = FK_NARROW(FK_TEST, 24);
- if (TEST_NZ(_flag_a, _flag_b)) goto L_3803C304; /* 0x3803C2F5: jne 0x3803c304 */
+ /* llr.dll!n3Tilemap_t::Intersect_Tile: reject a tile below both ray
+ * endpoints. Keep the original selection order for equal/NaN heights. */
+ SET_LO8(ebx, LO8(eax));
+ edx = *(float *)ADDR(ebp - 20) > *(float *)ADDR(ebp - 44) ? ebp - 20 : ebp - 44;
+ ecx = *(float *)ADDR(ebp - 8) > *(float *)ADDR(ebp - 32) ? ebp - 8 : ebp - 32;
+ if (!(*(float *)ADDR(ecx) > *(float *)ADDR(edx))) ecx = edx;
+ double maximum_height = *(float *)ADDR(ecx);
+ _fpu_cmp = FPU_CMP((double)*(float *)ADDR(0x3806f02c), maximum_height);
+ if (_fpu_cmp == 1)
+ _fpu_cmp = FPU_CMP((double)*(float *)ADDR(0x3806f00c), maximum_height);
+
+ /* Retain the comparison status and the emptied x87 scratch slots. */
+ eax = (eax & 0xffff0000u) | (_fpu_cmp == 2 ? 0x4500u :
+ _fpu_cmp < 0 ? 0x0100u : _fpu_cmp == 0 ? 0x4000u : 0);
+ _st[6] = _st[7] = 0;
+ g_st_i64[6] = g_st_i64[7] = 0;
+ _flag_a = (uint32_t)HI8(eax) << 24; _flag_b = 0x41000000u;
+ _flag_k = FK_NARROW(FK_TEST, 24); _cf = 0;
+ if (_fpu_cmp == 1) goto L_3803C2F7;
+ goto L_3803C304;
L_3803C2F7:
edi = POP32_VAL(esp); /* 0x3803C2F7: pop edi */
esi = POP32_VAL(esp); /* 0x3803C2F8: pop esi */
@@ -6021,8 +5981,6 @@ L_3803C2F7:
esp = ebp; /* 0x3803C2FC: mov esp, ebp */
ebp = POP32_VAL(esp); /* 0x3803C2FE: pop ebp */
RECOMP_FLAGS_OUT(); esp += 20; { RECOMP_REGS_OUT(); return; } /* 0x3803C2FF: ret 0x10 */
-L_3803C302:
- { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x3803C302: fstp st(0) */
L_3803C304:
/* test LO8(ebx), LO8(ebx) */ /* 0x3803C304: test bl, bl */
_flag_a = (uint32_t)(LO8(ebx)) << 24; _flag_b = (uint32_t)(LO8(ebx)) << 24;
diff --git a/src/tests/terrain_tile.c b/src/tests/terrain_tile.c
@@ -0,0 +1,166 @@
+#include "recomp_types.h"
+#include <assert.h>
+
+void sub_3803C220(void);
+void sub_3803C6B0(void);
+void sub_3B001000(void);
+void sub_3B001010(void);
+void sub_3B0010B0(void);
+void sub_3B0010E0(void);
+void sub_3B001110(void);
+void sub_3B001170(void);
+void sub_3B0011B0(void);
+void sub_3B0011F0(void);
+void sub_3B001240(void);
+void sub_3B001500(void);
+void sub_3B0015A0(void);
+void sub_3B001610(void);
+void sub_3B003730(void);
+static float corners[12];
+static unsigned parity, registrations;
+
+static void corner_points(void)
+{
+ for (unsigned i = 0; i < 4; ++i)
+ memcpy((void *)ADDR(MEM32(g_esp + 12 + i * 4)), corners + i * 3, 12);
+ g_eax = parity;
+ g_esp += 28;
+}
+static void register_exit(void) { ++registrations; g_eax = 0; g_esp += 4; }
+recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
+recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
+recomp_func_t recomp_lookup(uint32_t address)
+{
+ switch (address) {
+ case 0x38038370: return corner_points;
+ case 0x3804b54c: return register_exit;
+ case 0x3803c6b0: return sub_3803C6B0;
+ case 0x3b001000: return sub_3B001000;
+ case 0x3b001010: return sub_3B001010;
+ case 0x3b0010b0: return sub_3B0010B0;
+ case 0x3b0010e0: return sub_3B0010E0;
+ case 0x3b001110: return sub_3B001110;
+ case 0x3b001170: return sub_3B001170;
+ case 0x3b0011b0: return sub_3B0011B0;
+ case 0x3b0011f0: return sub_3B0011F0;
+ case 0x3b001240: return sub_3B001240;
+ case 0x3b001500: return sub_3B001500;
+ case 0x3b0015a0: return sub_3B0015A0;
+ case 0x3b001610: return sub_3B001610;
+ case 0x3b003730: return sub_3B003730;
+ default: fprintf(stderr, "Unexpected terrain call %08x\n", address); abort();
+ }
+}
+static void triangle_hit(unsigned diagonal, float x, float z, float slope)
+{
+ memset((void *)ADDR(0x50000000), 0, 0x20000);
+ memset((void *)ADDR(0x3806f000), 0, 0x300);
+ float tile[] = {0,0,0, 4,4*slope,0, 4,4*slope,4, 0,0,4};
+ memcpy(corners, tile, sizeof(tile));
+ parity = diagonal; registrations = 0;
+ g_ecx = 0x50000000; g_esp = 0x5001ff00;
+ g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456;
+ g_fp_top = 2;
+ g_st[0] = 17.25; g_st[1] = -31.5;
+ g_st_i64[0] = INT64_MAX; g_st_i64[1] = INT64_MIN;
+ MEM32(g_esp + 4) = 0; MEM32(g_esp + 8) = 0;
+ MEM32(g_esp + 12) = 0x50001000; MEM32(g_esp + 16) = 0x5000100c;
+ float origin[] = {x,10,z}, direction[] = {0,-20,0}, end[] = {x,-10,z};
+ memcpy((void *)ADDR(0x3806f028), origin, 12);
+ memcpy((void *)ADDR(0x3806f018), direction, 12);
+ memcpy((void *)ADDR(0x3806f008), end, 12);
+
+ sub_3803C220();
+
+ assert((g_eax & 255) == 1);
+ assert(*(float *)ADDR(0x50001000) == x);
+ assert(*(float *)ADDR(0x50001004) == x * slope);
+ assert(*(float *)ADDR(0x50001008) == z);
+ assert(fabs(*(float *)ADDR(0x5000100c) - (slope ? -.4472136f : 0)) < 1e-6);
+ assert(fabs(*(float *)ADDR(0x50001010) - (slope ? .8944272f : 1)) < 1e-6);
+ assert(*(float *)ADDR(0x50001014) == 0);
+ assert(registrations == 2);
+ assert(g_esp == 0x5001ff14 && g_ebp == 123 && g_ebx == 234 && g_esi == 345 && g_edi == 456);
+ assert(g_fp_top == 2 && g_st[0] == 17.25 && g_st[1] == -31.5);
+ assert(g_st_i64[0] == INT64_MAX && g_st_i64[1] == INT64_MIN);
+}
+static void outside_triangle(void)
+{
+ triangle_hit(1, 3, .5f, 0);
+ *(float *)ADDR(0x3806f028) = 5;
+ *(float *)ADDR(0x3806f008) = 5;
+ g_ecx = 0x50000000; g_esp = 0x5001ff00;
+ MEM32(g_esp + 16) = 0x5000100c;
+
+ sub_3803C220();
+
+ assert((g_eax & 255) == 0);
+ assert(*(float *)ADDR(0x50001000) == 5);
+ assert(*(float *)ADDR(0x50001004) == 0);
+ assert(*(float *)ADDR(0x50001008) == .5f);
+ assert(*(float *)ADDR(0x5000100c) == 0);
+ assert(*(float *)ADDR(0x50001010) == -16);
+ assert(*(float *)ADDR(0x50001014) == 0);
+}
+static void above_tile(void)
+{
+ triangle_hit(1, 3, .5f, 0);
+ *(float *)ADDR(0x3806f00c) = 20;
+ *(float *)ADDR(0x3806f01c) = 10;
+ g_ecx = 0x50000000; g_esp = 0x5001ff00;
+ MEM32(g_esp + 16) = 0x5000100c;
+ memset((void *)ADDR(0x50001000), 0xa5, 24);
+
+ sub_3803C220();
+
+ assert((g_eax & 255) == 0);
+ for (unsigned i = 0; i < 6; ++i) assert(MEM32(0x50001000 + i * 4) == 0xa5a5a5a5);
+}
+static void maximum_corner(unsigned corner, float height)
+{
+ triangle_hit(1, 3, .5f, 0);
+ corners[corner * 3 + 1] = height;
+ *(float *)ADDR(0x3806f00c) = 20;
+ *(float *)ADDR(0x3806f01c) = 10;
+ g_ecx = 0x50000000; g_esp = 0x5001ff00;
+ MEM32(g_esp + 16) = 0x5000100c;
+ memset((void *)ADDR(0x50001000), 0xa5, 24);
+
+ sub_3803C220();
+
+ /* The height check must allow triangle testing; the upward ray then misses.
+ * Failed triangle tests write a normal, unlike the height rejection. */
+ assert((g_eax & 255) == 0);
+ assert(*(float *)ADDR(0x50001010) == -16);
+}
+int main(void)
+{
+ assert(guest_map("terrain tile fixture", 0x50000000, 0x20000));
+ assert(guest_map("terrain tile imports", 0x38040000, 0x10000));
+ assert(guest_map("terrain tile globals", 0x38060000, 0x10000));
+ assert(guest_map("vector constants", 0x3b000000, 0x10000));
+ MEM32(0x3804e4c8) = 0x3b001170; MEM32(0x3804e4d0) = 0x3b001610;
+ MEM32(0x3804e4d4) = 0x3b001110; MEM32(0x3804e4d8) = 0x3b001010;
+ MEM32(0x3804e4e0) = 0x3b001240; MEM32(0x3804e4e8) = 0x3b0010b0;
+ MEM32(0x3804e4ec) = 0x3b003730; MEM32(0x3804e4f4) = 0x3b0010e0;
+ MEM32(0x3804e4fc) = 0x3b0015a0; MEM32(0x3804e504) = 0x3b0011f0;
+ MEM32(0x3804e508) = 0x3b001500; MEM32(0x3804e51c) = 0x3b001000;
+ MEM32(0x3804e528) = 0x3b0011b0;
+ *(float *)ADDR(0x3b006048) = 1; *(float *)ADDR(0x3804e8cc) = 1;
+ *(float *)ADDR(0x3804e8e4) = 0; *(float *)ADDR(0x3804f7a4) = -.0005f;
+
+ /* Both diagonals, both triangles, their shared edge, and a sloped surface. */
+ for (unsigned diagonal = 0; diagonal < 2; ++diagonal) {
+ triangle_hit(diagonal, 3, .5f, 0);
+ triangle_hit(diagonal, .5f, 3, 0);
+ triangle_hit(diagonal, 2, 2, 0);
+ triangle_hit(diagonal, 3, .5f, .5f);
+ triangle_hit(diagonal, .5f, 3, .5f);
+ }
+ for (unsigned corner = 0; corner < 4; ++corner) maximum_corner(corner, 30);
+ maximum_corner(2, 10);
+ outside_triangle();
+ above_tile();
+ puts("terrain tile intersections passed");
+ return 0;
+}