cwoffenden / hello-webgpu

Cross-platform C++ example for WebGPU and Dawn

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some questions about hello-webgpu

FuXiii opened this issue · comments

When I research hello-webgpu use Emscripten, I had met some questions:

  1. Using Emscripten to compile main.cpp (./src/main.cpp) will include webgpu.h(./inc/webgpu.h), and webgpu.h(./inc/webgpu.h) will also include webgpu/webgpu.h. Where is this webgpu/webgpu.h?
  2. Where can I found the documents for the various flags set in CMAKE_CXX_FLAGS? I can't found the description about -Wno-nonportable-include-path in gcc doc
  3. Where can I find documents such as -s WASM=1,-s USE_WEBGPU=1 in CMAKE_EXE_LINKER_FLAGS
  4. Why would there be a shell.html file? For what purpose? Where can I find the document?
  5. What is glue.h/glue.cpp used for? Where can I find the document?
  6. Where are documents such as WGPUDevice? In https://www.w3.org/TR/webgpu/ the WGPUDevice was not found in the official documentation of WebGPU, but GPUDevice could be found, but such as WGPUSurfaceDescriptorFromCanvasHTMLSelector or GPUSurfaceDescriptorFromCanvasHTMLSelector could not be found.
  7. Where is the WebGPU document for Emscripten?

Thanks~(●'◡'●)ノ♥

Hi! Some answers:

  1. webgpu/webgpu.h is part of Emscripten, in the system folder (see here).
  2. The nonportable-include-path warning is from Clang, and I read about it years ago.
  3. All the Emscripten flags are documented here. Anything missing can be found in the settings sources.
  4. The shell file is to have a cut-down HTML page that does the loading, display, etc., of the Wasm content. You can build without it, but then you get all of the Emscripten console and other widgets on your page (details, little as they are, are found on the flags docs above, search for --shell-file).
  5. glue.h and glue.cpp are my code, used to hold other parts of the code together. Since this is written for Windows, Mac and the browser, it smooths out some of the features. Compare with the Mac and Windows sources.
  6. I wrote this years ago to experiment with Dawn and WebGPU. Not many docs existed at the time and I worked things out from whatever I could, some from the spec docs, some from the Dawn examples.
  7. It's all very incomplete, and you'll need to look at Dawn and the JavaScript APIs to make sense of it.

Hope that helps!

Thank you for your answer! o(〃'▽'〃)o
In the third item the documented here link url is https://github.com/cwoffenden/hello-webgpu/issues/nonportable-include-path it look like a invalid link. (`・ω・′)ゞ

Link fixed!

Thanks~(●'◡'●)ノ♥

Some question about compile hello-webgpu base emscripten:

  • Why must use #define KEEP_IN_MODULE extern "C" __attribute__((used, visibility("default"))) before _glue_main_() ?
    Where I can find the documents about it?

    #ifndef KEEP_IN_MODULE
    #define KEEP_IN_MODULE extern "C" __attribute__((used, visibility("default")))
    #endif
    
    KEEP_IN_MODULE void _glue_main_() { __main__(0, nullptr); }
  • In glue.cpp the _glue_main_() will call __main__(int argc, char* argv[]) which defined in main.cpp ,but in JavaScript code void glue_preint() which defined in EM_JS it will call the var entry which is __glue_main_ .Why entry equal to __glue_main_ instead of _glue_main_ (one less underline) ?

    KEEP_IN_MODULE void _glue_main_() { __main__(0, nullptr); }
    
    EM_JS(void, glue_preint, (), {
      var entry = __glue_main_;  // Why can't be: var entry = _glue_main_;

    I try to find the JavaScript code in browser, I found:

    ...
    var __glue_main_, _main, _malloc, _free, stackSave, stackRestore, stackAlloc, dynCall_jiji;
    
    WebAssembly.instantiate(Module['wasm'], imports).then(function(output) {
        asm = output.instance.exports;
        __glue_main_ = asm["_glue_main_"];
    ...
    }

    Is that means all exposed C/C++ function into WebAssembly will add a underline in front of it?

ヾ(๑╹◡╹)ノ" Thanks~

The KEEP_IN_MODULE macro is used because I want to call _glue_main_() from JavaScript (once the WebGPU context has been asynchronously created). Without this, and depending on other compiler flags, it risks being either name mangled or removed from the exports.

And yes, in JS an extra _ is inserted.

Ok, Thanks~ ٩(๑❛ᴗ❛๑)۶

If I run hello-webgpu in python -m http.server server, it will work well. but if I copy hello-webgpu.html , hello-webgpu.js and hello-webgpu.wasm into real http server and open the page in same browser it will show nothing and output No support for WebGPU error message.

image

The page URL is this

It fails because WebGPU use needs serving from a secure context (HTTPS). This link works, for example:

https://wip.numfum.com/cw/2023-05-12/index.html

Thank you very much!!! ~(●'◡'●)ノ♥