Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a WebGPU backend #1403

Open
athas opened this issue Jul 19, 2021 · 3 comments
Open

Add a WebGPU backend #1403

athas opened this issue Jul 19, 2021 · 3 comments
Labels
student-viable Viable as a student project wasm

Comments

@athas
Copy link
Member

athas commented Jul 19, 2021

This is a big one. Current plan is to generate C that calls native WebGPU, which Emscripten can translate to WASM+WebGPU. Two major challenges exist:

  • We must generate shaders in the WebGPU Shader Language (WGSL), which is completely unrelated to C. We cannot just shim it like we did with CUDA, but must write an entirely new code generator.
  • The WebGPU host API is also very dissimilar to CUDA/OpenCL, and reminds me more of Vulkan. Significant effort must be expended here.

Before we can even think about starting the implementation, we need to gain more WebGPU expertise. The best starting point is to write some WebGPU compute shader programs, initially in straight JavaScript, and then in C translated with Emscripten.

On top of these significant tasks, WebGPU is also not a ratified standard and browser support is really shaky. This is a technically very challenging project in all regards.

@athas athas added the wasm label Jul 19, 2021
@athas athas changed the title Add a WebGPU backend. Add a WebGPU backend Jul 19, 2021
@athas
Copy link
Member Author

athas commented Jul 27, 2021

Some notes.

  • Apart from (obviously) running in a browser, there are also "native" WebGPU backends. Specifically wgpu (used to implement WebGPU in Firefox) and Dawn (used for Chrome).
  • These implement a shared header webgpu.h, which is also handled by Emscripten.
  • Wgpu is written in Rust, but they have https://github.com/gfx-rs/wgpu-native, which presents a C API. I have tried it and it works.
  • There is this simple example of compute shaders. It does not work with Emscripten, because it uses the wgpu-specific function wgpuDevicePoll to wait until various async operations complete. Dawn has a similar function. Apparently browsers normally hook this into their own event loop or something, but maybe webgpu.h will need to be extended for our purposes.

Apart from the blocking/polling issue, the WebGPU C API looks reasonably simple (although more complex than OpenCL and CUDA). I don't think it will present major issues. I have yet to closely study WGSL (the language for writing shaders/kernels), but I did notice that it is missing many important scalar types (integers other than 32-bit for example).

@athas
Copy link
Member Author

athas commented Aug 15, 2021

It may be worth investigating how feasible it is to convert SPIR-V shaders to WGSL shaders. We already have parts of a SPIR-V codegen in the Vulkan backend branch, and we'll probably need a SPIR-V codegen eventually. The biggest question is whether we can expect such a conversion program to be available to our users.

@kfl
Copy link
Member

kfl commented Aug 16, 2021

Naga, which is used in the wgpu and gfx-rs projects, can translate SPIR-V to WGSL:

cargo run my_shader.spv my_shader.wgsl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
student-viable Viable as a student project wasm
Projects
None yet
Development

No branches or pull requests

2 participants