cwoffenden / hello-webgpu

Cross-platform C++ example for WebGPU and Dawn
272 stars 36 forks source link

configureSwapChain error in browser for emcc build #11

Closed tanzle-aames closed 3 years ago

tanzle-aames commented 3 years ago

This may not be the best place to report this. The only thing that changed was Chrome Canary daily build today.

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'configureSwapChain')
    at k (818984c9-28d3-4b7e-873b-6bf7bbbf543b:formatted:498)
    at 455e65be:0x4d6
    at 818984c9-28d3-4b7e-873b-6bf7bbbf543b:formatted:158

This is the browser code that throws:

k: function(a, b, d) {
            a = H.mgrDevice.get(a);
            b = H.ba.get(b);
            b.width = A[d + 16 >> 2];
            b.height = A[d + 20 >> 2];
            b = b.getContext("gpupresent");
            a = {
                label: void 0,
                device: a,
                format: H.T[A[d + 12 >> 2]],
                usage: A[d + 8 >> 2]
            };
            (d = z[d + 4 >> 2]) && (a.label = h(d));
            d = b.configureSwapChain(a); // !!!!!!!! THROWS HERE !!!!!!!!
            return H.ca.create(d)

Native Windows build works fine. libdawn has not changed. So, this is caused by some interaction between hello-webgpu, libdawn, and Chrome.

Thanks in advance.

cwoffenden commented 3 years ago

This is in Emscripten's WebGPU code:

https://github.com/emscripten-core/emscripten/blob/07f9930c088c2df0dd096d9ca29ebfd626ef4710/src/library_webgpu.js#L1878

I think the browser API has changed, so this will need a fix in Emscripten (the first hurdle being finding up-to-date JS docs).

briangavin commented 3 years ago

I am experiencing the same thing. I think the API has changed related to swap chains. I am not sure where to find up-to-date JS docs, but I found this code example. https://austin-eng.com/webgpu-samples/samples/helloTriangle

and this youtube video https://youtu.be/yTkGXYlIjEw

cwoffenden commented 3 years ago

It should be fixed with this Emscripten PR:

https://github.com/emscripten-core/emscripten/pull/14951

But you can change the basics by editing library_webgpu.js, search for wgpuDeviceCreateSwapChain then:

Change the getContext() line to:

var ctx = canvas.getContext('webgpu');

Change the configureSwapChain line to:

var swapChain = ctx["configure"](desc);

And finally change the return statement to:

return WebGPU.mgrSwapChain.create(ctx);

That's enough to run this simple demo in both older Chrome (removing the warnings) and the latest Chrome Canary.

tanzle-aames commented 3 years ago

Nice workaround. Gotta love bleeding edge.

cwoffenden commented 3 years ago

Closing this - Emscripten latest is now updated (run emsdk update tot to get it) and the code here brought up-to-date with both it and the latest Dawn.