commit 10c3c153852d922743929b41e1e2065a5a2c320d
parent f66c7305cec1d20ae245b70d2d8cfc78ed114be7
Author: anon <anon@example.com>
Date: Sun, 18 Oct 2026 18:31:05 +0000
Keep PNG Paeth row cursors local between yield boundaries
Diffstat:
2 files changed, 64 insertions(+), 0 deletions(-)
diff --git a/src/game/bbr.dll/lifted-0047.c b/src/game/bbr.dll/lifted-0047.c
@@ -3757,6 +3757,49 @@ L_250CBEF8:
L_250CBEFB:
ebx = MEM32(ebp + 0x8); /* 0x250CBEFB: mov ebx, dword ptr [ebp + 8] */
L_250CBEFE:
+ /* Keep row cursors in C locals. Publish the emulated scratch frame only
+ * when yielding or returning; neither row may alias that frame. */
+ {
+ uint32_t left = edx, upper_left = ebx;
+ uint32_t output = MEM32(ebp + 0x10), remaining = MEM32(ebp + 0x14);
+ uint64_t frame_begin = ebp - 4, frame_end = (uint64_t)ebp + 0x1c;
+ if ((uint64_t)left + remaining <= 0x100000000ull &&
+ (uint64_t)upper_left + remaining <= 0x100000000ull &&
+ (uint64_t)esi + remaining <= 0x100000000ull &&
+ (uint64_t)output + remaining <= 0x100000000ull &&
+ ((uint64_t)left + remaining <= frame_begin || left >= frame_end) &&
+ ((uint64_t)upper_left + remaining <= frame_begin || upper_left >= frame_end) &&
+ ((uint64_t)esi + remaining <= frame_begin || esi >= frame_end) &&
+ ((uint64_t)output + remaining <= frame_begin || output >= frame_end)) {
+ do {
+ unsigned a = MEM8(left++), b = MEM8(esi++), c = MEM8(upper_left++);
+ int pa = (int)b - (int)c, pb = (int)a - (int)c, pc = pa + pb;
+ if (pa < 0) pa = -pa;
+ if (pb < 0) pb = -pb;
+ if (pc < 0) pc = -pc;
+ unsigned predictor = pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
+ unsigned sum = MEM8(output) + predictor;
+ MEM8(output++) = sum;
+ --remaining;
+
+ /* The scheduler and trace hook see the same registers and
+ * scratch values as the byte-at-a-time implementation. */
+ if (!remaining || (g_backedges + 1 >= RECOMP_YIELD_EVERY && recomp_yield_hook)) {
+ MEM32(ebp + 0x18) = left;
+ MEM32(ebp - 4) = b;
+ MEM32(ebp + 8) = upper_left;
+ MEM32(ebp + 0xc) = pa;
+ MEM32(ebp + 0x10) = output;
+ MEM32(ebp + 0x14) = remaining;
+ eax = output; ebx = pb; ecx = pa; edx = predictor; edi = a;
+ _cf = sum > 255;
+ _flag_a = remaining + 1; _flag_b = 1; _flag_k = FK_DEC;
+ }
+ RECOMP_BACKEDGE();
+ } while (remaining);
+ goto L_250CBF60;
+ }
+ }
/* PNG Paeth predictor: choose left, above or upper-left by distance.
* Keep the guest frame and registers visible at the row loop's yield. */
ecx = MEM8(edx);
diff --git a/src/tests/png_filter.c b/src/tests/png_filter.c
@@ -6,6 +6,22 @@ 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) { abort(); }
+static unsigned yields;
+static void observe_row_yield(void)
+{
+ ++yields;
+ assert(g_esp == 0x5002fff4 && g_ebp == 0x50030004);
+ assert(g_eax == 0x50010001 && g_esi == 0x50020001);
+ assert(g_ebx == 11 && g_ecx == 48 && g_edx == 70 && g_edi == 33);
+ assert(MEM32(g_ebp + 0x18) == 0x5000fffe);
+ assert(MEM32(g_ebp - 4) == 70 && MEM32(g_ebp + 8) == 0x5001fffe);
+ assert(MEM32(g_ebp + 0xc) == 48 && MEM32(g_ebp + 0x14) == 8);
+ assert(MEM32(g_ebp + 0x10) == 0x50010001);
+ assert(MEM8(0x5000fffd) == 33 && MEM8(0x5000fffe) == 208);
+ assert(MEM8(0x5000ffff) == 194 && MEM8(0x50010000) == 75);
+ assert(MEM8(0x50010001) == 99);
+}
+
int main(void)
{
assert(guest_map("PNG row filter", 0x50000000, 0x40000));
@@ -41,12 +57,17 @@ int main(void)
MEM32(g_esp + 12) = 0x5000fffd;
MEM32(g_esp + 16) = 0x5001fffd;
MEM32(g_esp + 20) = 4;
+ /* Yield on the first Paeth byte after the initial RGB pixel. */
+ recomp_yield_hook = i == 0 ? observe_row_yield : NULL;
+ g_backedges = RECOMP_YIELD_EVERY - 4;
+ yields = 0;
sub_250CBDE0();
assert(!memcmp((void *)ADDR(0x5000fffd), cases[i].expected, cases[i].count));
assert(!memcmp((void *)ADDR(0x5001fffd), cases[i].previous, cases[i].count));
assert(MEM8(0x5000fffc) == 0xa5 && MEM8(0x5000fffd + cases[i].count) == 0xa5);
+ assert(yields == (i == 0 ? 1 : 0));
assert(g_esp == 0x5003000c && g_ebp == 123);
assert(g_ebx == 234 && g_esi == 345 && g_edi == 456);
}