Emscripten: WebGPU js library error

Created on 5 Jun 2020  路  9Comments  路  Source: emscripten-core/emscripten

When inserting this code to my HTML:

    <script async type="text/javascript" src="library_webgpu.js"></script>

I have this error on Browser console:
Uncaught SyntaxError: expected property name, got '{'library_webgpu.js:176:14

Browser info:

  • Firefox nightly 79.0a1 (2020-06-03) (64-bit) (Ubuntu 20.04)
  • WebGPU enabled (I can run WebGPU demos)

All 9 comments

Without declaring the script library_webgpu.js to html file, my build has error:

Uncaught ReferenceError: WebGPU is not defined
    JS_wgpu_init http://localhost/bin/01_window.js:1930
    _main http://localhost/bin/01_window.js:5297
    callMain

In C++ code I use WebGPU object as:

EM_JS(void, JS_wgpu_init, (), {
  WebGPU.initManagers();

My link flags: -s USE_WEBGPU=1 -s WASM=1

library_webgpu.js is not actually a plain JavaScript file - it is not meant to be sourced from the browser. Instead, it's part of Emscripten's templates for JavaScript generation - if you have -s USE_WEBGPU=1, it automatically uses it to generate code included in your build's resulting .js file.

I'm still having Uncaught ReferenceError: WebGPU is not defined while using -s USE_WEBGPU=1. Could this be an Emscripten's bug? How do I investigate this further?

Sorry, I missed that part of your second comment before. You shouldn't use the WebGPU object from your code - it's internal. Take a look at this (outdated!) example for getting WebGPU from C/++:

https://github.com/kainino0x/webgpu-cross-platform-demo/blob/master/index.html#L229
https://github.com/kainino0x/webgpu-cross-platform-demo/blob/master/main.cpp#L27

tl;dr: Set Module.preinitializedWebGPUDevice before initializing the module, then call emscripten_webgpu_get_device() to get the WGPUDevice object.

(There will be a way to create a WebGPU device from C, but we haven't implemented it yet in Emscripten.)

Thanks @kainino0x. I've successfully init WebGPU using your suggestions!
CC: @floooh.

@manhnt9 I'm doing the WebGPU device and swapchain setup with mixed Javascript and C code (all the async setup is done in a JS function called from C code, and at the end of the async-callback chain there's a C function "_sapp_emsc_wgpu_ready" called which communicates all the setup object handles back to the C side. The C side checks each frame if the asynchronous Javascript setup has completed before advancing.

This is the JS setup function: https://github.com/floooh/sokol/blob/d3e1c1065bdf13c436d63eb885ffa77e8d091948/sokol_app.h#L2986-L3008)

And this is the "statemachine switch-case" in the frame callback which waits until the initialization on the Javascript side has completed:

https://github.com/floooh/sokol/blob/d3e1c1065bdf13c436d63eb885ffa77e8d091948/sokol_app.h#L3129-L3148

(PS: I need to do a new round of integrating WebGPU API changes soon, overall the code might be slightly out of date currently)

Yeah, Since Kai pointed out that WebGPU object should not be used directly, I tagged you for the notice.

Using the WebGPU object may be technically okay (although in theory you should not need it). I was concerned that it would break due to minification (maybe with closure compiler optimizations) but I'm not sure. However even if it does work, please avoid using it if possible, as it is not considered to have a stable API (and will definitely break in the future).

Was this page helpful?
0 / 5 - 0 ratings