Can SDL3 GPU functions/shader be compiled to WASM?

The new SDL3 GPU functions are awesome but can they also be used for the browser solution?
(could not get it working yet)

Functions like SDL_CreateGPUShader() or other functions from SDL_gpu.h

Have you tried getting the snake game (which doesn’t use gpu api) working with wasm?
I followed the instructions and got it running but it is easy to miss something and be confused.

If you have emsdk_env.sh you’re likely close. I run “source path/to/emsdk_env.sh” then I use em++

I don’t know for sure, but I can’t see why not. All my SDL2 programs which use shaders (by creating a new GL context and temporarily bypassing SDL) work fine using WebGL in a browser. For example this Voronoi graphic is shader code.

Have you tried getting the snake game (which doesn’t use gpu api) working with wasm?

No, there is some error upon generating the examples itself. (missing zlib, despite I have it installed). Completely beginner here, just discovered it recently.

work fine using WebGL in a browser

I can’t find the web page again but as far as I recall SDL3 will not support WebGL but for further plans will do so for WebGPU. But there was no word about WASM. It’s about high performance support. As far as I know WASM can run things quicker than WebGL and WebGPU.

That’s very strange. Here’s what I need to do

source “my/path/emsdk/emsdk_env.sh”
emcc snake.c -I/usr/local/include/wasm /usr/local/lib/wasm/libSDL3.a -o snake.html

That’s it. I get a js, html and wasm file. I did have to build my own libSDL3.a. I don’t know if there’s a version you can download? emsdk_env.sh tells the shell where the compiler is and emcc is a C like wrapper to generate wasm files

I started on this page SDL3/README-emscripten - SDL Wiki
I remember reading the linux instructions and it downloaded a version of the sdk. That’s what had the emsdk_env.sh script I used as the first step.

Thank you for helping out but I’m still missing something.
I got snake working in normal (g++) SDL3 and I got emcc working for a simple hallo world program. (just the generation of the html file. At testing it locally in Firefox I got no text.)

I also installed the emsdk and I could use emsdk_env.sh as source.
But I have no “/usr/local/include/wasm” “/usr/local/lib/wasm/libSDL3.a” (also in no other folder)
How to get these?

I have found some gpu examples: GitHub - TheSpydog/SDL_gpu_examples: Example collection for the SDL_GPU API
I got the git working with normal SDL3. (it cycles through examples)

e.g. Cubemap.c also has some shaders. If you like you could try this with emcc as wasm.

I’m not sure what you mean by that. WebGL and WebGPU are graphics subsystems (libraries) which can be called from WASM code, but run at the same speed whatever they are called from.

You can’t use SDL3 GPU for browser stuff right now because it doesn’t have a WebGPU backend yet, and doesn’t support OpenGL/WebGL.

1 Like

Thanks. So WASM ist not enough to port it.
Google search shows some SDL3 WebGPU extension but it looks like the SDL3 dev team want to do their own approach.
Haven’t found anything about the planned timeline.

Might it work in some months?

AFAIK WebGPU hasn’t even been finalized yet, let alone widely adopted.

1 Like

That’s part of the reason why I haven’t tried your suggestions you gave me a few weeks ago, about removing the double/triple buffering. There wasn’t enough examples for me to understand the correct way to implement my code. I do want to try it and I saw TheSpydog examples a few weeks ago.

What does that have to do with WebGPU?

Oh. I misread that as sdl gpu api isn’t complete. If that’s not the case than I’m not sure why the github repo has no examples

AFAIK the plan is to merge TheSpyDog’s examples in Eventually

1 Like