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

    xex video: native-resolution front buffers, scaler off

    The kernel's 640x480 surface was stretched by the display scaler to the
    1080p output: blurry, and 4:3 pulled to 16:9. On a progressive output the
    front buffers now have the output's size and are shown 1:1 (scaler off,
    as libxenon and xsdk do); the Guide restores that scaling when it closes
    and exit restores the kernel's. FAKE-08 then scales 8x itself (1024x1024
    on 1080p), writing each 32x32 tile in address order for the
    write-combined memory, and logs the video init.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/README.md          |  7 +++-
 platform/xex/runtime/video.c    | 89 +++++++++++++++++++++++++++++------------
 platform/xex/source/XexHost.cpp | 54 ++++++++++++++++++++-----
 3 files changed, 114 insertions(+), 36 deletions(-)

diff --git a/platform/xex/README.md b/platform/xex/README.md
index b12f705..018e145 100644
--- a/platform/xex/README.md
+++ b/platform/xex/README.md
@@ -90,7 +90,12 @@ The source is `source/XexHost.cpp`, the same logic as the libxenon host
   overflow can't be reported.
 - `xex_runtime.h`: `xex_notify()` (guide notification), timing helpers.
 - **Video** (`xex_video.h`): takes the GPU engines like Direct3D, flips two
-  tiled 32 bpp front buffers with `VdSwap` at vertical blank. On hardware a
+  tiled 32 bpp front buffers with `VdSwap` at vertical blank. On a
+  progressive output the front buffers have the output's size (for example
+  1920x1080) and are shown 1:1 with the display scaler off, so the title
+  scales sharply itself; otherwise (interlaced output, Xenia) the kernel's
+  640x480 surface is stretched by its scaler (`xex_video_native = 0` forces
+  that). On hardware a
   title's surface only shows once the display LUT is filled and the surface
   is committed the way XAM does it (0x2840/0x2844 + D1GRPH, then 0x60EC = 1);
   found by [xsdk](https://github.com/jessenic/xsdk) (docs/HARDWARE.md).
diff --git a/platform/xex/runtime/video.c b/platform/xex/runtime/video.c
index 210bd87..90f10e6 100644
--- a/platform/xex/runtime/video.c
+++ b/platform/xex/runtime/video.c
@@ -318,6 +318,8 @@ static struct {
      * surfaces through its registers. Xenia has none and shows what VdSwap
      * names; writing its scanout register directly turns its picture white. */
     int controller;
+    /* front buffers of the output's size, scaler off */
+    int native;
     uint32_t output_width, output_height;
 } display;

@@ -396,6 +398,7 @@ static const uint16_t scaler_offsets[SCALER_REGISTERS] = {
     0x6590, 0x6594, 0x6584, 0x65e8, 0x65b0, 0x65b4, 0x65b8, 0x65c0, 0x65c4, 0x65c8};
 #define REG_SCALER_UPDATE (0x65cc / 4)
 static uint32_t kernel_scaler[SCALER_REGISTERS];
+static uint32_t own_scaler[SCALER_REGISTERS];   /* for the title's surface */

 static void scaler_write(const uint32_t values[SCALER_REGISTERS])
 {
@@ -505,8 +508,8 @@ static void wait_for_flip(void)
         return;
     }
     if (display.showing_system) {
-        /* the Guide closed: the kernel's scaling for our surface again */
-        scaler_write(kernel_scaler);
+        /* the Guide closed: our surface's scaling again */
+        scaler_write(own_scaler);
         display.showing_system = 0;
         TRACE("guide: closed after %u frames", display.system_frames);
     }
@@ -752,6 +755,8 @@ const char *xex_video_error(void)
     return display.error ? display.error : "";
 }

+int xex_video_native = 1;
+
 int xex_video_init(void)
 {
     if (display.open) return 0;
@@ -772,25 +777,6 @@ int xex_video_init(void)
         display.error = "unsupported display window";
         return -1;
     }
-    display.pitch_tiles = sw / TILE_EDGE;
-    display.tile_x = info.Left / TILE_EDGE;
-    display.tile_y = info.Top / TILE_EDGE;
-    display.width = info.Right - info.Left;
-    display.height = info.Bottom - info.Top;
-
-    uint32_t tiles_high = (sh + TILE_EDGE - 1) / TILE_EDGE;
-    uint32_t bytes = display.pitch_tiles * tiles_high * TILE_EDGE * TILE_EDGE * 4;
-    for (int i = 0; i < 2; i++) {
-        front_buffer *fb = &display.buffers[i];
-        if (phys_alloc(&fb->memory, bytes, PAGE_WRITECOMBINE_) != 0) {
-            display.error = "out of memory (front buffer)";
-            return -1;
-        }
-        memset(fb->memory.words, 0, fb->memory.bytes);
-        front_buffer_fetch(fb->fetch_constant, (uint32_t)fb->memory.words, display.pitch_tiles, sw, sh);
-        TRACE("front buffer %d: virtual %08x physical %08x bytes %u", i,
-              (unsigned)(uint32_t)fb->memory.words, (unsigned)fb->memory.physical, (unsigned)fb->memory.bytes);
-    }

     /* Null microcode selects the kernel's own. */
     ULONG engines = VdInitializeEngines(0, NULL, NULL, NULL, NULL);
