Mesh component render batching does not work
- Dominant language
- C++
- Stars
- 6.3k
- Forks
- 455
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 130
Description
**Describe the bug (REQUIRED)**
Mesh component batch rendering does not work as expected. When two mesh components with an identical material (World space, no vertex constants, no fragment constants, no textures) and buffer declaration (except for the buffer resource name) are rendered it results in two draw calls rather than one.
**To Reproduce (REQUIRED)**
I am attaching a sample project that illustrates the issue. If you run the project you should see two triangles rendered and if you open the Web Profiler (Defold Editor, Debug -> Open Web Profiler), the profiler will display 2 draw calls.
**Expected behavior (REQUIRED)**
There should be only one draw call in this setup.
**Defold version (REQUIRED):**
1.9.5
**Platforms (REQUIRED):**
- Platforms: probably all platforms, but personally checked on Windows and Android
- OS: personally checked on Windows 10 and Android 13
- Device: Samsung Galaxy A32
**Minimal repro case project (OPTIONAL):**
See the attachment.
**Workaround (OPTIONAL):**
The issue stems from the code in https://github.com/defold/defold/blob/dev/engine/gamesys/src/gamesys/components/comp_mesh.cpp where the name of the mesh component's buffer resource is used when generating the component's m_MixedHash hash value (which is in turn used to determine if two components are eligible to be batched together for rendering).
More precisely in the ReHash function, lines 340-341:
```
BufferResource* br = GetVerticesBuffer(component, resource);
dmHashUpdateBuffer32(&state, &br->m_NameHash, sizeof(br->m_NameHash));
```
In the example project I've attached the name of the buffer resource for the first mesh is "mesh01BufferResource.bufferc" and "mesh02BufferResource.bufferc", and it is only this difference in the buffer resource name that causes the engine not to batch rendering of the two meshes.
If the mentioned two lines in comp_mesh.cpp are omitted, then running the attached sample project shows a single draw call (I've tested this myself).
However, as I am quite new to Defold source code, I have no idea if this change alone would mess up something else (doesn't seem to in simple tests I've done so far).
**Additional context (OPTIONAL):**
This issue has been raised in the Defold forum before, but was not explicitly answered (see [Mesh batching](https://forum.defold.com/t/mesh-batching/78509)).
[meshBatching.zip](https://github.com/user-attachments/files/18481718/meshBatching.zip)
Contributor guide
Research direction
Start in engine/gamesys/src/gamesys/components/comp_mesh.cpp, focusing on ReHash and the m_MixedHash value used for batching. Run the attached meshBatching.zip project and inspect draw calls in the Defold Web Profiler; done means identical meshes with different buffer resource names batch into one draw call without disrupting other rendering behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- computer-graphics, game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100