commit 5e0228db1e328a617d5dccbd3d6a6cc3dfb2a1c8
parent dc3fd9f9672201863e626e49a991aa75f8f1b281
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 09:27:07 +0000
Skip unused terrain sampling after failed shadow contacts
Diffstat:
4 files changed, 265 insertions(+), 0 deletions(-)
diff --git a/src/Makefile.linux b/src/Makefile.linux
@@ -227,6 +227,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/tile-corners
./build/linux/terrain-line
./build/linux/terrain-tile
+ ./build/linux/shadow-contact
./build/linux/network-queue
./build/linux/vertex-upload
./build/linux/vertex-upload-paged
@@ -498,3 +499,9 @@ build/linux/terrain-tile: tests/terrain_tile.c game/llr.dll/lifted-0023.c game/m
$(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@
test: build/linux/terrain-tile
+
+build/linux/shadow-contact: tests/shadow_contact.c game/kkr.dll/lifted-0081.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/shadow-contact
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -97,3 +97,7 @@ build/wasm/tile-corners.js: tests/tile_corners.c game/llr.dll/lifted-0021.c game
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 $@
+
+build/wasm/shadow-contact.js: tests/shadow_contact.c game/kkr.dll/lifted-0081.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/kkr.dll/lifted-0081.c b/src/game/kkr.dll/lifted-0081.c
@@ -5094,6 +5094,15 @@ L_3611778E:
_cf = 0;
_flag_k = FK_NARROW(FK_TEST, 24);
L_36117861:
+ /* With failed contacts and two low/unordered normal Ys, both terrain
+ * discrepancies remain zero. The eight height samples only fill dead
+ * contact locals; preserve the callback flag and the no-contact cleanup. */
+ if (!MEM8(ebp - 0xD) &&
+ !(*(float *)ADDR(ebp - 0x68) >= *(float *)ADDR(0x36170464)) &&
+ !(*(float *)ADDR(ebp - 0xC0) >= *(float *)ADDR(0x36170464))) {
+ MEM8(esi + 0x39) = 0;
+ goto L_36117C9F;
+ }
ecx = MEM32(esi + 0x30); /* 0x36117861: mov ecx, dword ptr [esi + 0x30] */
MEM8(esi + 0x39) = (uint8_t)(0); /* 0x36117864: mov byte ptr [esi + 0x39], 0 */
MEM32(ebp + (-0x54)) = 0; /* 0x36117868: mov dword ptr [ebp - 0x54], 0 */
diff --git a/src/tests/shadow_contact.c b/src/tests/shadow_contact.c
@@ -0,0 +1,245 @@
+#include "recomp_types.h"
+#include <assert.h>
+
+void sub_36117150(void);
+
+enum {
+ controller = 0x50000000, owner = 0x50001000,
+ shadow = 0x50004000, playfield = 0x50005000,
+ surface = 0x50006000, position = 0x50007000, engine = 0x50008000
+};
+static unsigned timing_calls, geometry_calls, update_calls, segment_calls;
+static unsigned line_calls, sample_calls;
+static int line_hit;
+static float contact_normal_y;
+
+/* Exercise the controller with stubbed engine and terrain boundaries. */
+static void timing(void) { ++timing_calls; g_esp += 4; }
+static void find_owner(void) { g_eax = owner; g_esp += 4; }
+static void get_playfield(void) { assert(g_ecx == owner); g_eax = playfield; g_esp += 4; }
+static void get_surface(void) { assert(g_ecx == playfield); g_eax = surface; g_esp += 4; }
+static void get_position(void) { assert(g_ecx == owner); g_eax = position; g_esp += 4; }
+
+static void geometry(void)
+{
+ assert(g_ecx == surface);
+ ++geometry_calls;
+ uint32_t point = MEM32(g_esp + 8), normal = MEM32(g_esp + 12);
+ memcpy((void *)ADDR(point), (void *)ADDR(position), 12);
+ *(float *)ADDR(point + 4) = 0;
+ *(float *)ADDR(normal + 4) = 1;
+ g_eax = 1;
+ g_esp += 24;
+}
+
+static void line(void)
+{
+ assert(g_ecx == surface);
+ ++line_calls;
+ uint32_t point = MEM32(g_esp + 12), normal = MEM32(g_esp + 16);
+ memcpy((void *)ADDR(point), (void *)ADDR(MEM32(g_esp + 4)), 12);
+ *(float *)ADDR(point + 4) = 0;
+ *(float *)ADDR(normal + 4) = contact_normal_y;
+ *(float *)ADDR(point) = 60 + line_calls;
+ g_eax = line_hit;
+ g_esp += 28;
+}
+
+static void sun(void) { g_eax = 0x5000d000; g_esp += 4; }
+static void dimension(void) { g_eax = 128; g_esp += 4; }
+static void sample(void) { ++sample_calls; fp_push_impl(g_st, &g_fp_top, 0); g_esp += 12; }
+static void integer(void) { g_eax = (int32_t)fp_pop_impl(g_st, &g_fp_top); g_esp += 4; }
+static void clamp(void)
+{
+ float value = *(float *)ADDR(g_esp + 4);
+ if (value < *(float *)ADDR(g_esp + 8)) value = *(float *)ADDR(g_esp + 8);
+ if (value > *(float *)ADDR(g_esp + 12)) value = *(float *)ADDR(g_esp + 12);
+ fp_push_impl(g_st, &g_fp_top, value);
+ g_esp += 4;
+}
+static void subtract(void)
+{
+ uint32_t rhs = MEM32(g_esp + 4);
+ for (unsigned i = 0; i < 12; i += 4)
+ *(float *)ADDR(g_ecx + i) -= *(float *)ADDR(rhs + i);
+ g_eax = g_ecx;
+ g_esp += 8;
+}
+
+static void cross(void)
+{
+ float left[3], right[3];
+ memcpy(left, (void *)ADDR(g_ecx), sizeof(left));
+ memcpy(right, (void *)ADDR(MEM32(g_esp + 4)), sizeof(right));
+ float result[3] = {left[1]*right[2] - left[2]*right[1],
+ left[2]*right[0] - left[0]*right[2], left[0]*right[1] - left[1]*right[0]};
+ memcpy((void *)ADDR(g_ecx), result, sizeof(result));
+ g_eax = g_ecx;
+ g_esp += 8;
+}
+
+static void update(void)
+{
+ assert(g_ecx == shadow && geometry_calls >= 1);
+ ++update_calls;
+ g_eax = 1;
+ g_esp += 20;
+}
+
+static void zero_segments(void)
+{
+ assert(g_ecx == shadow && MEM32(g_esp + 4) == 0);
+ ++segment_calls;
+ MEM32(shadow + 0x1fc) = 0;
+ g_esp += 8;
+}
+
+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 0x360fc300: return timing;
+ case 0x360d3b40: return sun;
+ case 0x360887d0: return clamp;
+ case 0x3615fd8c: return integer;
+ case 0x70000020: return line;
+ case 0x70000024: return dimension;
+ case 0x70000028: return sample;
+ case 0x7000002c: return subtract;
+ case 0x70000030: return cross;
+ case 0x70000000: return find_owner;
+ case 0x70000004: return get_playfield;
+ case 0x70000008: return get_surface;
+ case 0x7000000c: return get_position;
+ case 0x70000010: return geometry;
+ case 0x70000014: return update;
+ case 0x7000001c: return zero_segments;
+ default: fprintf(stderr, "Unexpected shadow dependency: %08x from %08x\n", address, g_icall_from[(g_icall_trace_idx - 1) & (ICALL_TRACE_SIZE - 1)]); abort();
+ }
+}
+
+static void arrange_fixture(void)
+{
+ memset((void *)ADDR(0x50000000), 0, 0x10000);
+ timing_calls = geometry_calls = update_calls = segment_calls = 0;
+ line_calls = sample_calls = 0; line_hit = 0; contact_normal_y = 0;
+ MEM8(0x3630d684) = 1;
+ MEM32(0x3616ff70) = 0x70000000;
+ MEM32(0x3616ffac) = 0x70000004;
+ MEM32(0x361700c0) = 0x70000008;
+ MEM32(0x361700a8) = 0x7000000c;
+ MEM32(0x3616f830) = 0x70000014;
+ MEM32(0x3616f82c) = 0x7000001c;
+ MEM32(0x3616ff74) = 0x50009000;
+ MEM32(0x50009000) = engine;
+ MEM32(engine + 0x80) = 0x5000a000;
+ *(float *)ADDR(0x36170d68) = .1f;
+ *(float *)ADDR(controller + 0x28) = 2;
+ MEM32(controller + 0x2c) = shadow;
+ MEM32(owner + 4) = 50000;
+ MEM32(shadow + 0x1fc) = 6;
+ MEM32(surface) = 0x5000b000;
+ MEM32(0x5000b004) = 0x70000010;
+ MEM32(0x5000b010) = 0x70000020;
+ MEM32(0x3616fda4) = MEM32(0x3616fda8) = 0x70000024;
+ MEM32(0x3616fe70) = 0x70000028;
+ MEM32(0x36170168) = 0x7000002c;
+ MEM32(0x3617017c) = 0x70000030;
+ MEM32(controller + 0x30) = surface;
+ *(float *)ADDR(controller + 0x34) = .25f;
+ *(float *)ADDR(position) = 64;
+ *(float *)ADDR(position + 4) = 1;
+ *(float *)ADDR(position + 8) = 64;
+ *(float *)ADDR(0x5000d004) = 1;
+ *(float *)ADDR(0x36170380) = 1;
+ *(float *)ADDR(0x36170464) = .5f;
+ *(float *)ADDR(0x36170854) = 20;
+ *(float *)ADDR(0x36170ef0) = .0001f;
+ *(float *)ADDR(0x36170eac) = .0001f;
+ *(float *)ADDR(0x36170f04) = .2f;
+ g_fs_base = 0x5000e000;
+ MEM32(g_fs_base) = 0x12345678;
+}
+
+static void run_controller(void)
+{
+ g_ecx = controller;
+ g_esp = 0x5000ff00;
+ g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
+ g_fp_top = 0;
+ memset(g_st, 0, sizeof(g_st));
+ memset(g_st_i64, 0, sizeof(g_st_i64));
+ sub_36117150();
+ assert(g_esp == 0x5000ff04);
+ assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
+ assert(g_fp_top == 0 && MEM32(g_fs_base) == 0x12345678);
+ assert(MEM32(controller + 0x10) == 0xbf800000);
+}
+
+static void failed_contact_with_low_normals_skips_samples(void)
+{
+ arrange_fixture();
+ MEM8(controller + 0x39) = 1;
+
+ run_controller();
+
+ assert(line_calls == 3 && sample_calls == 0);
+ assert(segment_calls == 1 && update_calls == 0);
+ assert(MEM8(controller + 0x39) == 0);
+}
+
+static void failed_contact_with_upward_normal_keeps_samples(void)
+{
+ arrange_fixture();
+ contact_normal_y = 1;
+ MEM8(controller + 0x39) = 1;
+
+ run_controller();
+
+ assert(line_calls == 3 && sample_calls == 8);
+ assert(segment_calls == 1 && update_calls == 0);
+ assert(MEM8(controller + 0x39) == 0);
+}
+
+static void successful_contact_restores_conformance(void)
+{
+ arrange_fixture();
+ MEM8(controller + 0x39) = 1;
+ run_controller();
+ line_hit = 1;
+ contact_normal_y = 1;
+
+ run_controller();
+
+ assert(sample_calls == 8 && update_calls == 1);
+ assert(MEM8(controller + 0x39) == 0);
+}
+
+static void normal_threshold_and_unordered_cases(void)
+{
+ float normals[] = {.49999997f, .5f, NAN};
+ unsigned expected_samples[] = {0, 8, 0};
+ for (unsigned i = 0; i < 3; ++i) {
+ arrange_fixture();
+ contact_normal_y = normals[i];
+ MEM8(controller + 0x39) = 1;
+
+ run_controller();
+
+ assert(sample_calls == expected_samples[i]);
+ assert(MEM8(controller + 0x39) == 0 && segment_calls == 1);
+ }
+}
+
+int main(void)
+{
+ assert(guest_map("shadow fixture", 0x50000000, 0x10000));
+ assert(guest_map("shadow imports and constants", 0x36160000, 0x20000));
+ assert(guest_map("shadow preference", 0x36300000, 0x10000));
+ failed_contact_with_low_normals_skips_samples();
+ failed_contact_with_upward_normal_keeps_samples();
+ successful_contact_restores_conformance();
+ normal_threshold_and_unordered_cases();
+ puts("shadow-contact: failed contacts, retained sampling, recovery and guest ABI passed");
+}