From 5bbb4fcdf8a180fdf2d9f0414d4d493989e612a8 Mon Sep 17 00:00:00 2001 From: Shaun Inman Date: Sat, 4 Mar 2023 16:08:18 -0500 Subject: [PATCH] fixed broken native scanline and grid scalers --- src/minarch/main.c | 124 +++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/src/minarch/main.c b/src/minarch/main.c index 9d7a6d2..78dc39e 100644 --- a/src/minarch/main.c +++ b/src/minarch/main.c @@ -1792,9 +1792,9 @@ static void scale1x(void* __restrict src, void* __restrict dst, uint32_t w, uint // eg. gb has a 160 pixel wide image but // gambatte uses a 256 pixel wide buffer // (only matters when using memcpy) - int src_pitch = w * SCREEN_BPP; - int src_stride = pitch / SCREEN_BPP; - int dst_stride = dst_pitch / SCREEN_BPP; + int src_pitch = w * FIXED_BPP; + int src_stride = pitch / FIXED_BPP; + int dst_stride = dst_pitch / FIXED_BPP; int cpy_pitch = MIN(src_pitch, dst_pitch); uint16_t* restrict src_row = (uint16_t*)src; @@ -1811,9 +1811,9 @@ static void scale1x_scanline(void* __restrict src, void* __restrict dst, uint32_ // eg. gb has a 160 pixel wide image but // gambatte uses a 256 pixel wide buffer // (only matters when using memcpy) - int src_pitch = w * SCREEN_BPP; - int src_stride = 2 * pitch / SCREEN_BPP; - int dst_stride = 2 * dst_pitch / SCREEN_BPP; + int src_pitch = w * FIXED_BPP; + int src_stride = 2 * pitch / FIXED_BPP; + int dst_stride = 2 * dst_pitch / FIXED_BPP; int cpy_pitch = MIN(src_pitch, dst_pitch); uint16_t k = 0x0000; @@ -1832,6 +1832,7 @@ static void scale1x_scanline(void* __restrict src, void* __restrict dst, uint32_ } static void scale2x(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; uint16_t* restrict dst_row = (void*)dst + y * dst_pitch * 2; @@ -1843,8 +1844,8 @@ static void scale2x(void* __restrict src, void* __restrict dst, uint32_t w, uint *(dst_row + 1 ) = s; // row 2 - *(dst_row + SCREEN_WIDTH ) = s; - *(dst_row + SCREEN_WIDTH + 1) = s; + *(dst_row + screen_w ) = s; + *(dst_row + screen_w + 1) = s; src_row += 1; dst_row += 2; @@ -1852,6 +1853,7 @@ static void scale2x(void* __restrict src, void* __restrict dst, uint32_t w, uint } } static void scale2x_lcd(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t k = 0x0000; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; @@ -1865,8 +1867,8 @@ static void scale2x_lcd(void* __restrict src, void* __restrict dst, uint32_t w, *(dst_row ) = r; *(dst_row + 1 ) = b; - *(dst_row + SCREEN_WIDTH * 1 ) = g; - *(dst_row + SCREEN_WIDTH * 1 + 1) = k; + *(dst_row + screen_w * 1 ) = g; + *(dst_row + screen_w * 1 + 1) = k; src_row += 1; dst_row += 2; @@ -1874,6 +1876,7 @@ static void scale2x_lcd(void* __restrict src, void* __restrict dst, uint32_t w, } } static void scale2x_scanline(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t k = 0x0000; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; @@ -1885,8 +1888,8 @@ static void scale2x_scanline(void* __restrict src, void* __restrict dst, uint32_ *(dst_row ) = c1; *(dst_row + 1 ) = c1; - *(dst_row + SCREEN_WIDTH ) = c2; - *(dst_row + SCREEN_WIDTH + 1) = c2; + *(dst_row + screen_w ) = c2; + *(dst_row + screen_w + 1) = c2; src_row += 1; dst_row += 2; @@ -1894,6 +1897,7 @@ static void scale2x_scanline(void* __restrict src, void* __restrict dst, uint32_ } } static void scale2x_grid(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t k = 0x0000; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; @@ -1905,8 +1909,8 @@ static void scale2x_grid(void* __restrict src, void* __restrict dst, uint32_t w, *(dst_row ) = c2; *(dst_row + 1 ) = c2; - *(dst_row + SCREEN_WIDTH ) = c2; - *(dst_row + SCREEN_WIDTH + 1) = c1; + *(dst_row + screen_w ) = c2; + *(dst_row + screen_w + 1) = c1; src_row += 1; dst_row += 2; @@ -1914,7 +1918,8 @@ static void scale2x_grid(void* __restrict src, void* __restrict dst, uint32_t w, } } static void scale3x(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { - int row3 = SCREEN_WIDTH * 2; + int screen_w = screen->w; + int row3 = screen_w * 2; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; uint16_t* restrict dst_row = (void*)dst + y * dst_pitch * 3; @@ -1927,9 +1932,9 @@ static void scale3x(void* __restrict src, void* __restrict dst, uint32_t w, uint *(dst_row + 2) = s; // row 2 - *(dst_row + SCREEN_WIDTH ) = s; - *(dst_row + SCREEN_WIDTH + 1) = s; - *(dst_row + SCREEN_WIDTH + 2) = s; + *(dst_row + screen_w ) = s; + *(dst_row + screen_w + 1) = s; + *(dst_row + screen_w + 2) = s; // row 3 *(dst_row + row3 ) = s; @@ -1942,8 +1947,9 @@ static void scale3x(void* __restrict src, void* __restrict dst, uint32_t w, uint } } static void scale3x_lcd(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t k = 0x0000; - int row3 = SCREEN_WIDTH * 2; + int row3 = screen_w * 2; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; uint16_t* restrict dst_row = (void*)dst + y * dst_pitch * 3; @@ -1959,9 +1965,9 @@ static void scale3x_lcd(void* __restrict src, void* __restrict dst, uint32_t w, *(dst_row + 2) = k; // row 2 - *(dst_row + SCREEN_WIDTH ) = r; - *(dst_row + SCREEN_WIDTH + 1) = g; - *(dst_row + SCREEN_WIDTH + 2) = b; + *(dst_row + screen_w ) = r; + *(dst_row + screen_w + 1) = g; + *(dst_row + screen_w + 2) = b; // row 3 *(dst_row + row3 ) = r; @@ -1974,8 +1980,9 @@ static void scale3x_lcd(void* __restrict src, void* __restrict dst, uint32_t w, } } static void scale3x_dmg(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t g = 0xffff; - int row3 = SCREEN_WIDTH * 2; + int row3 = screen_w * 2; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; uint16_t* restrict dst_row = (void*)dst + y * dst_pitch * 3; @@ -1990,9 +1997,9 @@ static void scale3x_dmg(void* __restrict src, void* __restrict dst, uint32_t w, *(dst_row + 2) = a; // row 2 - *(dst_row + SCREEN_WIDTH ) = b; - *(dst_row + SCREEN_WIDTH + 1) = a; - *(dst_row + SCREEN_WIDTH + 2) = a; + *(dst_row + screen_w ) = b; + *(dst_row + screen_w + 1) = a; + *(dst_row + screen_w + 2) = a; // row 3 *(dst_row + row3 ) = c; @@ -2005,6 +2012,7 @@ static void scale3x_dmg(void* __restrict src, void* __restrict dst, uint32_t w, } } static void scale3x_scanline(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t k = 0x0000; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; @@ -2019,14 +2027,14 @@ static void scale3x_scanline(void* __restrict src, void* __restrict dst, uint32_ *(dst_row + 2) = c2; // row 2 - *(dst_row + SCREEN_WIDTH * 1 ) = c1; - *(dst_row + SCREEN_WIDTH * 1 + 1) = c1; - *(dst_row + SCREEN_WIDTH * 1 + 2) = c1; + *(dst_row + screen_w * 1 ) = c1; + *(dst_row + screen_w * 1 + 1) = c1; + *(dst_row + screen_w * 1 + 2) = c1; // row 3 - *(dst_row + SCREEN_WIDTH * 2 ) = c1; - *(dst_row + SCREEN_WIDTH * 2 + 1) = c1; - *(dst_row + SCREEN_WIDTH * 2 + 2) = c1; + *(dst_row + screen_w * 2 ) = c1; + *(dst_row + screen_w * 2 + 1) = c1; + *(dst_row + screen_w * 2 + 2) = c1; src_row += 1; dst_row += 3; @@ -2034,6 +2042,7 @@ static void scale3x_scanline(void* __restrict src, void* __restrict dst, uint32_ } } static void scale3x_grid(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { + int screen_w = screen->w; uint16_t k = 0x0000; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; @@ -2049,14 +2058,14 @@ static void scale3x_grid(void* __restrict src, void* __restrict dst, uint32_t w, *(dst_row + 2) = c1; // row 2 - *(dst_row + SCREEN_WIDTH * 1 ) = c2; - *(dst_row + SCREEN_WIDTH * 1 + 1) = c1; - *(dst_row + SCREEN_WIDTH * 1 + 2) = c1; + *(dst_row + screen_w * 1 ) = c2; + *(dst_row + screen_w * 1 + 1) = c1; + *(dst_row + screen_w * 1 + 2) = c1; // row 3 - *(dst_row + SCREEN_WIDTH * 2 ) = c3; - *(dst_row + SCREEN_WIDTH * 2 + 1) = c2; - *(dst_row + SCREEN_WIDTH * 2 + 2) = c2; + *(dst_row + screen_w * 2 ) = c3; + *(dst_row + screen_w * 2 + 1) = c2; + *(dst_row + screen_w * 2 + 2) = c2; src_row += 1; dst_row += 3; @@ -2064,8 +2073,9 @@ static void scale3x_grid(void* __restrict src, void* __restrict dst, uint32_t w, } } static void scale4x(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { - int row3 = SCREEN_WIDTH * 2; - int row4 = SCREEN_WIDTH * 3; + int screen_w = screen->w; + int row3 = screen_w * 2; + int row4 = screen_w * 3; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; uint16_t* restrict dst_row = (void*)dst + y * dst_pitch * 4; @@ -2079,10 +2089,10 @@ static void scale4x(void* __restrict src, void* __restrict dst, uint32_t w, uint *(dst_row + 3) = s; // row 2 - *(dst_row + SCREEN_WIDTH ) = s; - *(dst_row + SCREEN_WIDTH + 1) = s; - *(dst_row + SCREEN_WIDTH + 2) = s; - *(dst_row + SCREEN_WIDTH + 3) = s; + *(dst_row + screen_w ) = s; + *(dst_row + screen_w + 1) = s; + *(dst_row + screen_w + 2) = s; + *(dst_row + screen_w + 3) = s; // row 3 *(dst_row + row3 ) = s; @@ -2102,8 +2112,9 @@ static void scale4x(void* __restrict src, void* __restrict dst, uint32_t w, uint } } static void scale4x_scanline(void* __restrict src, void* __restrict dst, uint32_t w, uint32_t h, uint32_t pitch, uint32_t dst_pitch) { - int row3 = SCREEN_WIDTH * 2; - int row4 = SCREEN_WIDTH * 3; + int screen_w = screen->w; + int row3 = screen_w * 2; + int row4 = screen_w * 3; uint16_t k = 0x0000; for (unsigned y = 0; y < h; y++) { uint16_t* restrict src_row = (void*)src + y * pitch; @@ -2119,10 +2130,10 @@ static void scale4x_scanline(void* __restrict src, void* __restrict dst, uint32_ *(dst_row + 3) = c1; // row 2 - *(dst_row + SCREEN_WIDTH ) = c2; - *(dst_row + SCREEN_WIDTH + 1) = c2; - *(dst_row + SCREEN_WIDTH + 2) = c2; - *(dst_row + SCREEN_WIDTH + 3) = c2; + *(dst_row + screen_w ) = c2; + *(dst_row + screen_w + 1) = c2; + *(dst_row + screen_w + 2) = c2; + *(dst_row + screen_w + 3) = c2; // row 3 *(dst_row + row3 ) = c1; @@ -2146,7 +2157,7 @@ static void scaleNN(void* __restrict src, void* __restrict dst, uint32_t w, uint int dy = -renderer.dst_h; unsigned lines = h; bool copy = false; - size_t cpy_w = renderer.dst_w * SCREEN_BPP; + size_t cpy_w = renderer.dst_w * FIXED_BPP; while (lines) { int dx = -renderer.dst_w; @@ -2223,8 +2234,9 @@ static void scaleNN_text(void* __restrict src, void* __restrict dst, uint32_t w, unsigned lines = h; bool copy = false; - size_t cpy_w = renderer.dst_w * SCREEN_BPP; - + size_t cpy_w = renderer.dst_w * FIXED_BPP; + int screen_p = screen->pitch; + int safe = w - 1; // don't look behind when there's nothing to see uint16_t l1,l2; while (lines) { @@ -2235,8 +2247,8 @@ static void scaleNN_text(void* __restrict src, void* __restrict dst, uint32_t w, if (copy) { copy = false; - memcpy(dst, dst - SCREEN_PITCH, cpy_w); - dst += SCREEN_PITCH; + memcpy(dst, dst - screen_p, cpy_w); + dst += screen_p; dy += h; } else if (dy < 0) { int col = w; @@ -2267,7 +2279,7 @@ static void scaleNN_text(void* __restrict src, void* __restrict dst, uint32_t w, psrc16++; } - dst += SCREEN_PITCH; + dst += screen_p; dy += h; }