Is sf::RenderWindow draw faster when you draw sprites with similar textures first

Viewed 93

For example, I have:

sf::RenderWindow window;
sf::Texture text1, text2;
sf::Sprite a, b, c, d;

All of it initialized. text1 and text2 has different files. Then I do:

a.setTexture(text1);
b.setTexture(text1);
c.setTexture(text2);
d.setTexture(text2);

Is it faster to do:

window.clear()
window.draw(a);
window.draw(b);
window.draw(c);
window.draw(d);
window.display();

than if I do:

window.clear();
window.draw(a);
window.draw(c);
window.draw(b);
window.draw(d);
window.display();

?

2 Answers

You can do some timings in your code to test if such is true - Time how long it would take to the draw 4 sprites in order a couple thousand times, then do that again test again but drawing the 4 sprites out of order. Check the difference between the two times, and this will answer your question, for YOUR computer - see below.

With this said, I'd assume the results will vary depending on your SFML library version, the OpenGL library version in use / even the GPU itself. How SFML or OpenGL handles textures could vary from version to version, so what may be fast on one version could be slow on another.

With respect to the GPU, textures may be cached(temporarily saved) inside the GPU, so after rendering a sprint for the first time, its texture will be saved and can be re-rendered much faster next time. I know this happens with 3D objects (VBOs?).

I should also note, if you want to render a new sprite and your GPU doesnt have any spare memory for the sprite texture, then it will have to flush its cache and load the new texture. This could extremely slow and decrease your games performance. However most GPU's these days have many Gigabytes of memory, so this shouldn't really be a problem.

Test your code and find out, post your results here.

Not necessarily.
But what would definitely be faster would be to draw everything that uses the same texture at once. This is done by using sf::VertexArrays instead of sprites.
In your case, you would have 2 arrays, each containing 8 vertices (4 vertices for each sprite and 1 array for each texture).

Related