Gregor Sattel
Gregor Sattel

Reputation: 400

What is the proper way to render multiobject scenes in d3d 11

If I want to render a specific mesh I create it's vertex/index data, create vertex buffer, index buffer, bind them to the IA, set the desired shaders and call context->DrawIndexed. I guess one option of rendering multiple objects will be to loop this procedure for each object, but on msdn it says "Input assembly (IA): You need to attach each vertex and index buffer for each object in the scene.". I'm assuming that means to draw multiple objects with single Draw call.

For vertex buffers there is a slot number (assuming each object's vertex buffer gets a slot), but there is no such thing for index buffer? Can someone point me to some examples how this should be set up?

The paper I was talking about: https://learn.microsoft.com/en-us/windows/win32/direct3dgetstarted/understand-the-directx-11-2-graphics-pipeline

Upvotes: 0

Views: 1058

Answers (1)

Chuck Walbourn
Chuck Walbourn

Reputation: 41127

Direct3D is a low-level graphics API, so there are many ways you can render objects.

One common model for basic rendering is to organize each mesh as a Vertex Buffer, an Index Buffer, and a list of 'subsets' that share the same material/surface properties (a set of vertex shaders, pixel shaders, texture resources, etc).

See DirectX Tool Kit for an example of the Model/Mesh/MeshPart organization for rendering.

So for example, this model:

Microsoft Cup

It consists one VB, one IB, and has 'two subsets', one for the textured outside and the other for the untextured interior. This scene therefore requires two calls to DrawIndexed.

If you wanted to render multiple (n) instances of this mesh, you'd use the same VB, an same IB, and same texture resources, but typically the Vertex Shader applies a transform so you can position it in the scene. You would therefore call DrawIndexed n times with the texture and n times without the texture.

You'd also need an Input Layout object for each combination of Vertex Buffer layout and shader signature, which you can arrange here to be just one IL.

While this model doesn't have any transparency, you also need to keep mind that 'alpha-blended' subsets usually have to be rendered after all the opaque subsets are rendered to get the expected results even with z-buffer hidden surface removal.

There are many other ways to optimize drawing many copies of the same mesh. For example, you could use instancing to draw many copies of the outside of the cup in one call to DrawIndexedInstanced and then draw all the interiors of the cup in a second call. See SimpleInstancing. You'd need a different Input Layout, Vertex Shader, and additional VB stream to provide and use the instancing data.

PS: Most games these days call DrawIndexed thousands or tens of thousands of times, so don't worry about optimizing it too much when you are just getting started.

Upvotes: 2

Related Questions