SpriteBatch vs. per‑sprite draw calls for animated tiles under a 100‑draw‑call limit
0 reputation · 19 Jan 2022, 21:08 UTC
0 reputation · 19 Jan 2022, 21:08 UTC
Goal: Determine whether to render a layer of 500 identical moving sprites each frame using a single SpriteBatch or individual love.graphics.draw calls while staying under a fixed draw‑call budget of 100 calls per frame.
Constraints: SpriteBatch reduces draw calls but requires updating its vertex buffer each frame when sprite positions change; frequent :setBuffer or :add calls can add CPU overhead. Per‑sprite draw calls keep overhead low for few sprites but increase draw‑call count and state changes, potentially exceeding the budget.
Uncertainty: At what sprite count or movement frequency does the CPU cost of rebuilding a dynamic SpriteBatch outweigh the savings from fewer draw calls? Does the 'stream' usage hint mitigate this cost for rapidly changing data? How does packing multiple textures into an atlas affect the decision when the sprites share different images?
35275 reputation · 20 Jan 2022, 07:14 UTC
Use a single love.graphics.SpriteBatch with the stream usage hint. It keeps the draw‑call count at 1, well below the 100‑call budget, and the per‑frame CPU cost of updating the batch for 500 sprites is modest compared to 500 separate love.graphics.draw calls that would exceed the limit.
:setBuffer or :add is linear in the number of sprites; 500 sprites use 2000 vertices, far below the typical 65535 vertex limit.stream hint tells the GPU that the buffer will change each frame, avoiding costly reallocations.local batch = love.graphics.newSpriteBatch(texture, 500, "stream")
batch:clear()
for i=1,500 do
local x, y = getSpritePosition(i) -- your own logic
batch:add(x, y)
end
love.graphics.draw(batch)
local atlas = love.graphics.newImage("atlas.png")
-- each sprite uses a quad from the atlas
batch:add(x, y, 0, 1, 1, quad)
print(love.graphics.getStats().drawcalls) -- should output 1
Do all 500 sprites share the same texture (or are they already packed into a single atlas)? If not, you’ll need additional batches or an atlas to keep the draw‑call count low.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.