Why does the batching system prefer world coordinates?

What’s the justification for performing model → world transformations on the CPU? From what I know about Defold’s batching system, it performs all model → world transformations by default with the material’s vertex space set to World, then passes those transformed positions in a vertex buffer to the vertex shader to be multiplied by the view and projection matrices, which are uniforms.

I’ve never seen a batching system that prefers vertices to be passed to the GPU already transformed in world coordinates because these transformations are much faster if they’re done in parallel on the GPU. The world transformation matrix for each object should be passed to the vertex shader inside the corresponding vertex buffer. This would also eliminate the problem I’ve seen where some developers want easy access to model coordinates without breaking the batch.

Maybe it was designed this way to support some limitation on older hardware? Maybe it’s focused on some very old version of OpenGL or something to do with embedded OpenGL?

1 Like

It’s legacy code.

For sprites, batching bakes sense as they have a low number of vertices (originally we had only 4 vertices).

ANd it was only long after that we added support for models. And at that time, the models needed were very simple.

For models, that can have a very large number of points, it’s not very efficient to be doing it on the CPU.
And, in fact, @jhonny.goransson is currently implementing instancing for models now. We’re not that far off, maybe one or two releases.
We also need to support GPU skinning, which is the logical next step of that feature.

7 Likes

Do you think it will eventually be changed such that the batching system requires model-space vertices? I suppose this would break projects that don’t make updates though.

You will be able to choose either model (local) space or world space.
In the case of local space, you will benefit from instancing on the GPU.
For models (and meshes), we’ll switch the default from world space to local space, so that new projects will get to use the more efficient approach out-of-the-box.

We’re also adding support for adding per-instance properties, so that you don’t have to break batches by using uniforms.

4 Likes

Cool, thanks for the info! :slight_smile: