Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated WebGPU #11067

Merged
merged 15 commits into from
May 15, 2020
67 changes: 51 additions & 16 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,23 @@
},
makeCheckDescriptor: function(descriptor) {
// Assert descriptor is non-null, then that its nextInChain is null.
// For descriptors that aren't the first in the chain (e.g ShaderModuleSPIRVDescriptor),
// there is no nextInChain pointer but a ChainedStruct object named chain.
// So we need to check if chain.nextInChain is null. As long as nextInChain and chain are both the
// first member in the struct, descriptor.nextInChain and descriptor.chain.nextInChain should have the same offset (0)
Tandaradei marked this conversation as resolved.
Show resolved Hide resolved
// to the descriptor pointer and we can check it to be null.
var OffsetOfNextInChainMember = 0;
return this.makeCheck(descriptor) + this.makeCheck(makeGetValue(descriptor, OffsetOfNextInChainMember, '*') + ' === 0');
},
},

// Must be in sync with webgpu.h.
PresentMode: {
Fifo: 2,
},
SType: {
SurfaceDescriptorFromHTMLCanvasId: 4,
Tandaradei marked this conversation as resolved.
Show resolved Hide resolved
ShaderModuleSPIRVDescriptor: 5,
ShaderModuleWGSLDescriptor: 6,
},
};
return null;
Expand Down Expand Up @@ -223,6 +230,11 @@ var LibraryWebGPU = {
};
},

// maps deviceId to the queueId of the device's defaultQueue
defaultQueues: {
0: 0
},

// This section is auto-generated:
// https://dawn.googlesource.com/dawn/+/refs/heads/master/generator/templates/library_webgpu_enum_tables.json
AddressMode: [
Expand Down Expand Up @@ -322,11 +334,6 @@ var LibraryWebGPU = {
'clear',
'load',
],
PresentMode: [
'immediate',
'mailbox',
'fifo',
],
PrimitiveTopology: [
'point-list',
'line-list',
Expand Down Expand Up @@ -502,9 +509,19 @@ var LibraryWebGPU = {
// wgpuDevice

wgpuDeviceGetDefaultQueue: function(deviceId) {
assert(WebGPU.mgrQueue.objects.length === 1, 'there is only one queue');
var device = WebGPU["mgrDevice"].get(deviceId);
return WebGPU.mgrQueue.create(device["defaultQueue"]);
var queueId = WebGPU.defaultQueues[deviceId];
#if ASSERTIONS
assert(queueId != 0, 'got invalid queue');
#endif
if(queueId === undefined) {
var device = WebGPU["mgrDevice"].get(deviceId);
WebGPU.defaultQueues[deviceId] = WebGPU.mgrQueue.create(device["defaultQueue"]);
queueId = WebGPU.defaultQueues[deviceId];
}
else {
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
WebGPU.mgrQueue.reference(queueId);
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
}
return queueId;
},

// wgpuDeviceCreate*
Expand Down Expand Up @@ -930,20 +947,38 @@ var LibraryWebGPU = {
},

wgpuDeviceCreateShaderModule: function(deviceId, descriptor) {
{{{ gpu.makeCheckDescriptor('descriptor') }}}
var count = {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUShaderModuleDescriptor.codeSize) }}};
var start = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUShaderModuleDescriptor.code, '*') }}};
{{{ gpu.makeCheck('descriptor') }}}
var nextInChainPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUSurfaceDescriptor.nextInChain, '*') }}};
#if ASSERTIONS
assert(nextInChainPtr !== 0);
#endif
var sType = {{{ gpu.makeGetU32('nextInChainPtr', C_STRUCTS.WGPUChainedStruct.sType) }}};
#if ASSERTIONS
assert(sType === {{{ gpu.SType.ShaderModuleSPIRVDescriptor }}}
|| sType === {{{ gpu.SType.ShaderModuleWGSLDescriptor }}});
#endif
var desc = {
"label": undefined,
"code": HEAPU32.subarray(start >> 2, (start >> 2) + count),
"code": "",
};
var labelPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUShaderModuleDescriptor.label, '*') }}};
if (labelPtr) desc["label"] = UTF8ToString(labelPtr);

if(sType === {{{ gpu.SType.ShaderModuleSPIRVDescriptor }}}) {
var count = {{{ gpu.makeGetU32('nextInChainPtr', C_STRUCTS.WGPUShaderModuleSPIRVDescriptor.codeSize) }}};
var start = {{{ makeGetValue('nextInChainPtr', C_STRUCTS.WGPUShaderModuleSPIRVDescriptor.code, '*') }}};
desc["code"] = HEAPU32.subarray(start >> 2, (start >> 2) + count);
}
else if(sType === {{{ gpu.SType.ShaderModuleWGSLDescriptor }}}) {
kainino0x marked this conversation as resolved.
Show resolved Hide resolved
var sourcePtr = {{{ makeGetValue('nextInChainPtr', C_STRUCTS.WGPUShaderModuleWGSLDescriptor.source, '*') }}};
if (sourcePtr) {
desc["code"] = UTF8ToString(sourcePtr);
}
}

var device = WebGPU["mgrDevice"].get(deviceId);
return WebGPU.mgrShaderModule.create(device["createShaderModule"](desc));
},


Tandaradei marked this conversation as resolved.
Show resolved Hide resolved
wgpuDeviceCreateQuerySet: function(deviceId, descriptor) {
{{{ gpu.makeCheckDescriptor('descriptor') }}}
Expand All @@ -953,7 +988,7 @@ var LibraryWebGPU = {
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUQuerySetDescriptor.type) }}}],
"count": {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUQuerySetDescriptor.count) }}}
};
var labelPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUShaderModuleDescriptor.label, '*') }}};
var labelPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUQuerySetDescriptor.label, '*') }}};
if (labelPtr) desc["label"] = UTF8ToString(labelPtr);

var device = WebGPU["mgrDevice"].get(deviceId);
Expand Down Expand Up @@ -1497,7 +1532,7 @@ var LibraryWebGPU = {
assert({{{ gpu.SType.SurfaceDescriptorFromHTMLCanvasId }}} ===
{{{ gpu.makeGetU32('nextInChainPtr', C_STRUCTS.WGPUChainedStruct.sType) }}});
#endif
var descriptorFromHTMLCanvasId = {{{ gpu.makeGetU32('nextInChainPtr', C_STRUCTS.WGPUChainedStruct.next) }}};
var descriptorFromHTMLCanvasId = nextInChainPtr;

{{{ gpu.makeCheckDescriptor('descriptorFromHTMLCanvasId') }}}
var idPtr = {{{ makeGetValue('descriptorFromHTMLCanvasId', C_STRUCTS.WGPUSurfaceDescriptorFromHTMLCanvasId.id, '*') }}};
Expand Down
24 changes: 4 additions & 20 deletions src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1819,17 +1819,15 @@
],
"WGPUShaderModuleDescriptor": [
"nextInChain",
"label",
"codeSize",
"code"
"label"
],
"WGPUShaderModuleSPIRVDescriptor": [
"nextInChain",
"chain",
"codeSize",
"code"
],
"WGPUShaderModuleWGSLDescriptor": [
"nextInChain",
"chain",
"source"
],
"WGPUStencilStateFaceDescriptor": [
Expand All @@ -1843,23 +1841,9 @@
"label"
],
"WGPUSurfaceDescriptorFromHTMLCanvasId": [
"nextInChain",
"chain",
"id"
],
"WGPUSurfaceDescriptorFromMetalLayer": [
"nextInChain",
"layer"
],
"WGPUSurfaceDescriptorFromWindowsHWND": [
"nextInChain",
"hinstance",
"hwnd"
],
"WGPUSurfaceDescriptorFromXlib": [
"nextInChain",
"display",
"window"
],
"WGPUSwapChainDescriptor": [
"nextInChain",
"label",
Expand Down
14 changes: 6 additions & 8 deletions system/include/webgpu/webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,18 +632,16 @@ typedef struct WGPUSamplerDescriptor {
typedef struct WGPUShaderModuleDescriptor {
WGPUChainedStruct const * nextInChain;
char const * label;
uint32_t codeSize;
uint32_t const * code;
} WGPUShaderModuleDescriptor;

typedef struct WGPUShaderModuleSPIRVDescriptor {
WGPUChainedStruct const * nextInChain;
WGPUChainedStruct chain;
uint32_t codeSize;
uint32_t const * code;
} WGPUShaderModuleSPIRVDescriptor;

typedef struct WGPUShaderModuleWGSLDescriptor {
WGPUChainedStruct const * nextInChain;
WGPUChainedStruct chain;
char const * source;
} WGPUShaderModuleWGSLDescriptor;

Expand All @@ -660,23 +658,23 @@ typedef struct WGPUSurfaceDescriptor {
} WGPUSurfaceDescriptor;

typedef struct WGPUSurfaceDescriptorFromHTMLCanvasId {
WGPUChainedStruct const * nextInChain;
WGPUChainedStruct chain;
char const * id;
} WGPUSurfaceDescriptorFromHTMLCanvasId;

typedef struct WGPUSurfaceDescriptorFromMetalLayer {
WGPUChainedStruct const * nextInChain;
WGPUChainedStruct chain;
void * layer;
} WGPUSurfaceDescriptorFromMetalLayer;

typedef struct WGPUSurfaceDescriptorFromWindowsHWND {
WGPUChainedStruct const * nextInChain;
WGPUChainedStruct chain;
void * hinstance;
void * hwnd;
} WGPUSurfaceDescriptorFromWindowsHWND;

typedef struct WGPUSurfaceDescriptorFromXlib {
WGPUChainedStruct const * nextInChain;
WGPUChainedStruct chain;
void * display;
uint32_t window;
} WGPUSurfaceDescriptorFromXlib;
Expand Down