Ellipsis
Ellipsis

Reputation: 63

Fasted way to many 2d textured squares (quads) in opengl 3+

For a 2d game I need to draw many textured quads each frame. They do not share any vertexes and should should be considered to have a random location that change every frame. As I am using opengl 3+ I can no longer use quads (it has been removed). I could instead use 2 triangles to make each quad but this would create a large amount of redundant reused data being passed to the GPU each frame. So I was wondering what would be the best way to achieve my goal?

I could create a geometry shader and simply pass it the quads top left point as well as it's width, height and texture position. The shader could then create the 2 triangles. However I think this would be quite slow?

Upvotes: 0

Views: 942

Answers (1)

ssube
ssube

Reputation: 48347

You have two potential methods:

  • Use a trilist (not strips). Trying to use the transforms would be very slow, and unless you have a lot of squares, the data won't be that heavy. If you're worried, you can optimize the vertex format and get it down to 8 or 16 bytes per; remember that PCIe 2.0 x16 is gigabytes per second of bandwidth, a few megs is reasonable for a particle system or the like. Indices may help depending on your workload.
  • Use one or a small batch of triangles, indexed and in a buffer, and use shaders to reposition them. This takes advantage of batching (draw 100 squares with a set of coords, update the coords in the shader, draw again) and saves memory, but you have to worry about depth (handling it in the shader) and some other details. It's not the worst thing ever, and I can't definitely say it's better or not.

You probably should not use a geometry shader. They typically aren't meant to be run every frame, although they certainly can be. You get some benefits, particularly very little memory bandwidth being used, but the hardware requirements are significantly higher and the APIs are much stricter. If your data is coming from CPU calculation that can be optimized on there, you may also end up with better performance; at best you'll have to either translate the calculations or download the data to video memory.

You absolutely do not want to calculate two triangles from the geometry shader, unless you mean two per square. The main benefit of shaders is parallelism, so you'd want to run through as many squares as possible and draw those in large batches. Batching will almost always give you a performance boost, and there are a variety of ways to do it. Things like changing the transforms in the API are terribly slow and best avoided (probably not much of an issue if you're on 3.0+, but it used to be a huge one).

Upvotes: 2

Related Questions