OpenGL: How to draw multiple line strips in one call?
Asked Answered
R

1

6

I want to draw multiple line strips of different length. All vertices are in one common buffer.

The order looks for example as follow:

v_1_1,v_1_2,v_1_3,v_2_1,v_2_2,v_3_1,.. for each vertex v_i_j where i is the index of the strip and j the index of the vertex in the strip.

Is there a possibility to use an index buffer to specify the begin and end indices for each strip in that buffer?

Or any other way to solve that problem?

Ringo answered 3/8, 2019 at 14:41 Comment(1)
@NicolBolas I mean line strips. Triangles don't face the problem of different vertices per primitive.Ringo
T
9

In OpenGL, draw call overhead is not that high, compared to some other APIs. The issue is the overhead of state changes between draw calls. So the primary goal in terms of optimization should be to reduce the number of state changes (particularly expensive ones) that you need between different draw calls.

But draw calls aren't completely without cost, and there's no sense in throwing away free performance, so use a primitive restart index. Basically, what you do is designate an index (typically the maximum index for the index type. 16-bit indices would use 0xFFFF) to refer, not to an index, but to the intent to restart the primitive. So in your example, you would do this:

v_1_1, v_1_2, v_1_3, 0xFFFF, v_2_1, v_2_2, 0xFFFF, v_3_1,..

You put the restart index between the strips.

There are two forms of primitive restart: user-defined indices and fixed indices. The user-defined index version allows you to specify what index represents "restart"; the fixed index always uses the maximum index.

Even though fixed-index restart requires a higher GL version (4.3 rather than 3.1), the fixed-index version is actually more commonly capable among all GPU hardware. OpenGL ES for example doesn't have the non-fixed version, and neither does Vulkan. And there's no real downside to just using the max index. So even if the implementation doesn't support fixed restart indices, you should always use the maximum index as your user-defined restart index.

Tildi answered 3/8, 2019 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.