# Help Understanding Batch?

**URL:** <https://discourse.libcinder.org/t/help-understanding-batch/92>\
**Category:** Using Cinder\
**Created:** [June 16, 2016, 1:20am UTC](https://discourse.libcinder.org/t/help-understanding-batch/92 "2016-06-16T01:20:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [June 16, 2016, 1:20am UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/1 "2016-06-16T01:20:09Z")

</div>

Hey there. I’ve worked through the ‘OpenGL in Cinder’ guide and I’m looking to optimize some existing code with some of the techniques mentioned in the guide. Just to be clear up front - I’m only currently working in 2D.

In this basic setup, I’m loading an SVG and saving it as a `DocRef`. I’m drawing the SVG shape multiple times each frame, at different locations and with different colors. Currently I’ve achieved this by simple doing my translation and setting the color, then calling `gl::draw(mSvgDoc->getShapeAbsolute())` for each time I want to draw the shape.

I have adjusted my code and instead of saving the `DocRef`, I’m saving a `BatchRef` that I’ve constructed using a `VboMeshRef` created with `gl::VboMesh::create(Triangulator(mLogoSvgDoc->getShapeAbsolute(), 1.0f).calcMesh())`. Now I draw the SVG using `mSvgBatch->draw()`.

Everything is working the same way as before and it looks exactly the same. I’m just trying to understand if / why this is a more efficient way of drawing the same thing several times. The examples in the guide were all in 3D space, does this even make a difference for my project in 2D space only? Is it actually drawing them together in a batch at the same time, where my old version had a draw call for every time, this only has a single draw call that draws them all?

Thanks for any tips, much appreciated! Loving the new forum!

- Max.

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [June 16, 2016, 3:41am UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/2 "2016-06-16T03:41:54Z")

</div>

The easiest way of thinking about it (in this specific case), is that `gl::draw()` is, for all intents and purposes, doing what your second bit of code does, but every frame. This means that all the vertex information has be to constantly discarded and re-sent to the GPU even though it isn’t changing. By using a VBO, you’re caching the geometry on the GPU and just asking for it to be redrawn.

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [June 16, 2016, 11:31am UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/3 "2016-06-16T11:31:40Z")

</div>

> …but every frame.

It’s even worse: it’s sending the geometry **every time you draw the SVG** , which can be multiple times per frame.

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [June 16, 2016, 3:43pm UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/4 "2016-06-16T15:43:59Z")

</div>

Thanks guys! Yeah, I have my setup to draw the same SVG anywhere from 1 to 100 times per frame, so it seems like this is a more efficient way to go.

That definitely helps, appreciate it!

---

<div class="post-metadata">

**Author:** ![mike](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mike/32/56_2.png) [@mike](https://discourse.libcinder.org/u/mike)\
**Post date:** [June 16, 2016, 3:49pm UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/5 "2016-06-16T15:49:28Z")

</div>

Depending on your specific needs, you can also look into instanced drawing (gl::Batch has a drawInstanced member function), which is even more efficient as you’re only submitting one draw call to the GPU.

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [June 16, 2016, 3:51pm UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/6 "2016-06-16T15:51:27Z")

</div>

What is `drawInstanced` all about? I’m doing some model matrix translation and setting a new color for each time I draw the shape, is that still possible with `drawInstanced`?

---

<div class="post-metadata">

**Author:** ![mike](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mike/32/56_2.png) [@mike](https://discourse.libcinder.org/u/mike)\
**Post date:** [June 16, 2016, 4:02pm UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/7 "2016-06-16T16:02:12Z")

</div>

Instancing is basically a way to draw the same geometry multiple times with a single draw call (in OpenGL this is glDrawArraysInstanced, though there are a few variations). As you hinted at, there are indeed ways to set up vertex attributes (i.e. transformations like scale, rotation, and translation, color, etc.) that vary per instance.

There’s a really good example of this in the samples folder called “Earthquake.” Check out the constructor for the Earth class, where he sets up per-instance transformation matrices.

It does require a bit more setup, so if simply drawing your Batch a bunch of times in a for-loop works, I wouldn’t worry about it… just wanted to throw the idea out there.

---

<div class="post-metadata">

**Author:** ![mst1228](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mst1228/32/77_2.png) [@mst1228](https://discourse.libcinder.org/u/mst1228)\
**Post date:** [June 16, 2016, 4:03pm UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/8 "2016-06-16T16:03:31Z")

</div>

Cool, I’ll work through that example and see what else I can pick up. If not for this project, perhaps something in the future.

Thanks again!

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [June 16, 2016, 11:31pm UTC](https://discourse.libcinder.org/t/help-understanding-batch/92/9 "2016-06-16T23:31:13Z")

</div>

The sample \_opengl/InstancedTeapots is a good place to start.
