sortingopengl-eswebglrenderingtranslucency

Global translucency sorting with instanced rendering


My rendering code is structured such that there are models, and there are model instances. You can have N instances per model, and all visible instances of the same model are rendered at the same time with instanced rendering.

This works fine as far as performance goes - my code needs to render hundreds to thousands of instances, each one of them possibly being composed of multiple render calls, and the amount of render/uniform/texture/etc. calls was an issue.

The problem comes when I want to consider instances that use translucency, which in this case is a whole lot of them - in such cases, the order in which they are rendered matters, since the models use various blending functions. I can sort instances per model, but once there are instances of multiple models being rendered, the order is arbitrary (in practice it is based on the order at which the models were loaded).

I can't for the life of me figure any way to do such global sorting with instanced rendering.

Is this at all possible? Should instanced rendering be used purely for opaque objects?

My code uses WebGL1, which lacks so many modern features, but I'll be interested to know if this is possible, even if only in a more modern API.


Solution

  • Instancing is ultimately a performance optimization; there is nothing you can render via instancing that you can't do without it. It's simply a matter of how many draw calls you make.

    If the order in which different meshes are rendered is important (which with blending, it is), then you cannot use instancing. If you have to draw everything back-to-front in order to make the rendering work out, then that's what you have to do. No matter how many draw calls it takes.