[solved] SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
opex
Prole
Posts: 3
Joined: Fri Apr 28, 2017 8:45 am

[solved] SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by opex »

Please explain why fps is the same?
I wrote a small test, and I don't understand what happens...
I always thought that rendering through SpriteBatch should work faster than multiples love.graphics.draw().
Where is my mistake?

Code: Select all

local a,b,c
local q1,q2
local batch1,batch2,batch3
local mode=1
local yPos,direction=100,1
local t1={}
local t2={}

local function getModeName(m)
  if m==1 then
    return 'multiple draws'
  elseif m==2 then
    return 'stream batch'
  elseif m==3 then
    return 'dynamic batch'
  elseif m==4 then
    return 'static batch'
  end
end

function love.load()
  a=love.graphics.newImage('1.png')
  b=love.graphics.newImage('2.png')
  c=love.graphics.newImage('3.png')

  q1=love.graphics.newQuad(0,0,160,160,c:getWidth(),c:getHeight())
  q2=love.graphics.newQuad(160,0,160,160,c:getWidth(),c:getHeight())

  batch1=love.graphics.newSpriteBatch(c,2000,'stream')
  batch2=love.graphics.newSpriteBatch(c,2000,'dynamic')
  for i=1,1000 do
    t1[#t1+1] = batch2:add(q1,i,yPos)
    t2[#t2+1] = batch2:add(q2,i,yPos+200)             
  end
  batch3=love.graphics.newSpriteBatch(c,2000,'static')
  for i=1,1000 do
    batch3:add(q1,i,yPos)
    batch3:add(q2,i,yPos+200)             
  end
end

function love.draw()
  if mode==1 then --multiple draws
    for i=1,1000 do
      love.graphics.draw(a,i,yPos)
      love.graphics.draw(b,i,yPos+200)
    end    
  elseif mode==2 then --stream batch
    love.graphics.draw(batch1)
  elseif mode==3 then --dynamic batch
    love.graphics.draw(batch2)
  elseif mode==4 then --static batch
    love.graphics.draw(batch3)
  end
  love.graphics.print('click to change draw mode\nfps: '..love.timer.getFPS()..'\nmode: '..getModeName(mode))
end

function love.mousepressed()
  mode = mode+1
  if mode>4 then
    mode=1
  end
end

function love.update(dt)
  --change y position
  yPos=yPos+100*dt*direction
  if yPos<100 then
    direction=1
  elseif yPos>200 then
    direction=-1
  end

  --change stream batch
  batch1:clear()
  for i=1,1000 do
    batch1:add(q1,i,yPos)
    batch1:add(q2,i,yPos+200)             
  end

  --change dynamic batch
  for i=1,1000 do
    batch2:set(t1[i],q1,i,yPos)
    batch2:set(t2[i],q2,i,yPos+200)             
  end

end
Attachments
test.love
(75.54 KiB) Downloaded 103 times
Last edited by opex on Fri Apr 28, 2017 4:09 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by raidho36 »

That's because by default FPS is locked with vsync and won't go any higher, and you're not drawing nearly enough sprites to saturate draw call queue for FPS to drop below that.
User avatar
opex
Prole
Posts: 3
Joined: Fri Apr 28, 2017 8:45 am

Re: SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by opex »

I turned off vsync and nothing changed.
In my test love.graphics.draw() is called 2000 times every frame,
but SpriteBatch draws only one time. Why FPS identical?
Probably LOVE have internal optimisation mechanism in love.graphics.draw()?
What test should I write to see that the SpriteBatch really works faster?
Attachments
test.love
(75.76 KiB) Downloaded 86 times
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by zorg »

opex: Try your test code with 40 000 images instead of a measly 1000. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by drunken_munki »

I ran your code with a conf file setting vsync off and with the multiple draw I had 100 fps, all other modes had 500 fps. However the last mode stopped the animation from moving around.
User avatar
opex
Prole
Posts: 3
Joined: Fri Apr 28, 2017 8:45 am

Re: SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by opex »

I wrote another test, where I compare speed 400 different pngs vs 1 sprite atlas.
I saw that SpriteBatch works about 40 percent faster even on 1000 draws.
The question is resolved.

Interesting fact: there is no difference in speed when using 'stream','dynamic' or 'static' SpriteBatch
Attachments
speedtest.love
(8.44 MiB) Downloaded 92 times
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by zorg »

opex wrote: Fri Apr 28, 2017 4:07 pm Interesting fact: there is no difference in speed when using 'stream','dynamic' or 'static' SpriteBatch
Time to write another test where you actually modify sprite properities or the sprites themselves in the batches, then you may see a difference between the SpriteBatch modes. :3 (static should probably perform worse than dynamic, and dynamic worse than stream, if you modify all the sprites each frame)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: [solved] SpriteBatch vs multiple love.graphics.draw(). Why I got identical FPS?

Post by slime »

The amount of performance difference between the SpriteBatch and Mesh usage hints for a given use-case are extremely dependent on the graphics driver and hardware you use. So while there might be no difference for you on your computer, there will probably be a difference on a user's computer.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 147 guests