aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-12 20:31:10 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-12 20:31:10 +0200
commite06434fd89345fdc4e931f6fa3904626d75097db (patch)
tree75287102b1e1ec85db4cdb7d634da4b5fee6f9a7
parentc6f5cca39dad3f395e8b479f6106feb10e710695 (diff)
Fix Byte Order for Surface Pixels
I went down a deep rabbithole to understand the pixel order, and it turns out that the Emscripten library code is setting the wrong `format` value for what it actually uses, which threw me off. We now set the correct pixel format tag, and handle pixel byte ordering correctly on the host. Some other minor fixes.
-rw-r--r--doc/journal.md15
-rw-r--r--src/fb.c11
-rw-r--r--src/fb.h3
-rw-r--r--src/game.c50
-rw-r--r--src/picow.c5
-rw-r--r--stub/pico8.wat10
6 files changed, 58 insertions, 36 deletions
diff --git a/doc/journal.md b/doc/journal.md
index 7bfce6a..a0d18b5 100644
--- a/doc/journal.md
+++ b/doc/journal.md
@@ -1,3 +1,18 @@
+# 12.10.2026
+
+Looking at the exported Emscripted glue code, it encodes the pixel format as `0x86462004` which is `SDL_PIXELFORMAT_RGBA8888` - an endian-independent pixel format. For example, the PICO-8 baby blue is encoded as:
+
+```
+[0x29, 0xad, 0xff, 0xff]
+#ffffad29 r=29 g=ad b=ff a=ff
+```
+
+However, this contradicts my understanding of SDL pixel formats. From the docs:
+
+> Names with a bit count per component, such as ARGB8888 [...], are "packed" into an appropriately-sized integer in the platform's native endianness. For example, ARGB8888 is a sequence of 32-bit integers; in each integer, the most significant bits are alpha, and the least significant bits are blue. On a little-endian CPU such as x86, the least significant bits of each integer are arranged first in memory, but on a big-endian CPU such as s390x, the most significant bits are arranged first. Use these names if the property of a format that is important to you is the meaning of each bit position within a native-endianness integer.
+
+Which suggests, given the mask and pixel order, should be `ABGR8888` instead. Use the correct value `0x86762004` instead of just copying what the Emscripten glue code does, we can revert it in case it causes issues with the actual PICO-8 engine once we get there.
+
# 02.10.2026
I figured out that PICO-8 is built with Emscripten using the [SDL 1.3 built-in](https://github.com/emscripten-core/emscripten/tree/main/system/include/SDL). We can use the headers to understand memory layout of the various pieces used by PICO-8. I noticed that:
diff --git a/src/fb.c b/src/fb.c
index e46cdce..a50ff03 100644
--- a/src/fb.c
+++ b/src/fb.c
@@ -10,10 +10,11 @@ struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer,
SDL_Texture *texture = NULL;
struct pw_framebuffer *fb = NULL;
- // Note that we use a different pixel format here, this is to ensure we match
- // the PICO-8 engine whose format is endian-dependant.
+ // Web Assembly guarantees little-endian semantics, so the pixel data we get
+ // will _always_ be RGBA in **byte** sequence. Use the appropriate single bit
+ // count format, which has a stable byte order regardless of our endianness.
texture =
- SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
+ SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_STREAMING, PW_FB_WIDTH, PW_FB_HEIGHT);
if (!texture) {
goto error;
@@ -116,9 +117,9 @@ bool pw_fb_blit(struct pw_framebuffer *fb, uint8_t const pixels[PW_FB_SIZE]) {
return false;
}
- bool ok = pitch == sizeof(uint32_t[PW_FB_WIDTH]);
+ bool ok = pitch == PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL;
if (ok) {
- SDL_memcpy(buffer, pixels, sizeof(uint32_t[PW_FB_WIDTH * PW_FB_HEIGHT]));
+ SDL_memcpy(buffer, pixels, PW_FB_SIZE);
}
SDL_UnlockTexture(fb->texture);
diff --git a/src/fb.h b/src/fb.h
index 0ac503c..7656921 100644
--- a/src/fb.h
+++ b/src/fb.h
@@ -7,7 +7,8 @@
#define PW_FB_WIDTH 128
#define PW_FB_HEIGHT 128
-#define PW_FB_SIZE (128 * 128 * 4)
+#define PW_FB_BYTES_PER_PIXEL 4
+#define PW_FB_SIZE (PW_FB_WIDTH * PW_FB_HEIGHT * PW_FB_BYTES_PER_PIXEL)
/**
* A PICO-8 framebuffer.
diff --git a/src/game.c b/src/game.c
index af87116..2505e2a 100644
--- a/src/game.c
+++ b/src/game.c
@@ -46,6 +46,11 @@ static void emrt_set_main_loop(wasm_exec_env_t exec, uint64_t *args) {
(void)simulate_infinite_loop;
}
+// PICO-8 engine is compiled with the Emscripten built-in SDL1.3 library, which
+// has a different pixel format encoding than SDL3 which is used on the host.
+// The pixel format used by the engine is:
+#define EMRT_PIXELFORMAT_ABGR8888 0x86762004
+
static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
native_raw_return_type(int32_t, args);
native_raw_get_arg(int32_t, w, args);
@@ -66,14 +71,14 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
uint8_t *memory;
{
uint32_t size = 60 + 44 + PW_FB_SIZE;
- uint32_t args[] = {size};
- if (!wasm_runtime_call_wasm(exec, game->malloc, 1, args) || !args[0]) {
+ uint32_t margs[] = {size};
+ if (!wasm_runtime_call_wasm(exec, game->malloc, 1, margs) || !margs[0]) {
SDL_LogWarn(0, "failed to allocate surface");
native_raw_set_return(0);
return;
}
- surface = args[0];
+ surface = margs[0];
memory = emrt_memory(exec, surface, size);
if (!memory) {
SDL_LogWarn(0, "invalid surface allocation");
@@ -94,20 +99,20 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
emrt_write(&cursor, uint32_t, 0); // userdata
emrt_write(&cursor, uint32_t, 0); // locked
emrt_write(&cursor, uint32_t, 0); // lock_data
- emrt_write(&cursor, uint32_t, 0); // clip_rect.x
- emrt_write(&cursor, uint32_t, 0); // clip_rect.y
- emrt_write(&cursor, uint32_t, w); // clip_rect.w
- emrt_write(&cursor, uint32_t, h); // clip_rect.h
+ emrt_write(&cursor, int32_t, 0); // clip_rect.x
+ emrt_write(&cursor, int32_t, 0); // clip_rect.y
+ emrt_write(&cursor, int32_t, w); // clip_rect.w
+ emrt_write(&cursor, int32_t, h); // clip_rect.h
emrt_write(&cursor, uint32_t, 0); // map
emrt_write(&cursor, uint32_t, 1); // refcount
};
// pixel format
{
void *cursor = memory + 60;
- emrt_write(&cursor, uint32_t, 0x86462004); // RGBA8888
- emrt_write(&cursor, uint32_t, 0); // palette
- emrt_write(&cursor, int8_t, bpp);
- emrt_write(&cursor, int8_t, bpp / 8);
+ emrt_write(&cursor, uint32_t, EMRT_PIXELFORMAT_ABGR8888);
+ emrt_write(&cursor, uint32_t, 0); // palette
+ emrt_write(&cursor, int8_t, (int8_t)bpp);
+ emrt_write(&cursor, int8_t, (int8_t)bpp / 8);
emrt_write(&cursor, uint16_t, 0); // padding
emrt_write(&cursor, uint32_t, 0x000000ff); // rmask
emrt_write(&cursor, uint32_t, 0x0000ff00); // gmask
@@ -118,9 +123,9 @@ static void emrt_set_video_mode(wasm_exec_env_t exec, uint64_t *args) {
emrt_write(&cursor, uint8_t, 0); // bloss
emrt_write(&cursor, uint8_t, 0); // aloss
emrt_write(&cursor, uint8_t, 0); // rshift
- emrt_write(&cursor, uint8_t, 0); // gshift
- emrt_write(&cursor, uint8_t, 0); // bshift
- emrt_write(&cursor, uint8_t, 0); // ashift
+ emrt_write(&cursor, uint8_t, 8); // gshift
+ emrt_write(&cursor, uint8_t, 16); // bshift
+ emrt_write(&cursor, uint8_t, 24); // ashift
emrt_write(&cursor, int32_t, 1); // refcount
emrt_write(&cursor, uint32_t, 0); // next
}
@@ -187,6 +192,12 @@ static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
return;
}
+ // Always clear the locked flag as unlocking should never fail.
+ {
+ void *cursor = memory + 28;
+ emrt_write(&cursor, uint32_t, 0); // locked
+ };
+
uint32_t pixels;
SDL_memcpy(&pixels, memory + 20, sizeof(pixels));
@@ -196,13 +207,8 @@ static void emrt_unlock_surface(wasm_exec_env_t exec, uint64_t *args) {
}
if (!pw_fb_blit(game->fb, buffer)) {
- return;
+ SDL_LogWarn(0, "failed to blit surface to framebuffer: %s", SDL_GetError());
}
-
- {
- void *cursor = memory + 28;
- emrt_write(&cursor, uint32_t, 0); // locked
- };
}
struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode) {
@@ -372,7 +378,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *cart) {
game->dyncall = dyncall;
game->mix = mix;
if (!wasm_runtime_call_wasm(game->exec, ctor, 0, NULL)) {
- return SDL_SetError("error calling constructory: %s",
+ return SDL_SetError("error calling constructor: %s",
wasm_runtime_get_exception(game->instance));
}
@@ -459,7 +465,7 @@ bool pw_game_iterate(struct pw_game *game) {
{
uint32_t args[] = {42, 1337};
if (!wasm_runtime_call_wasm(game->exec, game->mix, 2, args)) {
- return SDL_SetError("error calling audio mixery: %s",
+ return SDL_SetError("error calling audio mixer: %s",
wasm_runtime_get_exception(game->instance));
}
if (!args[0]) {
diff --git a/src/picow.c b/src/picow.c
index d6358e3..bc26b44 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -1,4 +1,3 @@
-#include "SDL3/SDL_render.h"
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
@@ -21,7 +20,7 @@ static SDL_AppResult failf(char const *fmt, ...) {
}
char const *err = SDL_GetError();
- if (err) {
+ if (err && *err) {
SDL_LogCritical(0, "%s: %s", msg, err);
} else {
SDL_LogCritical(0, "%s", msg);
@@ -84,7 +83,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
return failf("failed to configure minimum window size");
}
if (!SDL_SetRenderVSync(state->renderer, 1)) {
- return failf("failed to enable V-sync");
+ SDL_LogWarn(0, "failed to enable V-sync: %s", SDL_GetError());
}
state->players = pw_players_init();
diff --git a/stub/pico8.wat b/stub/pico8.wat
index 4c631aa..aaa4461 100644
--- a/stub/pico8.wat
+++ b/stub/pico8.wat
@@ -93,22 +93,22 @@
(i32.eq (local.get $x) (local.get $mx))
(i32.eq (local.get $y) (local.get $my)))
(then
- (local.set $color (i32.const 0x000000ff))
+ (local.set $color (i32.const 0xff000000))
(if (i32.and (local.get $buttons) (i32.const 1))
(then
(local.set $color
- (i32.or (local.get $color) (i32.const 0xff000000)))))
+ (i32.or (local.get $color) (i32.const 0x000000ff)))))
(if (i32.and (local.get $buttons) (i32.const 2))
(then
(local.set $color
- (i32.or (local.get $color) (i32.const 0x00ff0000)))))
+ (i32.or (local.get $color) (i32.const 0x0000ff00)))))
(if (i32.and (local.get $buttons) (i32.const 4))
(then
(local.set $color
- (i32.or (local.get $color) (i32.const 0x0000ff00)))))
+ (i32.or (local.get $color) (i32.const 0x00ff0000)))))
)
(else
- (local.set $color (i32.const 0x1d2b53ff))))
+ (local.set $color (i32.const 0xff532b1d))))
(i32.store (local.get $addr) (local.get $color))
(local.set $x (i32.add (local.get $x) (i32.const 1)))
(br $x_loop)))