https://playground.babylonjs.com/#2KRNG9#4053 This code runs fine in WebGL mode, but in WebGPU mode, the WebGPU context is lost after a certain number of loops. Is it because dispose fails to release resources promptly?
It totally looks like it cc @Evgeni_Popov
The problem is that we can’t destroy a texture right away, because it could be used in the current graphics command.
For example, if in the current frame you use a texture as a diffuse texture for a material used by a mesh, but you know you won’t need this texture anymore afterwards (because you will change the material of the mesh in the next frame, for eg), you could dispose the texture. However, we can’t just dispose the texture at WebGPU level, else we will get an error that a “texture used in a submit has been destroyed”. So, we postpone the deletion of textures at the end of the current frame, after graphic commands have been submitted.
In your PG, you destroy the textures in a setTimeout, which executes outside a frame boundary. It means the deletion will happen at the end of next frame. However, because of the way your PG is setup, during next frame, you are going to allocate more textures, and the previous textures won’t have been disposed yet. That’s why we get an “out of memory” error.
To be on the safe side, you should execute texture disposal inside the regular frame loop. You can do it in a onAfterRenderObservable observer, for example:
Thank you for your reply, but your example project is empty.
Sorry, I fixed the link!
Here is a better link: The First Step | Babylon.js Playground