Skip to content

Commit

Permalink
Support a few simple WebGPU entry points (via dawn.h)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Sep 7, 2019
1 parent 0ea89ab commit f951f63
Show file tree
Hide file tree
Showing 8 changed files with 1,268 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2019 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*
* WebGPU support. (TODO: Add documentation.)
*/
var LibraryWebGPU = {
$WebGPU: {
buffers: {
nextId: 1,
objects: []
},
},

dawnDeviceRelease: function() {
console.warn("dawnDeviceRelease not implemented");
},

dawnDeviceCreateBuffer: function(deviceId, descriptor) {
#if ASSERTIONS
assert(deviceId === 1);
assert(descriptor !== 0);
var nextInChain = {{{ makeGetValue('descriptor', C_STRUCTS.DawnBufferDescriptor.nextInChain, '*') }}};
assert(nextInChain === 0);
#endif
var desc = {
usage: {{{ makeGetValue('descriptor', C_STRUCTS.DawnBufferDescriptor.usage, 'i64') }}},
size: {{{ makeGetValue('descriptor', C_STRUCTS.DawnBufferDescriptor.size, 'i64') }}},
};

var buffer = Module['preinitializedWebGPUDevice'].createBuffer(desc);

var id = WebGPU.buffers.nextId;
WebGPU.buffers.nextId++;
WebGPU.buffers.objects[id] = { refcount: 1, object: buffer };
return id;
},

dawnBufferRelease: function(bufferId) {
var b = WebGPU.buffers.objects[bufferId];
b.refcount--;
if (b.refcount === 0) {
WebGPU.buffers.objects[bufferId] = undefined;
}
},

dawnBufferSetSubData: function(bufferId, start_l, start_h, count_l, count_h, data) {
var buffer = WebGPU.buffers.objects[bufferId].object;
#if ASSERTIONS
assert(buffer);
assert(start_h < 0x200000);
assert(count_h < 0x200000);
#endif
var start = start_h * 0x100000000 + start_l;
var count = count_h * 0x100000000 + count_l;
buffer.setSubData(start, HEAPU8, data, count);
},

dawnBufferMapReadAsync: function(bufferId, callback, userdata) {
var buffer = WebGPU.buffers.objects[bufferId].object;
#if ASSERTIONS
assert(buffer);
assert(callback);
#endif
buffer.mapReadAsync().then(function(mapped) {
var DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS = 0;
var data = _malloc(mapped.byteLength);
HEAP8.set(new Uint8Array(mapped), data);
var dataLength_h = (mapped.byteLength / 0x100000000) | 0;
var dataLength_l = mapped.byteLength | 0;
dynCall('viiji', callback, [DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS, data, dataLength_l, dataLength_h, userdata]);
}, function() {
// TODO(kainino0x): Figure out how to pick other error status values.
var DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR = 1;
dynCall('viiji', callback, [DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, 0, 0, 0, userdata]);
});
},
};

autoAddDeps(LibraryWebGPU, '$WebGPU');

mergeInto(LibraryManager.library, LibraryWebGPU);
1 change: 1 addition & 0 deletions src/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ var LibraryManager = {
if (!STRICT && !MINIMAL_RUNTIME) {
libraries = libraries.concat([
'library_webgl.js',
'library_webgpu.js',
'library_openal.js',
'library_vr.js',
'library_sdl.js',
Expand Down
3 changes: 3 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ var GL_FFP_ONLY = 0;
// WebGL initialization afterwards will use this GL context to render.
var GL_PREINITIALIZED_CONTEXT = 0;

// If true, enables WebGPU support via dawn.h.
var WEBGPU = 0;

// Enables building of stb-image, a tiny public-domain library for decoding
// images, allowing decoding of images without using the browser's built-in
// decoders. The benefit is that this can be done synchronously, however, it
Expand Down
14 changes: 14 additions & 0 deletions src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1615,5 +1615,19 @@
"EMSCRIPTEN_FETCH_SYNCHRONOUS",
"EMSCRIPTEN_FETCH_WAITABLE"
]
},
// ===========================================
// WebGPU
// ===========================================
{
"file": "dawn/dawn.h",
"defines": [],
"structs": {
"DawnBufferDescriptor": [
"nextInChain",
"usage",
"size"
]
}
}
]
2 changes: 1 addition & 1 deletion src/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ function makeBigInt(low, high, unsigned) {
function dynCall(sig, ptr, args) {
if (args && args.length) {
#if ASSERTIONS
assert(args.length == sig.length-1);
assert(args.length === sig.substring(1).replace(/j/g, '--').length);
#endif
#if ASSERTIONS
assert(('dynCall_' + sig) in Module, 'bad function pointer type - no table for sig \'' + sig + '\'');
Expand Down
Loading

0 comments on commit f951f63

Please sign in to comment.