Page 1 of 2

autobatch: Automates the use of SpriteBatches when drawing

Posted: Sat Jul 16, 2016 12:21 pm
by rxi
Image

autobatch is a small module which when dropped into an existing project, will override the love.graphics functions so that SpriteBatches are automatically used in cases where an image is drawn multiple times in a row. No changes to your project's code need to be made, besides calling require() on the module at the top of your main.lua file.

In the best case scenario -- where the same image is being drawn lots of times back-to-back and a SpriteBatch is not already being used -- autobatch can offer a significant boost in performance. In the worst case scenario the module should have little noticeable effect, with a possible slight reduction in performance (the module should be omitted in such cases).

There are a couple of demo projects (the gif above is of one) in the demo directory -- you can press [space] to toggle the use of the module on these.

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Sat Jul 16, 2016 7:36 pm
by Whatthefuck
I prefer manually controlling the sprites in a spritesheet, but this is a great module regardless.

Really nice work dude!

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Sat Jul 16, 2016 8:07 pm
by slime
Nifty! It doesn't seem to handle Shader:send calls though, which require a flush if were any draw calls before the Shader:send that used the same shader and same batch.

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Sun Jul 17, 2016 8:08 am
by rxi
slime wrote:Nifty! It doesn't seem to handle Shader:send calls though, which require a flush if were any draw calls before the Shader:send that used the same shader and same batch.
Good catch! Just pushed a commit to flush when :send() and :sendColor() are used. Thanks!

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Sun Jul 17, 2016 9:50 am
by asmageddon
Ah! Awesome thing. I thought of doing something like that myself, a fair while ago, but never got around to it. I might take it for a spin sometime, see if I can integrate my texture atlas into it somehow, to allow batching even when the original images aren't the same.

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Sun Jul 17, 2016 1:24 pm
by undef
Great Idea!

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Mon Jul 18, 2016 3:15 am
by alloyed
asmageddon wrote:Ah! Awesome thing. I thought of doing something like that myself, a fair while ago, but never got around to it. I might take it for a spin sometime, see if I can integrate my texture atlas into it somehow, to allow batching even when the original images aren't the same.
If you're already using a texture atlas you shouldn't have to do anything special: both love.graphics.draw(img, ...) and love.graphics.draw(img, quad, ...) can be batched the same way.

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Mon Jul 18, 2016 2:06 pm
by asmageddon
Yeah but that doesn't seamlessly improve the performance of code not using a texture atlas, and I think that would be a really nice feature.

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Mon Jul 18, 2016 4:08 pm
by slime
There are edge-case pitfalls to watch out for when switching from regular images to a texture atlas (e.g. border extrusion, wrap modes, shader or Mesh access of texture coordinates, etc.). I don't think automatically doing it is necessarily a good idea because it can cause subtle issues that are hard to track down.

Re: autobatch: Automates the use of SpriteBatches when drawing

Posted: Mon Jul 18, 2016 5:56 pm
by asmageddon
You do make a fair point :c I suppose it could still be nice if it was used only for drawing images directly, without shadders or wrapping, and included some padding in the atlas to prevent bleeding.