I’ve got a jpeg texture and the first time it’s drawn there’s a costly gl.texImage2d call with Image Decode that takes together about 100ms and causes a short freeze.
but I get errors:
playcanvas-stable.js:4623 WebGL: INVALID_ENUM: texImage2D: invalid format
playcanvas-stable.js:4651 WebGL: INVALID_ENUM: generateMipmap: invalid texture target
Please provide a simple small project that takes 5 mins to make, with replica of the problem, that dramatically saves time for people that can help you.
I hate to revive a very old thread, but we are facing basically the same problem, where we would want manually upload a texture to GPU on scene load in order to prevent frame drops during the game.
Has this been updated over the last 5 years? Are there any other fixes for this, or is the “best practice” still to place objects with that texture in view for the first frame in order to get the texture uploaded to GPU?
I’m afraid yes, that’s the current workaround, keep for at least a frame all models in view to force the engine to compile the required shaders and uploaded textures to the GPU.
Another workaround is to turn off your cameras Frustum Culling in the editor, then after one frame has passed turn it back on via JavaScript. This will load everything on the first frame and prevent any hick ups during gameplay. One issue with this method is that it will cause a delay/pause at the beginning of the game.
Unfortunately, in our case it is a bit more complicated, as we have unticked pre-load on all textures in order for them to not be loaded on game-startup / in main menu. So when we switch to the game scene these textures may still be mid-loading in the first frame and the time the loading operation will take is not predictable. I.e. rendering the texture with a cam in the first frame won’t work since it will not upload the texture to GPU, since it is not fully loaded yet.
As a workaround we will probably now implement a script, which waits for all scene assets to be fully loaded, then render the entire scene with a cam and then disable our loading UI overlay.
It would still be nice to have a better way of manually uploading textures to GPU. Do you happen to know whether such a feature is planned for the future, or whether there is an existing issue on GitHub for this?
IIRC, the main hit is when the texture is first used as it gets uploaded to the GPU on the main thread.
Having an API to manually upload textures to the GPU would be great so users can control when the stutter happens and potentially phase it over several frames.
Wondering if it’s possible to do this on separate thread? Would that require offscreen canvas support?
If you do it on a separate thread, you need to do all rendering on a separate thread. There’s only a single thread a WebGl calls can be called from, you cannot do rendering on one thread and resource creation on another.
@yaustar@mvaligursky Thanks for your help! Since this effects all our games and It’s hard for me to estimate the complexity of the issue: Do you have a rough estimate on when you’ll adress the issue?