5
\$\begingroup\$

Is glDrawArraysInstanced in OpenGL parallel when drawing those instances?

I cannot figure out by referring to its reference and numerous online tutorials.

[Update] To be more clear, I mean, for example, we have 100 instances. Are the drawing of those 100 instances parallel? For example, we have the 100 commands "glDrawArrays(mode, first, count);"? Is execution of those 100 commands sequential or parallel?

I think it should be sequential. GPU begins to draw the second instance, only AFTER the first instance drawing finished.

Or glDrawArraysInstanced just save the time for "giving your GPU the commands"?

[Update 2] The reason I wanted to know the answer, is that: If it is sequential, personally I think drawing thousands of tree leaves in this way is time consuming. I may draw them in one shader program with some algorithm tweak.

\$\endgroup\$
  • \$\begingroup\$ What exactly do you mean by "parallel"? If the driver is running it on multiple threads? If the GPU is drawing on parallel? Either way, this will be driver implementation or hardware specific. \$\endgroup\$ – glampert Nov 4 '15 at 1:29
  • \$\begingroup\$ From your perspective, it may as well be, since it's behind the driver and should be considered to be an atomic operation, regardless of how the driver / GPU actually implements any glDrawInstanced*() call. Why do you need to know, anyway? The point is, it's very, very fast indeed. Fast enough that you may as well consider it the same as any other glDraw*() operation... also atomic. \$\endgroup\$ – Engineer Nov 4 '15 at 10:16
  • \$\begingroup\$ @ArcaneEngineer Yes, I want to know what mechanism behind it. From the reference, it is equal to a for loop, then it should be sequential, right? Sequential drawing cannot be very fast if you draw thousands of tree leaves. \$\endgroup\$ – user1914692 Nov 4 '15 at 22:19
  • \$\begingroup\$ @user1914692 You'll still haven't explained why you want to know. It may well be that your question is rendered (ha ha) moot! \$\endgroup\$ – Engineer Nov 4 '15 at 22:45
  • \$\begingroup\$ @ArcaneEngineer The reason I wanted to know, is to know if it is sequential, personally I think drawing thousands of tree leaves in this way is time consuming. I may draw them in one shader program with some algorithm tweak. \$\endgroup\$ – user1914692 Nov 5 '15 at 20:34
5
\$\begingroup\$

According to the docs, glDrawArraysInstanced has the same effect as:

if ( mode or count is invalid )
    generate appropriate error
else {
    for (int i = 0; i < primcount ; i++) {
        instanceID = i;
        glDrawArrays(mode, first, count);
    }
    instanceID = 0;
}

So it's basically a for-loop... But we need to take a step back and ask the real question, does the GPU draw things in parallel ? Roughly speaking what happens is the following:

  1. The CPU batches draw commands to the GPU command buffer.
  2. GPU starts parsing the commands at the same time with the CPU.
  3. GPU executes the draw calls by fetching vertices from buffers
  4. Data is managed in blocks so it can be processed by vertex shaders.
  5. Vertex shader is executed per vertex in parallel.

It's not only about draw calls executed together, they may be executed together if they use the same shader for example. But in the end it's about making the GPU stages as busy as possible. The GPU is parallel in nature but many factors come into play to decide if two calls will be executed in parallel.

[Edit] Regarding your update, it's possible that the GPU execute multiple render calls together and only perform what is needed to be sequential in a sequential manner. For instance nothing prevents executing the vertex shader for two draw calls in parallel and only do the blending in a sequential order.

\$\endgroup\$
  • \$\begingroup\$ Is execution of the count of commands "glDrawArrays" sequential or parallel? I think it should be sequential. GPU begins to draw the second instance before the first instance drawing finished. So it is not as fast much if we draw millions instances sequentially if my understanding is correct. \$\endgroup\$ – user1914692 Nov 4 '15 at 15:10
  • \$\begingroup\$ @user1914692 The command submission is sequential (CPU bound), the execution depends on a lot of factors... but GPU in general is parallel \$\endgroup\$ – concept3d Nov 4 '15 at 15:16
  • \$\begingroup\$ "The GPU is parallel in nature": I think it means for each commands, drawing vertexes and pixels are parallel. But if we issue different glDrawArrays commands, I think it should be sequential. After all, some of blending mode depends of sequence. \$\endgroup\$ – user1914692 Nov 4 '15 at 22:21
  • 1
    \$\begingroup\$ @user1914692 I asked a followup question computergraphics.stackexchange.com/questions/1692/… you can check it out. \$\endgroup\$ – concept3d Nov 5 '15 at 13:38
  • 1
    \$\begingroup\$ @ArcaneEngineer you might want to check that too \$\endgroup\$ – concept3d Nov 5 '15 at 13:44
1
\$\begingroup\$

After your comment... not as good an answer as concept3D's but perhaps more to the point of what you're trying to achieve in a sense.

The trade-off you face is between hardware instancing, which allows for repeated but independently moving parts of a complex geometry such as a tree, or static batching which will allow massive vertex counts without necessarily repeating geometry, but which won't allow independently moving parts in a single batch (not without re-uploading the entire, massive batch).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.