@@ -348,6 +348,7 @@ std::shared_ptr<GPUSharedFence> GPUDevice::importSharedFence(
348348}
349349
350350async::AsyncTaskHandle GPUDevice::createComputePipelineAsync (
351+ jsi::Runtime &runtime,
351352 std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
352353 wgpu::ComputePipelineDescriptor desc{};
353354 Convertor conv;
@@ -360,12 +361,16 @@ async::AsyncTaskHandle GPUDevice::createComputePipelineAsync(
360361 descriptor->label .has_value () ? descriptor->label .value () : " " );
361362 auto pipelineHolder = std::make_shared<GPUComputePipeline>(nullptr , label);
362363
363- return _async->postTask ([device = _instance, desc, descriptor,
364- pipelineHolder](
365- const async::AsyncTaskHandle::ResolveFunction
366- &resolve,
367- const async::AsyncTaskHandle::RejectFunction
368- &reject) {
364+ // Post to the CALLING runtime's context so the promise settles on the
365+ // thread that requested it (see GPUBuffer::mapAsync).
366+ auto context =
367+ async::RuntimeContext::getOrCreate (runtime, _async->instance ());
368+ return context->postTask ([device = _instance, desc, descriptor,
369+ pipelineHolder](
370+ const async::AsyncTaskHandle::ResolveFunction
371+ &resolve,
372+ const async::AsyncTaskHandle::RejectFunction
373+ &reject) {
369374 (void )descriptor;
370375 device.CreateComputePipelineAsync (
371376 &desc, wgpu::CallbackMode::AllowProcessEvents,
@@ -389,6 +394,7 @@ async::AsyncTaskHandle GPUDevice::createComputePipelineAsync(
389394}
390395
391396async::AsyncTaskHandle GPUDevice::createRenderPipelineAsync (
397+ jsi::Runtime &runtime,
392398 std::shared_ptr<GPURenderPipelineDescriptor> descriptor) {
393399 wgpu::RenderPipelineDescriptor desc{};
394400 Convertor conv;
@@ -402,12 +408,16 @@ async::AsyncTaskHandle GPUDevice::createRenderPipelineAsync(
402408 descriptor->label .has_value () ? descriptor->label .value () : " " );
403409 auto pipelineHolder = std::make_shared<GPURenderPipeline>(nullptr , label);
404410
405- return _async->postTask ([device = _instance, desc, descriptor,
406- pipelineHolder](
407- const async::AsyncTaskHandle::ResolveFunction
408- &resolve,
409- const async::AsyncTaskHandle::RejectFunction
410- &reject) {
411+ // Post to the CALLING runtime's context so the promise settles on the
412+ // thread that requested it (see GPUBuffer::mapAsync).
413+ auto context =
414+ async::RuntimeContext::getOrCreate (runtime, _async->instance ());
415+ return context->postTask ([device = _instance, desc, descriptor,
416+ pipelineHolder](
417+ const async::AsyncTaskHandle::ResolveFunction
418+ &resolve,
419+ const async::AsyncTaskHandle::RejectFunction
420+ &reject) {
411421 (void )descriptor;
412422 device.CreateRenderPipelineAsync (
413423 &desc, wgpu::CallbackMode::AllowProcessEvents,
@@ -433,13 +443,18 @@ void GPUDevice::pushErrorScope(wgpu::ErrorFilter filter) {
433443 _instance.PushErrorScope (filter);
434444}
435445
436- async::AsyncTaskHandle GPUDevice::popErrorScope () {
446+ async::AsyncTaskHandle GPUDevice::popErrorScope (jsi::Runtime &runtime ) {
437447 auto device = _instance;
438448
439- return _async->postTask ([device](const async::AsyncTaskHandle::ResolveFunction
440- &resolve,
441- const async::AsyncTaskHandle::RejectFunction
442- &reject) {
449+ // Post to the CALLING runtime's context so the promise settles on the
450+ // thread that requested it (see GPUBuffer::mapAsync).
451+ auto context =
452+ async::RuntimeContext::getOrCreate (runtime, _async->instance ());
453+ return context->postTask ([device](
454+ const async::AsyncTaskHandle::ResolveFunction
455+ &resolve,
456+ const async::AsyncTaskHandle::RejectFunction
457+ &reject) {
443458 device.PopErrorScope (
444459 wgpu::CallbackMode::AllowProcessEvents,
445460 [resolve, reject](wgpu::PopErrorScopeStatus status,
0 commit comments