commit c69c89ff15dd9831a8e5a3b9e6f3d2c211ce1eb9
Author: mrfox <jens.se@icloud.com>
AuthorDate: Tue Nov 24 22:43:03 2026 +0100
Commit: mrfox <jens.se@icloud.com>
CommitDate: Tue Nov 24 22:43:03 2026 +0100
xex video: program the display scaler like Direct3D, trace output
Console log showed flips latching at 61 fps but a black TV: the title
surface (640x480) was never connected to the output through the scaler.
After engine init we now call VdQueryVideoMode and
VdInitializeScalerCommandBuffer (12 arguments, custom stub that moves
args 9-12 to the console ABI's 8-byte stack slots) and submit the scaler
packets in the ring, as D3D does. Video init/flip tracing with timeouts
(xex_video_trace); the video test logs to game:/video-log.txt and exits
after 20 s.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
platform/xex/runtime/extra_imports.S | 38 +++++++++++
platform/xex/runtime/video.c | 120 ++++++++++++++++++++++++++++++++++-
platform/xex/runtime/xex_video.h | 3 +
platform/xex/tests/video/main.cpp | 36 ++++++++++-
4 files changed, 191 insertions(+), 6 deletions(-)
diff --git a/platform/xex/runtime/extra_imports.S b/platform/xex/runtime/extra_imports.S
index b2bf222..27d3f01 100644
--- a/platform/xex/runtime/extra_imports.S
+++ b/platform/xex/runtime/extra_imports.S
@@ -7,3 +7,41 @@
* Areas is one 64-bit register in the console ABI: callers pass it as a
* 32-bit value in r5 (see xex_notify in runtime/xex.h), Text in r6. */
xbox360_stub XNotifyQueueUI, 0x01010290
+
+/* VOID VdQueryVideoMode(PVOID Mode) - xboxkrnl ordinal 0x1CA. Mode starts
+ * with DisplayWidth, DisplayHeight (ULONG); the block is 0x30 bytes. */
+xbox360_stub VdQueryVideoMode, 0x010001ca
+
+/*
+ * ULONG VdInitializeScalerCommandBuffer(
+ * ULONG SourceXY, ULONG SourceWH, ULONG OutputXY, ULONG OutputWH,
+ * ULONG FrontBufferWH, ULONG VerticalFilterType, PVOID VerticalFilter,
+ * ULONG HorizontalFilterType, PVOID HorizontalFilter, ULONG Unknown9,
+ * PULONG Commands, ULONG CommandDwords) - xboxkrnl ordinal 0x1C5
+ * XY/WH are (x << 16 | y) and (w << 16 | h). Writes scaler packets for the
+ * command ring into Commands (Direct3D pre-fills it with 0x80000000 NOPs).
+ *
+ * 12 arguments: 9-12 are on the stack. SysV callers put them at 8(r1) of
+ * their frame, 4 bytes each; console callees read 8-byte slots from 0x50
+ * of the caller frame (as in xenon-forge's VdSwap stub).
+ */
+.ifndef VdInitializeScalerCommandBuffer
+ xbox360_function VdInitializeScalerCommandBuffer
+ xbox360_frame 0x70
+ li 12, 0
+ stw 12, 0x50(1)
+ lwz 11, 0x78(1)
+ stw 11, 0x54(1)
+ stw 12, 0x58(1)
+ lwz 11, 0x7c(1)
+ stw 11, 0x5c(1)
+ stw 12, 0x60(1)
+ lwz 11, 0x80(1)
+ stw 11, 0x64(1)
+ stw 12, 0x68(1)
+ lwz 11, 0x84(1)
+ stw 11, 0x6c(1)
+ xbox360_call VdInitializeScalerCommandBuffer
+ xbox360_return 0x70
+ xbox360_end VdInitializeScalerCommandBuffer, 0x010001c5
+.endif
diff --git a/platform/xex/runtime/video.c b/platform/xex/runtime/video.c
index 8cf3635..30af81e 100644
--- a/platform/xex/runtime/video.c
+++ b/platform/xex/runtime/video.c
@@ -16,6 +16,26 @@
#include "xex_video.h"
+/* Imports declared in extra_imports.S */
+extern void VdQueryVideoMode(void *mode);
+extern ULONG VdInitializeScalerCommandBuffer(ULONG source_xy, ULONG source_wh, ULONG output_xy,
+ ULONG output_wh, ULONG front_buffer_wh,
+ ULONG vertical_filter_type, void *vertical_filter,
+ ULONG horizontal_filter_type, void *horizontal_filter,
+ ULONG unknown9, PULONG commands, ULONG command_dwords);
+
+void (*xex_video_trace)(const char *fmt, ...);
+#define TRACE(...) do { if (xex_video_trace) xex_video_trace(__VA_ARGS__); } while (0)
+
+static inline uint64_t timebase_now(void)
+{
+ uint64_t tb;
+ __asm__ volatile("mftb %0" : "=r"(tb));
+ return tb;
+}
+/* 100 ms at 49.875 MHz */
+#define WAIT_TIMEOUT_TB 4987500ull
+
/* ------------------------------------------------------------------------ */
/* GPU registers, memory, processor helpers */
@@ -310,6 +330,7 @@ static void retrain_edram(int at_init)
ULONG state = 0;
PULONG commands = NULL;
ULONG due = VdRetrainEDRAM(flags, &commands, RETRAIN_COMMAND_DWORDS, &state, work, RETRAIN_WORK_BYTES);
+ if (at_init || due) TRACE("retrain(%s): due=%u state=%u", at_init ? "init" : "swap", (unsigned)due, (unsigned)state);
if (due) {
if (!at_init) {
ring_block_until_idle(&display.ring);
@@ -327,19 +348,50 @@ static void retrain_edram(int at_init)
KeLeaveCriticalRegion();
}
+static unsigned flip_count;
+
static void wait_for_flip(void)
{
if (!display.pending) return;
+ uint64_t start = timebase_now();
+ int verbose = flip_count < 4;
/* the swap identifier lands first ... */
for (;;) {
uint32_t completed = read_word(&display.ring.writeback, SWAP_IDENTIFIER_OFFSET);
if ((int32_t)(completed - display.pending_identifier) >= 0) break;
+ if (timebase_now() - start > WAIT_TIMEOUT_TB) {
+ TRACE("flip %u: TIMEOUT waiting for identifier %08x (completed %08x, rptr %u, wptr %u)",
+ flip_count, display.pending_identifier, completed,
+ read_word(&display.ring.writeback, READ_POINTER_OFFSET), display.ring.write);
+ display.pending = 0;
+ return;
+ }
low_priority_hint();
}
/* ... then the display controller takes the address ... */
- while (gpu_read(SCANOUT_ADDRESS) != display.pending_address) low_priority_hint();
+ start = timebase_now();
+ while (gpu_read(SCANOUT_ADDRESS) != display.pending_address) {
+ if (timebase_now() - start > WAIT_TIMEOUT_TB) {
+ TRACE("flip %u: TIMEOUT scanout address %08x, expected %08x",
+ flip_count, gpu_read(SCANOUT_ADDRESS), display.pending_address);
+ display.pending = 0;
+ return;
+ }
+ low_priority_hint();
+ }
/* ... and latches it at the next blank: one flip per refresh */
- while (gpu_read(SCANOUT_UPDATE) & (UPDATE_PENDING | UPDATE_TAKEN)) low_priority_hint();
+ start = timebase_now();
+ while (gpu_read(SCANOUT_UPDATE) & (UPDATE_PENDING | UPDATE_TAKEN)) {
+ if (timebase_now() - start > WAIT_TIMEOUT_TB) {
+ TRACE("flip %u: TIMEOUT scanout update %08x", flip_count, gpu_read(SCANOUT_UPDATE));
+ break;
+ }
+ low_priority_hint();
+ }
+ if (verbose) {
+ TRACE("flip %u: done, scanout %08x control %08x update %08x",
+ flip_count, gpu_read(SCANOUT_ADDRESS), gpu_read(SCANOUT_CONTROL), gpu_read(SCANOUT_UPDATE));
+ }
display.pending = 0;
}
@@ -367,6 +419,12 @@ static void submit_flip(uint32_t address, const uint32_t fetch_constant[6])
ring_commit(&display.ring, FLIP_DWORDS + SWAP_DWORDS);
ring_kick(&display.ring);
+ if (flip_count < 4) {
+ TRACE("flip %u: submitted address %08x identifier %08x sysbuf %08x/%u state %u",
+ flip_count, address, (unsigned)identifier, (unsigned)system_buffer.Address,
+ (unsigned)system_buffer.Dwords, (unsigned)system_buffer.State);
+ }
+ flip_count++;
retrain_edram(0);
display.pending = 1;
@@ -374,6 +432,51 @@ static void submit_flip(uint32_t address, const uint32_t fetch_constant[6])
display.pending_address = address;
}
+/*
+ * Program the display scaler the way Direct3D does after device creation:
+ * the kernel writes scaler packets (source surface -> output mode) into a
+ * NOP-filled buffer, which goes into the command ring. Without it the
+ * scanout of a title surface is not shown (black screen on hardware;
+ * Xenia does not emulate the scaler).
+ */
+#define SCALER_COMMAND_DWORDS 200 /* 0x320 bytes, as D3D uses */
+
+static void setup_scaler(uint32_t surface_w, uint32_t surface_h)
+{
+ uint32_t mode[16];
+ memset(mode, 0, sizeof(mode));
+ VdQueryVideoMode(mode);
+ uint32_t out_w = mode[0] ? mode[0] : surface_w;
+ uint32_t out_h = mode[1] ? mode[1] : surface_h;
+ TRACE("video mode: %ux%u interlaced %u widescreen %u hidef %u",
+ (unsigned)mode[0], (unsigned)mode[1], (unsigned)mode[2], (unsigned)mode[3], (unsigned)mode[4]);
+
+ static uint32_t vertical_filter[4] __attribute__((aligned(16)));
+ static uint32_t horizontal_filter[4] __attribute__((aligned(16)));
+ static uint32_t commands[SCALER_COMMAND_DWORDS] __attribute__((aligned(16)));
+ memset(vertical_filter, 0, sizeof(vertical_filter));
+ memset(horizontal_filter, 0, sizeof(horizontal_filter));
+ for (int i = 0; i < SCALER_COMMAND_DWORDS; i++) commands[i] = 0x80000000u;
+
+ uint32_t surface_wh = (surface_w << 16) | surface_h;
+ ULONG written = VdInitializeScalerCommandBuffer(0, surface_wh, 0, (out_w << 16) | out_h, surface_wh,
+ 1, vertical_filter, 1, horizontal_filter, 0,
+ (PULONG)commands, SCALER_COMMAND_DWORDS);
+ int used = 0;
+ for (int i = 0; i < SCALER_COMMAND_DWORDS; i++) if (commands[i] != 0x80000000u) used = i + 1;
+ TRACE("scaler: %ux%u -> %ux%u, returned %u, %d dwords written, first %08x %08x %08x %08x",
+ (unsigned)surface_w, (unsigned)surface_h, (unsigned)out_w, (unsigned)out_h,
+ (unsigned)written, used, commands[0], commands[1], commands[2], commands[3]);
+ TRACE("scaler filters: v %08x %08x %08x %08x h %08x %08x %08x %08x",
+ vertical_filter[0], vertical_filter[1], vertical_filter[2], vertical_filter[3],
+ horizontal_filter[0], horizontal_filter[1], horizontal_filter[2], horizontal_filter[3]);
+
+ uint32_t *w = ring_reserve(&display.ring, SCALER_COMMAND_DWORDS);
+ memcpy(w, commands, sizeof(commands));
+ ring_commit(&display.ring, SCALER_COMMAND_DWORDS);
+ ring_kick(&display.ring);
+}
+
const char *xex_video_error(void)
{
return display.error ? display.error : "";
@@ -387,6 +490,9 @@ int xex_video_init(void)
memset(&info, 0, sizeof(info));
VdGetCurrentDisplayInformation(&info);
uint32_t sw = info.SurfaceWidth, sh = info.SurfaceHeight;
+ TRACE("display: surface %ux%u window %u,%u-%u,%u colorspace %u",
+ (unsigned)sw, (unsigned)sh, (unsigned)info.Left, (unsigned)info.Top,
+ (unsigned)info.Right, (unsigned)info.Bottom, (unsigned)info.ColorSpace);
if (sw == 0 || sh == 0 || sw % TILE_EDGE != 0) {
display.error = "unsupported display mode";
return -1;
@@ -412,19 +518,27 @@ int xex_video_init(void)
}
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. */
- VdInitializeEngines(0, NULL, NULL, NULL, NULL);
+ ULONG engines = VdInitializeEngines(0, NULL, NULL, NULL, NULL);
+ TRACE("VdInitializeEngines -> %u, scanout before: %08x control %08x",
+ (unsigned)engines, gpu_read(SCANOUT_ADDRESS), gpu_read(SCANOUT_CONTROL));
if (ring_init(&display.ring) != 0) {
VdShutdownEngines();
display.error = "out of memory (ring)";
return -1;
}
+ TRACE("ring: commands %08x writeback %08x, rptr %u wptr %u",
+ (unsigned)display.ring.commands.physical, (unsigned)display.ring.writeback.physical,
+ read_word(&display.ring.writeback, READ_POINTER_OFFSET), display.ring.write);
display.open = 1;
display.back = 0;
display.pending = 0;
retrain_edram(1);
+ setup_scaler(sw, sh);
atexit(xex_video_shutdown);
return 0;
}
diff --git a/platform/xex/runtime/xex_video.h b/platform/xex/runtime/xex_video.h
index ad54cd8..2cfff09 100644
--- a/platform/xex/runtime/xex_video.h
+++ b/platform/xex/runtime/xex_video.h
@@ -35,6 +35,9 @@ xex_frame xex_video_begin(void);
/* Flips to the back buffer at the next vertical blank. */
void xex_video_present(void);
+/* Optional trace of initialization and flips (diagnostics). */
+extern void (*xex_video_trace)(const char *fmt, ...);
+
/* Gives the GPU back to the system (also done at exit). */
void xex_video_shutdown(void);
diff --git a/platform/xex/tests/video/main.cpp b/platform/xex/tests/video/main.cpp
index 62954ea..9f316bd 100644
--- a/platform/xex/tests/video/main.cpp
+++ b/platform/xex/tests/video/main.cpp
@@ -4,14 +4,33 @@
// the frame rate. Back+Start returns to the dashboard.
#include <cstdio>
#include <cstdint>
+#include <cstdarg>
#include "xex_runtime.h"
#include "xex_video.h"
#include "xex_input.h"
+// Diagnostics: every trace line is appended to game:/video-log.txt (the
+// file is closed after each line so it survives a hang).
+static void logLine(const char *fmt, ...) {
+ char line[256];
+ va_list ap;
+ va_start(ap, fmt);
+ vsnprintf(line, sizeof line, fmt, ap);
+ va_end(ap);
+ printf("video: %s\n", line);
+ if (FILE *f = fopen("game:/video-log.txt", "a")) {
+ fprintf(f, "%s\n", line);
+ fclose(f);
+ }
+}
+
int main(int, char **) {
+ remove("game:/video-log.txt");
+ logLine("start");
+ xex_video_trace = logLine;
if (xex_video_init() != 0) {
- printf("video: init failed: %s\n", xex_video_error());
+ logLine("init failed: %s", xex_video_error());
xex_notify("video init failed");
xex_sleep_ms(4000);
return 1;
@@ -24,9 +43,17 @@ int main(int, char **) {
int frames = 0, fps = 0;
unsigned t = 0;
+ logLine("init ok");
+ uint64_t startTime = xex_timebase();
+ int seconds = 0;
for (;;) {
xex_pad pad;
- xex_input_get(0, &pad);
+ int connected = xex_input_get(0, &pad);
+ // stop by itself after 20 s, so a black screen still tells whether the loop runs
+ if (xex_timebase() - startTime > 20 * freq) {
+ logLine("20 s elapsed, exiting");
+ break;
+ }
if ((pad.buttons & XEX_PAD_BACK) && (pad.buttons & XEX_PAD_START)) break;
int speed = 6;
@@ -66,12 +93,15 @@ int main(int, char **) {
uint64_t now = xex_timebase();
if (now - second >= freq) {
fps = frames;
- printf("video: %d fps, %dx%d window at %d,%d\n", fps, f.window_width, f.window_height, f.window_x, f.window_y);
+ logLine("second %d: %d fps, %dx%d window at %d,%d, pad %s buttons %04x",
+ ++seconds, fps, f.window_width, f.window_height, f.window_x, f.window_y,
+ connected == 0 ? "ok" : "none", pad.buttons);
frames = 0;
second = now;
}
last = now;
}
(void)last;
+ logLine("exit");
return 0;
}