libvita2d icon indicating copy to clipboard operation
libvita2d copied to clipboard

Delayed texture deletion (Fixes #28)

Open thp opened this issue 6 years ago • 4 comments

Since I ran into a crash a few times already by now, here's a patch that delays the "real" deletion of the texture until a swap operation (or until vita2d_gc_textures() is called manually).

This way, a library user can just allocate and free textures normally and not have to worry about freeing textures too early, vita2d will take care of delaying the deletion until the next swap. Note that right now all free texture operations are delayed, even in cases where a texture wasn't used in the current frame (and could potentially be freed immediately).

Apart from the linked list overhead, the only situation when this would be bad is if an application allocates and frees textures while drawing, and these free operations are delayed until the swap. But for this case, the application developer can use vita2d_gc_textures() to explicitly flush the free operations before vita2d_swap_buffers().

thp avatar Feb 10 '19 13:02 thp

Rebased against latest master branch.

thp avatar Sep 17 '22 19:09 thp

@xerpi When you have a moment, can you have a quick look if this is something that can be merged?

thp avatar Dec 20 '22 14:12 thp

@Rinnegatamante what do you think?

xerpi avatar Dec 20 '22 14:12 xerpi

Personally I don't like the approach, wait_rendering_done can be very expensive depending on the scene and putting CPU on wait for freeing textures can result in huge stutters. I'd suggest a more "delayed" approach like the one I used in vitaGL. In vitaGL, in order to avoid busy waiting for GPU, all resources that must be freed and are used at least once by the gpu get marked for garbage collection (if gpu never used it, it's instantly freed instead) and garbage collection works each frame in async on a separate thread (but i also have a preprocessor flag to make it run on main thread eventually) freeing textures marked for deletion 3 frames before. This ensures that we never end up freeing data still in use by the GPU and completely skips any busy wait.

Some references:

  • https://github.com/Rinnegatamante/vitaGL/blob/master/source/gxm.c#L220-L257
  • https://github.com/Rinnegatamante/vitaGL/blob/master/source/shared.h#L666-L681

Rinnegatamante avatar Dec 22 '22 20:12 Rinnegatamante