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

Author Topic: Better FPS drawing 10.000 sprite  (Read 8899 times)

0 Members and 1 Guest are viewing this topic.

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Better FPS drawing 10.000 sprite
« on: March 09, 2012, 03:06:00 pm »
I'm testing this code to lern how can i get more FPS. Running it in Release without debugging mode:
Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
sf::Clock clock;
sf::Texture prova;
prova.LoadFromFile("icon.png");
std::vector<sf::Sprite> test;
for (int i=0; i<10000; ++i){
test.push_back(sf::Sprite(prova));
test[i].SetPosition(0.1*i,0.05*i);
}
sf::RenderWindow App(sf::VideoMode(1024,768,32),"Test Test",sf::Style::Close);
App.SetFramerateLimit(300);
sf::RenderTexture renderTest;
clock.Restart();
renderTest.Create(1024,768);
std::cout << clock.GetElapsedTime().AsMilliseconds() << std::endl;
while(App.IsOpen()){
sf::Event events;
while(App.PollEvent(events)){
if (events.Type == sf::Event::KeyPressed && events.Key.Code == sf::Keyboard::Escape)
App.Close();
}
renderTest.Clear(sf::Color::Blue);
clock.Restart();
for (unsigned int i=0; i<test.size(); ++i)
renderTest.Draw(test[i]);
std::cout << clock.GetElapsedTime().AsMilliseconds()<<std::endl;
renderTest.Display();

App.Clear();

// Draw the texture
sf::Sprite sprite(renderTest.GetTexture());
App.Draw(sprite);
App.Display();
}
return EXIT_SUCCESS;
}

Using playclaw/fraps i get 14~19 FPS to draw all item and less than 50ms to draw all 10.000 item into renderTest.
How can i get more FPS drawing all this 10.000 sprites?

luiscubal

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #1 on: March 09, 2012, 03:27:24 pm »
For starters, console IO does lower the performance.
Perhaps it would be better to only show the FPS every few frames(sum the total times of every 20 frames, and then divide by 20 when showing).

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Better FPS drawing 10.000 sprite
« Reply #2 on: March 10, 2012, 07:12:26 am »
Fraps will lower your fps to hell.. Make your own fps class.. its very easy.. there is an example on the wiki
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #3 on: March 10, 2012, 10:28:13 am »
Quote from: "mateandmetal"
Fraps will lower your fps to hell.. Make your own fps class.. its very easy.. there is an example on the wiki

Ok i will and we will see how much FPS i do with 10.000 sprite drawing :lol:
Some1 can explain me how can i use VertexArray to draw all these 10.000 sprites?

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #4 on: March 10, 2012, 11:23:13 am »
i want to hear why are you doing this test. this sounds completely useless. what are going to do with the result when you finally measure your fps?

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #5 on: March 10, 2012, 11:28:23 am »
Quote from: "dydya-stepa"
i want to hear why are you doing this test. this sounds completely useless. what are going to do with the result when you finally measure your fps?


I was thinking to make a game with 10.000+ object to draw/update! And i want to see how can i manage this as well as i can!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Better FPS drawing 10.000 sprite
« Reply #6 on: March 10, 2012, 05:41:45 pm »
Quote
Fraps will lower your fps to hell.

Hm? It's very lightweight, it has no impact on the performances.

Quote
I was thinking to make a game with 10.000+ object to draw/update! And i want to see how can i manage this as well as i can!

It's not enough to know how to manage them in the most efficient way. It really depends on what you'll do with these objects, there's no generic answer.
If they are static and all share the same texture, then put them all in one big vertex array and that's it.
Otherwise, you'll have to tell us more details.
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #7 on: March 10, 2012, 08:01:52 pm »
Quote from: "Laurent"
Quote
Fraps will lower your fps to hell.

Hm? It's very lightweight, it has no impact on the performances.

Quote
I was thinking to make a game with 10.000+ object to draw/update! And i want to see how can i manage this as well as i can!

It's not enough to know how to manage them in the most efficient way. It really depends on what you'll do with these objects, there's no generic answer.
If they are static and all share the same texture, then put them all in one big vertex array and that's it.
Otherwise, you'll have to tell us more details.


I was thinking to make a city in 2d! So houses, npcs, objects, enemies, all sprite to write and update.
Maybe for enemies and npcs i can use the same texture! House maybe will be different! But this is still an idea of what i want to make. Maybe i will open a new thread when i'm ready to start this project!

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #8 on: March 10, 2012, 08:06:16 pm »
Quote from: "TheEnigmist"
I was thinking to make a city in 2d! So houses, npcs, objects, enemies, all sprite to write and update.
Maybe for enemies and npcs i can use the same texture! House maybe will be different! But this is still an idea of what i want to make. Maybe i will open a new thread when i'm ready to start this project!


But you won't draw the whole city at once on the scree, will you? :o

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #9 on: March 11, 2012, 11:49:39 am »
Quote from: "Mjonir"
Quote from: "TheEnigmist"
I was thinking to make a city in 2d! So houses, npcs, objects, enemies, all sprite to write and update.
Maybe for enemies and npcs i can use the same texture! House maybe will be different! But this is still an idea of what i want to make. Maybe i will open a new thread when i'm ready to start this project!


But you won't draw the whole city at once on the scree, will you? :o

Uhm i think no, i wont :O A good way is to draw only object in view+little offset, isn't it?

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #10 on: March 11, 2012, 01:55:06 pm »
Yep. Also depending on what you want to do, you don't have to simulate de whole city either. If you can, separate it block by block and just simulate/draw the visible blocks and possibly its surroundings.

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #11 on: March 11, 2012, 02:10:19 pm »
Quote from: "Mjonir"
Yep. Also depending on what you want to do, you don't have to simulate de whole city either. If you can, separate it block by block and just simulate/draw the visible blocks and possibly its surroundings.


Updating the city is faster and doesn't affect game's FPS?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Better FPS drawing 10.000 sprite
« Reply #12 on: March 11, 2012, 03:08:18 pm »
Look the problem will most probably(Haven't tested especially for SFML but for 3D this is valid) be the fill rate of the GPU and the driver calls(draw calls). Any overdraw of pixels that have already been painted on is a waste and GPU's rasterizers are pretty slow for modern graphics. The vertex assembler, shader, etc. etc. is insanely fast and you must reach several millions vertexes before it becomes a problem. Though the rasterizer is starting to have a problem as soon as you reach the pixel amount of the max resolution. Some GPU's can't even handle the max resolution.(mine for instance can't, it just about manages)

So if you want to draw ~10 000 sprites on screen you will face two bottlenecks. The draw calls for each sprite and the pixel overdraw.

So what are your options? Well since SFML doesn't have a depth buffer you can't do a front to back render to remove the pixel overdraw so your stuck with that. What you can do is render static environment to in-memory textures and render those only once and such remove the overdraw from there. And then we have the draw calls. First step is to cull and only render stuff that is shown on the screen. But that won't be enough because you most probably will still be showing a lot of sprites at once on the screen. The solution here is Instancing but is only supported on later hardware(OpenGL 3+) but kind Almighty Laurent have provided us with an alternative ;) Vertex arrays. If possible you can link the sprites together into one vertex array and render them with one draw call.

These things might be seen as pretty advanced but when it comes to optimizing the GPU it becomes pretty advanced ^^
Summary is more or less, limit your draw calls and the amount of pixels you render and your performance will go up.

If needed I can give the research and data and explanation for why these things are valid ^^
Though thumb of rule, never trust anything until you've tested it yourself. Always benchmark and profile everything when you are optimizing.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Better FPS drawing 10.000 sprite
« Reply #13 on: March 11, 2012, 03:26:16 pm »
Quote from: "TheEnigmist"
Updating the city is faster and doesn't affect game's FPS?


It depends on what you simulate. And even if your display is very fast, it might still cause problems if you can only simulate once out of ten display frames for example.

It really depends on what you're doing though but for my own project (which is roughly about displaying between 500 and 1000 objects on screen, each having its own complex logic), I'm pretty sure the bottleneck is in the simulation, not drawing.

Quote from: "Groogy"
Though thumb of rule, never trust anything until you've tested it yourself. Always benchmark and profile everything when you are optimizing.


On that subject, which tools do you use for profiling?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Better FPS drawing 10.000 sprite
« Reply #14 on: March 11, 2012, 04:22:37 pm »
Quote from: "Mjonir"
Quote from: "Groogy"
Though thumb of rule, never trust anything until you've tested it yourself. Always benchmark and profile everything when you are optimizing.


On that subject, which tools do you use for profiling?


For CPU we have my own profiling library, Intel VTune Performance Analyzer and for GPU I use Nvidia Parallell Nsight and Pix. I don't use VTune anymore since my profiling library replaces it without the gigantic overhead Vtune adds but for really hard tracked bottlenecks(I have NO idea where they are more or less) I still use Vtune.

And no I can''t give out the library as it's closed source and not owned by me. I can however answer questions and write code-snippets based on the library. More or less I am allowed to educate someone in how it works and show example code.

A quick way to see if you are GPU/Graphics bound or CPU/Logic bound is to check your CPU usage and compare to your framerate. So open up the task manager, if you are having low FPS and your CPU usage is low then something is wrong on the GPU and if you have high usage and low FPS then something is wrong on the CPU. That's probably the simplest test.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio