Page 1 of 1

Spritebatch doesn't speed up things?

Posted: Sat Jan 04, 2014 3:25 pm
by tapir
Hi people,

When I run below codes I get similar FPS. Shouldn't I get more when using a spritebatch considering it's single draw call vs 200 per frame?

Second question, when I look at wiki examples about spritebatch I've noticed that bind() and unbind() are not used. When should I use them exactly? In below examples it didn't make any difference.

Thanks

Code: Select all

function love.load()
	image = love.graphics.newImage("sprite.png")
	sb = love.graphics.newSpriteBatch(image, 250)
	q1 = love.graphics.newQuad(0, 0, 128, 128, 256, 256)
	q2 = love.graphics.newQuad(0, 128, 128, 128, 256, 256)
	q3 = love.graphics.newQuad(128, 0, 128, 128, 256, 256)
	q4 = love.graphics.newQuad(128, 128, 128, 128, 256, 256)
end

function love.update(dt)
	sb:clear()
	for i = 1, 50, 1 do
		sb:add(q1, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
		sb:add(q2, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
		sb:add(q3, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
		sb:add(q4, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
	end
end

function love.draw()
	love.graphics.draw(sb)
	love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 0, 0)
end

Code: Select all

function love.load()
	image = love.graphics.newImage("sprite.png")
	sb = love.graphics.newSpriteBatch(image, 250)
	q1 = love.graphics.newQuad(0, 0, 128, 128, 256, 256)
	q2 = love.graphics.newQuad(0, 128, 128, 128, 256, 256)
	q3 = love.graphics.newQuad(128, 0, 128, 128, 256, 256)
	q4 = love.graphics.newQuad(128, 128, 128, 128, 256, 256)
end

function love.update(dt)
end

function love.draw()
	for i = 1, 50, 1 do
		love.graphics.draw(image, q1, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
		love.graphics.draw(image, q2, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
		love.graphics.draw(image, q3, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
		love.graphics.draw(image, q4, math.random(0, love.window.getWidth()), math.random(0, love.window.getHeight()))
	end
	love.graphics.print("Current FPS: "..tostring(love.timer.getFPS()), 0, 0)
end

Re: Spritebatch doesn't speed up things?

Posted: Sat Jan 04, 2014 8:38 pm
by slime
tapir wrote:When I run below codes I get similar FPS. Shouldn't I get more when using a spritebatch considering it's single draw call vs 200 per frame?
That depends on what's bottlenecking your framerate.

If it's CPU-side draw calls, then you will probably get more performance with your first code (if you add bind/unbind around the loop.)
If it's not that (for example if it's GPU-side fill rate or texture usage), then that change won't show much difference in performance until you get rid of the other bottlenecks.
tapir wrote:Second question, when I look at wiki examples about spritebatch I've noticed that bind() and unbind() are not used. When should I use them exactly?
You should use them whenever you change or add more than a couple things at once in the SpriteBatch - but try not to have more than one bind/unbind pair per spritebatch in between draws.

Also, to get the best performance out of a SpriteBatch, you should only change it whenever it's absolutely necessary. For example in a tile-based game, you'll probably have lots of background tiles which don't change relative to each other - that's a good opportunity to add them all to a spritebatch once when they're first loaded, and only draw the spritebatch afterwards without calling add/set/clear/etc.

Also somewhat relevant: http://blogs.msdn.com/b/shawnhar/archiv ... bound.aspx
And http://blogs.msdn.com/b/shawnhar/archiv ... a-box.aspx
And http://blogs.msdn.com/b/shawnhar/archiv ... -line.aspx

(I like his blog...)

Re: Spritebatch doesn't speed up things?

Posted: Sun Jan 05, 2014 9:32 am
by tapir
Looks like it's GPU bound then.
Thanks for the articles.