Rafael Ferreira
Rafael Ferreira

Reputation: 91

How to properly render multiple 3D models in Direct3D11?

After some search I've learned it is possible to create multiple Vertex Buffers, each for a specific 3D model, and set them in the Input Assembler to be read by my shaders, or at least this is what I could understand. But by reading Microsoft's documentation I've got very confused of how to do this the right way, this is what I was reading, and they say I can pass in an array of Vertex Buffers to the IA stage, but it also says that the maximum number of Vertex Buffers my Input Assembler can take in D3D11 is 32. What would I do if I needed 50 different models being rendered at the same time? And also if someone could clarify how the pOffset work in this situation with multiple models would also help, as I could understand it should always be assigned a 0 value as the beginning of my buffers is always the vertex data, but I could've understood wrong. And by last I want to add I've already rendered some buffers which consists of multiple models together, but I don't know exactly how could I deal with many individual models.

Upvotes: 0

Views: 254

Answers (1)

Chuck Walbourn
Chuck Walbourn

Reputation: 41127

The short answer is: You don't try to draw all your models in one Draw call.

You are free to organize rendering in many ways, but here is one approach:

A 'model' consists of a one or more 'meshes'. Each mesh is collection of a vertices (in a VB), indices (in an IB), and some material information associated with each 'subset' of indices.

To draw:

foreach M in models
    foreach mesh in M
        foreach part in mesh
            Set shaders based on material
            Set VB/IB based on mesh
            DrawIndexed

Since this is a number of nested loops, there are several ways to improve the performance. For example, you might just queue up the information instead of actually calling DrawIndexed, then sort by material. Then call DrawIndexed from the sorted queue.

For alpha-blending to appear correct, you have to do at least two rendering passes: First to render opaque things, then the second to render alpha-blended things.

You may also want to combine all the content in a given model into one VB and one IB with offsets rather than use individual resources.

You may have the same model in multiple locations in the world, so you may have many model instances sharing the same mesh data. In this case, sorting by VB/IB as well as material could be useful. If you are drawing the same model in many locations (100s or 1000s), then you should to look into hardware instancing.

An example implementation of this can be found in DirectX Tool Kit as Model, ModelMesh, and ModelMeshPart.

Upvotes: 1

Related Questions