@@ -812,19 +798,70 @@ int xex_video_init(void)
      * arguments, so it is off unless a test turns it on. */
     if (xex_video_scaler_via_kernel) setup_scaler(sw, sh);
     display.presented = 0;
-    display.surface_height = sh;

-    /* The display's LUT is zero while a title runs: load a ramp, then show
-     * the (black) second buffer through the same path XAM uses. */
+    /* The display's LUT is zero while a title runs: load a ramp. Whether
+     * the autofill finishes also tells whether a display controller is
+     * there (not in Xenia). */
     display.controller = load_identity_gamma();
     old_surface.address = gpu_read(SCANOUT_ADDRESS);
     old_surface.pitch = gpu_read(REG_D1GRPH_PITCH);
     old_surface.x_end = gpu_read(REG_D1GRPH_X_END);
     old_surface.y_end = gpu_read(REG_D1GRPH_Y_END);
     for (int i = 0; i < SCALER_REGISTERS; i++) kernel_scaler[i] = gpu_read(scaler_offsets[i] / 4);
+
+    /* Native mode: front buffers of the output's size shown 1:1 with the
+     * scaler off (as libxenon does; xsdk docs/HARDWARE.md, "a 1280x720
+     * surface shows 1:1"), instead of the kernel's 640x480 surface that the
+     * scaler stretches (blurry, and 4:3 pulled to 16:9). Progressive modes
+     * only. */
+    uint32_t mode[16];
+    memset(mode, 0, sizeof(mode));
+    VdQueryVideoMode(mode);
+    display.output_width = mode[0];
+    display.output_height = mode[1];
+    display.native = xex_video_native && display.controller && mode[0] && mode[1] && !mode[2] &&
+                     mode[0] % TILE_EDGE == 0 && mode[0] <= 1920 && mode[1] <= 1088;
+    TRACE("output %ux%u interlaced %u -> %s", (unsigned)mode[0], (unsigned)mode[1], (unsigned)mode[2],
+          display.native ? "native surface, scaler off" : "kernel surface, kernel scaling");
+    if (display.native) {
+        sw = mode[0];
+        sh = mode[1];
+        info.Left = info.Top = 0;
+        info.Right = sw;
+        info.Bottom = sh;
+    }
+    display.pitch_tiles = sw / TILE_EDGE;
+    display.tile_x = info.Left / TILE_EDGE;
+    display.tile_y = info.Top / TILE_EDGE;
+    display.width = info.Right - info.Left;
+    display.height = info.Bottom - info.Top;
+    display.surface_height = sh;
+
+    uint32_t tiles_high = (sh + TILE_EDGE - 1) / TILE_EDGE;
+    uint32_t bytes = display.pitch_tiles * tiles_high * TILE_EDGE * TILE_EDGE * 4;
+    for (int i = 0; i < 2; i++) {
+        front_buffer *fb = &display.buffers[i];
+        if (phys_alloc(&fb->memory, bytes, PAGE_WRITECOMBINE_) != 0) {
+            display.error = "out of memory (front buffer)";
+            xex_video_shutdown();
+            return -1;
+        }
+        volatile uint32_t *w = fb->memory.words;
+        for (uint32_t j = 0; j < fb->memory.bytes / 4; j++) w[j] = 0x000000ffu;   /* opaque black */
+        front_buffer_fetch(fb->fetch_constant, (uint32_t)fb->memory.words, display.pitch_tiles, sw, sh);
+        TRACE("front buffer %d: %ux%u virtual %08x physical %08x bytes %u", i, (unsigned)sw, (unsigned)sh,
+              (unsigned)(uint32_t)fb->memory.words, (unsigned)fb->memory.physical, (unsigned)fb->memory.bytes);
+    }
+
+    if (display.native) {
+        scaler_plan(sw, sh, own_scaler);
+        scaler_write(own_scaler);
+    } else {
+        memcpy(own_scaler, kernel_scaler, sizeof(own_scaler));
+    }
     describe_display(sw, sh);
     int shown = display_show(display.buffers[1].memory.physical);
