<p>Consider using</p>
<pre><code>glDrawElementsInstanced(blablabla, base_instance). 
</code></pre>
<p>Put all the model matrix in an array (in an SSBO or a Uniform buffer) GLSL will provide a gl_InstanceIndex variable to the shader that will start at the base_instance value you provide to the draw elements function.</p>
<pre><code>mat4 model = ssbo_model_mats[gl_instanceIndex];
</code></pre>
<p>After each instance the value will be incremented automatically. The biggest challenge becomes grouping the object model matrices so they are lined up all nice and neat for indexing the model matrix.</p>
<p>A natural extension of this is to run a compute shader that can update the matrices for physics, rotation, collision, whatever, then rearrange them for drawing those visible with one or two draw calls.</p>
<p>There is even an indirect version:</p>
<pre><code>glDrawElementsIndirect(...); // and several others
</code></pre>
<p>that allows the buffer the data is sitting in to be specified so that virtually the entire draw call can be setup on the GPU using compute shaders.</p>
<p>Combined with &quot;bindless&quot; rendering techniques this can be a very powerful method.</p>

CC BY-SA 4.0
2022-05-18T21:27:28.680Z
2022-05-18T21:35:42.817Z
2022-05-18T21:35:42.817Z
pmw1234
<p>Just realised I can apply the necessary transformations before uploading the vertex positions to the GPU.</p>
<p>Thanks for being a rubber duck!</p>

CC BY-SA 4.0
2024-11-12T04:41:48.560Z
Hey can you reflect on this approach now? Eg. Did it scale? Did you change methods later on? I'm also working on a similar problem and consdering using UBO's. Also @pmw1234's method doesn't work for opengl3.3 which is what I'm targeting.
cuppajoeman
CC BY-SA 4.0
2022-05-17T11:15:05.453Z
2022-05-17T11:15:05.453Z
junglie85
<p>Where should the model matrix be uploaded to the GPU in a batch renderer?</p>
<p>All the tutorials I've ever seen use a uniform for this, but that's not with batch rendering. Since the uniform is only constant for a single draw call, it seems to me that this should be a vertex attribute. But then there will be a lot of duplication of data (once for each vertex).</p>
<p>How can one batch draw calls with different model matrices without excess duplication?</p>

<shader><uniform-buffer-object>
Model matrix for batch renderer as uniform or vertex attribute?
junglie85
# Model matrix for batch renderer as uniform or vertex attribute?

(Asked by: username_0 on 2022-05-17)

Where should the model matrix be uploaded to the GPU in a batch renderer?

All the tutorials I've ever seen use a uniform for this, but that's not with batch rendering. Since the uniform is only constant for a single draw call, it seems to me that this should be a vertex attribute. But then there will be a lot of duplication of data (once for each vertex).

How can one batch draw calls with different model matrices without excess duplication?

**Tags:** shader, uniform-buffer-object


---

## 2 answers

### Answer by username_1 on 2022-05-18. Score: 1

Consider using

```
glDrawElementsInstanced(blablabla, base_instance). 

```

Put all the model matrix in an array (in an SSBO or a Uniform buffer) GLSL will provide a gl\_InstanceIndex variable to the shader that will start at the base\_instance value you provide to the draw elements function.

```
mat4 model = ssbo_model_mats[gl_instanceIndex];

```

After each instance the value will be incremented automatically. The biggest challenge becomes grouping the object model matrices so they are lined up all nice and neat for indexing the model matrix.

A natural extension of this is to run a compute shader that can update the matrices for physics, rotation, collision, whatever, then rearrange them for drawing those visible with one or two draw calls.

There is even an indirect version:

```
glDrawElementsIndirect(...); // and several others

```

that allows the buffer the data is sitting in to be specified so that virtually the entire draw call can be setup on the GPU using compute shaders.

Combined with "bindless" rendering techniques this can be a very powerful method.


---

### Answer by username_0 on 2022-05-17. Score: 0

Just realised I can apply the necessary transformations before uploading the vertex positions to the GPU.

Thanks for being a rubber duck!

> **Comments:**
> **username_2**: Hey can you reflect on this approach now? Eg. Did it scale? Did you change methods later on? I'm also working on a similar problem and consdering using UBO's. Also @username_1's method doesn't work for opengl3.3 which is what I'm targeting.  

---

