将我的 C 语言游戏移植到 WASM,这是我遇到的所有 Bug
Ported my C game to WASM, here's everybug that I hit

原始链接: http://ernesernesto.github.io/writes/portingmatchmorphosistowasm/

通过 Emscripten 将自研 C 引擎移植到 Web 是一个很有收获的过程,前提是你需要处理好 64 位原生环境与 32 位 WebAssembly (WASM) 之间的差异。以下是我在移植过程中总结的关键经验: * **指针与结构体大小:** WASM 的 32 位架构会导致序列化原始指针时出现结构体布局不匹配的问题。请将烘焙数据(baking data)与运行时数据分离,以确保内存对齐的一致性。 * **在 32 位原生环境下调试:** 不要直接在浏览器中调试。使用带有 `AddressSanitizer` 的 32 位原生构建版本来捕捉内存损坏问题——在 WASM 中导致崩溃的错误通常可以在此处重现,并获得完整的调试器支持。 * **平台严苛性:** OpenGL ES (WebGL) 的容错性远低于 Direct3D。请确保顶点布局与着色器输入完全匹配,并注意帧缓冲区中 Y 坐标的翻转问题。 * **着色器移植:** 使用 `mix()` 代替 `lerp()`,并确保所有浮点数都明确标注(例如使用 `1.0` 而非 `1`)。 * **工具链:** 使用 `vcvars32` 直接从命令行构建 32 位原生二进制文件,无需复杂的解决方案文件。 * **Web 特性:** 如果音频库需要,请确保 Emscripten 导出了必要的运行时方法(如 `HEAPF32`)。 移植工作在很大程度上是为了修复那些仅在 64 位环境下“碰巧能运行”的代码。

```Hacker News最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 投稿登录将我的 C 语言游戏移植到 WASM,我遇到的所有错误 (ernesernesto.github.io)9 分,由 birdculture 发布于 2 小时前 | 隐藏 | 过往 | 收藏 | 1 条评论帮助 pioh 2 分钟前 [–] 我想破解《林中之夜》(Night in the Forest)99 回复 准则 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索: ```
相关文章

原文

I wrote a game in plain C with a custom engine (bgfx, SDL2, miniaudio, cimgui) and recently ported it to web via Emscripten. Its live on itchio now. Here’s everything non-obvious that I ran into, hopefully saves someone some pain.

0. Had to go back to Visual Studio. Ugh.

I use RemedyBG as my daily debugger and its great, but it doesnt support 32-bit processes. Since WASM is 32-bit, I needed a 32-bit native build to reproduce bugs locally, which meant firing up Visual Studio again.

Turns out you don’t need a solution file. Just run:

devenv build\main.exe

and before you build, add vcvars32 to your build process

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"

On VS, just Hit F5 or F11 and it runs the exe directly. No sln file needed, works fine for stepping through code and catching crashes. Not ideal but got the job done.

1. Web is 32-bit. Your 64-bit structs will break.

This was the root cause of most of my bugs. WASM is 32-bit address space, pointers are 4 bytes not 8. I was serializing asset structs directly to disk (pak file) that had raw pointers in them:

typedef struct AssetSprite {
    u32 width, height;
    u8* dataBytes;  // 8 bytes on 64-bit, 4 bytes on WASM
    i32 dataSize;
} AssetSprite;

When I packed assets on 64-bit Windows and loaded them on WASM, the struct layout was completely different. sizeof(Assets) was 26328 on native and 25556 on web. Every field after the first pointer was at the wrong offset, so all texture and shader data came out as garbage.

In hindsight this is probably obvious to anyone who builds cross platform regularly, but I havent built 32-bit in years so I tripped on the pointer size thing.

Fix: I separated runtime data from baking data entirely. Instead of a pointer living inside the asset struct, I now have a flat array on the side:

AssetDataBytes assetData[TOTAL_ASSET_COUNT];
i32 assetDataId;

typedef struct AssetDataBytes {
    u8* data;
    i32 size;
} AssetDataBytes;

Every time I add a new asset during baking, just bump assetDataId and write the bytes there. The serialized asset struct has no pointers at all, so layout is identical on 32 and 64-bit. Packer is single threaded and still finishes under 3 seconds for the whole game, good enough for my use case since asset count is relatively small.

2. Debug in 32-bit native, not the browser

