0
$\begingroup$

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?

$\endgroup$
7
  • 1
    $\begingroup$ What graphics API, version, etc are you using/targeting? That will affect what options are available. $\endgroup$
    – Kevin Reid
    Feb 27 at 17:12
  • $\begingroup$ "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. $\endgroup$ Feb 27 at 17:51
  • $\begingroup$ @KevinReid I'm using the wgpu rust crate v0.15.0 on a gtx 970. $\endgroup$
    – Rokit
    Feb 27 at 18:45
  • $\begingroup$ @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. $\endgroup$
    – Rokit
    Feb 27 at 18:45
  • $\begingroup$ @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. $\endgroup$ Feb 27 at 18:50

1 Answer 1

1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.