<p>The problem was my class handling the population fo the <code>draw indirect commands</code>.</p>

<p>Each time I added a new command to the indirect buffer I also incremented the <code>baseInstance</code>, so the cube had <code>baseInstance = 0</code> and the sphere had <code>baseInstance = 1</code>.</p>

<p>The reason why it doesn't render the sphere is because <code>glMultiDrawArraysIndirect</code> pretty much evaluates as <code>glDrawArraysInstancedBaseInstance(mode,  cmd-&gt;first,  cmd-&gt;count,  cmd-&gt;instanceCount, cmd-&gt;baseInstance);</code> (as per the <a href="https://www.opengl.org/wiki/GLAPI/glMultiDrawArraysIndirect" rel="nofollow">OGL wiki</a>).</p>

<p>The drawID is calculated as <code>(gl_InstanceID / divisor) +</code><strong><code>baseInstance</code></strong> (<a href="https://www.opengl.org/wiki/GLAPI/glDrawArraysInstancedBaseInstance" rel="nofollow">link</a>), which means that the drawID is out of range when trying to render with an offset, as the sphere now appears as the first element in the indirect buffer.</p>

CC BY-SA 3.0
2015-10-26T16:24:09.740Z
2015-10-26T16:24:09.740Z
Soapy
<p>I'm trying to use <code>glMultiDrawArraysIndirect</code> to render a batch of object.</p>

<p>In my example I have a cube and a sphere <strong>(in this order)</strong> in a <code>VBO</code> and a <code>GL_SHADER_STORAGE_BUFFER (SSBO)</code> that contains matrix transforms as well as a <code>GL_DRAW_INDIRECT_BUFFER (DIB)</code> that contains the draw commands.</p>

<p>The allocation of both the <code>SSBO</code> and <code>DIB</code> are managed by a custom class.</p>

<p>I can currently render both objects successfully, each using their own transform. The problem I'm encountering is rendering just the sphere by specifying an offset. As per the OpenGL spec:</p>

<blockquote>
  <p>If a buffer is bound to the <code>GL_DRAW_INDIRECT_BUFFER</code> binding at the
  time of a call to <strong>glMultiDrawArraysIndirect</strong>, <em>indirect</em> is interpreted
  as an offset, in basic machine units, into that buffer and the
  parameter data is read from the buffer rather than from client memory.</p>
</blockquote>

<p>but doing the following code to render results in nothing being rendered:</p>

<pre><code>// Calculates the offset &amp; size and calls BindBufferRange 
this-&gt;_indrMatrixBuffer.BindBufferRange(0, 1, 1);
// A simple call to glBindBuffer, binding to GL_DRAW_INDIRECT_BUFFER
this-&gt;_indrBuffer.BindBuffer();
// Render
glMultiDrawArraysIndirect(GL_TRIANGLES, (void*)sizeof(DAICmd), 1, 0);
</code></pre>

<p><sub>The <code>DAICmd</code> is a struct of four <code>unsigned int</code> (as per the <a href="https://www.opengl.org/sdk/docs/man/html/glMultiDrawArraysIndirect.xhtml" rel="nofollow">spec</a>)</sub></p>

<p>At first I though it might be my custom class but calling <code>glMultiDrawArraysIndirect(GL_TRIANGLES, 0, 1, 0);</code> and keeping the matrix buffer offset renders the cube in the spheres position!</p>

<p>What am I missing to get this to work?</p>

<hr>

<p><strong>Extra notes</strong></p>

<ul>
<li>Both the <code>SSBO</code> &amp; <code>DIB</code> are mapped using the flags <code>GL_MAP_WRITE_BIT, GL_MAP_PERSISTENT_BIT and GL_MAP_COHERENT_BIT</code> and are created with the same flags plus <code>GL_DYNAMIC_STORAGE_BIT</code></li>
<li>I'm using OpenGL 4.5</li>
<li>Using <code>glMultiDrawArraysIndirect(GL_TRIANGLES, (void*)sizeof(DAICmd), 1, 0);</code> does not produce an error when using <code>glGetError()</code></li>
<li>Using KHR_debug produces no errors either</li>
</ul>

