<p>In OpenGL ES there is Instancing which provides allows for rendering one object multiple times. </p>

<p>When using Instancing, you can use uniform arrays to provide different information, e.g. a transformation matrix, for each of the particles. In the shader you can then use <code>gl_InstanceID</code> to distinguish between the individual particles and pick the appropriate index from the uniform array.</p>

<pre><code>attribute vec4 position;
uniform mat4 transformations[16];
// other attributes, uniforms, etc
void main() {
    // ...
    gl_Position = ... * transformations[gl_InstanceID] * position;
}
</code></pre>

<p>And you need to draw your objects using <code>glDrawElementsInstanced</code> which takes the number of objects as an additional parameter (compared to <code>glDrawElements</code>).</p>

<hr />

<p>Note: In OpenGL ES 2, you need to use <code>gl_InstanceIDEXT</code> and <code>glDrawElementsInstancedEXT</code> as it is an <a href="https://www.khronos.org/registry/gles/extensions/EXT/draw_instanced.txt" rel="nofollow">extension</a> there. Also you have to enable it in the shader using</p>

<pre><code>#extension GL_EXT_draw_instanced : enable
</code></pre>

CC BY-SA 3.0
2015-08-16T16:26:37.870Z
2015-08-16T16:26:37.870Z
Nero
<p>There are many, many ways to draw things in OpenGL, so this is naturally confusing sometimes. The first method you describe, setting the shader parameters and issuing one draw call per object is usually the most inefficient, due to the high API overhead. The second one, using instanced drawing is a much smarter approach for objects with the same parameters.</p>

<p>When it comes to particles, specifically, there are two other methods which I'm aware of and have tested: </p>

<ul>
<li><p>The first one, more traditional and easier to implement, is to generate a unique quadrilateral for each particle in the application code. Then use one of the <a href="https://www.opengl.org/wiki/Buffer_Object_Streaming">several optimized buffer streaming</a> paths of OpenGL to upload this data and issue a single draw call. This is the most straightforward method and provides good results. It will involve very few API calls if you can map the vertex/index buffers (<code>glMapBuffer/MapBufferRange</code>).</p></li>
<li><p>Another method is to move the whole thing to a shader program, using <a href="https://www.opengl.org/wiki/Transform_Feedback">Transform Feedback</a>. This method is a little more complicated to get up and running, but you can find a lot of references on the subject, <a href="http://ogldev.atspace.co.uk/www/tutorial28/tutorial28.html">such as this tutorial</a>. This one should be the optimal path, since it moves the whole simulation to the GPU.</p></li>
</ul>

<p>Those are some of the optimized ways of rendering particle effects. But OpenGL provides several other rendering paths that are better for different cases, one of such is <a href="https://www.opengl.org/wiki/Vertex_Rendering#Indirect_rendering">indirect draw</a>, which isn't available on ES at the moment, but is probably one of the fastest drawing paths available on modern PC OpenGL. Transform Feedback also requires a geometry shader, so it is not available for current OpenGL-ES.</p>

<p>For more on the optimized rendering paths of OpenGL, I recommend watching this very good presentation by NVidia: <a href="http://gdcvault.com/play/1020791/">Approaching Zero Driver Overhead in OpenGL</a>. At the end of the talk, they show a very interesting benchmark of several methods and how they compare with the Direct3D equivalents.</p>

CC BY-SA 3.0
2015-08-16T18:37:46.270Z
2015-08-16T18:37:46.270Z
glampert
<p>Especially when rendering particle effects, the same object needs to be rendered several times with slightly modified properties. But these changes are often limited to properties like pose or textures, and are not in the geometry of the object itself. </p>

<p>A different pose (translation, rotation) is merely a matrix multiplication with the vertices, and a different texture with an applied texture atlas are merely different texture coordinates.</p>

<p>Nevertheless each of the particles needs to be drawn and I currently do that by calling <code>glDrawElements</code> for every particle, while setting uniforms appropriately. Until now this is sufficient for rendering the scene in real-time, but for more complex scenes or simply for more particles emitted by the particle system this could easily lead to dropping frame rates.</p>

<p>So, is there a way to reduce the number of draw calls for very similar objects? </p>

