Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Recreating Vertex Arrays vs. Adjusting Vertex Position  (Read 4995 times)

0 Members and 1 Guest are viewing this topic.

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Recreating Vertex Arrays vs. Adjusting Vertex Position
« on: December 09, 2013, 02:57:55 am »
When using vertex arrays to draw sprites, is it better to clear the vertex array at the beginning of every frame and then add all the objects again, or to give every object it's own vertex pointer and let it change the position of that quad every frame?

I recently stopped using sf::Sprites and drawing them onto panels every frame in favor of using a vertex array. After I did this I actually saw a drop in performance and I'm trying to figure out why.
« Last Edit: December 09, 2013, 02:59:53 am by zachprinz »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #1 on: December 09, 2013, 07:02:14 am »
It really depends what you're foing. If you have to change all vertices anyways you can easily recreate the wohle thing. Just make sure that the memory allocation stays the same.

You could write a minimal and complete example that serms to perform bad, maybe we can find an issue.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #2 on: December 09, 2013, 08:19:50 am »
My project is quite long, If need be I'll go through and try to write out a minimal example. For now though, here is the basic steps I go through in the sprite batching process. Please let me know if anything seems terribly off.

gameArray is the sf::VertexArray
quadCount is an int that keeps track of the amount of vertices already set up in gameArray
gameRenderStates is the sf::RenderStates that carries the info for gameArray

Quote
SetUp
-quadCount = 0;
-gameArray.setPrimitiveType(sf::Quads);
-gameArray.resize(2000);

Go through and draw sprite
-gameArray[quadCount].position = position;
-gameArray[quadCount++].texCoords = textureCoordinates;
-(repeat for each corner)

Draw the Vertex Array to the Window
-window.draw(gameArray,gameRenderStates);
-gameArray.clear();
-quadCount = 0;
-gameArray.resize(2000);
« Last Edit: December 09, 2013, 08:21:35 am by zachprinz »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #3 on: December 09, 2013, 08:28:22 am »
Quote
-window.draw(gameArray,gameRenderStates);
-gameArray.clear();
-quadCount = 0;
-gameArray.resize(2000);
Calling clear() on the vertex array (which internally uses a std::vector) won't deallocate memory, thus you don't have to call resize again. It might very well be that the resize() call is slowing down things.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #4 on: December 09, 2013, 08:56:04 am »
Quote
-window.draw(gameArray,gameRenderStates);
-gameArray.clear();
-quadCount = 0;
-gameArray.resize(2000);
Calling clear() on the vertex array (which internally uses a std::vector) won't deallocate memory, thus you don't have to call resize again. It might very well be that the resize() call is slowing down things.

Should I be using gameArray.append() then?

When I remove the resize call I get an out of range error and in Visual Studios Autos I can see that gameArray has a size of 0 and a capacity of 2000.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #5 on: December 09, 2013, 09:07:26 am »
Should I be using gameArray.append() then?
Yes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #6 on: December 09, 2013, 09:12:00 am »
Should I be using gameArray.append() then?
Yes.

Unfortunately that has no effect on performance.

How large (ballpark) of a performance increase should I see when switching from drawing individual sprites to sprite batching?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #7 on: December 09, 2013, 09:42:08 am »
Well it always depends on the task at hand. A modern GPU can easily handle 2000 draw calls per frame reducing it one draw call should however still give you some better performance.
But again it's impossible to tell without the source. How did you measure performance?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Recreating Vertex Arrays vs. Adjusting Vertex Position
« Reply #8 on: December 09, 2013, 09:48:51 am »
Well it always depends on the task at hand. A modern GPU can easily handle 2000 draw calls per frame reducing it one draw call should however still give you some better performance.
But again it's impossible to tell without the source. How did you measure performance?

I measured it using the FPS viewer of fraps. Which I know isn't the best way. I'm going to experiment some more and hope I can find something. Any ideas would be very welcome though.

The game is using barely any of the CPU, and nearly all of the graphics card. But I would guess this is par for the course.
« Last Edit: December 09, 2013, 09:52:36 am by zachprinz »