HaxeFoundation/hashlink

With SDL backend on Windows, adding an element to a SpriteBatch can cause a large lag spike

csclyde opened this issue · 2 comments

I've been profiling my game recently, and found that when adding elements to a SpriteBatch (for a custom particle engine), it frequently creates a lag spike. I can confirm that it happens on the frame in which a particle is added, but I can't tell why certain adds trigger this behavior, and some do not. You can see a profile here, where frames are regularly dropped, corresponding to when a particle (BatchElement) is added to the SpriteBatch:

image

Zooming into the offending frame, you can see that the SpriteBatch flush is creating a massive delay:

image

Once it gets into allocBuffer, I have no clue what's happening. But I don't feel that I'm using the SpriteBatch improperly. I'm just creating a pool of BatchElement subclasses. Then periodically adding them to the SpriteBatch. Then, after a time, removing them from the sprite batch and returning them to the pool.

You can try to setup a GPU buffer cache with hxd.impl.Allocator.set(new hxd.impl.CacheAllocator())
This will permit the reuse of GPU buffers for dynamic draws.