aorecomp

Anarchy Online classic in the browser

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


commit 89273f9047923b291e935e6c7eda62e540715005
parent 6979ef844909369bf07c7c8a62640ba189e0eb47
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 04:24:21 +0000

Reduce surface lookup and skinned vertex CPU work

Diffstat:
Msrc/Makefile.linux | 7+++++++
Msrc/Makefile.wasm | 4++++
Msrc/game/bbr.dll/lifted-0061.c | 310++++++++++++++++++++++++++++++++++---------------------------------------------
Msrc/runtime/linux_graphics.c | 50+++++++++++++++++++++++++++-----------------------
Asrc/tests/skinning.c | 172+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/tests/surfaces.c | 43+++++++++++++++++++++++++++++++++++++++++++
6 files changed, 388 insertions(+), 198 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -221,6 +221,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/matrix4 ./build/linux/fpu-stack ./build/linux/water-ring + ./build/linux/skinning ./build/linux/tile-corners ./build/linux/terrain-line ./build/linux/network-queue @@ -470,3 +471,9 @@ build/linux/terrain-line: tests/terrain_line.c game/llr.dll/lifted-0023.c runtim $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/terrain-line + +build/linux/skinning: tests/skinning.c game/bbr.dll/lifted-0061.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/skinning diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -66,3 +66,7 @@ build/wasm/web/mixer-test.js: tests/audio.c runtime/linux_audio.c runtime/browse build/wasm/terrain-line.js: tests/terrain_line.c game/llr.dll/lifted-0023.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/skinning.js: tests/skinning.c game/bbr.dll/lifted-0061.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/bbr.dll/lifted-0061.c b/src/game/bbr.dll/lifted-0061.c @@ -10723,186 +10723,146 @@ L_250F67C9: if (4u) _cf = (((eax) >> 28) & 1u); /* 0x250F67D2: shl eax, 4 */ if (4u) { _flag_a = recomp_flags_pack((eax << 4u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } eax = eax << 4u; /* 0x250F67D2: shl eax, 4 */ - fp_push(*(float*)&MEM32(eax + edx + 0xC)); /* 0x250F67D5: fld dword ptr [eax + edx + 0xc] */ _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(edx); _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); eax = eax + edx; /* 0x250F67D9: add eax, edx */ _flag_k = FK_ADD; - _st[0] *= (double)*(float*)ADDR(ecx + 0x4); /* 0x250F67DB: fmul dword ptr [ecx + 4] */ - fp_push(*(float*)&MEM32(ecx)); /* 0x250F67DE: fld dword ptr [ecx] */ - _st[0] *= (double)*(float*)ADDR(eax); /* 0x250F67E0: fmul dword ptr [eax] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F67E2: faddp st(1) */ - fp_push(*(float*)&MEM32(ecx + 0x8)); /* 0x250F67E4: fld dword ptr [ecx + 8] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x18); /* 0x250F67E7: fmul dword ptr [eax + 0x18] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F67EA: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(eax + 0x24); /* 0x250F67EC: fadd dword ptr [eax + 0x24] */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + (-0xC)) = _v; } /* 0x250F67EF: fstp dword ptr [esi - 0xc] */ - fp_push(*(float*)&MEM32(ecx + 0x2C)); /* 0x250F67F2: fld dword ptr [ecx + 0x2c] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x18); /* 0x250F67F5: fmul dword ptr [eax + 0x18] */ - fp_push(*(float*)&MEM32(eax + 0xC)); /* 0x250F67F8: fld dword ptr [eax + 0xc] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x28); /* 0x250F67FB: fmul dword ptr [ecx + 0x28] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F67FE: faddp st(1) */ - fp_push(*(float*)&MEM32(eax)); /* 0x250F6800: fld dword ptr [eax] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x24); /* 0x250F6802: fmul dword ptr [ecx + 0x24] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6805: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi) = _v; } /* 0x250F6807: fstp dword ptr [esi] */ - fp_push(*(float*)&MEM32(eax + 0x1C)); /* 0x250F6809: fld dword ptr [eax + 0x1c] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x8); /* 0x250F680C: fmul dword ptr [ecx + 8] */ - fp_push(*(float*)&MEM32(eax + 0x4)); /* 0x250F680F: fld dword ptr [eax + 4] */ - _st[0] *= (double)*(float*)ADDR(ecx); /* 0x250F6812: fmul dword ptr [ecx] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6814: faddp st(1) */ - fp_push(*(float*)&MEM32(eax + 0x10)); /* 0x250F6816: fld dword ptr [eax + 0x10] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x4); /* 0x250F6819: fmul dword ptr [ecx + 4] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F681C: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(eax + 0x28); /* 0x250F681E: fadd dword ptr [eax + 0x28] */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + (-0x8)) = _v; } /* 0x250F6821: fstp dword ptr [esi - 8] */ - fp_push(*(float*)&MEM32(eax + 0x4)); /* 0x250F6824: fld dword ptr [eax + 4] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x24); /* 0x250F6827: fmul dword ptr [ecx + 0x24] */ - fp_push(*(float*)&MEM32(ecx + 0x2C)); /* 0x250F682A: fld dword ptr [ecx + 0x2c] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x1C); /* 0x250F682D: fmul dword ptr [eax + 0x1c] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6830: faddp st(1) */ - fp_push(*(float*)&MEM32(ecx + 0x28)); /* 0x250F6832: fld dword ptr [ecx + 0x28] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x10); /* 0x250F6835: fmul dword ptr [eax + 0x10] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6838: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + 0x4) = _v; } /* 0x250F683A: fstp dword ptr [esi + 4] */ - fp_push(*(float*)&MEM32(eax + 0x20)); /* 0x250F683D: fld dword ptr [eax + 0x20] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x8); /* 0x250F6840: fmul dword ptr [ecx + 8] */ - fp_push(*(float*)&MEM32(eax + 0x8)); /* 0x250F6843: fld dword ptr [eax + 8] */ - _st[0] *= (double)*(float*)ADDR(ecx); /* 0x250F6846: fmul dword ptr [ecx] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6848: faddp st(1) */ - fp_push(*(float*)&MEM32(eax + 0x14)); /* 0x250F684A: fld dword ptr [eax + 0x14] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x4); /* 0x250F684D: fmul dword ptr [ecx + 4] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6850: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(eax + 0x2C); /* 0x250F6852: fadd dword ptr [eax + 0x2c] */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + (-0x4)) = _v; } /* 0x250F6855: fstp dword ptr [esi - 4] */ - fp_push(*(float*)&MEM32(eax + 0x8)); /* 0x250F6858: fld dword ptr [eax + 8] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x24); /* 0x250F685B: fmul dword ptr [ecx + 0x24] */ - fp_push(*(float*)&MEM32(ecx + 0x2C)); /* 0x250F685E: fld dword ptr [ecx + 0x2c] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x20); /* 0x250F6861: fmul dword ptr [eax + 0x20] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6864: faddp st(1) */ - fp_push(*(float*)&MEM32(ecx + 0x28)); /* 0x250F6866: fld dword ptr [ecx + 0x28] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x14); /* 0x250F6869: fmul dword ptr [eax + 0x14] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F686C: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + 0x8) = _v; } /* 0x250F686E: fstp dword ptr [esi + 8] */ - goto L_250F6997; /* 0x250F6871: jmp 0x250f6997 */ + /* Transform position and normal, retaining the original sum/store order. */ + { + /* Vertex: first bind position [0..2], second [3..5], normal [9..11]. */ + float *vertex = (float *)ADDR(ecx); + float *first_bone = (float *)ADDR(eax); + float *position = (float *)ADDR(esi - 12); + float *normal = (float *)ADDR(esi); + double value; + + value = (double)first_bone[3] * (double)vertex[1]; + value += (double)vertex[0] * (double)first_bone[0]; + value += (double)vertex[2] * (double)first_bone[6]; + value += (double)first_bone[9]; + position[0] = (float)value; + + value = (double)vertex[11] * (double)first_bone[6]; + value += (double)first_bone[3] * (double)vertex[10]; + value += (double)first_bone[0] * (double)vertex[9]; + normal[0] = (float)value; + + value = (double)first_bone[7] * (double)vertex[2]; + value += (double)first_bone[1] * (double)vertex[0]; + value += (double)first_bone[4] * (double)vertex[1]; + value += (double)first_bone[10]; + position[1] = (float)value; + + value = (double)first_bone[1] * (double)vertex[9]; + value += (double)vertex[11] * (double)first_bone[7]; + value += (double)vertex[10] * (double)first_bone[4]; + normal[1] = (float)value; + + value = (double)first_bone[8] * (double)vertex[2]; + value += (double)first_bone[2] * (double)vertex[0]; + value += (double)first_bone[5] * (double)vertex[1]; + value += (double)first_bone[11]; + position[2] = (float)value; + + value = (double)first_bone[2] * (double)vertex[9]; + value += (double)vertex[11] * (double)first_bone[8]; + value += (double)vertex[10] * (double)first_bone[5]; + normal[2] = (float)value; + + /* Two temporary slots discard the bottom of both caller stacks. */ + memset(_st + 6, 0, 2 * sizeof(*_st)); + memset(g_st_i64 + 6, 0, 2 * sizeof(*g_st_i64)); + } + goto L_250F6997; L_250F6876: - eax = MEM32(ecx + 0x38); /* 0x250F6876: mov eax, dword ptr [ecx + 0x38] */ - fp_push(*(float*)&MEM32(ecx + 0x40)); /* 0x250F6879: fld dword ptr [ecx + 0x40] */ - edx = MEM32(ecx + 0x3C); /* 0x250F687C: mov edx, dword ptr [ecx + 0x3c] */ - fp_push(*(float*)&MEM32(0x251517A4)); /* 0x250F687F: fld dword ptr [0x251517a4] */ - _st[0] -= _st[1]; /* 0x250F6885: fsub st(1) */ - ebx = MEM32(ebx + 0x18); /* 0x250F6887: mov ebx, dword ptr [ebx + 0x18] */ - eax = eax + eax * 2; /* 0x250F688A: lea eax, [eax + eax*2] */ - edx = edx + edx * 2; /* 0x250F688D: lea edx, [edx + edx*2] */ - if (4u) _cf = (((eax) >> 28) & 1u); /* 0x250F6890: shl eax, 4 */ - if (4u) { _flag_a = recomp_flags_pack((eax << 4u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } - eax = eax << 4u; /* 0x250F6890: shl eax, 4 */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(ebx); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - eax = eax + ebx; /* 0x250F6893: add eax, ebx */ - _flag_k = FK_ADD; - if (4u) _cf = (((edx) >> 28) & 1u); /* 0x250F6895: shl edx, 4 */ - if (4u) { _flag_a = recomp_flags_pack((edx << 4u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } - edx = edx << 4u; /* 0x250F6895: shl edx, 4 */ - fp_push(*(float*)&MEM32(edx + ebx + 0xC)); /* 0x250F6898: fld dword ptr [edx + ebx + 0xc] */ - _flag_a = (uint32_t)(edx); _flag_b = (uint32_t)(ebx); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - edx = edx + ebx; /* 0x250F689C: add edx, ebx */ - _flag_k = FK_ADD; - _st[0] *= (double)*(float*)ADDR(ecx + 0x10); /* 0x250F689E: fmul dword ptr [ecx + 0x10] */ - fp_push(*(float*)&MEM32(edx + 0x18)); /* 0x250F68A1: fld dword ptr [edx + 0x18] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x14); /* 0x250F68A4: fmul dword ptr [ecx + 0x14] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68A7: faddp st(1) */ - fp_push(*(float*)&MEM32(edx)); /* 0x250F68A9: fld dword ptr [edx] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0xC); /* 0x250F68AB: fmul dword ptr [ecx + 0xc] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68AE: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(edx + 0x24); /* 0x250F68B0: fadd dword ptr [edx + 0x24] */ - _st[0] *= _st[1]; /* 0x250F68B3: fmul st(1) */ - fp_push(*(float*)&MEM32(eax + 0xC)); /* 0x250F68B5: fld dword ptr [eax + 0xc] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x4); /* 0x250F68B8: fmul dword ptr [ecx + 4] */ - fp_push(*(float*)&MEM32(eax)); /* 0x250F68BB: fld dword ptr [eax] */ - _st[0] *= (double)*(float*)ADDR(ecx); /* 0x250F68BD: fmul dword ptr [ecx] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68BF: faddp st(1) */ - fp_push(*(float*)&MEM32(ecx + 0x8)); /* 0x250F68C1: fld dword ptr [ecx + 8] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x18); /* 0x250F68C4: fmul dword ptr [eax + 0x18] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68C7: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(eax + 0x24); /* 0x250F68C9: fadd dword ptr [eax + 0x24] */ - _st[0] *= _st[3]; /* 0x250F68CC: fmul st(3) */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68CE: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + (-0xC)) = _v; } /* 0x250F68D0: fstp dword ptr [esi - 0xc] */ - fp_push(*(float*)&MEM32(ecx + 0x2C)); /* 0x250F68D3: fld dword ptr [ecx + 0x2c] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x18); /* 0x250F68D6: fmul dword ptr [eax + 0x18] */ - fp_push(*(float*)&MEM32(eax)); /* 0x250F68D9: fld dword ptr [eax] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x24); /* 0x250F68DB: fmul dword ptr [ecx + 0x24] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68DE: faddp st(1) */ - fp_push(*(float*)&MEM32(eax + 0xC)); /* 0x250F68E0: fld dword ptr [eax + 0xc] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x28); /* 0x250F68E3: fmul dword ptr [ecx + 0x28] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68E6: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi) = _v; } /* 0x250F68E8: fstp dword ptr [esi] */ - fp_push(*(float*)&MEM32(edx + 0x4)); /* 0x250F68EA: fld dword ptr [edx + 4] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0xC); /* 0x250F68ED: fmul dword ptr [ecx + 0xc] */ - fp_push(*(float*)&MEM32(edx + 0x10)); /* 0x250F68F0: fld dword ptr [edx + 0x10] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x10); /* 0x250F68F3: fmul dword ptr [ecx + 0x10] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68F6: faddp st(1) */ - fp_push(*(float*)&MEM32(edx + 0x1C)); /* 0x250F68F8: fld dword ptr [edx + 0x1c] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x14); /* 0x250F68FB: fmul dword ptr [ecx + 0x14] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F68FE: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(edx + 0x28); /* 0x250F6900: fadd dword ptr [edx + 0x28] */ - _st[0] *= _st[1]; /* 0x250F6903: fmul st(1) */ - fp_push(*(float*)&MEM32(ecx + 0x8)); /* 0x250F6905: fld dword ptr [ecx + 8] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x1C); /* 0x250F6908: fmul dword ptr [eax + 0x1c] */ - fp_push(*(float*)&MEM32(eax + 0x10)); /* 0x250F690B: fld dword ptr [eax + 0x10] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x4); /* 0x250F690E: fmul dword ptr [ecx + 4] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6911: faddp st(1) */ - fp_push(*(float*)&MEM32(ecx)); /* 0x250F6913: fld dword ptr [ecx] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x4); /* 0x250F6915: fmul dword ptr [eax + 4] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6918: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(eax + 0x28); /* 0x250F691A: fadd dword ptr [eax + 0x28] */ - _st[0] *= _st[3]; /* 0x250F691D: fmul st(3) */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F691F: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + (-0x8)) = _v; } /* 0x250F6921: fstp dword ptr [esi - 8] */ - fp_push(*(float*)&MEM32(ecx + 0x2C)); /* 0x250F6924: fld dword ptr [ecx + 0x2c] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x1C); /* 0x250F6927: fmul dword ptr [eax + 0x1c] */ - fp_push(*(float*)&MEM32(ecx + 0x28)); /* 0x250F692A: fld dword ptr [ecx + 0x28] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x10); /* 0x250F692D: fmul dword ptr [eax + 0x10] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6930: faddp st(1) */ - fp_push(*(float*)&MEM32(ecx + 0x24)); /* 0x250F6932: fld dword ptr [ecx + 0x24] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x4); /* 0x250F6935: fmul dword ptr [eax + 4] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6938: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + 0x4) = _v; } /* 0x250F693A: fstp dword ptr [esi + 4] */ - fp_push(*(float*)&MEM32(edx + 0x8)); /* 0x250F693D: fld dword ptr [edx + 8] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0xC); /* 0x250F6940: fmul dword ptr [ecx + 0xc] */ - fp_push(*(float*)&MEM32(edx + 0x14)); /* 0x250F6943: fld dword ptr [edx + 0x14] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x10); /* 0x250F6946: fmul dword ptr [ecx + 0x10] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6949: faddp st(1) */ - fp_push(*(float*)&MEM32(edx + 0x20)); /* 0x250F694B: fld dword ptr [edx + 0x20] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x14); /* 0x250F694E: fmul dword ptr [ecx + 0x14] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6951: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(edx + 0x2C); /* 0x250F6953: fadd dword ptr [edx + 0x2c] */ - _st[0] *= _st[1]; /* 0x250F6956: fmul st(1) */ - fp_push(*(float*)&MEM32(eax + 0x8)); /* 0x250F6958: fld dword ptr [eax + 8] */ - _st[0] *= (double)*(float*)ADDR(ecx); /* 0x250F695B: fmul dword ptr [ecx] */ - fp_push(*(float*)&MEM32(eax + 0x14)); /* 0x250F695D: fld dword ptr [eax + 0x14] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x4); /* 0x250F6960: fmul dword ptr [ecx + 4] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6963: faddp st(1) */ - fp_push(*(float*)&MEM32(eax + 0x20)); /* 0x250F6965: fld dword ptr [eax + 0x20] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x8); /* 0x250F6968: fmul dword ptr [ecx + 8] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F696B: faddp st(1) */ - _st[0] += (double)*(float*)ADDR(eax + 0x2C); /* 0x250F696D: fadd dword ptr [eax + 0x2c] */ - _st[0] *= _st[3]; /* 0x250F6970: fmul st(3) */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6972: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + (-0x4)) = _v; } /* 0x250F6974: fstp dword ptr [esi - 4] */ - { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x250F6977: fstp st(0) */ - { _st[0] = _st[0]; g_st_i64[0] = g_st_i64[0]; fp_pop(); } /* 0x250F6979: fstp st(0) */ - fp_push(*(float*)&MEM32(ecx + 0x2C)); /* 0x250F697B: fld dword ptr [ecx + 0x2c] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x20); /* 0x250F697E: fmul dword ptr [eax + 0x20] */ - fp_push(*(float*)&MEM32(ecx + 0x28)); /* 0x250F6981: fld dword ptr [ecx + 0x28] */ - _st[0] *= (double)*(float*)ADDR(eax + 0x14); /* 0x250F6984: fmul dword ptr [eax + 0x14] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F6987: faddp st(1) */ - fp_push(*(float*)&MEM32(eax + 0x8)); /* 0x250F6989: fld dword ptr [eax + 8] */ - _st[0] *= (double)*(float*)ADDR(ecx + 0x24); /* 0x250F698C: fmul dword ptr [ecx + 0x24] */ - { double _v = fp_pop(); _st[0] += _v; } /* 0x250F698F: faddp st(1) */ - { float _v = (float)fp_pop(); *(float*)ADDR(esi + 0x8) = _v; } /* 0x250F6991: fstp dword ptr [esi + 8] */ + { + /* Blend the two bind positions; the normal uses only the first bone. */ + float *vertex = (float *)ADDR(ecx); + double weight = (double)vertex[16]; + double complement = (double)*(float *)ADDR(0x251517A4u) - weight; + eax = MEM32(ecx + 0x38); /* 0x250F6876: mov eax, dword ptr [ecx + 0x38] */ + edx = MEM32(ecx + 0x3C); /* 0x250F687C: mov edx, dword ptr [ecx + 0x3c] */ + ebx = MEM32(ebx + 0x18); /* 0x250F6887: mov ebx, dword ptr [ebx + 0x18] */ + eax = eax + eax * 2; /* 0x250F688A: lea eax, [eax + eax*2] */ + edx = edx + edx * 2; /* 0x250F688D: lea edx, [edx + edx*2] */ + if (4u) _cf = (((eax) >> 28) & 1u); /* 0x250F6890: shl eax, 4 */ + if (4u) { _flag_a = recomp_flags_pack((eax << 4u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } + eax = eax << 4u; /* 0x250F6890: shl eax, 4 */ + _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(ebx); + _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); + eax = eax + ebx; /* 0x250F6893: add eax, ebx */ + _flag_k = FK_ADD; + if (4u) _cf = (((edx) >> 28) & 1u); /* 0x250F6895: shl edx, 4 */ + if (4u) { _flag_a = recomp_flags_pack((edx << 4u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } + edx = edx << 4u; /* 0x250F6895: shl edx, 4 */ + _flag_a = (uint32_t)(edx); _flag_b = (uint32_t)(ebx); + _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); + edx = edx + ebx; /* 0x250F689C: add edx, ebx */ + _flag_k = FK_ADD; + float *first_bone = (float *)ADDR(eax); + float *second_bone = (float *)ADDR(edx); + float *position = (float *)ADDR(esi - 12); + float *normal = (float *)ADDR(esi); + double value, secondary; + + secondary = (double)second_bone[3] * (double)vertex[4]; + secondary += (double)second_bone[6] * (double)vertex[5]; + secondary += (double)second_bone[0] * (double)vertex[3]; + secondary += (double)second_bone[9]; + secondary *= complement; + value = (double)first_bone[3] * (double)vertex[1]; + value += (double)first_bone[0] * (double)vertex[0]; + value += (double)vertex[2] * (double)first_bone[6]; + value += (double)first_bone[9]; + value *= weight; + secondary += value; + position[0] = (float)secondary; + + value = (double)vertex[11] * (double)first_bone[6]; + value += (double)first_bone[0] * (double)vertex[9]; + value += (double)first_bone[3] * (double)vertex[10]; + normal[0] = (float)value; + + secondary = (double)second_bone[1] * (double)vertex[3]; + secondary += (double)second_bone[4] * (double)vertex[4]; + secondary += (double)second_bone[7] * (double)vertex[5]; + secondary += (double)second_bone[10]; + secondary *= complement; + value = (double)vertex[2] * (double)first_bone[7]; + value += (double)first_bone[4] * (double)vertex[1]; + value += (double)vertex[0] * (double)first_bone[1]; + value += (double)first_bone[10]; + value *= weight; + secondary += value; + position[1] = (float)secondary; + + value = (double)vertex[11] * (double)first_bone[7]; + value += (double)vertex[10] * (double)first_bone[4]; + value += (double)vertex[9] * (double)first_bone[1]; + normal[1] = (float)value; + + secondary = (double)second_bone[2] * (double)vertex[3]; + secondary += (double)second_bone[5] * (double)vertex[4]; + secondary += (double)second_bone[8] * (double)vertex[5]; + secondary += (double)second_bone[11]; + secondary *= complement; + value = (double)first_bone[2] * (double)vertex[0]; + value += (double)first_bone[5] * (double)vertex[1]; + value += (double)first_bone[8] * (double)vertex[2]; + value += (double)first_bone[11]; + value *= weight; + secondary += value; + position[2] = (float)secondary; + + value = (double)vertex[11] * (double)first_bone[8]; + value += (double)vertex[10] * (double)first_bone[5]; + value += (double)first_bone[2] * (double)vertex[9]; + normal[2] = (float)value; + + /* Five temporary slots discard the bottom of both caller stacks. */ + memset(_st + 3, 0, 5 * sizeof(*_st)); + memset(g_st_i64 + 3, 0, 5 * sizeof(*g_st_i64)); + } L_250F6994: ebx = MEM32(ebp + (-0x1C)); /* 0x250F6994: mov ebx, dword ptr [ebp - 0x1c] */ L_250F6997: diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -55,9 +55,9 @@ static struct surface_object { struct gl_depth *depth; struct surface_object *attached; struct surface_data *data; - struct surface_object *next; + struct surface_object *next, *bucket_next[2]; } *surfaces; -static struct surface_object *surface_cache[4096]; +static struct surface_object *surface_buckets[2][4096]; struct device_light { uint32_t index, enabled, data[26]; struct device_light *next; @@ -850,22 +850,32 @@ static unsigned interface_bucket(uint32_t address) static struct surface_object *find_surface(uint32_t address) { - /* Cache hot COM identities without putting host pointers in guest memory. */ unsigned bucket = interface_bucket(address); - struct surface_object *cached = surface_cache[bucket]; - if (cached) - for (unsigned i = 0; i < 2; ++i) - if (cached->interfaces[i] == address && cached->references[i]) return cached; - for (struct surface_object *surface = surfaces; surface; surface = surface->next) - for (unsigned i = 0; i < 2; ++i) - if (surface->interfaces[i] == address && surface->references[i]) { - surface_cache[bucket] = surface; - return surface; - } + for (unsigned i = 0; i < 2; ++i) + for (struct surface_object *surface = surface_buckets[i][bucket]; surface; surface = surface->bucket_next[i]) + if (surface->interfaces[i] == address) return surface; fprintf(stderr, "Invalid guest surface interface %08x\n", address); abort(); } +/* Each live COM identity has its own chain, including older surface aliases. */ +static void create_surface_interface(struct surface_object *surface, unsigned index) +{ + surface->interfaces[index] = create_interface(index + 3); + unsigned bucket = interface_bucket(surface->interfaces[index]); + surface->bucket_next[index] = surface_buckets[index][bucket]; + surface_buckets[index][bucket] = surface; +} + +static void destroy_surface_interface(struct surface_object *surface, unsigned index) +{ + struct surface_object **link = &surface_buckets[index][interface_bucket(surface->interfaces[index])]; + while (*link != surface) link = &(*link)->bucket_next[index]; + *link = surface->bucket_next[index]; + guest_free(surface->interfaces[index]); + surface->interfaces[index] = 0; +} + static uint32_t release_surface(struct surface_object *surface, unsigned index); static void create_surface(void) @@ -959,7 +969,7 @@ static void create_surface(void) surface->description_flags = 0x100f | (flags & 0x100000) | (mip ? 0x20000 : 0); surface->owner = owner; owner->references[1] += mip ? (level ? 0 : 2) : 1; - surface->interfaces[0] = create_interface(3); + create_surface_interface(surface, 0); surface->references[0] = 1; surface->next = surfaces; surfaces = surface; @@ -989,7 +999,7 @@ static void surface_query_interface(void) if (!memcmp((void *)ADDR(iid), surface7, 16)) index = 0; else if (!memcmp((void *)ADDR(iid), unknown, 16) || !memcmp((void *)ADDR(iid), surface1, 16)) index = 1; else { MEM32(output) = 0; g_eax = 0x80004002u; return; } - if (!surface->interfaces[index]) surface->interfaces[index] = create_interface(index + 3); + if (!surface->interfaces[index]) create_surface_interface(surface, index); ++surface->references[index]; MEM32(output) = surface->interfaces[index]; g_eax = 0; @@ -1006,11 +1016,7 @@ static void surface_add_reference(void) static uint32_t release_surface(struct surface_object *surface, unsigned index) { uint32_t remaining = --surface->references[index]; - if (!remaining) { - surface_cache[interface_bucket(surface->interfaces[index])] = NULL; - guest_free(surface->interfaces[index]); - surface->interfaces[index] = 0; - } + if (!remaining) destroy_surface_interface(surface, index); if (!surface->references[0] && !surface->references[1]) { struct surface_object **link = &surfaces; while (*link != surface) link = &(*link)->next; @@ -1026,9 +1032,7 @@ static uint32_t release_surface(struct surface_object *surface, unsigned index) /* Implicit mip attachments are destroyed with their parent even if * callers retain a sublevel interface, as in DirectDraw. */ if (surface->mip_count) { - surface_cache[interface_bucket(surface->attached->interfaces[1])] = NULL; - guest_free(surface->attached->interfaces[1]); - surface->attached->interfaces[1] = 0; + if (surface->attached->interfaces[1]) destroy_surface_interface(surface->attached, 1); surface->attached->references[1] = 0; surface->attached->references[0] = 1; } diff --git a/src/tests/skinning.c b/src/tests/skinning.c @@ -0,0 +1,172 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_250F66E0(void); +static unsigned events[4], event_count; + +static void lock_vertices(void) +{ + assert(event_count == 0); + events[event_count++] = 1; + g_eax = MEM32(g_ecx); + g_esp += 4; +} + +static void unlock_vertices(void) +{ + assert(event_count == 3); + events[event_count++] = 2; + g_eax = 0; + g_esp += 4; +} + +static void callback(void) +{ + assert(event_count == 2); + events[event_count++] = 3; + assert(MEM32(g_esp + 4) == 0x50000000); + assert(MEM32(g_esp + 8) == 1); + assert(MEM32(g_esp + 36) == 123); + MEM32(MEM32(g_esp + 16) + 24) = 0x3f000000; + MEM32(MEM32(g_esp + 16) + 28) = 0x3e800000; + g_esp += 4; +} + +static void yield(void) +{ + assert(event_count == 1); + events[event_count++] = 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 0x250f72e0: return lock_vertices; + case 0x25118ef0: return unlock_vertices; + case 0x70000000: return callback; + default: abort(); + } +} + +static void arrange(void) +{ + /* One section, one vertex, two bones, and one post-transform callback. */ + memset((void *)ADDR(0x50000000), 0, 0x10000); + MEM32(0x25151778) = 0x3f7d70a4; + MEM32(0x251517a4) = 0x3f800000; + MEM32(0x50000008) = 1; + MEM32(0x5000000c) = 1; + MEM32(0x50000010) = 0x50000100; + MEM32(0x50000018) = 0x50001000; + MEM32(0x50000030) = 0x50000500; + MEM32(0x50000034) = 0x50000508; + MEM32(0x50000500) = 123; + MEM32(0x50000504) = 0x70000000; + MEM32(0x50000100) = 0x50000200; + MEM32(0x50000108) = 0x50000300; + MEM32(0x50000244) = 0x50000400; + MEM32(0x50000410) = 1; + MEM32(0x50000414) = 0x50000600; + MEM32(0x50000300) = 0x50008000; + MEM32(0x5000060c) = 0x50002000; + MEM32(0x50000614) = 0x50000700; + MEM32(0x5000062c) = 1; + MEM32(0x50000630) = 2; + + /* Bind positions, copied position, normal, bone indices, and weight. */ + const float matrices[] = {1,0,0, 0,1,0, 0,0,1, 10,20,30, + 1,0,0, 0,1,0, 0,0,1, -10,-20,-30}; + const float vertex[] = {1,2,3, 5,6,7, -7,8,9, 1,2,3}; + memcpy((void *)ADDR(0x50001000), matrices, sizeof(matrices)); + memcpy((void *)ADDR(0x50002000), vertex, sizeof(vertex)); + MEM32(0x50002038) = 0; + MEM32(0x5000203c) = 1; + MEM32(0x50002040) = 0x3f800000; + + g_eax = 123; g_ecx = 0x50000000; g_edx = 234; g_ebx = 345; + g_esp = 0x5000ff00; g_ebp = 456; g_esi = 567; g_edi = 678; + g_flag_k = FK_CMP; g_flag_a = 111; g_flag_b = 222; g_flag_cf = 1; + g_fp_top = 6; + g_fpu_cw = 0x027f; + for (unsigned i = 0; i < 8; ++i) { + g_st[i] = i + .125; + g_st_i64[i] = INT64_MAX - i; + } + g_backedges = 65535; + recomp_yield_hook = yield; + event_count = 0; +} + +static void test_vertices(unsigned kind) +{ + arrange(); + if (kind == 1) MEM32(0x50002040) = 0x3e800000; + if (kind == 2) MEM8(0x50000028) = 1; + if (kind == 3) MEM32(0x50000300) = 0x50001000; + + sub_250F66E0(); + + const float expected[][8] = { + {11,22,33, 1,2,3, .5f,.25f}, + {-1,-5,-9, 1,2,3, .5f,.25f}, + {-7,8,9, 1,2,3, .5f,.25f}, + {11,22,33, 11,24,36, .5f,.25f}, + }; + uint32_t output = kind == 3 ? 0x50001000 : 0x50008000; + assert(!memcmp((void *)ADDR(output), expected[kind], sizeof(expected[kind]))); + assert(g_esp == 0x5000ff04 && g_ebp == 456 && g_ebx == 345 && g_esi == 567 && g_edi == 678); + assert(g_fp_top == 6 && g_fpu_cw == 0x027f); + assert(g_st[0] == .125 && g_st[2] == 2.125); + assert(g_st_i64[0] == INT64_MAX && g_st_i64[2] == INT64_MAX - 2); + unsigned retained = kind == 1 ? 3 : kind == 2 ? 8 : 6; + for (unsigned i = 0; i < 8; ++i) { + assert(g_st[i] == (i < retained ? i + .125 : 0)); + assert(g_st_i64[i] == (i < retained ? INT64_MAX - i : 0)); + } + const unsigned expected_events[] = {1,4,3,2}; + assert(event_count == 4 && !memcmp(events, expected_events, sizeof(events))); +} + +static void test_double_intermediates(void) +{ + arrange(); + const float position[] = {16777216,1,-16777216}; + memcpy((void *)ADDR(0x50002000), position, sizeof(position)); + *(float *)ADDR(0x5000100c) = 1; + *(float *)ADDR(0x50001018) = 1; + *(float *)ADDR(0x50001024) = 0; + + sub_250F66E0(); + + assert(*(float *)ADDR(0x50008000) == 1); +} + +static void test_nan_weight(uint32_t weight) +{ + arrange(); + MEM32(0x50002040) = weight; + + sub_250F66E0(); + + for (unsigned i = 0; i < 3; ++i) { + uint32_t word = MEM32(0x50008000 + i * 4); + assert((word & 0x7fc00000) == 0x7fc00000); + } + const float normal_and_uv[] = {1,2,3, .5f,.25f}; + assert(!memcmp((void *)ADDR(0x5000800c), normal_and_uv, sizeof(normal_and_uv))); +} + +int main(void) +{ + assert(guest_map("skinning fixture", 0x50000000, 0x10000)); + assert(guest_map("skinning constants", 0x25150000, 0x10000)); + for (unsigned kind = 0; kind < 4; ++kind) test_vertices(kind); + test_double_intermediates(); + test_nan_weight(0x7fc12345); + test_nan_weight(0xffc12345); + test_nan_weight(0x7f800001); + test_nan_weight(0xff800001); + puts("skinning: positions, normals, aliases, callbacks, precision, NaNs and guest ABI passed"); +} diff --git a/src/tests/surfaces.c b/src/tests/surfaces.c @@ -670,6 +670,10 @@ static void partial_mip_chain_preserves_padded_rows(void) memset(memory(caps), 0, 16); *memory(caps) = 0x401000; assert(!invoke(top, 12, 3, caps, workspace())); uint32_t child = *memory(workspace()); + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + memcpy(memory(workspace() + 16), unknown, 16); + assert(!invoke(child, 0, 3, workspace() + 16, workspace())); + /* The parent also owns the retained older-interface alias of this mip. */ assert(!invoke(child, 25, 5, 0u, desc, 0x21u, 0u)); uint32_t pixels = *memory(desc + 36); @@ -687,6 +691,44 @@ static void partial_mip_chain_preserves_padded_rows(void) assert(!invoke(top, 2, 1) && !invoke(draw, 2, 1)); } +static void surface_identity_collisions_and_alias_lifetimes(void) +{ + uint32_t draw = create_draw(), surfaces[4097], aliases[4097]; + uint32_t iid = workspace() + 16, desc = workspace() + 128; + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + const uint32_t surface7[] = {0x06675a80, 0x11d23b9b, 0x60002fb9, 0x5bea9797}; + memcpy(memory(iid), unknown, 16); + for (unsigned i = 0; i < 4097; ++i) { + surfaces[i] = create_surface(draw, 0x840); + assert(!invoke(surfaces[i], 0, 3, iid, workspace())); + aliases[i] = *memory(workspace()); + } + + for (unsigned i = 0; i < 4097; ++i) { + if (i % 2) { + assert(!invoke(aliases[i], 2, 1)); + aliases[i] = 0; + } else { + assert(!invoke(surfaces[i], 2, 1)); + surfaces[i] = 0; + } + } + memcpy(memory(iid), surface7, 16); + for (unsigned i = 0; i < 4097; i += 2) { + assert(!invoke(aliases[i], 0, 3, iid, workspace())); + surfaces[i] = *memory(workspace()); + } + + for (unsigned i = 0; i < 4097; ++i) { + *memory(desc) = 124; + assert(!invoke(surfaces[i], 22, 2, desc)); + assert(*memory(desc + 8) == 16 && *memory(desc + 12) == 32); + if (aliases[i]) assert(!invoke(aliases[i], 2, 1)); + assert(!invoke(surfaces[i], 2, 1)); + } + assert(!invoke(draw, 2, 1)); +} + int main(void) { #ifndef _WIN32 @@ -708,6 +750,7 @@ int main(void) absent_mip_attachment_returns_not_found(); mip_chain_levels_and_pixels(); partial_mip_chain_preserves_padded_rows(); + surface_identity_collisions_and_alias_lifetimes(); puts("surfaces: descriptions, COM ownership, clipper regions, palettes, depth attachments, private data, rectangle locks, blits and pixel round trips passed"); return 0; }