Instanced rendering with multiple framebuffers

#1

I am currently trying to use several framebuffers for each camera in my scene (more than 2). I am also using glDrawArraysInstancedBaseInstance to draw the scene. However, only one framebuffer gets outputted correctly when doing this (the first one).

I changed my glDrawArraysInstancedBaseInstance call to glDrawArrays call and each framebuffer got output (but it was obviously incorrect output), which reinforces my belief that the problem is with glDrawArraysInstancedBaseInstance. Finally, I render a skybox via glDrawArrays for each framebuffer, and that skybox is outputted correctly.

I would like to mention that I have been using glDrawArraysInstancedBaseInstance for a while and have had no problems, but I have only been using 1 framebuffer during that time. Only recently I tried to add more framebuffers and had this problem arise.

Pseudocode:

foreach framebuffer 

    bind framebuffer

    render via baseInstanced (changed this to drawArrays and there was output)

    unbind framebuffer


foreach framebuffer

    blit framebuffer to texture

    render quad using framebuffer texture

Does anyone have any ideas as to what is going on?

#2

I would look carefully at what state you are (or should be) setting up for glDrawArraysInstancedBaseInstance() that is not used by glDrawArrays().

For example, your instance variation data.

  • Are you using instanced arrays? If so, check the vertex attribute bindings, enables, and divisor settings on the vertex attributes containing this instance variation data.
  • Are you using gl_InstanceID (and possibly gl_BaseInstance) in your shader code? If so, check the bindings on the buffer(s) and/or texture(s) which contain the instance variation data that you’re indexing into via that/those instance IDs.

Be sure and check these for the 2nd and subsequent iterations of your “render framebuffer” loop, not only the first. If they weren’t setup properly for the 2nd and subsequent iterations, that might explain the behavior you’re seeing.

#3

I am using instanced arrays and believe it to be setup correctly because I have not had any problems until now with this specific api call and framebuffer relationship. I do not use any shader input variables like gl_InstanceId.

I would like to add that these are multisampled framebuffers, if that has any impact.

I have tried several things

  • Rebinding VAO/VBO for each framebuffer (not ideal because it defeats whole purpose of instanced rendering)
  • Alternating framebuffer order (so instead of bottom left having the output, the top right would, then top left, etc)

Here is the code for the draw command

framebuffer->bind();
// Bind camera view
shaders.setMatrix("PV", camera->getProjectionMatrix() * camera->getViewMatrix());
shaders.setVec3("viewPosition", camera->getPosition());

// (This one doesn't work) (Also, in this scene, instancedDrawn == 0, so this call is the equivalent of glDrawArraysInstanced)
//glDrawArraysInstancedBaseInstance(GL_TRIANGLES, vBuffer.vertexOffset, numVertices, componentTransformBuffer.offsets.size(), instancesDrawn);

// (This one does)
glDrawArraysInstanced(GL_TRIANGLES, vBuffer.vertexOffset, numVertices, componentTransformBuffer.offsets.size());

framebuffer->unbind();

And framebuffer->bind(); is, where FBO is an instance variable

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FBO);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);

framebuffer->unbind(); is:
glBindFramebuffer(GL__DRAW_FRAMEBUFFER, 0);

What is strange is that the skybox is being outputted, but the cubes are not.

I appreciate the help, because this one is giving me a lot of trouble.

#4

I figured it out. At some time later on my instancesDrawn was being incremented again.Thanks for the help tho, looking at the subsequent states helped.