Skip to content

Commit

Permalink
Proxy webgpu calls back to the main thread. NFC
Browse files Browse the repository at this point in the history
Until we have real multi-threaded webgpu support this is probable
the best we can do.

Fixes: #19645
  • Loading branch information
sbc100 committed Aug 14, 2023
1 parent ec54d65 commit 5595b97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/library_html5_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ var LibraryHTML5WebGPU = {
{{{ html5_gpu.makeImportExport('render_bundle_encoder', 'RenderBundleEncoder') }}}
{{{ html5_gpu.makeImportExport('render_bundle', 'RenderBundle') }}}

for (const key of Object.keys(LibraryHTML5WebGPU)) {
LibraryHTML5WebGPU[key + '__proxy'] = 'sync';
}

mergeInto(LibraryManager.library, LibraryHTML5WebGPU);
12 changes: 6 additions & 6 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ var LibraryWebGPU = {
#endif

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

Expand Down Expand Up @@ -2582,23 +2582,22 @@ var LibraryWebGPU = {
var device = WebGPU.mgrDevice.get(deviceId);
var context = WebGPU.mgrSurface.get(surfaceId);


#if ASSERTIONS
assert({{{ gpu.PresentMode.Fifo }}} ===
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.presentMode) }}});
#endif

var canvasSize = [
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.width) }}},
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.height) }}}
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.width) }}},
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUSwapChainDescriptor.height) }}}
];

if (canvasSize[0] !== 0) {
context["canvas"]["width"] = canvasSize[0];
context["canvas"]["width"] = canvasSize[0];
}

if (canvasSize[1] !== 0) {
context["canvas"]["height"] = canvasSize[1];
context["canvas"]["height"] = canvasSize[1];
}

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

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

autoAddDeps(LibraryWebGPU, '$WebGPU');
Expand Down
9 changes: 9 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4648,10 +4648,19 @@ def test_webgpu_basic_rendering(self):
for args in [[], ['-sASSERTIONS', '--closure=1'], ['-sMAIN_MODULE=1']]:
self.btest_exit('webgpu_basic_rendering.cpp', args=['-sUSE_WEBGPU'] + args)

@requires_graphics_hardware
@requires_threads
def test_webgpu_basic_rendering_pthreads(self):
self.btest_exit('webgpu_basic_rendering.cpp', args=['-sUSE_WEBGPU', '-pthread', '-sPROXY_TO_PTHREAD'])

def test_webgpu_get_device(self):
for args in [['-sASSERTIONS', '--closure=1']]:
self.btest_exit('webgpu_get_device.cpp', args=['-sUSE_WEBGPU'] + args)

@requires_threads
def test_webgpu_get_device_pthreads(self):
self.btest_exit('webgpu_get_device.cpp', args=['-sUSE_WEBGPU', '-pthread', '-sPROXY_TO_PTHREAD'])

# Tests the feature that shell html page can preallocate the typed array and place it
# to Module.buffer before loading the script page.
# In this build mode, the -sINITIAL_MEMORY=xxx option will be ignored.
Expand Down
13 changes: 10 additions & 3 deletions test/webgpu_get_device.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include <emscripten.h>
#include <stdio.h>

#include <emscripten/em_asm.h>
#include <emscripten/html5_webgpu.h>

int main() {
__attribute__((constructor)) void init() {
EM_ASM({
Module['preinitializedWebGPUDevice'] = { this_is: 'a_dummy_object' };
});
emscripten_webgpu_get_device();
}

int main() {
WGPUDevice d = emscripten_webgpu_get_device();
printf("emscripten_webgpu_get_device: %p\n", d);
return 0;
}

0 comments on commit 5595b97

Please sign in to comment.