swift-webgpu icon indicating copy to clipboard operation
swift-webgpu copied to clipboard

Support for browsers via SwiftWASM

Open cuberoot opened this issue 1 year ago • 6 comments
trafficstars

Any thoughts of extending your project to also support WebGPU via WASM now that WASM support has been pushed to the mainline of the Swift compiler (although it's not in the official Xcode distro yet...)?

cuberoot avatar Nov 22 '24 03:11 cuberoot

This should be possible - maybe not now, but at some point.

Currently, this project is geared towards Dawn specifically. But once Dawn is compatible with the upstream header (which I think is getting closer), then I will switch to generating from webgpu.yml, which in theory should enable compatibility with Dawn, wgpu-native and indeed Emscripten/WASM.

henrybetts avatar Nov 24 '24 17:11 henrybetts

Thanks for that. I am just getting into Swift. However, I think that the clang-based Swift toolchain is not based on emscripten. It can compile C/C++/Swift to WASM, but it doesn't seem to include the emscripten glue code for C/C++ APIs. I am in the process of seeing how all of this is put together.

In the meantime, I've been pointed to another web-only Swift WebGPU binding based in the IDL for the API.

cuberoot avatar Nov 25 '24 03:11 cuberoot

Ah I see. At a brief glance, that does look like it would be more suitable if you know that you are only targeting web assembly.

henrybetts avatar Nov 25 '24 23:11 henrybetts

That's the issue. We do need to target a wide range of platforms, so your bindings are tempting. However, I don't know that there's a way to use the emscripten bindings. The Switch compiler for WASM doesn't have the system glue code that emscripten does. Their JavaScript binding strategy seems to be JavaScriptKit.

cuberoot avatar Nov 26 '24 04:11 cuberoot

Right, got it. I'll admit, I'm not too familiar with the current state of web assembly in swift. I'll try to find a moment to read up on it.

In theory then, it might be possible to create bindings from the webgpu native api to the JS api, via JavaScriptKit. Whether this can be generated automatically, I'm not sure, since there are some subtle differences between the native and web versions.

henrybetts avatar Nov 27 '24 00:11 henrybetts

It looks like emscripten has the glue code that maps the C api to the JS api. If there's a way to repurpose this but make it compatible with swift-wasm, that could be an efficient solution.

henrybetts avatar Nov 27 '24 01:11 henrybetts