tron-room-demo: store the framebuffer in panel wire order (fixes swapped colors on HW)

The ST7789 receives pixel bytes high-byte-first, and TFT_eSprite's
pushSprite sends the sprite buffer raw — which is why the sprite's own
primitives store colors byte-swapped (drawPixel: color = color>>8 |
color<<8). RoomViewCore wrote native little-endian values instead, so
every pixel reached the panel byte-swapped: BG 0x0002 showed as green
(0x0200), the fogged floor 0x0882 as pink (0x8208) — exactly the
symptoms reported from hardware.

The core now stores pixels in panel wire order (toWire byteswap on
store, one 16-bit rotate per pixel); the emulator blit unswaps before
the 565-to-8888 expansion, so its output is pixel-identical. Also add
-O2 to the firmware env: PlatformIO's default -Os optimizes for size
and was likely a large part of the 13 fps report.
This commit is contained in:
Dejvino
2026-11-16 22:49:06 +01:00
parent 5310079870
commit 23b3c88caa
3 changed files with 25 additions and 12 deletions
+20 -8
View File
@@ -2,10 +2,12 @@
* RoomViewCore — the RoomView renderer, shared between targets.
*
* Header-only, device-agnostic: renders the whole scene into a caller
* supplied 320x240 RGB565 buffer (the exact pixel format the T-Deck's
* ST7789 panel consumes over SPI). On hardware the caller hands in a
* TFT_eSprite's frameBuffer() pointer so the rasterizer draws straight
* into the sprite — zero copies. In the emulator it is a static array.
* supplied 320x240 RGB565 buffer held in PANEL WIRE ORDER — big-endian,
* the byte order the ST7789 receives over SPI, the same convention as
* TFT_eSprite's own primitives (its drawPixel byteswaps on store). On
* hardware the caller hands in a TFT_eSprite's frameBuffer() pointer so
* the rasterizer draws straight into the sprite — zero copies. In the
* emulator it is a static array, and the shell unswaps+expands to 8888.
*
* Palette comes from NetDeck's nd_layout.h RGB565 constants; the world
* is NetDeck's real demo floorplan via nd_demomap.h / MapSource.
@@ -78,9 +80,15 @@ static inline int fogFor(float d) {
}
// --- Drawing helpers (uint16_t throughout) -----------------------------------
// The buffer holds pixels in PANEL WIRE ORDER: the ST7789 receives bytes
// big-endian (high byte first), and TFT_eSprite's pushSprite sends the
// buffer raw — so, exactly like TFT_eSprite's own primitives, we store
// byte-swapped values. The emulator blit unswaps on its way to 8888.
static inline uint16_t toWire(uint16_t c) { return (uint16_t)((c << 8) | (c >> 8)); }
static inline void putPixel(int x, int y, uint16_t c) {
if (x < 0 || x >= W || y < 0 || y >= H || !g_fb) return;
g_fb[y * W + x] = c;
g_fb[y * W + x] = toWire(c);
}
static void drawLine(int x0, int y0, int x1, int y1, uint16_t color) {
@@ -102,13 +110,14 @@ static void fillCircle(int cx, int cy, int r, uint16_t color) {
if (x * x + y * y <= r * r) putPixel(cx + x, cy + y, color);
}
// Horizontal span with clipping.
// Horizontal span with clipping. Writes in panel wire order (see toWire).
static inline void hline(int y, int xa, int xb, uint16_t c) {
if (y < 0 || y >= H || !g_fb) return;
if (xa > xb) { int t = xa; xa = xb; xb = t; }
if (xb < 0 || xa >= W) return;
if (xa < 0) xa = 0;
if (xb >= W) xb = W - 1;
c = toWire(c);
for (int x = xa; x <= xb; x++) g_fb[y * W + x] = c;
}
@@ -368,8 +377,11 @@ static void rvFrame(float dt, unsigned long nowMs) {
}
}
// --- clear the 565 device framebuffer ---
for (int i = 0; i < W * H; i++) g_fb[i] = COL_BG;
// --- clear the 565 device framebuffer (panel wire order) ---
{
uint16_t bg = toWire(COL_BG);
for (int i = 0; i < W * H; i++) g_fb[i] = bg;
}
// --- floor: flat lit quads for every non-wall tile, checker shaded ---
for (int ty = 0; ty < nd::TILE_ROWS; ty++) {
@@ -51,11 +51,11 @@ void loop() {
rvcore::rvFrame(dt, now);
// --- "SPI push": expand the 565 device buffer into the emulator's 8888 ---
// texture. On the T-Deck this step doesn't exist — the 565 buffer is
// what the panel receives. This is purely a desktop-presentation shim.
// --- "SPI push": the buffer is in panel wire order (big-endian 565),
// exactly what the T-Deck's ST7789 receives. For the desktop window,
// unswap back to native RGB565 and expand to 8888.
for (int i = 0; i < rvcore::W * rvcore::H; i++) {
uint16_t c = fb565[i];
uint16_t c = (uint16_t)((fb565[i] << 8) | (fb565[i] >> 8)); // wire -> native
uint32_t r5 = (c >> 11) & 31, g6 = (c >> 5) & 63, b5 = c & 31;
uint32_t r = (r5 << 3) | (r5 >> 2); // 5 -> 8 bit expansion
uint32_t g = (g6 << 2) | (g6 >> 4); // 6 -> 8 bit expansion
+1
View File
@@ -30,5 +30,6 @@ build_flags =
-DARDUINO_USB_CDC_ON_BOOT=1
-DDISABLE_ALL_LIBRARY_WARNINGS
-DSPI_FREQUENCY=80000000
-O2
-I../../src/netdeck
-I..