Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 701
Matthias Avatar asked Aug 03 '19 14:08

Matthias


1 Answers

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.

like image 158
Nicol Bolas Avatar answered Oct 31 '22 10:10

Nicol Bolas