Lua_beg
Lua_beg

Reputation: 61

WGPU and Dawn (WebGpu)

I am trying to understand the general concept of wgpu and Dawn.

As I understand it, there are two main implementations of the WebGPU standard from Khronos: wgpu from Mozilla and Dawn from Google.

Do I understand correctly that:

-wgpu: is it a C/Rust library that can be compiled from into an executable file of the Operating System and into WebAssembly code for the browser ?

-Dawn: Can only create Web Assembly code for the browser.

And I also wanted to ask: can wgpu create WebAssembly code, does wgpu convert only its API code or C/C++ code too? I can't figure it out.

Upvotes: 4

Views: 5483

Answers (2)

Celso Jr
Celso Jr

Reputation: 295

Both wgpu-native and Dawn are equivalent implementations of the webgpu-headers, which is equivalent to the WebGPU API implemented by wgpu in Rust, based on the official WebGPU standard to be used by Mozilla Firefox/Deno and Chromium based browsers respectively. There is also an emscripten implementation for WASM.

They are a level of abstraction on the way the browser wants to allow you to communicate with the GPU device from hardware vendors. WebGPU is for browsers "pretty much" the same thing Vulkan is for operating systems like Linux. But, of course, with different levels of details/abstractions. Actually, WebGPU runs natively on Vulkan and other backends.

Despite these WebGPU API implementations are intended to be used inside their corresponding browsers, they can also be used as low-level APIs in the operating systems as a desktop app without an embed browser.

-wgpu: is it a C/Rust library that can be compiled from into an executable file of the Operating System and into WebAssembly code for the browser ?

So, wgpu is a pure Rust API implementation of the official WebGPU standard. Think of it as a core implementation. But there is also the webgpu-native, with C headers equivalent to the WebGPU API in Rust. Please do not confuse these with the wgpu-native Rust implementation that has an extra small header ffi/wgpu.h with specific items. Those headers also allow you to foreign function interfaces (FFI) to write C/C++ code with it, as well as use other languages that interface with C. You can compile wgpu-native into binaries to be used in the desired OS, same as you would do with any other Rust program. However, for your convenience, there are pre-built binaries available on the release page.

But you don't need to compile anything if you want to work with the WebGPU in the supported browsers using JavaScript/TypeScript because WebGPU is kind of "embed". In fact, WebGPU is a JavaScript API made for the Web. You just might need a specific compilation to use it in a desktop app if you want to work with other programming languages without "FFIing" with JavaScript via emscripten. And this may or may not change in the future.

-Dawn: Can only create Web Assembly code for the browser.

Dawn is pretty much the same thing wgpu-native is, but implemented by Google in C++ instead of Rust. And without that extra ffi/wgpu.h header with specific items. Those more familiar with the C/C++ ecosystem should feel more comfortable working with Dawn because of fairly obvious factors that I won't mention for the sake of brevity.

And I also wanted to ask: can wgpu create WebAssembly code, does wgpu convert only its API code or C/C++ code too? I can't figure it out.

wgpu itself does not produce anything, the compiler does. So, for example, if you want to produce binaries to be used in the desktop, you can compile from the last commits. You should compile these implementations tailored for your target operating system or environment. Or you can just grab a copy of someone's binaries and just use it right away. Just be careful and try to not use binaries from untrusted/unknown sources for security reasons.

If you like C/C++, it's more recommended to use the Dawn implementation that is an one-to-one mapping with the WebGPU with more detailed error messages and no panic.

Any other languages that can interface with C, like Python, can foreign function interface (FFI) with wgpu-headers too. But just keep in mind that non-system languages will come with some extra overhead costs to give you better developer experience and (maybe) productivity tools. Not a big concern if you aren't working on mission critical/resource intensive operations.

Upvotes: 1

Jinlei Li
Jinlei Li

Reputation: 360

Basically, wgpu and dawn are the WebGPU spec implementations for Firefox and Chrome browsers respectively.

Currently, dawn is the most advanced implementation of the WebGPU spec, and will officially support WebGPU 1.0 in Chrome around May 2023.

A wgpu-based project compiled as a wasm target will be able to run on all WebGPU-enabled browsers.

More than that, the wgpu APIs is based on the WebGPU spec, but it can runs natively on Vulkan, Metal, Dx12, Dx11, and OpenGLES, and also provides additional features support for native applications.

For example, wgpu-in-app demonstrates how easy it is to integrate wgpu into iOS and/or Android app

Upvotes: 11

Related Questions