Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Sep 25, 2019
1 parent 80bba8a commit b1fa126
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 95 deletions.
5 changes: 3 additions & 2 deletions src/library_html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2593,9 +2593,10 @@ var LibraryJSEvents = {
emscripten_webgpu_get_device: 'emscripten_webgpu_do_get_device',
emscripten_webgpu_do_get_device__deps: ['$WebGPU'],
emscripten_webgpu_do_get_device: function() {
assert(Module['preinitializedWebGPUDevice']);
// TODO(kainino0x): make it possible to actually create devices
return WebGPU.mgrDevice().create(Module['preinitializedWebGPUDevice']);
assert(Module['preinitializedWebGPUDevice']);
WebGPU.initManagers();
return WebGPU.mgrDevice.create(Module['preinitializedWebGPUDevice']);
},
#endif

Expand Down
183 changes: 91 additions & 92 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,101 @@
*
* WebGPU support. (TODO: Add documentation.)
*/

{{{ (function() {
// Helper functions for code generation
global.gpu = {
makeInitManager: function(type) {
var mgr = 'this.mgr' + type
return mgr + ' = ' + mgr + ' || makeManager();';
},

makeReferenceRelease: function(type) {
let s = '';
s += 'dawn' + type + 'Reference: function(id) {\n';
s += ' WebGPU.mgr' + type + '.reference(id);\n'
s += '},\n';
s += 'dawn' + type + 'Release: function(id) {\n';
s += ' WebGPU.mgr' + type + '.release(id);\n'
s += '},';
return s;
},
};
return '';
})(); }}}

var LibraryWebGPU = {
$WebGPU: {
makeManager: function() {
return {
objects: [null],
create: function(object) {
var id = this.objects.length;
initManagers: function() {
function makeManager() {
return {
objects: [null],
create: function(object) {
var id = this.objects.length;
#if ASSERTIONS
assert(typeof this.objects[id] === 'undefined');
assert(typeof this.objects[id] === 'undefined');
#endif
this.objects[id] = { refcount: 1, object };
return id;
},
get: function(id) {
this.objects[id] = { refcount: 1, object };
return id;
},
get: function(id) {
#if ASSERTIONS
assert(typeof this.objects[id] !== 'undefined');
assert(typeof this.objects[id] !== 'undefined');
#endif
return this.objects[id].object;
},
reference: function(id) {
var o = this.objects[id];
return this.objects[id].object;
},
reference: function(id) {
var o = this.objects[id];
#if ASSERTIONS
assert(typeof o !== 'undefined');
assert(typeof o !== 'undefined');
#endif
this.objects[id].refcount++;
},
release: function(id) {
var o = this.objects[id];
this.objects[id].refcount++;
},
release: function(id) {
var o = this.objects[id];
#if ASSERTIONS
assert(typeof o !== 'undefined');
assert(o.refcount > 0);
assert(typeof o !== 'undefined');
assert(o.refcount > 0);
#endif
o.refcount--;
if (o.refcount <= 0) {
delete this.objects[id];
}
},
};
},
{{{ (function() {
var mgrTypes = [
'Device',
'Queue',
'Buffer',
'Texture',
'CommandBuffer',
'CommandEncoder',
];
var s = '';
for (var i = 0; i < mgrTypes.length; ++i) {
var type = mgrTypes[i];
s += 'mgr' + type + ': function() {\n';
s += ' return this._mgr' + type + ' || (this._mgr' + type + ' = this.makeManager());\n';
s += '},\n';
o.refcount--;
if (o.refcount <= 0) {
delete this.objects[id];
}
},
};
}
return s;
})() }}}

{{{ gpu.makeInitManager('Device') }}}
{{{ gpu.makeInitManager('Queue') }}}
{{{ gpu.makeInitManager('Buffer') }}}
{{{ gpu.makeInitManager('Texture') }}}
{{{ gpu.makeInitManager('CommandBuffer') }}}
{{{ gpu.makeInitManager('CommandEncoder') }}}
},
},

// *Reference/*Release

{{{ (function() {
var rcTypes = [
'Device',
'Queue',
'Buffer',
'Texture',
'CommandBuffer',
'CommandEncoder',
];
var s = '';
for (var i = 0; i < rcTypes.length; ++i) {
var type = rcTypes[i];
s += 'dawn' + type + 'Reference: function(id) {\n';
s += ' WebGPU.mgr' + type + '().reference(id);\n'
s += '},\n';
s += 'dawn' + type + 'Release: function(id) {\n';
s += ' WebGPU.mgr' + type + '().release(id);\n'
s += '},\n';
}
return s;
})() }}}
{{{ gpu.makeReferenceRelease('Device') }}}
{{{ gpu.makeReferenceRelease('Queue') }}}
{{{ gpu.makeReferenceRelease('Buffer') }}}
{{{ gpu.makeReferenceRelease('Texture') }}}
{{{ gpu.makeReferenceRelease('CommandBuffer') }}}
{{{ gpu.makeReferenceRelease('CommandEncoder') }}}

// *Destroy

dawnBufferDestroy: function(bufferId) { WebGPU.mgrBuffer().get(bufferId).destroy(); },
dawnTextureDestroy: function(textureId) { WebGPU.mgrTexture().get(textureId).destroy(); },
dawnBufferDestroy: function(bufferId) { WebGPU.mgrBuffer.get(bufferId).destroy(); },
dawnTextureDestroy: function(textureId) { WebGPU.mgrTexture.get(textureId).destroy(); },

// dawnDevice

// dawnDeviceCreate*

dawnDeviceCreateQueue: function(deviceId) {
assert(WebGPU.mgrQueue().objects.length === 1, 'there is only one queue');
var device = WebGPU.mgrDevice().get(deviceId);
return WebGPU.mgrQueue().create(device.getQueue());
assert(WebGPU.mgrQueue.objects.length === 1, 'there is only one queue');
var device = WebGPU.mgrDevice.get(deviceId);
return WebGPU.mgrQueue.create(device.getQueue());
},

dawnDeviceCreateCommandEncoder: function(deviceId, descriptor) {
Expand All @@ -111,8 +110,8 @@ var LibraryWebGPU = {
assert(nextInChain === nullptr);
}
#endif
var device = WebGPU.mgrDevice().get(deviceId);
return WebGPU.mgrCommandEncoder().create(device.createCommandEncoder());
var device = WebGPU.mgrDevice.get(deviceId);
return WebGPU.mgrCommandEncoder.create(device.createCommandEncoder());
},

dawnDeviceCreateBuffer: function(deviceId, descriptor) {
Expand All @@ -126,8 +125,8 @@ var LibraryWebGPU = {
size: {{{ makeGetValue('descriptor', C_STRUCTS.DawnBufferDescriptor.size, 'i64') }}},
};

var device = WebGPU.mgrDevice().get(deviceId);
return WebGPU.mgrBuffer().create(device.createBuffer(desc));
var device = WebGPU.mgrDevice.get(deviceId);
return WebGPU.mgrBuffer.create(device.createBuffer(desc));
},

// dawnQueue
Expand All @@ -136,40 +135,40 @@ var LibraryWebGPU = {
#if ASSERTIONS
assert(commands % 4 === 0);
#endif
var queue = WebGPU.mgrQueue().get(queueId);
var queue = WebGPU.mgrQueue.get(queueId);
var cmds = Array.from(HEAP32.subarray(commands >> 2, (commands >> 2) + commandCount),
function(id) { return WebGPU.mgrCommandBuffer().get(id); });
function(id) { return WebGPU.mgrCommandBuffer.get(id); });
queue.submit(cmds);
},

// dawnCommandEncoder

dawnCommandEncoderFinish: function(commandEncoderId) {
var commandEncoder = WebGPU.mgrCommandEncoder().get(commandEncoderId);
return WebGPU.mgrCommandBuffer().create(commandEncoder.finish());
var commandEncoder = WebGPU.mgrCommandEncoder.get(commandEncoderId);
return WebGPU.mgrCommandBuffer.create(commandEncoder.finish());
},

dawnCommandEncoderCopyBufferToBuffer: function(commandEncoderId, srcId, srcOffset_l, srcOffset_h, dstId, dstOffset_l, dstOffset_h, size_l, size_h) {
var commandEncoder = WebGPU.mgrCommandEncoder().get(commandEncoderId);
var src = WebGPU.mgrBuffer().get(srcId);
var dst = WebGPU.mgrBuffer().get(dstId);
var commandEncoder = WebGPU.mgrCommandEncoder.get(commandEncoderId);
var src = WebGPU.mgrBuffer.get(srcId);
var dst = WebGPU.mgrBuffer.get(dstId);
commandEncoder.copyBufferToBuffer(
src, {{{ makeU64ToNumber('srcOffset_h', 'srcOffset_l') }}},
dst, {{{ makeU64ToNumber('dstOffset_h', 'dstOffset_l') }}},
{{{ makeU64ToNumber('size_h', 'size_l') }}});
src, {{{ makeU64ToNumber('srcOffset_l', 'srcOffset_h') }}},
dst, {{{ makeU64ToNumber('dstOffset_l', 'dstOffset_h') }}},
{{{ makeU64ToNumber('size_l', 'size_h') }}});
},

// dawnBuffer

dawnBufferSetSubData: function(bufferId, start_l, start_h, count_l, count_h, data) {
var buffer = WebGPU.mgrBuffer().get(bufferId);
var start = {{{ makeU64ToNumber('start_h', 'start_l') }}};
var count = {{{ makeU64ToNumber('count_h', 'count_l') }}};
var buffer = WebGPU.mgrBuffer.get(bufferId);
var start = {{{ makeU64ToNumber('start_l', 'start_h') }}};
var count = {{{ makeU64ToNumber('count_l', 'count_h') }}};
buffer.setSubData(start, HEAPU8, data, count);
},

dawnBufferMapReadAsync: function(bufferId, callback, userdata) {
var bufferEntry = WebGPU.mgrBuffer().objects[bufferId];
var bufferEntry = WebGPU.mgrBuffer.objects[bufferId];
bufferEntry.mapped = 'write';
var buffer = bufferEntry.object;

Expand All @@ -188,7 +187,7 @@ var LibraryWebGPU = {
},

dawnBufferMapWriteAsync: function(bufferId, callback, userdata) {
var e = WebGPU.mgrBuffer().objects[bufferId];
var e = WebGPU.mgrBuffer.objects[bufferId];
var buffer = e.object;

buffer.mapWriteAsync().then(function(mapped) {
Expand All @@ -209,7 +208,7 @@ var LibraryWebGPU = {
},

dawnBufferUnmap: function(bufferId) {
var e = WebGPU.mgrBuffer().objects[bufferId];
var e = WebGPU.mgrBuffer.objects[bufferId];
if (e.mapWriteSrc) {
new Uint8Array(e.mapWriteDst).set(HEAP8.subarray(e.mapWriteSrc, e.mapWriteSrc + e.mapWriteDst.byteLength));
}
Expand Down
2 changes: 1 addition & 1 deletion src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ function makeModuleReceiveWithVar(localName, moduleName, defaultValue, noAssert)
return ret;
}

function makeU64ToNumber(highName, lowName) {
function makeU64ToNumber(lowName, highName) {
var ret = '('
if (ASSERTIONS) {
ret += 'assert(' + highName + ' < 0x200000), ';
Expand Down

0 comments on commit b1fa126

Please sign in to comment.