<p>One approach is to reframe the problem from “objects which change their models” to “objects which are replaced with other objects that happen to have the same position”.</p>
<p>Create buffers (or ranges in one buffer) of instances that are all supposed to draw the <em>same</em> model. Each frame, go through your list of objects, determine which model is to be drawn, and add the object's position (and other instance data) to the instance buffer <em>for that model.</em></p>
<p>This of course requires uploading lots of instance data every frame, but you need to do that anyway if the objects are independently moving/animating, and it'll be faster than the GPU processing thousands of extraneous vertices.</p>

CC BY-SA 4.0
2023-02-27T19:18:03.340Z
2023-02-27T19:18:03.340Z
Kevin Reid
<p>I need to render a lot of objects that swap their model from frame to frame.</p>
<p>Currently, I am putting every model into a single vertex buffer and giving every vertex an id. I store the same id in the instance buffer. If the vertex and instance buffer ids match in the vertex shader, then I render it. It works, but since I'm rendering tens of thousands of objects, I can only fit about 1000 vertices into the vertex buffer before rendering exceeds 16ms. This translates to about 2-3 models.</p>
<p>Is there some other technique I could use to swap them out where I could get maybe 10-20 models?</p>

CC BY-SA 4.0
2023-02-27T17:12:49.823Z
What graphics API, version, etc are you using/targeting? That will affect what options are available.
Kevin Reid
CC BY-SA 4.0
2023-02-27T17:51:43.123Z
"*objects that swap their model from frame to frame*" What exactly do you mean by that? Do you mean that you have some conceptual entity that chooses which mesh(es) to use to represent itself on any individual frame from a suite of meshes? If that's what you're doing... why does the number of meshes matter to *performance*? Are you trying to upload meshes every frame on demand? If so, why? Do you have too much mesh data to just store it all on the GPU? In short, we need a better explanation of exactly what you're doing and why you have a performance problem.
Nicol Bolas
CC BY-SA 4.0
2023-02-27T18:45:29.200Z
@KevinReid I'm using the wgpu rust crate v0.15.0 on a gtx 970.
Rokit
CC BY-SA 4.0
2023-02-27T18:45:42.923Z
@NicolBolas Your first sentence is correct. All models are stored in a single vertex buffer, which amounts to 1000 vertices total. I'm rendering about 20k instances. However I only render a subset of the vertex buffer. If I'm rendering model id #1, that might be vertices 0-250, model #2 might be vertices 251-600, etc. Any of the 20k objects can be one of these models at any given time. The more models I add to the vertex buffer, the slower it gets.
Rokit
CC BY-SA 4.0
2023-02-27T18:50:08.493Z
@Rokit: "*The more models I add to the vertex buffer, the slower it gets.*" The slower *what* gets? Are you including the setup time for creating and uploading to the buffer? How much "slower" is it; are you measuring performance in FPS or in absolute time? Are you re-uploading to the buffer every frame, or do you just leave it there? Basically, we can't answer this question from the minimal information you've given.
Nicol Bolas
CC BY-SA 4.0
2023-02-27T18:50:32.127Z
@Rokit: And please put the appropriate information *in the question itself*, not in comments below it.
Nicol Bolas
CC BY-SA 4.0
2023-02-27T18:56:43.507Z
@NicolBolas "The slower what gets?" It's in my question: "I can only fit about 1000 vertices into the vertex buffer before rendering exceeds 16ms", meaning the slower rendering gets. The more models I add, the more milliseconds are added to render time. The vertex buffer is set when the program starts. Only the instance buffer is updated.
Rokit
<3d><mesh><performance>
What technique can I use to swap thousands of models per frame?
Rokit
# What technique can I use to swap thousands of models per frame?

(Asked by: username_0 on 2023-02-27)

I need to render a lot of objects that swap their model from frame to frame.

Currently, I am putting every model into a single vertex buffer and giving every vertex an id. I store the same id in the instance buffer. If the vertex and instance buffer ids match in the vertex shader, then I render it. It works, but since I'm rendering tens of thousands of objects, I can only fit about 1000 vertices into the vertex buffer before rendering exceeds 16ms. This translates to about 2-3 models.

Is there some other technique I could use to swap them out where I could get maybe 10-20 models?

**Tags:** 3d, mesh, performance

> **Comments:**
> **username_1**: What graphics API, version, etc are you using/targeting? That will affect what options are available.  
> **username_2**: "*objects that swap their model from frame to frame*" What exactly do you mean by that? Do you mean that you have some conceptual entity that chooses which mesh(es) to use to represent itself on any individual frame from a suite of meshes? If that's what you're doing... why does the number of meshes matter to *performance*? Are you trying to upload meshes every frame on demand? If so, why? Do you have too much mesh data to just store it all on the GPU? In short, we need a better explanation of exactly what you're doing and why you have a performance problem.  
> **username_0**: @username_1 I'm using the wgpu rust crate v0.15.0 on a gtx 970.  
> **username_0**: @username_2 Your first sentence is correct. All models are stored in a single vertex buffer, which amounts to 1000 vertices total. I'm rendering about 20k instances. However I only render a subset of the vertex buffer. If I'm rendering model id #1, that might be vertices 0-250, model #2 might be vertices 251-600, etc. Any of the 20k objects can be one of these models at any given time. The more models I add to the vertex buffer, the slower it gets.  
> **username_2**: @username_0: "*The more models I add to the vertex buffer, the slower it gets.*" The slower *what* gets? Are you including the setup time for creating and uploading to the buffer? How much "slower" is it; are you measuring performance in FPS or in absolute time? Are you re-uploading to the buffer every frame, or do you just leave it there? Basically, we can't answer this question from the minimal information you've given.  
> **username_2**: @username_0: And please put the appropriate information *in the question itself*, not in comments below it.  
> **username_0**: @username_2 "The slower what gets?" It's in my question: "I can only fit about 1000 vertices into the vertex buffer before rendering exceeds 16ms", meaning the slower rendering gets. The more models I add, the more milliseconds are added to render time. The vertex buffer is set when the program starts. Only the instance buffer is updated.  

---

## 1 answers

### Answer by username_1 on 2023-02-27. Score: 1 | Accepted answer

One approach is to reframe the problem from “objects which change their models” to “objects which are replaced with other objects that happen to have the same position”.

Create buffers (or ranges in one buffer) of instances that are all supposed to draw the *same* model. Each frame, go through your list of objects, determine which model is to be drawn, and add the object's position (and other instance data) to the instance buffer *for that model.*

This of course requires uploading lots of instance data every frame, but you need to do that anyway if the objects are independently moving/animating, and it'll be faster than the GPU processing thousands of extraneous vertices.


---

