I am using instancing to draw a lot of geometry that in some cases is valid for only one frame. This is why for view frustum culling, I use a compute shader that writes the indices of all visible instances into a shader storage buffer. Let’s call that buffer visibleInstanceIndices. Right now, I then call glDrawElementsInstanced and fetch attributes such as the transformation in the vertex shader like this:
int instance = visibleInstanceIndices[gl_InstanceID]; mat4 trafo = transformations[instance];
transformations is only updated once at loading time, only the visible instance indices change at runtime. The indirection this introduces is annoying and - so I suspect - unnecessary. Is it possible (any OpenGL version, any extension supported on Nvidia Maxwell cards) to render all of my instances with a single draw call such that gl_InstanceID is not incremented internally for each instance, but the value is taken from a bound buffer (visibleInstanceIndices in my case)?
Edit: I might also add a follow-up question right away. I am not only culling single instances, but also groups of instances. In this case, the instances are arranged such that the instances of one group have consecutive indices. For example, if group 0 is visible, then I want to draw 1000 instances with transformations to transformations. I believe that I can do that with glMultiDrawElementsIndirect, but only for the first group. If I add another DrawElementsIndirectCommand struct for each group, gl_InstanceID will start from 0 for each group/draw command. So I would need to specify an offset for gl_InstanceID for each command, which I think is not possible. But maybe I’m heading in the right direction?