PDA

View Full Version : Question using glMultiDrawArraysIndirect and indirect buffer



A_Shuang
03-12-2015, 08:01 AM
I have two objects (A and B) to be rendered using glMultiDrawArraysIndirect(). Each object has a setupBuffers() method for creating, binding a vao, then creating and binding the indirect buffer, instanced ID buffer, vertices buffer and texture coords buffer.

Here is the problem: if I call setupBuffers() of the object A before calling setupBuffers() in B, then only B will be drawn; if I call setupBuffers() of the object B before calling setupBuffers() in A, then only A will be drawn. (No exception is found.) The data stored should be all correct since I've tested both of them.

Object A:

protected void setupBuffers()
{
gl.glGenVertexArrays(1, vaoBuff);
gl.glBindVertexArray(vaoBuff.get(0));

gl.glGenBuffers(4, vboBuff);

//bind indirect buffer object
gl.glBindBuffer(GL4.GL_DRAW_INDIRECT_BUFFER, vboBuff.get(0));
gl.glBufferData(GL4.GL_DRAW_INDIRECT_BUFFER, instanceCount * 4 * Integer.SIZE / 8, null, GL4.GL_STATIC_DRAW);
gl.glBufferSubData(...);

//bind draw instance ID in the shader with a buffer object
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboBuff.get(1));
gl.glBufferData(GL4.GL_ARRAY_BUFFER, instanceCount * Integer.SIZE / 8, drawIndexBuff, GL4.GL_STATIC_DRAW);
gl.glVertexAttribIPointer(d_idLoc, 1, GL4.GL_UNSIGNED_INT, 0, 0);
gl.glVertexAttribDivisor(d_idLoc, 1);
gl.glEnableVertexAttribArray(d_idLoc);

//bind vertex data buffer object
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboBuff.get(2));
gl.glBufferData(GL4.GL_ARRAY_BUFFER, instanceCount * vertBuffSize * Float.SIZE / 8, null, GL4.GL_STATIC_DRAW);
gl.glBufferSubData(...);
gl.glVertexAttribPointer(verPosLoc, 3, GL4.GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(verPosLoc);

//bind texture coordinate data buffer object
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboBuff.get(3));
gl.glBufferData(GL4.GL_ARRAY_BUFFER, instanceCount * texBuffSize * Float.SIZE / 8, null, GL4.GL_STATIC_DRAW);
gl.glBufferSubData(...);
gl.glVertexAttribPointer(tc_inLoc, 2, GL4.GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(tc_inLoc);
}

drawing method in Object A:

gl.glBindVertexArray(vaoBuff.get(0));
gl.glMultiDrawArraysIndirect(GL4.GL_TRIANGLE_STRIP , 0, instanceCount, 0);



Object B:

protected void setupBuffers()
{
very similar to A...
}

drawing method in Object B:

gl.glBindVertexArray(vaoBuff.get(0));
gl.glMultiDrawArraysIndirect(GL4.GL_PATCHES, 0, instanceCount, 0);


Finally I find the problem which is at:
gl.glBindBuffer(GL4.GL_DRAW_INDIRECT_BUFFER, vboBuff.get(0));

This method used to bind a indirect buffer is called once both in Object A & B, resulting in that problem, yet I have no idea why. Any ideas about that?

Alfonse Reinheart
03-12-2015, 08:43 AM
The GL_DRAW_INDIRECT_BUFFER binding point is not part of vertex array object state. It's global context state. So you'll have to set this to the buffer you want to pull from before performing indirect operations from that buffer.

A_Shuang
03-12-2015, 09:35 AM
The GL_DRAW_INDIRECT_BUFFER binding point is not part of vertex array object state. It's global context state. So you'll have to set this to the buffer you want to pull from before performing indirect operations from that buffer.

Great thanks.;)