VBOs vs. immediate mode for 2D sprites

Started by
14 comments, last by Erik Rufelt 13 years, 6 months ago
I am working on a project using C++ and SDL+OpenGL. I was rendering 2D sprites to the screen using immediate mode. However, hoping to improve rendering performance, I spent the last few days figuring out how to use VBOs. Unfortunately, now that I've got them working, I have found that they have almost no effect on performance whatsoever. In fact, the game runs slightly slower (a .02 ms/frame difference on average).

To clarify, I'm storing an array of vertices and indices in a VBO for each spritesheet. So, for example, the player will have one VBO for all of his animations.

Does this sound like I've done something wrong in implementing VBOs, or could it just be that VBOs aren't very helpful with what I'm doing?
Advertisement
I'm going to guess that it's not helping you that much, depending on how you're batching your calls.

The big draw of VBO is that it allows you to minimize data transfer and the number of API calls. When you have big meshes moving from immediate to VBO saves you from having to transmit the entire mesh each frame. Though with small quads the saving is pretty minimal.

The one thing you could do is to try to minimize your number of draw calls, this is one way you could get extra performance.

Calling draw once for each tile is pretty bad, try to put all of your stationary objects into a single buffer (or as many of them as you can that share a single spritesheet).

[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Do you mean limiting my use of glDrawElements() ?

That does get called once per tile. However, I don't see how I could call it any less, as I change things using glTranslate, etc. in between draw calls.

Is there something I am missing?
Yes, you want to reduce glDrawElements as much as possible. If every single thing on your screen is moving independently of each other than you probably can't do any better.

Is that really your case though? If there's anything in your scene that is stationary you may be able to combine them together.

For example instead of drawing a tile at (0,0), translating to the right 1, and then drawing another tile at (0,0), you can make a VBO that draws two tiles at (0,0) and (1,0). See if you can batch anything together into a single call (multiple grass tiles, trees, walls, etc). I don't have any idea what your game is like though so it might not be applicable.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Well, rats, then it probably won't help me much. I am working on the rendering code for a platformer project I have, but I am much more interested in using it for a strategy game (which will pretty much be only moving objects, heh).

So, on a slightly related topic, is this pretty much as good as it gets for a 2D game? I'd really love to be able to render tens of thousands (a couple tens of thousands, anyway) of sprites onscreen and get some kind of decent performance. Is this completely impractical? I wouldn't mind moving on to a 3D engine like Ogre or something if I could create a 2D (gameplay-wise) game with tons of units flying about.
Even if you can't reduce the number of draw calls, there's still other performance tricks you could use.

Are you minimizing your state/texture changes? (Don't set the texture for every tile, group all tiles that use the same texture and render them together).

Hows your fill rate? Could you get any benefit from sorting your objects front to back to minimize overdraw?

I'm having a hard time imagining any game that needs tens of thousands of independently moving entities on screen at the same time, could you describe more what kind of game you're envisioning? That seems pretty excessive.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I already do limit texture changes to an extent (that is something I will improve upon in the new project).

I don't believe there is anything else I could do to limit overdraw, as the tiles prevent the player (or other stuff) from going on top/behind things.

I'm picturing a really large-scale space strategy game, with tons of ships (just a few different kinds). So, very little to render aside from some backgrounds, the ships themselves, and planets or whatever. The only thing that would be large in number would be the ships, but I wanted to play around with a very large number just to see if it is fun. I can still just do that and see what happens, I just wanted to work on the rendering stuff now, since I'm working on it for something else.

Perhaps tens of thousands of ships onscreen is an overestimate. Basically, I am thinking they would be large enough to only show a reasonable number onscreen if zoomed all the way in, but I want to add the ability to zoom the camera out. Perhaps beyond a certain point, only large ships (if any) would show up, thus preventing tens of thousands of tiny ships being shown.
Another thing you may want to look into is geometry instancing. It's a relatively new extension so I don't know too much about it, but its essentially a way to stamp down hundreds of copies of a mesh in different places from one draw call. Might help you out in your spaceships example.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
That sounds intriguing, I'm going to look into that further tonight, thanks :-)
glDrawElements in itself isn't necessarily a problem. When I tested NVidias stateless drawing extension a while ago, I wrote a test that draws 12,500 objects with a separate glDrawElements for each object, and that runs at 60 FPS. Changing matrices with glTranslatef for 10k objects however can be significantly more expensive. (Though you definitely want to avoid calling it 10k times a frame, but 1k should be OK most of the time).

Just to reiterate what karwosts have said, if you have a tile-map, you can easily draw 10,000 tiles with a single glDrawElements, since these tiles don't move independently of each other. If you have 10,000 ships that move independently, then you might need more calls. Instancing can be good if each ship uses the same model and texture, but has a different translation.

There are also other ways of drawing the ships in a single draw call. Do you plan to keep your game in 2D so that each ship is a single quad?
Do you rotate the quads to rotate the ships, or is everything handled by changing the sprite texture, so that the quad can always remain a rectangle?
Also, what OpenGL version and hardware are you targeting?
And lastly, are you using shaders, or are you otherwise willing to use them?

This topic is closed to new replies.

Advertisement