foxygit / fake-08-xbox360 Log in
commit edd3b96e1aedbf0459dd65720e8267be28e2679a
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Wed Nov 25 17:53:40 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Wed Nov 25 17:53:40 2026 +0100

    xex host: faster blit (row scaling + tile-order copy)

    At 1080x1080 the per-pixel blit took 11.4 ms and Celeste's heavier rooms
    dropped to ~41 fps. Each source row is now scaled across once into cached
    memory, and whole tiles are filled four words at a time in address order
    (the tile layout's row pairs; checked against xex_video_tiled_index);
    only edge tiles go pixel by pixel.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/source/XexHost.cpp | 43 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/platform/xex/source/XexHost.cpp b/platform/xex/source/XexHost.cpp
index e3470ea..c37929f 100644
--- a/platform/xex/source/XexHost.cpp
+++ b/platform/xex/source/XexHost.cpp
@@ -61,6 +61,9 @@ static std::vector<uint8_t> xMap, yMap;   // dest pixel -> source pixel
 static int clearFrames = 2;               // both front buffers need clearing

 static uint32_t frameRgb[PicoScreenWidth * PicoScreenHeight];
+// Each source row scaled to the image's width (nearest neighbour)
+static const int MaxDestWidth = 1920;
+static uint32_t scaledRows[PicoScreenHeight * MaxDestWidth];

 static const uint32_t OpaqueBlack = 0x000000ffu;   // opaque black (B, G, R, A)

@@ -116,6 +119,7 @@ static bool fbInit() {
 }

 static void setDestRect(int w, int h) {
+    if (w > MaxDestWidth) w = MaxDestWidth;
     if (w > fbWidth) w = fbWidth;
     if (h > fbHeight) h = fbHeight;
     destW = w;
@@ -610,18 +614,43 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
         clearFrames--;
     }

-    // 3. nearest-neighbour scale into the tiled back buffer, tile by tile
-    //    in address order (write-combined memory)
+    // 3. nearest-neighbour scale into the tiled back buffer.
+    //    a) each source row scaled across into cached memory
+    for (int sy = 0; sy < PicoScreenHeight; sy++) {
+        const uint32_t *src = &frameRgb[sy * PicoScreenWidth];
+        uint32_t *row = &scaledRows[sy * MaxDestWidth];
+        for (int dx = 0; dx < destW; dx++) row[dx] = src[xMap[dx]];
+    }
+    //    b) tiles in address order: in a 32x32 tile each pair of rows is 64
+    //       words, eight groups of 4 pixels of the even row then 4 of the
+    //       odd row; the group order flips (xor 4) when y & 8.
     const int x0 = fbOriginX + destX, y0 = fbOriginY + destY;
     const int x1 = x0 + destW, y1 = y0 + destH;
     for (int ty = y0 & ~31; ty < y1; ty += 32) {
         for (int tx = x0 & ~31; tx < x1; tx += 32) {
             uint32_t *tile = pixels + xex_video_tiled_index(tx, ty, fbPitch);
-            const bool whole = tx >= x0 && tx + 32 <= x1 && ty >= y0 && ty + 32 <= y1;
-            for (int m = 0; m < 1024; m++) {
-                int x = tx + tileX[m], y = ty + tileY[m];
-                if (!whole && (x < x0 || x >= x1 || y < y0 || y >= y1)) continue;
-                tile[m] = frameRgb[yMap[y - y0] * PicoScreenWidth + xMap[x - x0]];
+            if (tx >= x0 && tx + 32 <= x1 && ty >= y0 && ty + 32 <= y1) {
+                const int cx = tx - x0;
+                for (int pair = 0; pair < 16; pair++) {
+                    const int y = ty + pair * 2 - y0;
+                    const uint32_t *even = &scaledRows[yMap[y] * MaxDestWidth + cx];
+                    const uint32_t *odd = &scaledRows[yMap[y + 1] * MaxDestWidth + cx];
+                    uint32_t *out = tile + pair * 64;
+                    const int flip = (pair & 4) ? 4 : 0;
+                    for (int g = 0; g < 8; g++) {
+                        const int xg = (g ^ flip) * 4;
+                        uint32_t *o = out + g * 8;
+                        o[0] = even[xg]; o[1] = even[xg + 1]; o[2] = even[xg + 2]; o[3] = even[xg + 3];
+                        o[4] = odd[xg];  o[5] = odd[xg + 1];  o[6] = odd[xg + 2];  o[7] = odd[xg + 3];
+                    }
+                }
+            } else {
+                // edge tile: only the words inside the image
+                for (int m = 0; m < 1024; m++) {
+                    int x = tx + tileX[m], y = ty + tileY[m];
+                    if (x < x0 || x >= x1 || y < y0 || y >= y1) continue;
+                    tile[m] = scaledRows[yMap[y - y0] * MaxDestWidth + (x - x0)];
+                }
             }
         }
     }