Skip to content

Commit 5595b97

Browse files
committed
Proxy webgpu calls back to the main thread. NFC
Until we have real multi-threaded webgpu support this is probable the best we can do. Fixes: #19645
1 parent ec54d65 commit 5595b97

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/library_html5_webgpu.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ var LibraryHTML5WebGPU = {
9090
{{{ html5_gpu.makeImportExport('render_bundle_encoder', 'RenderBundleEncoder') }}}
9191
{{{ html5_gpu.makeImportExport('render_bundle', 'RenderBundle') }}}
9292

93+
for (const key of Object.keys(LibraryHTML5WebGPU)) {
94+
LibraryHTML5WebGPU[key + '__proxy'] = 'sync';
95+
}
96+
9397
mergeInto(LibraryManager.library, LibraryHTML5WebGPU);

src/library_webgpu.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ var LibraryWebGPU = {
21962196
#endif
21972197

21982198
var bundles = Array.from(HEAP32.subarray(bundlesPtr >> 2, (bundlesPtr >> 2) + count),
2199-
function(id) { return WebGPU.mgrRenderBundle.get(id); });
2199+
(id) => WebGPU.mgrRenderBundle.get(id));
22002200
pass["executeBundles"](bundles);
22012201
},
22022202

@@ -2582,23 +2582,22 @@ var LibraryWebGPU = {
25822582
var device = WebGPU.mgrDevice.get(deviceId);
25832583
var context = WebGPU.mgrSurface.get(surfaceId);
25842584

2585-
25862585
#if ASSERTIONS
25872586
assert({{{ gpu.PresentMode.Fifo }}} ===
25882587
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.presentMode) }}});
25892588
#endif
25902589

25912590
var canvasSize = [
2592-
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.width) }}},
2593-
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.height) }}}
2591+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.width) }}},
2592+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.height) }}}
25942593
];
25952594

25962595
if (canvasSize[0] !== 0) {
2597-
context["canvas"]["width"] = canvasSize[0];
2596+
context["canvas"]["width"] = canvasSize[0];
25982597
}
25992598

26002599
if (canvasSize[1] !== 0) {
2601-
context["canvas"]["height"] = canvasSize[1];
2600+
context["canvas"]["height"] = canvasSize[1];
26022601
}
26032602

26042603
var configuration = {
@@ -2642,6 +2641,7 @@ for (var value in LibraryWebGPU.$WebGPU.FeatureName) {
26422641

26432642
for (const key of Object.keys(LibraryWebGPU)) {
26442643
LibraryWebGPU[key + '__i53abi'] = true;
2644+
LibraryWebGPU[key + '__proxy'] = 'sync';
26452645
}
26462646

26472647
autoAddDeps(LibraryWebGPU, '$WebGPU');

test/test_browser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4648,10 +4648,19 @@ def test_webgpu_basic_rendering(self):
46484648
for args in [[], ['-sASSERTIONS', '--closure=1'], ['-sMAIN_MODULE=1']]:
46494649
self.btest_exit('webgpu_basic_rendering.cpp', args=['-sUSE_WEBGPU'] + args)
46504650

4651+
@requires_graphics_hardware
4652+
@requires_threads
4653+
def test_webgpu_basic_rendering_pthreads(self):
4654+
self.btest_exit('webgpu_basic_rendering.cpp', args=['-sUSE_WEBGPU', '-pthread', '-sPROXY_TO_PTHREAD'])
4655+
46514656
def test_webgpu_get_device(self):
46524657
for args in [['-sASSERTIONS', '--closure=1']]:
46534658
self.btest_exit('webgpu_get_device.cpp', args=['-sUSE_WEBGPU'] + args)
46544659

4660+
@requires_threads
4661+
def test_webgpu_get_device_pthreads(self):
4662+
self.btest_exit('webgpu_get_device.cpp', args=['-sUSE_WEBGPU', '-pthread', '-sPROXY_TO_PTHREAD'])
4663+
46554664
# Tests the feature that shell html page can preallocate the typed array and place it
46564665
# to Module.buffer before loading the script page.
46574666
# In this build mode, the -sINITIAL_MEMORY=xxx option will be ignored.

test/webgpu_get_device.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
#include <emscripten.h>
1+
#include <stdio.h>
2+
3+
#include <emscripten/em_asm.h>
24
#include <emscripten/html5_webgpu.h>
35

4-
int main() {
6+
__attribute__((constructor)) void init() {
57
EM_ASM({
68
Module['preinitializedWebGPUDevice'] = { this_is: 'a_dummy_object' };
79
});
8-
emscripten_webgpu_get_device();
10+
}
11+
12+
int main() {
13+
WGPUDevice d = emscripten_webgpu_get_device();
14+
printf("emscripten_webgpu_get_device: %p\n", d);
15+
return 0;
916
}

0 commit comments

Comments
 (0)