<p>The rule of thumb is not about the number of instances; it's about the size of each instance.</p>
<p><a href="https://community.khronos.org/t/gpu-vertex-dispatch-for-multidrawindirect-and-or-instanced-draw-calls/77308/2" rel="nofollow noreferrer">You don't want instances that are too small</a>. You want instances that have a good number of vertices to them. In the hundreds to low thousands range. Quads only have 4 vertices, so they are a particularly bad choice for instancing.</p>
<p>The benefit of instancing quads is having less vertex data to read and upload. However, there are ways of achieving these benefits without actually having to use instancing.</p>
<p>Using indexed rendering means that you can use 4 positions instead of six. However, you also need an index buffer. Fortunately, you can <a href="https://stackoverflow.com/a/59707766/734069">use the same index buffer for each draw call</a>, as the <em>topology</em> of a list of quads is the same regardless of their vertices. This buffer can use 16-bits indices for improved performance, and you can use multidraw rendering alongside the base vertex to render more than 16K quads from a single draw call.</p>
<p>For the other per-quad attributes, you can access an SSBO (or UBO if you don't have that much per-quad data) given each quad's index. To compute a quad's index, you need to use <code>gl_VertexIndex</code>, which gives you the index of the vertex + its base vertex offset. <code>gl_VertexIndex / 4</code> is the index of that quad, which you can then use to fetch data from your SSBO.</p>
<p>Technically, you could do this for the positions too. You would use no vertex buffers at all; using an index buffer as above to compute the quad index. And you would compute the positions of each vertex in the quad using <code>gl_VertexIndex % 4</code> to figure out which vertex that VS invocation needs to compute.</p>
<p>However, it is difficult to ensure continuity if two quads share a common edge (this can be done via using fixed-point integer math in the scaling and offsetting). <a href="https://community.khronos.org/t/a-review-of-a-sprite-drawing-strategy/108534/11" rel="nofollow noreferrer">But if you're rendering a particle system, continuity is not all that important, so this is also an option</a>. If continuity is important, you should just use 4 positions per-quad and use per-quad indices from SSBOs for the rest.</p>

CC BY-SA 4.0
2022-10-30T15:35:02.913Z
2022-10-30T15:53:41.410Z
2022-10-30T15:53:41.410Z
Nicol Bolas
<p>So let's say I want to draw <em>a lot</em> of textured quads to the framebuffer.</p>
<p>In each shader invocation, I will bind an array of indicies, an array of positions, and an array of textures to index into.  Then I will draw instanced.</p>
<p>Is there any rule of thumb for how many instances I can or should draw in a single call?  For instance, should I draw tens or hundreds or thousands of instances at once, keeping in mind the number of instances will also determine the size of the arrays bound to my shader.</p>

<shader><glsl><vulkan>
Is there a good rule of thumb for the optimal / maximum number of instances to draw in a single call?
sak
# Is there a good rule of thumb for the optimal / maximum number of instances to draw in a single call?

(Asked by: username_0 on 2022-10-30)

So let's say I want to draw *a lot* of textured quads to the framebuffer.

In each shader invocation, I will bind an array of indicies, an array of positions, and an array of textures to index into. Then I will draw instanced.

Is there any rule of thumb for how many instances I can or should draw in a single call? For instance, should I draw tens or hundreds or thousands of instances at once, keeping in mind the number of instances will also determine the size of the arrays bound to my shader.

**Tags:** shader, glsl, vulkan


---

## 1 answers

### Answer by username_1 on 2022-10-30. Score: 3 | Accepted answer

The rule of thumb is not about the number of instances; it's about the size of each instance.

[You don't want instances that are too small](https://community.khronos.org/t/gpu-vertex-dispatch-for-multidrawindirect-and-or-instanced-draw-calls/77308/2). You want instances that have a good number of vertices to them. In the hundreds to low thousands range. Quads only have 4 vertices, so they are a particularly bad choice for instancing.

The benefit of instancing quads is having less vertex data to read and upload. However, there are ways of achieving these benefits without actually having to use instancing.

Using indexed rendering means that you can use 4 positions instead of six. However, you also need an index buffer. Fortunately, you can [use the same index buffer for each draw call](https://stackoverflow.com/a/59707766/734069), as the *topology* of a list of quads is the same regardless of their vertices. This buffer can use 16-bits indices for improved performance, and you can use multidraw rendering alongside the base vertex to render more than 16K quads from a single draw call.

For the other per-quad attributes, you can access an SSBO (or UBO if you don't have that much per-quad data) given each quad's index. To compute a quad's index, you need to use `gl_VertexIndex`, which gives you the index of the vertex + its base vertex offset. `gl_VertexIndex / 4` is the index of that quad, which you can then use to fetch data from your SSBO.

Technically, you could do this for the positions too. You would use no vertex buffers at all; using an index buffer as above to compute the quad index. And you would compute the positions of each vertex in the quad using `gl_VertexIndex % 4` to figure out which vertex that VS invocation needs to compute.

However, it is difficult to ensure continuity if two quads share a common edge (this can be done via using fixed-point integer math in the scaling and offsetting). [But if you're rendering a particle system, continuity is not all that important, so this is also an option](https://community.khronos.org/t/a-review-of-a-sprite-drawing-strategy/108534/11). If continuity is important, you should just use 4 positions per-quad and use per-quad indices from SSBOs for the rest.


---