This was the biggest productivity unlock honestly. Since 32-bit native has the same struct sizes as WASM, bugs that only appeared on web also appeared on 32-bit native, where I had real breakpoints, memory watch, and call stacks.

For actually hunting the bugs I used a combination of /fsanitize=address when compiling plus data breakpoints. Trigger the bug, ASan will catch the bad access. Data breakpoint would also tells you exactly what wrote to that address. Makes what would be a multi hour hunt into something you can solve pretty quickly. Dont try to debug WASM crashes from the browser console alone since its painful and slow.

3. A bug that was silently correct on 64-bit

typedef struct ThingHandle {
    i32 id;
    i32 generation;
} ThingHandle;

// wrong
game->boardPieces = swAlloc(sizeof(ThingHandle*) * row * column);

// correct
game->boardPieces = swAlloc(sizeof(ThingHandle) * row * column);

On 64-bit, sizeof(ThingHandle*) is 8, which happens to be the same as sizeof(ThingHandle). So the wrong code allocated exactly the right amount of memory by coincidence and worked fine for a while. On 32-bit WASM, sizeof(ThingHandle*) is 4, so it allocated half the memory it needed and corrupted whatever came after it. Pretty classic mixup, just hidden for a long time by 64-bit making them accidentally equal.

4. OpenGL ES (WebGL) is way stricter than Direct3D

bgfx uses Direct3D on Windows and OpenGL ES on web. A bunch of things I got away with on D3D broke hard on WebGL:

Vertex layout renderer type: I was passing BGFX_RENDERER_TYPE_NOOP to bgfx_vertex_layout_begin. Works on D3D, broken on OpenGL because it cant assign correct attribute locations. Use bgfx_get_renderer_type() instead.

Component count mismatch: I had COLOR1 declared as 2 components in the layout but the shader used vec4. D3D ignores the mismatch. OpenGL ES throws a fatal every frame. Component counts must exactly match what the shader declares.

Framebuffer Y flip - OpenGL has Y=0 at the bottom, D3D has Y=0 at the top. My fullscreen blit was upside down on web. Fixed by flipping UV V coordinates in the final render target texture blit.

5. Shaders need recompiling for GLSL ES

bgfx’s shaderc compiles for specific backends. My shaders were HLSL compiled for DirectX. On web I needed GLSL ES, profile flag changes from -p s_5_0 to -p 300_es.

Two things that tripped me up:

  • lerp() is HLSL only. GLSL uses mix(). bgfx’s bgfx_shader.sh already defines mix as a cross platform macro so just use that everywhere and both platforms work.
  • GLSL ES is strict about integer vs float. Passing 0 or 1 to a float parameter is a compile error. Has to be 0.0 and 1.0.

6. Web Audio autoplay + a weird Emscripten exports issue

Google has implemented a policy in their browsers that prevent automatic media output without first receiving some kind of user input. Miniaudio handles this internally by registering click and touchend listeners that resume the AudioContext automatically. I spend too much time trying to make miniaudio web build works messing around with a lot of it’s flags AUDIO_WORKLET, WASM_WORKERS, ASYNCIFY. Even trying to make a different initialization path between web & native, the web init after the first touch, but it still not working, there’s still an error throws on the js console when the AudioContext initialized.

Turns out newer versions of Emscripten seem to remove some runtime exports by default. miniaudio needs HEAPF32 to be available from JS side and it wasnt. Had to explicitly add it:

-s EXPORTED_RUNTIME_METHODS="['ccall','cwrap','HEAPF32']"

Not sure if this is a newer Emscripten behavior or a combination of my flags, couldn’t find anything on google about it, might save someone an hour of head scratching. All things considered, miniaudio really get the job done, nothing need to be initialized differently between native and web

Final thoughts

Genuinely happy with how it turned out, I spent a weekend on this port and honestly expected it to take longer. Writing a custom C engine, porting it to web, having the game load fast and play instantly with no Unity or Godot baggage, that feels really good.

The Emscripten toolchain is solid. Most of the pain came from things that worked by accident on Windows that the web holds you accountable for. Once you know what to look for, fixing them is pretty straightforward.

Game is live https://zhongda8.itch.io/matchmorphosis

And you can wishlist it https://store.steampowered.com/app/4131100/Match_Morphosis

Thanks for reading!

联系我们 contact @ memedata.com