CC BY-SA 3.0
2015-10-26T02:41:25.983Z
Code looks right on a first glance. Are you checking for GL errors? Do you have [KHR_debug](http://renderingpipeline.com/2013/09/opengl-debugging-with-khr_debug/) output set up?
Nathan Reed
<opengl><rendering>
glMultiDrawArraysIndirect does not work when specifying an offset
Soapy
# glMultiDrawArraysIndirect does not work when specifying an offset

(Asked by: username_0 on 2015-10-25)

I'm trying to use `glMultiDrawArraysIndirect` to render a batch of object.

In my example I have a cube and a sphere **(in this order)** in a `VBO` and a `GL_SHADER_STORAGE_BUFFER (SSBO)` that contains matrix transforms as well as a `GL_DRAW_INDIRECT_BUFFER (DIB)` that contains the draw commands.

The allocation of both the `SSBO` and `DIB` are managed by a custom class.

I can currently render both objects successfully, each using their own transform. The problem I'm encountering is rendering just the sphere by specifying an offset. As per the OpenGL spec:

> If a buffer is bound to the `GL_DRAW_INDIRECT_BUFFER` binding at the
> time of a call to **glMultiDrawArraysIndirect**, *indirect* is interpreted
> as an offset, in basic machine units, into that buffer and the
> parameter data is read from the buffer rather than from client memory.

but doing the following code to render results in nothing being rendered:

```
// Calculates the offset & size and calls BindBufferRange 
this->_indrMatrixBuffer.BindBufferRange(0, 1, 1);
// A simple call to glBindBuffer, binding to GL_DRAW_INDIRECT_BUFFER
this->_indrBuffer.BindBuffer();
// Render
glMultiDrawArraysIndirect(GL_TRIANGLES, (void*)sizeof(DAICmd), 1, 0);

```

The `DAICmd` is a struct of four `unsigned int` (as per the [spec](https://www.opengl.org/sdk/docs/man/html/glMultiDrawArraysIndirect.xhtml))

At first I though it might be my custom class but calling `glMultiDrawArraysIndirect(GL_TRIANGLES, 0, 1, 0);` and keeping the matrix buffer offset renders the cube in the spheres position!

What am I missing to get this to work?

---

**Extra notes**

* Both the `SSBO` & `DIB` are mapped using the flags `GL_MAP_WRITE_BIT, GL_MAP_PERSISTENT_BIT and GL_MAP_COHERENT_BIT` and are created with the same flags plus `GL_DYNAMIC_STORAGE_BIT`
* I'm using OpenGL 4.5
* Using `glMultiDrawArraysIndirect(GL_TRIANGLES, (void*)sizeof(DAICmd), 1, 0);` does not produce an error when using `glGetError()`
* Using KHR\_debug produces no errors either

**Tags:** opengl, rendering

> **Comments:**
> **username_1**: Code looks right on a first glance. Are you checking for GL errors? Do you have [KHR_debug](http://renderingpipeline.com/2013/09/opengl-debugging-with-khr_debug/) output set up?  

---

## 1 answers

### Answer by username_0 on 2015-10-26. Score: 2 | Accepted answer

The problem was my class handling the population fo the `draw indirect commands`.

Each time I added a new command to the indirect buffer I also incremented the `baseInstance`, so the cube had `baseInstance = 0` and the sphere had `baseInstance = 1`.

The reason why it doesn't render the sphere is because `glMultiDrawArraysIndirect` pretty much evaluates as `glDrawArraysInstancedBaseInstance(mode, cmd->first, cmd->count, cmd->instanceCount, cmd->baseInstance);` (as per the [OGL wiki](https://www.opengl.org/wiki/GLAPI/glMultiDrawArraysIndirect)).

The drawID is calculated as `(gl_InstanceID / divisor) +`**`baseInstance`** ([link](https://www.opengl.org/wiki/GLAPI/glDrawArraysInstancedBaseInstance)), which means that the drawID is out of range when trying to render with an offset, as the sphere now appears as the first element in the indirect buffer.


---

