aorecomp

Anarchy Online classic in the browser

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


commit 05870099a723d92d282de24fb7d5fa8b9659a09b
parent 0910e6809ed0fc4d58fc55c7b4a3a984968b62b6
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 11:02:08 +0000

Simplify and speed up indexed texture sampling

Diffstat:
Msrc/game/bbr.dll/lifted-0082.c | 138+++++++++++++++++++++++++++++++------------------------------------------------
Msrc/tests/texture_resampling.c | 59+++++++++++++++++++++++++++++++++++++----------------------
2 files changed, 91 insertions(+), 106 deletions(-)

diff --git a/src/game/bbr.dll/lifted-0082.c b/src/game/bbr.dll/lifted-0082.c @@ -11213,93 +11213,63 @@ L_251418C1: RECOMP_REGS_OUT(); } -void sub_251418C6(void) { - int _fpu_cmp = 0; - uint32_t _cf = 0; - int _df = 1; - uint32_t _flag_k = FK_NONE; - uint32_t _flag_a = 0, _flag_b = 0; - uint32_t _itail_tgt = 0; +void sub_251418C6(void) +{ + uint32_t _flag_k, _flag_a, _flag_b, _cf; RECOMP_REGS_LOCALS RECOMP_ENTER(0x251418C6u); -L_251418C6: - PUSH32(esp, ebp); /* 0x251418C6: push ebp */ - ebp = esp; /* 0x251418C7: mov ebp, esp */ - PUSH32(esp, ecx); /* 0x251418C9: push ecx */ - eax = MEM32(ebp + 0x8); /* 0x251418CA: mov eax, dword ptr [ebp + 8] */ - PUSH32(esp, esi); /* 0x251418CD: push esi */ - esi = MEM32(ebp + 0x10); /* 0x251418CE: mov esi, dword ptr [ebp + 0x10] */ - MEM32(ebp + 0x10) = 1; /* 0x251418D1: mov dword ptr [ebp + 0x10], 1 */ - /* cmp MEM32(eax), 0 */ /* 0x251418D8: cmp dword ptr [eax], 0 */ - _flag_a = (uint32_t)(MEM32(eax)); _flag_b = (uint32_t)(0); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - _flag_k = FK_CMP; - ecx = MEM32(esi + 0xB4); /* 0x251418DB: mov ecx, dword ptr [esi + 0xb4] */ - MEM32(ebp + (-0x4)) = ecx; /* 0x251418E1: mov dword ptr [ebp - 4], ecx */ - if (CMP_EQ(_flag_a, _flag_b)) goto L_25141927; /* 0x251418E4: je 0x25141927 */ -L_251418E6: - PUSH32(esp, edi); /* 0x251418E6: push edi */ - edi = MEM32(ebp + 0xC); /* 0x251418E7: mov edi, dword ptr [ebp + 0xc] */ - PUSH32(esp, ebx); /* 0x251418EA: push ebx */ - _flag_a = (uint32_t)(edi); _flag_b = (uint32_t)(eax); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - edi = edi - eax; /* 0x251418EB: sub edi, eax */ - _flag_k = FK_CMP; -L_251418ED: - /* test MEM32(ebp + 0x10), ecx */ /* 0x251418ED: test dword ptr [ebp + 0x10], ecx */ - _flag_a = (uint32_t)(MEM32(ebp + 0x10)); _flag_b = (uint32_t)(ecx); - _cf = 0; - _flag_k = FK_TEST; - if (TEST_NZ(_flag_a, _flag_b)) goto L_2514191A; /* 0x251418F0: jne 0x2514191a */ -L_251418F2: - ecx = MEM32(eax); /* 0x251418F2: mov ecx, dword ptr [eax] */ - edx = MEM32(esi + 0x2C); /* 0x251418F4: mov edx, dword ptr [esi + 0x2c] */ - _flag_a = (uint32_t)(0); _flag_b = (uint32_t)(0); - ebx = 0; /* 0x251418F7: xor ebx, ebx */ - _cf = 0; - _flag_k = FK_TEST; - ecx = (uint32_t)MEM8(ecx); /* 0x251418F9: movzx ecx, byte ptr [ecx] */ - ecx = MEM32(edx + ecx * 4); /* 0x251418FC: mov ecx, dword ptr [edx + ecx*4] */ - MEM32(ebp + 0x8) = ecx; /* 0x251418FF: mov dword ptr [ebp + 8], ecx */ - edx = ecx; /* 0x25141902: mov edx, ecx */ - SET_LO8(ebx, MEM8(ebp + 0xA)); /* 0x25141904: mov bl, byte ptr [ebp + 0xa] */ - _flag_a = (uint32_t)((edx | 0xFFFFFF00u)); _flag_b = (uint32_t)((edx | 0xFFFFFF00u)); - edx = edx | 0xFFFFFF00u; /* 0x25141907: or edx, 0xffffff00 */ - _cf = 0; - _flag_k = FK_TEST; - if (16u) _cf = (((edx) >> 16) & 1u); /* 0x2514190D: shl edx, 0x10 */ - if (16u) { _flag_a = recomp_flags_pack((edx << 16u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } - edx = edx << 16u; /* 0x2514190D: shl edx, 0x10 */ - SET_HI8(ebx, HI8(ecx)); /* 0x25141910: mov bh, ch */ - ecx = MEM32(ebp + (-0x4)); /* 0x25141912: mov ecx, dword ptr [ebp - 4] */ - _flag_a = (uint32_t)((edx | ebx)); _flag_b = (uint32_t)((edx | ebx)); - edx = edx | ebx; /* 0x25141915: or edx, ebx */ - _cf = 0; - _flag_k = FK_TEST; - MEM32(edi + eax) = edx; /* 0x25141917: mov dword ptr [edi + eax], edx */ -L_2514191A: - if (1u) _cf = (((MEM32(ebp + 0x10)) >> 31) & 1u); /* 0x2514191A: shl dword ptr [ebp + 0x10], 1 */ - if (1u) { _flag_a = recomp_flags_pack((MEM32(ebp + 0x10) << 1u), _cf, 0, 0); _flag_b = 0; _flag_k = FK_EFLAGS; } - MEM32(ebp + 0x10) = MEM32(ebp + 0x10) << 1u; /* 0x2514191A: shl dword ptr [ebp + 0x10], 1 */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(4); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - eax = eax + 4; /* 0x2514191D: add eax, 4 */ - _flag_k = FK_ADD; - /* cmp MEM32(eax), 0 */ /* 0x25141920: cmp dword ptr [eax], 0 */ - _flag_a = (uint32_t)(MEM32(eax)); _flag_b = (uint32_t)(0); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - _flag_k = FK_CMP; - RECOMP_BACKEDGE(); - if (CMP_NE(_flag_a, _flag_b)) goto L_251418ED; /* 0x25141923: jne 0x251418ed */ -L_25141925: - ebx = POP32_VAL(esp); /* 0x25141925: pop ebx */ - edi = POP32_VAL(esp); /* 0x25141926: pop edi */ -L_25141927: - MEM32(esi + 0xB4) = ecx; /* 0x25141927: mov dword ptr [esi + 0xb4], ecx */ - esi = POP32_VAL(esp); /* 0x2514192D: pop esi */ - esp = ebp; ebp = POP32_VAL(esp); /* 0x2514192E: leave */ - RECOMP_FLAGS_OUT(); esp += 4; { RECOMP_REGS_OUT(); return; } /* 0x2514192F: ret */ + /* Keep the callback's guest frame and scratch arguments visible to yields. */ + PUSH32(esp, ebp); + ebp = esp; + PUSH32(esp, ecx); + eax = MEM32(ebp + 8); + PUSH32(esp, esi); + esi = MEM32(ebp + 16); + MEM32(ebp + 16) = 1; + _flag_a = MEM32(eax); + _flag_b = 0; _cf = 0; _flag_k = FK_CMP; + ecx = MEM32(esi + 0xb4); + MEM32(ebp - 4) = ecx; + + /* The null-terminated pointer list selects palette indices; the mask skips samples. */ + if (_flag_a) { + PUSH32(esp, edi); + edi = MEM32(ebp + 12); + PUSH32(esp, ebx); + edi -= eax; + do { + struct { + uint32_t discard_mask, saved_ebp, return_address, color, output, bit; + } *frame = (void *)ADDR(ebp - 4); + _Static_assert(sizeof(*frame) == 24, "Palette callback frame layout"); + if (!(frame->bit & ecx)) { + uint32_t pixel = MEM32(eax); + uint32_t palette = MEM32(esi + 0x2c); + uint32_t color = MEM32(palette + MEM8(pixel) * 4); + frame->color = color; + /* Palette entries have low-byte red; filtered samples have low-byte blue. */ + ebx = ((color >> 16) & 255) | (color & 0xff00); + edx = 0xff000000u | ((color & 255) << 16) | ebx; + ecx = frame->discard_mask; + MEM32(edi + eax) = edx; + } + frame->bit <<= 1; + eax += 4; + _flag_a = MEM32(eax); + RECOMP_BACKEDGE(); + } while (_flag_a); + ebx = POP32_VAL(esp); + edi = POP32_VAL(esp); + } + + /* Preserve the mask, caller registers and final null-pointer comparison. */ + MEM32(esi + 0xb4) = ecx; + esi = POP32_VAL(esp); + esp = ebp; + ebp = POP32_VAL(esp); + RECOMP_FLAGS_OUT(); + esp += 4; RECOMP_REGS_OUT(); } diff --git a/src/tests/texture_resampling.c b/src/tests/texture_resampling.c @@ -3,10 +3,12 @@ void sub_25140DFB(void); void sub_25142392(void); +void sub_251418C6(void); 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) { + if (address == 0x251418c6) return sub_251418C6; assert(address == 0x25142392); return sub_25142392; } @@ -27,29 +29,42 @@ int main(void) {0xfefe0000, 0xfe7f7e00, 0xfe00fe00, 0xfe00fe00}, {0, 0, 0, 0} }; - for (unsigned i = 0; i < 6; ++i) { - memset((void *)ADDR(0x50000000), 0xa5, 0x20000); - memcpy((void *)ADDR(0x5000fffe), pixels, sizeof(pixels)); - MEM32(0x50001020) = 24; MEM32(0x50001024) = 6; - MEM32(0x50001028) = 0x5000fffe; - MEM32(0x50001054) = 0; MEM32(0x50001058) = 0; - MEM32(0x5000105c) = 2; MEM32(0x50001060) = 2; - MEM32(0x50001090) = positions[i][2]; - MEM32(0x500010a8) = positions[i][0] + 0x7fff; - MEM32(0x500010ac) = positions[i][1] + 0x7fff; - MEM32(0x500010b8) = 0x25142392; - g_esp = 0x51010020; g_ebp = 123; - g_ebx = 234; g_esi = 345; g_edi = 456; - MEM32(g_esp + 4) = 4; - MEM32(g_esp + 8) = 0x50006000; - MEM32(g_esp + 12) = 0x50001000; + for (unsigned indexed = 0; indexed < 2; ++indexed) { + for (unsigned i = 0; i < 6; ++i) { + memset((void *)ADDR(0x50000000), 0xa5, 0x20000); + if (indexed) { + const unsigned char indices[] = {255, 3, 77, 9}; + memcpy((void *)ADDR(0x5000fffe), indices, sizeof(indices)); + MEM32(0x50008000 + 255 * 4) = 0x120000ff; + MEM32(0x50008000 + 3 * 4) = 0x3400ff00; + MEM32(0x50008000 + 77 * 4) = 0x56ff0000; + MEM32(0x50008000 + 9 * 4) = 0x78ffffff; + MEM32(0x5000102c) = 0x50008000; + } else { + memcpy((void *)ADDR(0x5000fffe), pixels, sizeof(pixels)); + } + MEM32(0x50001020) = indexed ? 8 : 24; + MEM32(0x50001024) = indexed ? 2 : 6; + MEM32(0x50001028) = 0x5000fffe; + MEM32(0x50001054) = 0; MEM32(0x50001058) = 0; + MEM32(0x5000105c) = 2; MEM32(0x50001060) = 2; + MEM32(0x50001090) = positions[i][2]; + MEM32(0x500010a8) = positions[i][0] + 0x7fff; + MEM32(0x500010ac) = positions[i][1] + 0x7fff; + MEM32(0x500010b8) = indexed ? 0x251418c6 : 0x25142392; + g_esp = 0x51010020; g_ebp = 123; + g_ebx = 234; g_esi = 345; g_edi = 456; + MEM32(g_esp + 4) = 4; + MEM32(g_esp + 8) = 0x50006000; + MEM32(g_esp + 12) = 0x50001000; - sub_25140DFB(); + sub_25140DFB(); - for (unsigned j = 0; j < 4; ++j) - assert(MEM32(0x50006000 + j * 4) == expected[i][j]); - assert(g_esp == 0x51010024 && g_ebp == 123); - assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); - assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x50006010) == 0xa5a5a5a5); + for (unsigned j = 0; j < 4; ++j) + assert(MEM32(0x50006000 + j * 4) == expected[i][j]); + assert(g_esp == 0x51010024 && g_ebp == 123); + assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); + assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x50006010) == 0xa5a5a5a5); + } } }