aorecomp

Anarchy Online classic in the browser

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


commit 890d1ea212a6734f00f76c5b53358bc3c5b53815
parent e3611549d8480c19673e546f612666951f1a5adf
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 17:56:13 +0000

Speed up unchanged-size texture conversion during loading

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

diff --git a/src/game/bbr.dll/lifted-0082.c b/src/game/bbr.dll/lifted-0082.c @@ -8013,6 +8013,65 @@ void sub_25140DFB(void) RECOMP_REGS_LOCALS RECOMP_ENTER(0x25140DFBu); + /* At one-to-one pixel centers the bilinear weights are 255,0,0,0. + * Only the bottom-right corner has weight 256. Keep this truncation + * rather than replacing the filter with an unmodified image copy. */ + uint32_t context = MEM32(esp + 12); + uint32_t count = MEM32(esp + 4), output = MEM32(esp + 8); + uint32_t reader = MEM32(context + 0xb8); + uint32_t bytes = MEM32(context + 0x20) / 8; + uint32_t row = MEM32(context + 0xac) >> 16; + uint32_t height = MEM32(context + 0x60); + uint32_t pitch = MEM32(context + 0x24); + if (count && count < 32768 && count == MEM32(context + 0x5c) && + !MEM32(context + 0x54) && !MEM32(context + 0x58) && + MEM32(context + 0x90) == 65536 && MEM32(context + 0xa8) == 0x8000 && + (MEM32(context + 0xac) & 0xffff) == 0x8000 && row < 32768 && row < height && (int32_t)height > 0 && + ((reader == 0x25142392 && bytes == 3) || + ((reader == 0x25141276 || reader == 0x2514132c) && bytes == 4)) && + pitch >= count * bytes) { + uint64_t source_begin = (uint64_t)MEM32(context + 0x28) + (uint64_t)row * pitch; + uint64_t source_end = source_begin + (row + 1 < height ? pitch : 0) + count * bytes; + uint64_t output_end = (uint64_t)output + count * 4; + if (source_begin && source_end <= 0x100000000ull && output_end <= 0x100000000ull && + (output_end <= source_begin || output >= source_end) && + (output_end <= context || output >= (uint64_t)context + 0xbc)) { + uint32_t source = (uint32_t)source_begin; + for (uint32_t x = 0; x < count; ++x) { + uint32_t address = source + x * bytes; + uint32_t pixel = MEM8(address) | ((uint32_t)MEM8(address + 1) << 8) | + ((uint32_t)MEM8(address + 2) << 16); + pixel |= reader == 0x25141276 ? (uint32_t)MEM8(address + 3) << 24 : 0xff000000; + if (x + 1 < count || row + 1 < height) { + uint32_t red_blue = ((pixel & 0x00ff00ff) * 255 >> 8) & 0x00ff00ff; + uint32_t green_alpha = (((pixel >> 8) & 0x00ff00ff) * 255) & 0xff00ff00; + pixel = red_blue | green_alpha; + } + MEM32(output + x * 4) = pixel; + RECOMP_BACKEDGE(); + } + + /* Preserve the row cursor and last-pixel mask consumed by callers. + * The alpha reader additionally rejects completely zero samples. */ + uint32_t mask = row + 1 == height ? 14 : 10; + if (reader == 0x25141276) { + uint32_t last = source + (count - 1) * 4; + if (!MEM32(last)) mask |= 1; + if (row + 1 < height && !MEM32(last + pitch)) mask |= 4; + } + MEM32(context + 0xb4) = mask; + MEM32(context + 0xa8) = count * 65536 + 1; + MEM32(context + 0xac) -= 0x7fff; + eax = output; + _flag_k = FK_DEC; _flag_a = 1; _flag_b = 1; _cf = 0; + RECOMP_FLAGS_OUT(); + esp += 4; + RECOMP_REGS_OUT(); + return; + } + } + + /* The callback shares this frame, including arguments reused for the row cursor. */ struct { uint32_t addresses[5], pixels[4], weights[4], bounds[4]; diff --git a/src/tests/texture_resampling.c b/src/tests/texture_resampling.c @@ -19,6 +19,9 @@ recomp_func_t recomp_lookup(uint32_t address) return sub_25142392; } +static unsigned yield_count; +static void observe_yield(void) { ++yield_count; } + int main(void) { assert(guest_map("texture resampling", 0x50000000, 0x20000)); @@ -83,4 +86,57 @@ int main(void) assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x50006010) == 0xa5a5a5a5); } } + + const uint32_t colors[] = { + 0xff0100ff, 0, 0x8001ff02, 0xffffffff, + 0x01ff0100, 0x010001ff, 0, 0x01020304 + }; + const uint32_t opaque_expected[][4] = { + {0xfe0000fe, 0xfe000000, 0xfe00fe01, 0xfefefefe}, + {0xfefe0000, 0xfe0000fe, 0xfe000000, 0xff020304} + }; + const uint32_t alpha_expected[][4] = { + {0xfe0000fe, 0, 0x7f00fe01, 0xfefefefe}, + {0x00fe0000, 0x000000fe, 0, 0x01020304} + }; + for (unsigned format = 0; format < 3; ++format) { + for (unsigned row = 0; row < 2; ++row) { + memset((void *)ADDR(0x50000000), 0xa5, 0x20000); + unsigned bytes = format == 0 ? 3 : 4; + for (unsigned i = 0; i < 8; ++i) + for (unsigned channel = 0; channel < bytes; ++channel) + MEM8(0x5000fffe + i * bytes + channel) = colors[i] >> (channel * 8); + MEM32(0x50001020) = bytes * 8; + MEM32(0x50001024) = bytes * 4; + MEM32(0x50001028) = 0x5000fffe; + MEM32(0x50001054) = 0; MEM32(0x50001058) = 0; + MEM32(0x5000105c) = 4; MEM32(0x50001060) = 2; + MEM32(0x50001090) = 65536; + MEM32(0x500010a8) = 0x8000; + MEM32(0x500010ac) = row * 65536 + 0x8000; + MEM32(0x500010b8) = format == 0 ? 0x25142392 : format == 1 ? 0x25141276 : 0x2514132c; + 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; + recomp_yield_hook = observe_yield; + g_backedges = RECOMP_YIELD_EVERY - 1; + yield_count = 0; + + sub_25140DFB(); + + for (unsigned x = 0; x < 4; ++x) + assert(MEM32(0x50006000 + x * 4) == + (format == 1 ? alpha_expected[row][x] : opaque_expected[row][x])); + assert(MEM32(0x500010a8) == 0x40001); + assert(MEM32(0x500010ac) == row * 65536 + 1); + assert(MEM32(0x500010b4) == (row ? 14 : 10)); + assert(g_eax == 0x50006000 && g_esp == 0x51010024 && g_ebp == 123); + assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); + assert(MEM32(0x50005ffc) == 0xa5a5a5a5 && MEM32(0x50006010) == 0xa5a5a5a5); + assert(yield_count > 0); + } + } + }