-    TRACE("show buffer 1: 60EC %s, scanout %08x", shown ? "taken" : "TIMEOUT", gpu_read(SCANOUT_ADDRESS));
+    TRACE("show buffer 1: %s, scanout %08x", shown ? "taken" : "TIMEOUT", gpu_read(SCANOUT_ADDRESS));
     atexit(xex_video_shutdown);
     return 0;
 }
@@ -858,9 +895,9 @@ void xex_video_shutdown(void)
     if (!display.open) return;
     wait_for_flip();
     /* leave the scanout at address 0 as the kernel's own ring does */
-    if (display.showing_system) scaler_write(kernel_scaler);
     submit_flip(0, display.buffers[display.back ^ 1].fetch_constant, 0);
     wait_for_flip();
+    if (display.native || display.showing_system) scaler_write(kernel_scaler);
     display_apply(&old_surface);
     ring_detach(&display.ring);
     VdShutdownEngines();
diff --git a/platform/xex/source/XexHost.cpp b/platform/xex/source/XexHost.cpp
index f63ab54..e3470ea 100644
--- a/platform/xex/source/XexHost.cpp
+++ b/platform/xex/source/XexHost.cpp
@@ -14,6 +14,7 @@
  * Exit: Xbox button -> Xbox Home in the Guide, or Back+Start.
  */
 #include <stdio.h>
+#include <stdarg.h>
 #include <string.h>
 #include <dirent.h>
 #include <sys/stat.h>
@@ -67,12 +68,38 @@ static inline int tiledIndex(int x, int y) {
     return xex_video_tiled_index(fbOriginX + x, fbOriginY + y, fbPitch);
 }

+// Where each word of a 32x32 tile (4 KB, in address order) sits in the
+// tile: the inverse of xex_video_tiled_index inside one tile. Writing a
+// tile in address order keeps the write-combined stores sequential.
+static uint8_t tileX[1024], tileY[1024];
+
+static void initTileOrder() {
+    for (int y = 0; y < 32; y++)
+        for (int x = 0; x < 32; x++) {
+            int m = xex_video_tiled_index(x, y, 32);
+            tileX[m] = (uint8_t)x;
+            tileY[m] = (uint8_t)y;
+        }
+}
+
 static inline uint32_t fbColor(const Color &c) {
     return xex_video_rgb(c.Red, c.Green, c.Blue);
 }

+static void videoTrace(const char *fmt, ...) {
+    char line[256];
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(line, sizeof(line), fmt, ap);
+    va_end(ap);
+    Logger_Write("video: %s\n", line);
+}
+
 static bool fbInit() {
-    if (xex_video_init() != 0) {
+    xex_video_trace = videoTrace;
+    int failed = xex_video_init();
+    xex_video_trace = nullptr;   // init only; flips stay quiet
+    if (failed) {
         Logger_Write("video init failed: %s\n", xex_video_error());
         return false;
     }
@@ -84,6 +111,7 @@ static bool fbInit() {
     fbOriginX = f.window_x;
     fbOriginY = f.window_y;
     Logger_Write("video: %dx%d window at %d,%d, pitch %d\n", fbWidth, fbHeight, fbOriginX, fbOriginY, fbPitch);
+    initTileOrder();
     return true;
 }

@@ -576,17 +604,25 @@ void Host::drawFrame(uint8_t *picoFb, uint8_t *screenPaletteMap, uint8_t drawMod
     uint64_t tbBlitStart = now();

     if (clearFrames > 0) {
-        for (int y = 0; y < fbHeight; y++)
-            for (int x = 0; x < fbWidth; x++) pixels[tiledIndex(x, y)] = OpaqueBlack;
+        // the whole surface, in address order
+        size_t words = (size_t)fbPitch * (size_t)((fbOriginY + fbHeight + 31) & ~31);
+        for (size_t i = 0; i < words; i++) pixels[i] = OpaqueBlack;
         clearFrames--;
     }

-    // 3. nearest-neighbour scale into the tiled back buffer
-    for (int dy = 0; dy < destH; dy++) {
-        const uint32_t *srcRow = &frameRgb[yMap[dy] * PicoScreenWidth];
-        int y = destY + dy;
-        for (int dx = 0; dx < destW; dx++) {
-            pixels[tiledIndex(destX + dx, y)] = srcRow[xMap[dx]];
+    // 3. nearest-neighbour scale into the tiled back buffer, tile by tile
+    //    in address order (write-combined memory)
+    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]];
+            }
         }
     }
     drawPerfOverlay(pixels);