Skip to content

Commit 0443aa9

Browse files
authored
Fix WebGPU build with closure (#15180)
Remove usage of `this.` in library_webgpu.js Closure doesn't understand that functions that use `this.` are methods rather than constructors, and freely moves them around and inlines them. To work around it, replace `this.` with `WebGPU.` (which is consistent with how most of the file works already).
1 parent d48ae60 commit 0443aa9

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

src/library_html5_webgpu.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
s += 'LibraryHTML5WebGPU.emscripten_webgpu_import_' + snake_case + '__sig = "ii";';
77
s += 'LibraryHTML5WebGPU.emscripten_webgpu_import_' + snake_case + '__deps = ["$WebGPU", "$JsValStore"];';
88
s += 'LibraryHTML5WebGPU.emscripten_webgpu_import_' + snake_case + ' = function(handle) { '
9-
s += 'return WebGPU["mgr' + CamelCase + '"].create(JsValStore.get(handle));'
9+
s += 'return WebGPU.mgr' + CamelCase + '.create(JsValStore.get(handle));'
1010
s += '};';
1111

1212
s += 'LibraryHTML5WebGPU.emscripten_webgpu_export_' + snake_case + '__sig = "ii";';
1313
s += 'LibraryHTML5WebGPU.emscripten_webgpu_export_' + snake_case + '__deps = ["$WebGPU", "$JsValStore"];';
1414
s += 'LibraryHTML5WebGPU.emscripten_webgpu_export_' + snake_case + ' = function(handle) { '
15-
s += 'return JsValStore.add(WebGPU["mgr' + CamelCase + '"].get(handle));'
15+
s += 'return JsValStore.add(WebGPU.mgr' + CamelCase + '.get(handle));'
1616
s += '};';
1717
return s;
1818
},
@@ -62,7 +62,7 @@ var LibraryHTML5WebGPU = {
6262
#endif
6363
var device = Module['preinitializedWebGPUDevice'];
6464
var deviceWrapper = { queueId: WebGPU.mgrQueue.create(device["queue"]) };
65-
return WebGPU["mgrDevice"].create(device, deviceWrapper);
65+
return WebGPU.mgrDevice.create(device, deviceWrapper);
6666
},
6767
};
6868

src/library_webgpu.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// Helper functions for code generation
2626
global.gpu = {
2727
makeInitManager: function(type) {
28-
var mgr = 'this.mgr' + type
28+
var mgr = 'WebGPU.mgr' + type
2929
return mgr + ' = ' + mgr + ' || makeManager();';
3030
},
3131

@@ -145,7 +145,7 @@ var LibraryWebGPU = {
145145
$WebGPU__postset: 'WebGPU.initManagers();',
146146
$WebGPU: {
147147
initManagers: function() {
148-
if (this.mgrDevice) return;
148+
if (WebGPU.mgrDevice) return;
149149

150150
function makeManager() {
151151
return {
@@ -242,7 +242,7 @@ var LibraryWebGPU = {
242242
makeImageCopyTexture: function(ptr) {
243243
{{{ gpu.makeCheckDescriptor('ptr') }}}
244244
return {
245-
"texture": this.mgrTexture.get(
245+
"texture": WebGPU.mgrTexture.get(
246246
{{{ makeGetValue('ptr', C_STRUCTS.WGPUImageCopyTexture.texture, '*') }}}),
247247
"mipLevel": {{{ gpu.makeGetU32('ptr', C_STRUCTS.WGPUImageCopyTexture.mipLevel) }}},
248248
"origin": WebGPU.makeOrigin3D(ptr + {{{ C_STRUCTS.WGPUImageCopyTexture.origin }}}),
@@ -264,8 +264,8 @@ var LibraryWebGPU = {
264264
makeImageCopyBuffer: function(ptr) {
265265
{{{ gpu.makeCheckDescriptor('ptr') }}}
266266
var layoutPtr = ptr + {{{ C_STRUCTS.WGPUImageCopyBuffer.layout }}};
267-
var bufferCopyView = this.makeTextureDataLayout(layoutPtr);
268-
bufferCopyView["buffer"] = this.mgrBuffer.get(
267+
var bufferCopyView = WebGPU.makeTextureDataLayout(layoutPtr);
268+
bufferCopyView["buffer"] = WebGPU.mgrBuffer.get(
269269
{{{ makeGetValue('ptr', C_STRUCTS.WGPUImageCopyBuffer.buffer, '*') }}});
270270
return bufferCopyView;
271271
},

tests/test_browser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4480,9 +4480,13 @@ def test_webgl_simple_enable_extensions(self):
44804480

44814481
@requires_graphics_hardware
44824482
def test_webgpu_basic_rendering(self):
4483-
for args in [[], ['-s', 'ASSERTIONS'], ['-s', 'MAIN_MODULE=1']]:
4483+
for args in [[], ['-s', 'ASSERTIONS', '--closure=1'], ['-s', 'MAIN_MODULE=1']]:
44844484
self.btest_exit('webgpu_basic_rendering.cpp', args=['-s', 'USE_WEBGPU', '-s', 'EXPORTED_RUNTIME_METHODS=["ccall"]'] + args)
44854485

4486+
def test_webgpu_get_device(self):
4487+
for args in [['-s', 'ASSERTIONS', '--closure=1']]:
4488+
self.btest_exit('webgpu_get_device.cpp', args=['-s', 'USE_WEBGPU', '-s', 'EXPORTED_RUNTIME_METHODS=["ccall"]'] + args)
4489+
44864490
# Tests the feature that shell html page can preallocate the typed array and place it
44874491
# to Module.buffer before loading the script page.
44884492
# In this build mode, the -s INITIAL_MEMORY=xxx option will be ignored.

tests/webgpu_get_device.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <emscripten.h>
2+
#include <emscripten/html5_webgpu.h>
3+
4+
int main() {
5+
EM_ASM({
6+
Module['preinitializedWebGPUDevice'] = { this_is: 'a_dummy_object' };
7+
});
8+
emscripten_webgpu_get_device();
9+
}

0 commit comments

Comments
 (0)