0
\$\begingroup\$

As far as I can tell instanced rendering is simply a way to separate a single vertex buffer into a buffer for vertex-specific data and instance-specific data. This could be useful for rendering multiple otherwise identical models, but each one is at a different offset in the world. You only have to store the model once, and you have a per instance translation that you add to each vertex in the shader. Metal and OpenGL both have special mechanisms (functions) for instanced rendering.

However, how is instanced rendering any different from just using normal, non-instanced rendering where I index the vertex buffer with vertex_id%VERTICES_PER_INSTANCE and index a pseudo-instance buffer with vertex_id/VERTICES_PER_INSTANCE within the shader? For example, are there performance gains from using official instanced rendering instead of just using / and % in the shader? Are there other more subtle differences that I am not aware of? What capabilities do the existing official instanced-rendering facilities provide that emulating instanced rendering using / and % in the shader does not?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Whenever instance data changes you will have to upload that new data to the GPU. If the instance data is part of the vertex buffer it means you must re-upload many parts of that vertex buffer to update your instance data, which semantically looks like you're uploading a new mesh through these APIs. (maybe driver optimizations watch for this?) When you use separate instance data you could leave the mesh alone and only have to update a smaller buffer through those intended APIs (and again driver optimizations might come in to play). \$\endgroup\$
    – Romen
    Oct 2, 2023 at 21:08
  • 1
    \$\begingroup\$ I also think it's possible that the GPU could schedule or execute shader programs differently if it knows that it's rendering many instances of one vertex buffer rather than one complicated vertex buffer. Sorry I only know enough to propose that it might matter, not enough to know what matters ;) \$\endgroup\$
    – Romen
    Oct 2, 2023 at 21:11

1 Answer 1

1
\$\begingroup\$

One significant disadvantage of doing manual vertex fetching is that it will generally not be able to take advantage of the post transform cache. That alone could easily reduce vertex processing performance by a factor of two or more if you're not using it.

If you want to take full manual control of mesh processing, you might want to look into mesh shaders (GL_MESH_SHADER_NV/GL_TASK_SHADER_NV in OpenGL).

That brings me to another reason that you might want to use the standard hardware instancing support - it's more portable. For example, you might not be able to do manual vertex fetching on every mobile phone GPU that supports hardware instancing. Mesh shaders require even more modern GPUs, and APIs.

\$\endgroup\$
2
  • \$\begingroup\$ How does manual vertex fetching impede the use of the post transform cache? \$\endgroup\$
    – CPlus
    Oct 2, 2023 at 22:27
  • 1
    \$\begingroup\$ With manual vertex fetching you're not using an index buffer, and the post transform cache only works when there is an index buffer - it assumes that the same input index will always produce the same result. \$\endgroup\$
    – Adam
    Oct 3, 2023 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .