You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cross-pthread creation and destruction of WebGPU objects #19645
Failing command line in full:
If this is compile or link-time failure please include the full failing command
along with its entire output.
Full link command and output with -v appended:
Even for runtime issues it helps a lot if you can include the full link command.
Adding -v to the link command will show all of the sub-commands run which
can help us diagnose your issue.
Because the "manager" objects like WebGPU.mgrBuffer are not synchronized across pthreads/web workers, an object that's created on one thread (e.g. via wgpuDeviceCreateBuffer) cannot be destroyed (e.g. via wgpuBufferDestroy) on another, as the destroying thread cannot find the given object id in its manager map.
Multithreading of this kind will require the underlying JS API to be multithreaded. That will happen eventually, but it will be some time before we know exactly what it looks like. Once we are making progress on it, Emscripten will be the first place we're prototyping support.
There are a few things that make this complicated, in particular synchronously sharing WebGPU objects across workers so that they can be used across threads without explicit send and async receive operations on the threads.
Vaguely, there could be a possibility of doing the proxy-to-worker thing that Emscripten for WebGL, has where it proxies all WebGL commands to a single thread so the underlying JS API doesn't have to be multithreaded. However our team isn't pursuing this right now as we are currently working on other WebGPU improvements, including improving and stabilizing webgpu.h across native and emscripten.
@mannkdev if we were to implement this via proxying all WebGPU calls back to the main thread would that solve your program?
@kainino0x do you think such an approach would be viable? i.e. is the number of WebGPU calls per frame generally high enough that the cost of proxying would be prohibitive?
FWIW I think it would be fairly easy to automatically proxy all calls back the to the main thread? Indeed maybe we be doing this by default or having the calls assert when made from a pthread?
I don't think it should be more prohibitive than proxying WebGL. I do think it would be more of a stopgap before we have real multithreading in the JS API, but it's going to be a while before we reach that point. So if it turns out not to be that difficult to proxy these, I'd be all for providing it (behind some build flag).
And maybe a runtime flag, too, in wgpuCreateInstance? Sometimes applications might want to use two completely independent WebGPU instances on different threads, which JS does already support, rather than proxying both instances to one thread for no reason.
Sounds good, if this could be implemented it would allow us to proceed with our planned usage of pthreads + WebGPU. Seems less than ideal performance wise, but guess we will cross that bridge and benchmark when we get to that point.
Do you have any idea of how long it might take for you guys to implement this?
And yes, having build + runtime flags is a great idea. Only one possible concern is how much further having a runtime flag would degrade performance (if it gets checked on every operation).
@sbc100 Yes, we have existing code that needs to be able to make WebGPU calls from multiple threads. That's how we came across this issue - those threads are currently disabled. We want to enable them, but doing so results in crashes as some threads attempt to destroy objects that were created on other threads. To clarify, these threads are part of cross-platform code and work fine on other platforms (for example D3D).
It is not on our roadmap. It wouldn't take a ton of work to implement with the right generators, for example it could take advantage of the code generator in Dawn which generates code from a json file dawn.json. However since I don't think our team will be working on this very soon you'd also have to become familiar with that code in order to use it.
For maintenance, we'd strongly prefer something generated, because there will still be both breaking changes and new features in the webgpu.h API.
As for how to implement it, the existing WebGL proxying code in Emscripten can be used as a model (though I'm not very familiar with it unfortunately).
@sbc100 yes, sorry, I probably should have caught this. WebGPU can be used from a worker - what we don't have is the ability to share anything between threads. I thought the proxying would only take effect with certain build options so people would be able to control this?
@warvstar good to know the changes needed are minimal. If you know a fix that works both on the main thread and on workers, I would love to take a PR.
@sbc100 yes, sorry, I probably should have caught this. WebGPU can be used from a worker - what we don't have is the ability to share anything between threads. I thought the proxying would only take effect with certain build options so people would be able to control this?
But can it be used from a worker with emscripten? I guess there are no tests for that? Would it depend on OFFSCREENCANVAS_SUPPORT? We can make it is so that the proxying only kicks in with OFFSCREENCANVAS_SUPPORT disabled perhaps? Is there any other way to use webgpu from a worker?
@warvstar good to know the changes needed are minimal. If you know a fix that works both on the main thread and on workers, I would love to take a PR.
But can it be used from a worker with emscripten? I guess there are no tests for that? Would it depend on OFFSCREENCANVAS_SUPPORT? We can make it is so that the proxying only kicks in with OFFSCREENCANVAS_SUPPORT disabled perhaps? Is there any other way to use webgpu from a worker?
It can almost certainly be used without a canvas. With a canvas it sounds like it's only slightly broken, and we want it to work.
WebGPU only uses canvases for output, so you can do lots of things in WebGPU without a canvas, especially GPGPU compute. It's also possible to use an OffscreenCanvas that isn't transferred from the main thread (but this is presumably just as broken in Emscripten as using an OffscreenCanvas from the main thread).
But can it be used from a worker with emscripten? I guess there are no tests for that? Would it depend on OFFSCREENCANVAS_SUPPORT? We can make it is so that the proxying only kicks in with OFFSCREENCANVAS_SUPPORT disabled perhaps? Is there any other way to use webgpu from a worker?
It can almost certainly be used without a canvas. With a canvas it sounds like it's only slightly broken, and we want it to work.
WebGPU only uses canvases for output, so you can do lots of things in WebGPU without a canvas, especially GPGPU compute. It's also possible to use an OffscreenCanvas that isn't transferred from the main thread (but this is presumably just as broken in Emscripten as using an OffscreenCanvas from the main thread).
I see.. I that case perhaps the proxying should just opt-in?
That would make sense. At least a build-time option to turn WebGPU proxying on and off would be good. We could consider a runtime switch, either thread global (easier) or somehow per-object (harder) but it probably isn't critically needed.
For those who came here looking to offscreen render from a webworker with webgpu, it works from 3.1.56 onwards, thanks to #21467. All that you need is to pass -pthread and -sOFFSCREENCANVAS_SUPPORT.
No recent changes. This issue is about using WebGPU from multiple threads at once, which doesn't work. But it should be possible to use WebGPU (with a canvas) in any single thread of a multithreaded application, as long as you only call into WebGPU from that one thread.
No recent changes. This issue is about using WebGPU from multiple threads at once, which doesn't work. But it should be possible to use WebGPU (with a canvas) in any single thread of a multithreaded application, as long as you only call into WebGPU from that one thread.
Yes, I managed to do it according to your description. What I missed yesterday was the function emscripten_pthread_attr_settransferredcanvases, which posts canvases to a specific thread.
Thanks for your help. Really appreciate!
No recent changes. This issue is about using WebGPU from multiple threads at once, which doesn't work. But it should be possible to use WebGPU (with a canvas) in any single thread of a multithreaded application, as long as you only call into WebGPU from that one thread.
Yes, I managed to do it according to your description. What I missed yesterday was the function emscripten_pthread_attr_settransferredcanvases, which posts canvases to a specific thread. Thanks for your help. Really appreciate!
@kainino0x@spking11 Sorry for replying to you late. You can take a look at my test demo, which demonstrates how to use offscreen canvas in a multi-threaded manner.
We're moving the WebGPU bindings to be maintained in Dawn instead of Emscripten, so I'm closing this in favor of this feature request for proxied multithreading: https://crbug.com/377733413
You can follow along there by logging in and clicking the "+1" button. I don't know if we are going to implement this or not, though.
Activity
kainino0x commentedon Jun 20, 2023
Multithreading of this kind will require the underlying JS API to be multithreaded. That will happen eventually, but it will be some time before we know exactly what it looks like. Once we are making progress on it, Emscripten will be the first place we're prototyping support.
There are a few things that make this complicated, in particular synchronously sharing WebGPU objects across workers so that they can be used across threads without explicit send and async receive operations on the threads.
Vaguely, there could be a possibility of doing the proxy-to-worker thing that Emscripten for WebGL, has where it proxies all WebGL commands to a single thread so the underlying JS API doesn't have to be multithreaded. However our team isn't pursuing this right now as we are currently working on other WebGPU improvements, including improving and stabilizing webgpu.h across native and emscripten.
sbc100 commentedon Jun 20, 2023
@mannkdev if we were to implement this via proxying all WebGPU calls back to the main thread would that solve your program?
@kainino0x do you think such an approach would be viable? i.e. is the number of WebGPU calls per frame generally high enough that the cost of proxying would be prohibitive?
FWIW I think it would be fairly easy to automatically proxy all calls back the to the main thread? Indeed maybe we be doing this by default or having the calls assert when made from a pthread?
kainino0x commentedon Jun 20, 2023
I don't think it should be more prohibitive than proxying WebGL. I do think it would be more of a stopgap before we have real multithreading in the JS API, but it's going to be a while before we reach that point. So if it turns out not to be that difficult to proxy these, I'd be all for providing it (behind some build flag).
kainino0x commentedon Jun 20, 2023
And maybe a runtime flag, too, in wgpuCreateInstance? Sometimes applications might want to use two completely independent WebGPU instances on different threads, which JS does already support, rather than proxying both instances to one thread for no reason.
mannkdev commentedon Jun 21, 2023
Sounds good, if this could be implemented it would allow us to proceed with our planned usage of pthreads + WebGPU. Seems less than ideal performance wise, but guess we will cross that bridge and benchmark when we get to that point.
Do you have any idea of how long it might take for you guys to implement this?
And yes, having build + runtime flags is a great idea. Only one possible concern is how much further having a runtime flag would degrade performance (if it gets checked on every operation).
sbc100 commentedon Jun 21, 2023
@mannkdev do you actually want multiple threads to be able to make WebGPU calls? Or will all of them come from the same thread?
mannkdev commentedon Jun 21, 2023
@sbc100 Yes, we have existing code that needs to be able to make WebGPU calls from multiple threads. That's how we came across this issue - those threads are currently disabled. We want to enable them, but doing so results in crashes as some threads attempt to destroy objects that were created on other threads. To clarify, these threads are part of cross-platform code and work fine on other platforms (for example D3D).
Mintyboi commentedon Jul 18, 2023
@kainino0x May I check if this is on your immediate roadmap and how much effort do you think it'll take to implement this?
kainino0x commentedon Jul 18, 2023
It is not on our roadmap. It wouldn't take a ton of work to implement with the right generators, for example it could take advantage of the code generator in Dawn which generates code from a json file dawn.json. However since I don't think our team will be working on this very soon you'd also have to become familiar with that code in order to use it.
For maintenance, we'd strongly prefer something generated, because there will still be both breaking changes and new features in the webgpu.h API.
As for how to implement it, the existing WebGL proxying code in Emscripten can be used as a model (though I'm not very familiar with it unfortunately).
Proxy webgpu calls back to the main thread. NFC
Proxy webgpu calls back to the main thread. NFC
Proxy webgpu calls back to the main thread. NFC
Proxy webgpu calls back to the main thread. NFC
10 remaining items
kainino0x commentedon Oct 24, 2023
@sbc100 yes, sorry, I probably should have caught this. WebGPU can be used from a worker - what we don't have is the ability to share anything between threads. I thought the proxying would only take effect with certain build options so people would be able to control this?
@warvstar good to know the changes needed are minimal. If you know a fix that works both on the main thread and on workers, I would love to take a PR.
sbc100 commentedon Oct 24, 2023
But can it be used from a worker with emscripten? I guess there are no tests for that? Would it depend on
OFFSCREENCANVAS_SUPPORT? We can make it is so that the proxying only kicks in withOFFSCREENCANVAS_SUPPORTdisabled perhaps? Is there any other way to use webgpu from a worker?kainino0x commentedon Oct 24, 2023
It can almost certainly be used without a canvas. With a canvas it sounds like it's only slightly broken, and we want it to work.
WebGPU only uses canvases for output, so you can do lots of things in WebGPU without a canvas, especially GPGPU compute. It's also possible to use an OffscreenCanvas that isn't transferred from the main thread (but this is presumably just as broken in Emscripten as using an OffscreenCanvas from the main thread).
sbc100 commentedon Oct 24, 2023
I see.. I that case perhaps the proxying should just opt-in?
kainino0x commentedon Oct 24, 2023
That would make sense. At least a build-time option to turn WebGPU proxying on and off would be good. We could consider a runtime switch, either thread global (easier) or somehow per-object (harder) but it probably isn't critically needed.
sbc100 commentedon Oct 24, 2023
I think build-time is the only one that is simple for us to implement at this point.
jspanchu commentedon Apr 23, 2024
For those who came here looking to offscreen render from a webworker with webgpu, it works from 3.1.56 onwards, thanks to #21467. All that you need is to pass
-pthreadand-sOFFSCREENCANVAS_SUPPORT.spking11 commentedon Oct 15, 2024
Anything update about using WebGPU with canvas in webworker?👀
kainino0x commentedon Oct 15, 2024
No recent changes. This issue is about using WebGPU from multiple threads at once, which doesn't work. But it should be possible to use WebGPU (with a canvas) in any single thread of a multithreaded application, as long as you only call into WebGPU from that one thread.
spking11 commentedon Oct 16, 2024
Yes, I managed to do it according to your description. What I missed yesterday was the function
emscripten_pthread_attr_settransferredcanvases, which posts canvases to a specific thread.Thanks for your help. Really appreciate!
xrui94 commentedon Dec 26, 2024
@kainino0x @spking11 Sorry for replying to you late. You can take a look at my test demo, which demonstrates how to use offscreen canvas in a multi-threaded manner.
kainino0x commentedon Apr 24, 2025
We're moving the WebGPU bindings to be maintained in Dawn instead of Emscripten, so I'm closing this in favor of this feature request for proxied multithreading: https://crbug.com/377733413
You can follow along there by logging in and clicking the "+1" button. I don't know if we are going to implement this or not, though.