OpenGL hardware instancing


From what I read on GitHub, hardware instancing should work with OpenGL, and Jjagg posted a screenshot of a working sample.
I wanted to try some examples (here, and here), but I encountered a problem when trying to set the vertex buffers using SetVertexBuffers. In these examples, we need two bindings but this seems not possible, given the exception Max number of vertex buffers is 1.

I checked the OpenGL GraphicsDevice class, and as you can see on this line, there is a todo about this limitation.

So, the question is, is there any working sample with OpenGL and hardware instancing? How is it possible to get the two previous examples work with using only one vertex buffer?

Thank you in advance

Yeah, my PR was not merged yet so instancing for GL is not yet in the 3.6 release or the develop branch. Tests are working so IMO that’s good to be merged. I just pinged the maintainers on that PR, I’ll post back here when it’s merged. When that happens you can grab a develop build and use that.


Cool, I haven’t seen them and the second one looks intriguing. I wonder whether that approach can make CPU particles compete with the GPU particle approach from the XNA samples ( have a dynamic vertex buffer and draw all particles in one go)

Ah alright, I actually thought the main things were merged and you forgot some others or I don’t know what, didn’t really check the commits. :slight_smile:
Anyways thanks a lot for your work. This was much needed.

1 Like

I thought it had been working for a long time just like it did in xna ?

Only for D3D, it hadn’t been implemented for GL.

When will there be an update on this? Anyone knows?
I’m using OpenGL too stuck on this part sadly.
Someone who knows?


It’s supported on develop. If you grab a development build or build from source you can use instancing with a GL project :slight_smile:

1 Like

Thanks that helped but there seems to be a bug??
When I’m using the following deprecated function the objects display visible on screen

GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,
                                                           meshPart.NumVertices, meshPart.StartIndex,
                                                           meshPart.PrimitiveCount, instanceTransforms.Length);

But if I use it with the new function…

GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, meshPart.NumVertices, meshPart.StartIndex,
                                                           meshPart.PrimitiveCount, instanceTransforms.Length);

It doesn’t render the objects.

You removed the wrong parameter. The original parameters were (PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount, int instanceCount). In the new function, minVertexIndex and numVertices have been removed. So if you want to convert your first call to the new version, it should be

GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0,
                                                       meshPart.PrimitiveCount, instanceTransforms.Length);

Well didn’t that make a whole lot of difference, thank you very much indeed.