Skip to content

Commit 8647224

Browse files
authored
Reuse device handle in emscripten_webgpu_get_device (#18082)
Currently, this function creates a new wrapper each time it's called. As a result, what is a single device in JavaScript looks like different devices in C (the handles are different), and there is a new entry in WebGPU.mgrDevice for each call. With this change, the same handle is returned each time. Note that each new device wrapper currently also leaks a queue wrapper (see the comment at <https://github.com/emscripten-core/emscripten/blob/0c4fba0371abc8c7219dc481a02ea4a464b14641/src/library_webgpu.js#L190>), so this change also reduces the impact of that issue.
1 parent 857bdf9 commit 8647224

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/library_html5_webgpu.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ var LibraryHTML5WebGPU = {
6060
#if ASSERTIONS
6161
assert(Module['preinitializedWebGPUDevice']);
6262
#endif
63-
var device = Module['preinitializedWebGPUDevice'];
64-
var deviceWrapper = { queueId: WebGPU.mgrQueue.create(device["queue"]) };
65-
return WebGPU.mgrDevice.create(device, deviceWrapper);
63+
if (WebGPU.preinitializedDeviceId === undefined) {
64+
var device = Module['preinitializedWebGPUDevice'];
65+
var deviceWrapper = { queueId: WebGPU.mgrQueue.create(device["queue"]) };
66+
WebGPU.preinitializedDeviceId = WebGPU.mgrDevice.create(device, deviceWrapper);
67+
}
68+
WebGPU.mgrDevice.reference(WebGPU.preinitializedDeviceId);
69+
return WebGPU.preinitializedDeviceId;
6670
},
6771
};
6872

0 commit comments

Comments
 (0)