I'm building a WebGL application that is suffering from a nasty performance bottleneck. The application often needs to burst upload a set of large textures (say 2000 x 2000) to the GPU. Right now it is choking on the texImage2D
call. Every time I call texImage2D
there is notable jank. When a burst of them happens, it completely kills the UI experience.
This is actually a port of an OpenGL application. In OpenGL this problem was solved by uploading the texture in a separate thread and that worked very well. As far as I can tell, there is no direct way to do anything similar in WebGL. I initially tried using OffScreenCanvas
with workers. The plan was to transfer an OffScreenCanvas
to a Worker
which would upload the texture, but I found out that I can access the WebGLRenderingContext
of an OffScreenCanvas
in only one Worker
. This defeats my purpose.
Is there any other approach to either speed up the texImage2D
call, or somehow have the CPU->GPU transfer happen in a separate Worker
?
There are many different sources I can pass to the texImage2D
call, can I expect some of them to be more performant than others? ImageBitmap
seems promising (I'm going to test this), but I can't quite figure out if the data for an ImageBitmap
is held in the GPU.
ImageBitmap
. I'm going to give that a shot. Do you happen to know ifImageBitmap
data is held in the GPU (for Chrome at least)? From the documentation I can't quite tell. It seems to suggest that both RAM or GPU is acceptable. Roughly I'm going to: create an OffScreenCanvas in a worker, draw to the 2d context, call createImageBitmap(), transfer it to the main thread, then call texImage2D(). Are there any pitfalls here? E.g. options to createImageBitmap()?createImageBitmap()
in a worker in the link you provided. That was informative, I didn't know thatcreateImageBitmap()
was threaded. I think for my use case I still need the worker because the textures are coming from another (2d) canvas and I need the draw to that to be on a worker thread. I can't see a way to get that data back from the worker thread to the main thread other than to call createImageBitmap() and transfer the result. Do you see any issue with this? I'm not really a graphics person, so I can't evaluate this very confidently.