Repository navigation
Description
emsdk version 3.1.46
Building and running webgpu samples with webgpu.h works fine without the closure pass, but when trying to enable closure with the linker option --closure 1, linking appears to work, but when running I get the following errors on the browser js console:
Uncaught (in promise) TypeError: Failed to execute 'configure' on 'GPUCanvasContext': Failed to read the 'format' property from 'GPUCanvasConfiguration': Required member is undefined.
at Da (triangle-sapp.js:54:9)
at triangle-sapp.wasm:0x1165
at triangle-sapp.wasm:0x1b7b
at triangle-sapp.js:42:369
at mc (triangle-sapp.js:27:301)
at triangle-sapp.js:42:252
onerror: Uncaught TypeError: Failed to execute 'configure' on 'GPUCanvasContext': Failed to read the 'format' property from 'GPUCanvasConfiguration': Required member is undefined.
triangle-sapp.js:54 Uncaught TypeError: Failed to execute 'configure' on 'GPUCanvasContext': Failed to read the 'format' property from 'GPUCanvasConfiguration': Required member is undefined.
at Da (triangle-sapp.js:54:9)
at triangle-sapp.wasm:0x1165
at triangle-sapp.wasm:0x3269
at Object.lb (triangle-sapp.js:18:70)
at b (triangle-sapp.js:11:46)
The JS file location triangle-sapp.js:54:9 looks like this:
The only place in library_webgpu.js which has a configure call seems to be:
emscripten/src/library_webgpu.js
Lines 2607 to 2614 in 1311f22
| var configuration = { | |
| "device": device, | |
| "format": WebGPU.TextureFormat[ | |
| {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.format) }}}], | |
| "usage": {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.usage) }}}, | |
| "alphaMode": "opaque", | |
| }; | |
| context["configure"](configuration); |
I don't know what's needed to make such a JS shim closure-compatible, e.g. whether there are generally some sort of annotations missing, or only in this specific location. I could probably help with investigation and fixing if I would know where to start :)
Cheers!
(cc: @kainino0x)
Activity
[-]webgpu.js shim doesn't seem to support closure pass[/-][+]library_webgpu.js shim doesn't seem to support closure pass[/+]kripken commentedon Oct 9, 2023
Maybe add a little logging - is the value sent into
formatundefined somehow? If the read on the typed array is out of bounds, it would be, but it does seem odd for closure to cause that... if it is undefined, comparing to a non-closure build could help.Otherwise, that code (both in the output and the source) seems ok.
The only other idea I'd have is if the error is actually on the internals of that object send into
device:,a(if it's expected to have a format field but doesn't - logging out that object might help).floooh commentedon Oct 10, 2023
I'll try to investigate, thanks for the hints!
kainino0x commentedon Oct 10, 2023
Most likely
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.format) }}}->z[c + 12 >> 2]is somehow producing a value that's either 0 or out of bounds ofWebGPU.TextureFormat. This could be flaky if the C value was uninitialized, though I have no idea why this would be affected by Closure.floooh commentedon Oct 11, 2023
Hmm, the incoming
WGPUSwapChainDescriptorlooks correct (usage, width, height) except for theformatwhich is 0....I'm getting that pixel format via
wgpuSurfaceGetPreferredFormat, maybe this returns zero when closure is active, looking at that next...floooh commentedon Oct 11, 2023
I think I found the culprit... not sure yet why it happens. But:
Closure-ized
wgpuSurfaceGetPreferredFormat(emscripten/src/library_webgpu.js
Lines 2577 to 2580 in 1311f22
'var a' is
'bgra8unorm'which is then used as key on theobobject (which should be a mapping from string names to numbers), but theobproperty names have been minified:So
ob['bgra8unorm']would return undefined, because there's no propertybgra8unormonob, because those have been minified by closure.floooh commentedon Oct 11, 2023
Hmm, those string-to-number mapping objects all look the same, I assume those are all affected by the minification, but the
PreferredFormatis just the first one that's encountered:emscripten/src/library_webgpu.js
Lines 317 to 330 in 1311f22
I guess those need to be annotated somehow so that closure leaves the keys alone. I'll see if I can find any similar mapping objects elsewhere and see how those are handled.
PS: hmm apparently putting the property names into quotes should be enough to prevent closure minification... why doesn't this work? (PS: apparently only in 'ADVANCED' closure mode, I'll keep digging)
floooh commentedon Oct 11, 2023
Ok I give up. I haven't found a way so far sneaking those string-to-number mappings past Closure.
Apparently there's an undocumented tag
/** @expose */, and there's also an official tag/** @export */. Either they don't work as I would expect, or maybe comments are removed before closure sees them.I also tried to create the mapping object differently like this:
...also didn't seem to work.
Halp!!1 :)
kripken commentedon Oct 11, 2023
Interesting... I tested various combinations here:
Those show up in the output JS like this:
So the first two are broken (missing
") while the last is ok. The last is by far the most common way we use quoted objects, that is, inside code and not on an object that is directly included itself, so I'd guess something in that direct inclusion path is going wrong. I'm investigating that now.floooh commentedon Oct 11, 2023
Hmm, one solution is to turn the mapping into a function:
...this allows it to get past the swapchain creation.
But then the next problem (and something entirely different) is in the
wgpuDeviceGetLimitsshim:emscripten/src/library_webgpu.js
Lines 712 to 727 in 4b33521
...this gets minified to:
Note how the original code:
...minified to:
...meaning
.limitsis now.nb!This might turn out to become a real rabbit hole...
kripken commentedon Oct 11, 2023
Unfortunately the issue is due to how JS parsing and toString works:
That is, JS engines keep a copy of the source JS for function, and
toString()retrieves that including any quotation marks. But JS engines do not do that for objects, where the quotes are simply lost.A hard way to fix this would be to not use the JS engine native parsing and instead use Acorn, whose AST does note quoted properties. But that would mean integration Acorn in an entirely new part of Emscripten, and could make compilation slower.
Another option might be to always quote object properties. But that would be excessive and increase code size.
Other than that, we could work around this in
library_webgpuusing a function as @floooh said. That would also increase code size a little, but just here. That might be the best option if no one else has a better idea.floooh commentedon Oct 11, 2023
That last format seems to work if the shim is changed like this:
...interestingly Closure seems to turn that function into:
...pretty crazy...
PS: this works too:
kripken commentedon Oct 11, 2023
If
.limitsarrives from the WebGPU API, thendevice.limitsshould bedevice['limits']inlibrary_webgpu.js.floooh commentedon Oct 11, 2023
Right. I guess somebody "just" needs to go through library_webgpu.js and fix all the little things.
On my side, one problem is that I don't have an exhaustive test code base which calls all WebGPU functions (and thus would catch all problems), e.g. I could only fix the stuff that's called in my code base, and I'm only using a subset of the WebGPU feature set.
But I can try to get my code working and provide a list of required changes (even if incomplete), or some sort of proposal PR.
22 remaining items
floooh commentedon Jul 31, 2024
...looking at the same code in the non-minified code now...
floooh commentedon Jul 31, 2024
In the unminified code, the result of the HEAPU32 lookup
HEAPU32[descriptor + 12 >> 2]is 23, and not 0 like in the minified version.But I just realized why I had such a strong deja-vu all the time. I had that all figured out already 😂
#20415 (comment)
floooh commentedon Jul 31, 2024
...e.g. the problem are these mapping objects:
emscripten/src/library_webgpu.js
Lines 398 to 417 in 87ef02c
...if you follow the discussion starting at #20415 (comment) there are various potential solutions (for instance wrapping those mappings into functions) - e.g. the decimal
23I'm seeing in the unminified version is that 0x17 inInt_PreferredFormatmapping.kainino0x commentedon Apr 14, 2025
If there are any remaining issues in Emscripten's current WebGPU implementation, they won't be fixed, but
emdawnwebgpu(see #23432) has been more thoroughly tested with Closure and should be working.floooh commentedon Apr 15, 2025
Oki doki, is it still planned to eventually upstream emdawnwebgpu into Emscripten?
sbc100 commentedon Apr 15, 2025
No, I believe the plan is to remove webgpu from emscripten and have the port live externally.
floooh commentedon Apr 15, 2025
Hmm, that will make integration harder, especially if this is still true (from https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/src/emdawnwebgpu/README.md):
There's this ticket though about unbundling from the Dawn repo (which is a 500 MBytes clone even without dependencies).
https://issues.chromium.org/issues/371024051
Personally I would very much prefer that WebGPU is handled the same as WebGL in the Emscripten SDK (e.g. webgpu.h plus libwebgpu.js shim).
(at least after the WebGPU API has stabilized)
PS: also when considering language bindings... for instance the Zig bindings for my C libraries currently only require the Emscripten SDK for headers and the link step and both WebGL and WebGPU builds out of the box. When WebGPU support is somewhere else that will just complicate things...
sbc100 commentedon Apr 15, 2025
We are hoping to keep it very simple. I believe @kainino0x is making releases that you can point to and you certainly won't need to be clone the whole repo.
In general I'm hoping to we can take some of the pressure off the emscripten repo by hosting more stuff like this externally.
floooh commentedon Apr 15, 2025
Sounds like Emscripten needs a package manager ;)
(e.g. would be kinda cool if there would be a quasi-standard for a 'C header plus JS shim' package format)
sbc100 commentedon Apr 15, 2025
An yes, under the hood I believe its basically the same webgpu.h + libwebgpu.js but maintained upstream and packaged as an emscripten port.
kainino0x commentedon Apr 15, 2025
The plan is to maintain it fully outside Emscripten. We may add a builtin port to Emscripten (that downloads it for you) once it's a bit more stable. But for now it'll still be super easy as soon as I get the packaging/publishing up and running. I already have preview packages you can use, see here:
#23432 (comment)
floooh commentedon Jun 11, 2025
Confirmed working with emdawnwebgpu, thanks @kainino0x!
floooh commentedon Jun 14, 2025
Ooooh.... correction, the problem is now back. I'm investigating and will write a ticket over at emdawnwebgpu.
floooh commentedon Jun 14, 2025
For reference, followup ticket: https://issues.chromium.org/issues/424836759
(the problem is missing items in the
webgpu-externs.jsfile)