Weird things happening with instance uniforms with too many instances

Godot Version

4.3

Question

I’m programmatically instantiating thousands of MeshInstance3D. Each uses the same simple mesh (about 30 vertices) and the same shader material.

I’m also using instance uniforms in my shader, in particular I have an array of 29 sampler2D and an integer instance uniform to tell the shader which sampler to use. There are also instance uniforms for UV offset and scale so that each mesh shows a different part of different textures, like a roll-my-own 3D texture grid.

If I only instantiate a small number of thousands, everything works fine. If I go over some limit, still under 10,000, then the earliest ones added to the scene don’t render correctly–they all render the same incorrect information. They behave as if the instance uniforms are populated with a (0,0) UV offset (not my default) and a 1.0 UV scale (my default) as far as I can tell.

The mesh instances added later display correctly, but once I go over a limit, then the more I add (and re-run), the more of the earlier ones fail. All of this is done in the _ready of the root, and after that the scene is static.

Is there some undocumented limit to the number of instance uniforms I can use, or is this likely a bug?

Looks like a bug, is good to report on Godot github and let them confirm if this is a bug or not.

I found this there:

The buffer limit it describes does work, and I’m able to use more meshes. But I don’t believe I should be hitting the limit. The project setting is set to 65536 and I hit the limit at 4096 meshes, which suggests I’m using 16 shader parameters per instance. But I’m only using 2 instance uniforms and 2 non-instance uniforms in the shader, so I shouldn’t be anywhere near 65536 yet.