Skip to content

Commit bfb2456

Browse files
lokokungDawn LUCI CQ
authored andcommitted
[dawn][emscripten] Use doubles for future ids.
- This fix is stemmed from an issue where because mapMode in mapAsync is a bitmask which means by default in WASM we are using a uint64_t to represent it. As a WASM type, this was a 'j' that needs to be taken as 2 args much like the future id. Because it was hard to find this issue hidden by the fact that we were manually turning __i53abi off for some shims, this change refactors futures to always be passed to JS from WASM as a uint64_t/'j' type with __i53abi as true, and passed from JS to WASM as a double which we can cast back. Bug: 374691288, 369445681 Change-Id: I6a3a7402a5cff8969eb8402e36b28b5bf68142cc Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/211940 Reviewed-by: Kai Ninomiya <[email protected]> Reviewed-by: Corentin Wallez <[email protected]> Commit-Queue: Loko Kung <[email protected]>
1 parent b09deb2 commit bfb2456

File tree

2 files changed

+56
-65
lines changed

2 files changed

+56
-65
lines changed

third_party/emdawnwebgpu/library_webgpu.js

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ var LibraryWebGPU = {
9999
// This might be possible with -sWASM_BIGINT, but I was unable to get that
100100
// to work properly at the time of writing.
101101
futures: [],
102-
futureInsert: (futureIdL, futureIdH, promise) => {
102+
futureInsert: (futureId, promise) => {
103103
#if ASYNCIFY
104-
var futureId = futureIdH * 0x100000000 + futureIdL;
105104
WebGPU.Internals.futures[futureId] =
106105
new Promise((resolve) => promise.finally(() => resolve(futureId)));
107106
#endif
@@ -700,15 +699,9 @@ var LibraryWebGPU = {
700699
return adapter.features.has(WebGPU.FeatureName[featureEnumValue]);
701700
},
702701

703-
emwgpuAdapterRequestDevice__i53abi: false,
704702
emwgpuAdapterRequestDevice__deps: ['emwgpuCreateQueue', 'emwgpuOnDeviceLostCompleted', 'emwgpuOnRequestDeviceCompleted', 'emwgpuOnUncapturedError'],
705703
emwgpuAdapterRequestDevice__sig: 'vpjjppp',
706-
emwgpuAdapterRequestDevice: (
707-
adapterPtr,
708-
futureIdL, futureIdH,
709-
deviceLostFutureIdL, deviceLostFutureIdH,
710-
devicePtr, queuePtr, descriptor
711-
) => {
704+
emwgpuAdapterRequestDevice: (adapterPtr, futureId, deviceLostFutureId, devicePtr, queuePtr, descriptor) => {
712705
var adapter = WebGPU.getJsObject(adapterPtr);
713706

714707
var desc = {};
@@ -790,22 +783,21 @@ var LibraryWebGPU = {
790783
}
791784

792785
{{{ runtimeKeepalivePush() }}}
793-
var hasDeviceLostFutureId = !!deviceLostFutureIdH || !!deviceLostFutureIdL;
794-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, adapter.requestDevice(desc).then((device) => {
786+
WebGPU.Internals.futureInsert(futureId, adapter.requestDevice(desc).then((device) => {
795787
{{{ runtimeKeepalivePop() }}}
796788
WebGPU.Internals.jsObjectInsert(queuePtr, device.queue);
797789
WebGPU.Internals.jsObjectInsert(devicePtr, device);
798790

799791
// Set up device lost promise resolution.
800-
if (hasDeviceLostFutureId) {
792+
if (deviceLostFutureId) {
801793
{{{ runtimeKeepalivePush() }}}
802-
WebGPU.Internals.futureInsert(deviceLostFutureIdL, deviceLostFutureIdH, device.lost.then((info) => {
794+
WebGPU.Internals.futureInsert(deviceLostFutureId, device.lost.then((info) => {
803795
{{{ runtimeKeepalivePop() }}}
804796
// Unset the uncaptured error handler.
805797
device.onuncapturederror = (ev) => {};
806798
var sp = stackSave();
807799
var messagePtr = stringToUTF8OnStack(info.message);
808-
_emwgpuOnDeviceLostCompleted(deviceLostFutureIdL, deviceLostFutureIdH, WebGPU.Int_DeviceLostReason[info.reason], messagePtr);
800+
_emwgpuOnDeviceLostCompleted(deviceLostFutureId, WebGPU.Int_DeviceLostReason[info.reason], messagePtr);
809801
stackRestore(sp);
810802
}));
811803
}
@@ -827,14 +819,14 @@ var LibraryWebGPU = {
827819
stackRestore(sp);
828820
};
829821

830-
_emwgpuOnRequestDeviceCompleted(futureIdL, futureIdH, {{{ gpu.RequestDeviceStatus.Success }}}, devicePtr, 0);
822+
_emwgpuOnRequestDeviceCompleted(futureId, {{{ gpu.RequestDeviceStatus.Success }}}, devicePtr, 0);
831823
}, (ex) => {
832824
{{{ runtimeKeepalivePop() }}}
833825
var sp = stackSave();
834826
var messagePtr = stringToUTF8OnStack(ex.message);
835-
_emwgpuOnRequestDeviceCompleted(futureIdL, futureIdH, {{{ gpu.RequestDeviceStatus.Error }}}, devicePtr, messagePtr);
836-
if (hasDeviceLostFutureId) {
837-
_emwgpuOnDeviceLostCompleted(deviceLostFutureIdL, deviceLostFutureIdH, {{{ gpu.DeviceLostReason.FailedCreation }}}, messagePtr);
827+
_emwgpuOnRequestDeviceCompleted(futureId, {{{ gpu.RequestDeviceStatus.Error }}}, devicePtr, messagePtr);
828+
if (deviceLostFutureId) {
829+
_emwgpuOnDeviceLostCompleted(deviceLostFutureId, {{{ gpu.DeviceLostReason.FailedCreation }}}, messagePtr);
838830
}
839831
stackRestore(sp);
840832
}));
@@ -937,19 +929,18 @@ var LibraryWebGPU = {
937929

938930
// In webgpu.h offset and size are passed in as size_t.
939931
// And library_webgpu assumes that size_t is always 32bit in emscripten.
940-
emwgpuBufferMapAsync__i53abi: false,
941932
emwgpuBufferMapAsync__deps: ['emwgpuOnMapAsyncCompleted'],
942933
emwgpuBufferMapAsync__sig: 'vpjjpp',
943-
emwgpuBufferMapAsync: (bufferPtr, futureIdL, futureIdH, mode, offset, size) => {
934+
emwgpuBufferMapAsync: (bufferPtr, futureId, mode, offset, size) => {
944935
var buffer = WebGPU.getJsObject(bufferPtr);
945936
WebGPU.Internals.bufferOnUnmaps[bufferPtr] = [];
946937

947938
{{{ gpu.convertSentinelToUndefined('size') }}}
948939

949940
{{{ runtimeKeepalivePush() }}}
950-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, buffer.mapAsync(mode, offset, size).then(() => {
941+
WebGPU.Internals.futureInsert(futureId, buffer.mapAsync(mode, offset, size).then(() => {
951942
{{{ runtimeKeepalivePop() }}}
952-
_emwgpuOnMapAsyncCompleted(futureIdL, futureIdH, {{{ gpu.MapAsyncStatus.Success }}}, 0);
943+
_emwgpuOnMapAsyncCompleted(futureId, {{{ gpu.MapAsyncStatus.Success }}}, 0);
953944
}, (ex) => {
954945
{{{ runtimeKeepalivePop() }}}
955946
var sp = stackSave();
@@ -958,7 +949,7 @@ var LibraryWebGPU = {
958949
ex instanceof AbortError ? {{{ gpu.MapAsyncStatus.Aborted }}} :
959950
ex instanceof OperationError ? {{{ gpu.MapAsyncStatus.Error }}} :
960951
{{{ gpu.MapAsyncStatus.Unknown }}};
961-
_emwgpuOnMapAsyncCompleted(futureIdL, futureIdH, status, messagePtr);
952+
_emwgpuOnMapAsyncCompleted(futureId, status, messagePtr);
962953
delete WebGPU.Internals.bufferOnUnmaps[bufferPtr];
963954
}));
964955
},
@@ -1504,18 +1495,17 @@ var LibraryWebGPU = {
15041495
return ptr;
15051496
},
15061497

1507-
emwgpuDeviceCreateComputePipelineAsync__i53abi: false,
15081498
emwgpuDeviceCreateComputePipelineAsync__deps: ['emwgpuCreateComputePipeline', 'emwgpuOnCreateComputePipelineCompleted'],
15091499
emwgpuDeviceCreateComputePipelineAsync__sig: 'vpjp',
1510-
emwgpuDeviceCreateComputePipelineAsync: (devicePtr, futureIdL, futureIdH, descriptor) => {
1500+
emwgpuDeviceCreateComputePipelineAsync: (devicePtr, futureId, descriptor) => {
15111501
var desc = WebGPU.makeComputePipelineDesc(descriptor);
15121502
var device = WebGPU.getJsObject(devicePtr);
15131503
{{{ runtimeKeepalivePush() }}}
1514-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, device.createComputePipelineAsync(desc).then((pipeline) => {
1504+
WebGPU.Internals.futureInsert(futureId, device.createComputePipelineAsync(desc).then((pipeline) => {
15151505
{{{ runtimeKeepalivePop() }}}
15161506
var pipelinePtr = _emwgpuCreateComputePipeline();
15171507
WebGPU.Internals.jsObjectInsert(pipelinePtr, pipeline);
1518-
_emwgpuOnCreateComputePipelineCompleted(futureIdL, futureIdH, {{{ gpu.CreatePipelineAsyncStatus.Success }}}, pipelinePtr, 0);
1508+
_emwgpuOnCreateComputePipelineCompleted(futureId, {{{ gpu.CreatePipelineAsyncStatus.Success }}}, pipelinePtr, 0);
15191509
}, (pipelineError) => {
15201510
{{{ runtimeKeepalivePop() }}}
15211511
var sp = stackSave();
@@ -1524,7 +1514,7 @@ var LibraryWebGPU = {
15241514
pipeline.reason === 'validation' ? {{{ gpu.CreatePipelineAsyncStatus.ValidationError }}} :
15251515
pipeline.reason === 'internal' ? {{{ gpu.CreatePipelineAsyncStatus.InternalError }}} :
15261516
{{{ gpu.CreatePipelineAsyncStatus.Unknown }}};
1527-
_emwgpuOnCreateComputePipelineCompleted(futureIdL, futureIdH, status, 0, messagePtr);
1517+
_emwgpuOnCreateComputePipelineCompleted(futureId, status, 0, messagePtr);
15281518
stackRestore(sp);
15291519
}));
15301520
},
@@ -1613,18 +1603,17 @@ var LibraryWebGPU = {
16131603
return ptr;
16141604
},
16151605

1616-
emwgpuDeviceCreateRenderPipelineAsync__i53abi: false,
16171606
emwgpuDeviceCreateRenderPipelineAsync__deps: ['emwgpuCreateRenderPipeline', 'emwgpuOnCreateRenderPipelineCompleted'],
16181607
emwgpuDeviceCreateRenderPipelineAsync__sig: 'vpjp',
1619-
emwgpuDeviceCreateRenderPipelineAsync: (devicePtr, futureIdL, futureIdH, descriptor) => {
1608+
emwgpuDeviceCreateRenderPipelineAsync: (devicePtr, futureId, descriptor) => {
16201609
var desc = WebGPU.makeRenderPipelineDesc(descriptor);
16211610
var device = WebGPU.getJsObject(devicePtr);
16221611
{{{ runtimeKeepalivePush() }}}
1623-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, device.createRenderPipelineAsync(desc).then((pipeline) => {
1612+
WebGPU.Internals.futureInsert(futureId, device.createRenderPipelineAsync(desc).then((pipeline) => {
16241613
{{{ runtimeKeepalivePop() }}}
16251614
var pipelinePtr = _emwgpuCreateRenderPipeline();
16261615
WebGPU.Internals.jsObjectInsert(pipelinePtr, pipeline);
1627-
_emwgpuOnCreateRenderPipelineCompleted(futureIdL, futureIdH, {{{ gpu.CreatePipelineAsyncStatus.Success }}}, pipelinePtr, 0);
1616+
_emwgpuOnCreateRenderPipelineCompleted(futureId, {{{ gpu.CreatePipelineAsyncStatus.Success }}}, pipelinePtr, 0);
16281617
}, (pipelineError) => {
16291618
{{{ runtimeKeepalivePop() }}}
16301619
var sp = stackSave();
@@ -1633,7 +1622,7 @@ var LibraryWebGPU = {
16331622
pipeline.reason === 'validation' ? {{{ gpu.CreatePipelineAsyncStatus.ValidationError }}} :
16341623
pipeline.reason === 'internal' ? {{{ gpu.CreatePipelineAsyncStatus.InternalError }}} :
16351624
{{{ gpu.CreatePipelineAsyncStatus.Unknown }}};
1636-
_emwgpuOnCreateRenderPipelineCompleted(futureIdL, futureIdH, status, 0, messagePtr);
1625+
_emwgpuOnCreateRenderPipelineCompleted(futureId, status, 0, messagePtr);
16371626
stackRestore(sp);
16381627
}));
16391628
},
@@ -1762,13 +1751,12 @@ var LibraryWebGPU = {
17621751
return device.features.has(WebGPU.FeatureName[featureEnumValue]);
17631752
},
17641753

1765-
emwgpuDevicePopErrorScope__i53abi: false,
17661754
emwgpuDevicePopErrorScope__deps: ['emwgpuOnPopErrorScopeCompleted'],
17671755
emwgpuDevicePopErrorScope__sig: 'vpj',
1768-
emwgpuDevicePopErrorScope: (devicePtr, futureIdL, futureIdH) => {
1756+
emwgpuDevicePopErrorScope: (devicePtr, futureId) => {
17691757
var device = WebGPU.getJsObject(devicePtr);
17701758
{{{ runtimeKeepalivePush() }}}
1771-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, device.popErrorScope().then((gpuError) => {
1759+
WebGPU.Internals.futureInsert(futureId, device.popErrorScope().then((gpuError) => {
17721760
{{{ runtimeKeepalivePop() }}}
17731761
var type = {{{ gpu.ErrorType.Unknown }}};
17741762
if (!gpuError) type = {{{ gpu.ErrorType.NoError }}};
@@ -1780,13 +1768,13 @@ var LibraryWebGPU = {
17801768
#endif
17811769
var sp = stackSave();
17821770
var messagePtr = gpuError ? stringToUTF8OnStack(gpuError.message) : 0;
1783-
_emwgpuOnPopErrorScopeCompleted(futureIdL, futureIdH, {{{ gpu.PopErrorScopeStatus.Success }}}, type, messagePtr);
1771+
_emwgpuOnPopErrorScopeCompleted(futureId, {{{ gpu.PopErrorScopeStatus.Success }}}, type, messagePtr);
17841772
stackRestore(sp);
17851773
}, (ex) => {
17861774
{{{ runtimeKeepalivePop() }}}
17871775
var sp = stackSave();
17881776
var messagePtr = stringToUTF8OnStack(ex.message);
1789-
_emwgpuOnPopErrorScopeCompleted(futureIdL, futureIdH, {{{ gpu.PopErrorScopeStatus.Success }}}, {{{ gpu.ErrorType.Unknown }}}, messagePtr);
1777+
_emwgpuOnPopErrorScopeCompleted(futureId, {{{ gpu.PopErrorScopeStatus.Success }}}, {{{ gpu.ErrorType.Unknown }}}, messagePtr);
17901778
stackRestore(sp);
17911779
}));
17921780
},
@@ -1864,10 +1852,9 @@ var LibraryWebGPU = {
18641852
return navigator["gpu"]["wgslLanguageFeatures"].has(WebGPU.WGSLFeatureName[featureEnumValue]);
18651853
},
18661854

1867-
emwgpuInstanceRequestAdapter__i53abi: false,
18681855
emwgpuInstanceRequestAdapter__deps: ['emwgpuCreateAdapter', 'emwgpuOnRequestAdapterCompleted'],
18691856
emwgpuInstanceRequestAdapter__sig: 'vpjp',
1870-
emwgpuInstanceRequestAdapter: (instancePtr, futureIdL, futureIdH, options) => {
1857+
emwgpuInstanceRequestAdapter: (instancePtr, futureId, options) => {
18711858
var opts;
18721859
if (options) {
18731860
{{{ gpu.makeCheckDescriptor('options') }}}
@@ -1882,29 +1869,29 @@ var LibraryWebGPU = {
18821869
if (!('gpu' in navigator)) {
18831870
var sp = stackSave();
18841871
var messagePtr = stringToUTF8OnStack('WebGPU not available on this browser (navigator.gpu is not available)');
1885-
_emwgpuOnRequestAdapterCompleted(futureIdL, futureIdH, {{{ gpu.RequestAdapterStatus.Unavailable }}}, 0, messagePtr);
1872+
_emwgpuOnRequestAdapterCompleted(futureId, {{{ gpu.RequestAdapterStatus.Unavailable }}}, 0, messagePtr);
18861873
stackRestore(sp);
18871874
return;
18881875
}
18891876

18901877
{{{ runtimeKeepalivePush() }}}
1891-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, navigator["gpu"]["requestAdapter"](opts).then((adapter) => {
1878+
WebGPU.Internals.futureInsert(futureId, navigator["gpu"]["requestAdapter"](opts).then((adapter) => {
18921879
{{{ runtimeKeepalivePop() }}}
18931880
if (adapter) {
18941881
var adapterPtr = _emwgpuCreateAdapter(instancePtr);
18951882
WebGPU.Internals.jsObjectInsert(adapterPtr, adapter);
1896-
_emwgpuOnRequestAdapterCompleted(futureIdL, futureIdH, {{{ gpu.RequestAdapterStatus.Success }}}, adapterPtr, 0);
1883+
_emwgpuOnRequestAdapterCompleted(futureId, {{{ gpu.RequestAdapterStatus.Success }}}, adapterPtr, 0);
18971884
} else {
18981885
var sp = stackSave();
18991886
var messagePtr = stringToUTF8OnStack('WebGPU not available on this browser (requestAdapter returned null)');
1900-
_emwgpuOnRequestAdapterCompleted(futureIdL, futureIdH, {{{ gpu.RequestAdapterStatus.Unavailable }}}, 0, messagePtr);
1887+
_emwgpuOnRequestAdapterCompleted(futureId, {{{ gpu.RequestAdapterStatus.Unavailable }}}, 0, messagePtr);
19011888
stackRestore(sp);
19021889
}
19031890
}, (ex) => {
19041891
{{{ runtimeKeepalivePop() }}}
19051892
var sp = stackSave();
19061893
var messagePtr = stringToUTF8OnStack(ex.message);
1907-
_emwgpuOnRequestAdapterCompleted(futureIdL, futureIdH, {{{ gpu.RequestAdapterStatus.Error }}}, 0, messagePtr);
1894+
_emwgpuOnRequestAdapterCompleted(futureId, {{{ gpu.RequestAdapterStatus.Error }}}, 0, messagePtr);
19081895
stackRestore(sp);
19091896
}));
19101897
},
@@ -1935,19 +1922,18 @@ var LibraryWebGPU = {
19351922
// Methods of Queue
19361923
// --------------------------------------------------------------------------
19371924

1938-
emwgpuQueueOnSubmittedWorkDone__i53abi: false,
19391925
emwgpuQueueOnSubmittedWorkDone__deps: ['emwgpuOnWorkDoneCompleted'],
19401926
emwgpuQueueOnSubmittedWorkDone__sig: 'vpj',
1941-
emwgpuQueueOnSubmittedWorkDone: (queuePtr, futureIdL, futureIdH) => {
1927+
emwgpuQueueOnSubmittedWorkDone: (queuePtr, futureId) => {
19421928
var queue = WebGPU.getJsObject(queuePtr);
19431929

19441930
{{{ runtimeKeepalivePush() }}}
1945-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, queue.onSubmittedWorkDone().then(() => {
1931+
WebGPU.Internals.futureInsert(futureId, queue.onSubmittedWorkDone().then(() => {
19461932
{{{ runtimeKeepalivePop() }}}
1947-
_emwgpuOnWorkDoneCompleted(futureIdL, futureIdH, {{{ gpu.QueueWorkDoneStatus.Success }}});
1933+
_emwgpuOnWorkDoneCompleted(futureId, {{{ gpu.QueueWorkDoneStatus.Success }}});
19481934
}, () => {
19491935
{{{ runtimeKeepalivePop() }}}
1950-
_emwgpuOnWorkDoneCompleted(futureIdL, futureIdH, {{{ gpu.QueueWorkDoneStatus.Error }}});
1936+
_emwgpuOnWorkDoneCompleted(futureId, {{{ gpu.QueueWorkDoneStatus.Error }}});
19511937
}));
19521938
},
19531939

@@ -2226,13 +2212,12 @@ var LibraryWebGPU = {
22262212
// Methods of ShaderModule
22272213
// --------------------------------------------------------------------------
22282214

2229-
emwgpuShaderModuleGetCompilationInfo__i53abi: false,
22302215
emwgpuShaderModuleGetCompilationInfo__deps: ['emwgpuOnCompilationInfoCompleted', '$stringToUTF8', '$lengthBytesUTF8', 'malloc'],
22312216
emwgpuShaderModuleGetCompilationInfo__sig: 'vpjp',
2232-
emwgpuShaderModuleGetCompilationInfo: (shaderModulePtr, futureIdL, futureIdH, compilationInfoPtr) => {
2217+
emwgpuShaderModuleGetCompilationInfo: (shaderModulePtr, futureId, compilationInfoPtr) => {
22332218
var shaderModule = WebGPU.getJsObject(shaderModulePtr);
22342219
{{{ runtimeKeepalivePush() }}}
2235-
WebGPU.Internals.futureInsert(futureIdL, futureIdH, shaderModule.getCompilationInfo().then((compilationInfo) => {
2220+
WebGPU.Internals.futureInsert(futureId, shaderModule.getCompilationInfo().then((compilationInfo) => {
22362221
{{{ runtimeKeepalivePop() }}}
22372222
// Calculate the total length of strings and offsets here to malloc them
22382223
// all at once. Note that we start at 1 instead of 0 for the total size
@@ -2278,9 +2263,9 @@ var LibraryWebGPU = {
22782263
{{{ makeSetValue('compilationInfoPtr', C_STRUCTS.WGPUCompilationInfo.messageCount, 'compilationInfo.messages.length', '*') }}}
22792264
{{{ makeSetValue('compilationInfoPtr', C_STRUCTS.WGPUCompilationInfo.messages, 'compilationMessagesPtr', '*') }}};
22802265

2281-
_emwgpuOnCompilationInfoCompleted(futureIdL, futureIdH, {{{ gpu.CompilationInfoRequestStatus.Success }}}, compilationInfoPtr);
2266+
_emwgpuOnCompilationInfoCompleted(futureId, {{{ gpu.CompilationInfoRequestStatus.Success }}}, compilationInfoPtr);
22822267
}, () => {
2283-
_emwgpuOnCompilationInfoCompleted(futureIdL, futureIdH, {{{ gpu.CompilationInfoRequestStatus.Error }}}, compilationInfoPtr);
2268+
_emwgpuOnCompilationInfoCompleted(futureId, {{{ gpu.CompilationInfoRequestStatus.Error }}}, compilationInfoPtr);
22842269
}));
22852270
},
22862271

@@ -2453,7 +2438,8 @@ for (var value in LibraryWebGPU.$WebGPU.FeatureName) {
24532438
for (const key of Object.keys(LibraryWebGPU)) {
24542439
if (typeof LibraryWebGPU[key] !== 'function') continue;
24552440
if (key + '__i53abi' in LibraryWebGPU) continue;
2456-
const sig = LibraryWebGPU[key + '__sig'];
2441+
const sigKey = key + '__sig';
2442+
const sig = LibraryWebGPU[sigKey] ? LibraryWebGPU[sigKey] : LibraryManager.library[sigKey];
24572443
if (!sig?.includes('j')) continue;
24582444
LibraryWebGPU[key + '__i53abi'] = true;
24592445
}

0 commit comments

Comments
 (0)