CC BY-SA 3.0
2015-08-16T16:28:04.357Z
I know there is Instancing in OpenGL, that's why I also posted an answer here. But maybe there is also some other way to achieve the same result.
Nero
CC BY-SA 3.0
2015-08-16T21:17:37.130Z
Seems you were right... :)
trichoplax is on Codidact now
<opengl><performance><real-time><mobile><opengl-es>
How to reduce the number of draw calls when rendering one object multiple times?
Nero
# How to reduce the number of draw calls when rendering one object multiple times?

(Asked by: username_0 on 2015-08-16)

Especially when rendering particle effects, the same object needs to be rendered several times with slightly modified properties. But these changes are often limited to properties like pose or textures, and are not in the geometry of the object itself.

A different pose (translation, rotation) is merely a matrix multiplication with the vertices, and a different texture with an applied texture atlas are merely different texture coordinates.

Nevertheless each of the particles needs to be drawn and I currently do that by calling `glDrawElements` for every particle, while setting uniforms appropriately. Until now this is sufficient for rendering the scene in real-time, but for more complex scenes or simply for more particles emitted by the particle system this could easily lead to dropping frame rates.

So, is there a way to reduce the number of draw calls for very similar objects?

**Tags:** opengl, performance, real-time, mobile, opengl-es

> **Comments:**
> **username_0**: I know there is Instancing in OpenGL, that's why I also posted an answer here. But maybe there is also some other way to achieve the same result.  
> **username_1**: Seems you were right... :)  

---

## 2 answers

### Answer by username_2 on 2015-08-16. Score: 8 | Accepted answer

There are many, many ways to draw things in OpenGL, so this is naturally confusing sometimes. The first method you describe, setting the shader parameters and issuing one draw call per object is usually the most inefficient, due to the high API overhead. The second one, using instanced drawing is a much smarter approach for objects with the same parameters.

When it comes to particles, specifically, there are two other methods which I'm aware of and have tested:

* The first one, more traditional and easier to implement, is to generate a unique quadrilateral for each particle in the application code. Then use one of the [several optimized buffer streaming](https://www.opengl.org/wiki/Buffer_Object_Streaming) paths of OpenGL to upload this data and issue a single draw call. This is the most straightforward method and provides good results. It will involve very few API calls if you can map the vertex/index buffers (`glMapBuffer/MapBufferRange`).
* Another method is to move the whole thing to a shader program, using [Transform Feedback](https://www.opengl.org/wiki/Transform_Feedback). This method is a little more complicated to get up and running, but you can find a lot of references on the subject, [such as this tutorial](http://ogldev.atspace.co.uk/www/tutorial28/tutorial28.html). This one should be the optimal path, since it moves the whole simulation to the GPU.

Those are some of the optimized ways of rendering particle effects. But OpenGL provides several other rendering paths that are better for different cases, one of such is [indirect draw](https://www.opengl.org/wiki/Vertex_Rendering#Indirect_rendering), which isn't available on ES at the moment, but is probably one of the fastest drawing paths available on modern PC OpenGL. Transform Feedback also requires a geometry shader, so it is not available for current OpenGL-ES.

For more on the optimized rendering paths of OpenGL, I recommend watching this very good presentation by NVidia: [Approaching Zero Driver Overhead in OpenGL](http://gdcvault.com/play/1020791/). At the end of the talk, they show a very interesting benchmark of several methods and how they compare with the Direct3D equivalents.


---

### Answer by username_0 on 2015-08-16. Score: 4

In OpenGL ES there is Instancing which provides allows for rendering one object multiple times.

When using Instancing, you can use uniform arrays to provide different information, e.g. a transformation matrix, for each of the particles. In the shader you can then use `gl_InstanceID` to distinguish between the individual particles and pick the appropriate index from the uniform array.

```
attribute vec4 position;
uniform mat4 transformations[16];
// other attributes, uniforms, etc
void main() {
    // ...
    gl_Position = ... * transformations[gl_InstanceID] * position;
}

```

And you need to draw your objects using `glDrawElementsInstanced` which takes the number of objects as an additional parameter (compared to `glDrawElements`).

---

Note: In OpenGL ES 2, you need to use `gl_InstanceIDEXT` and `glDrawElementsInstancedEXT` as it is an [extension](https://www.khronos.org/registry/gles/extensions/EXT/draw_instanced.txt) there. Also you have to enable it in the shader using

```
#extension GL_EXT_draw_instanced : enable

```


---

