1
votes

Let's say I create two vertex buffers, for two different meshes.

     (I'm assuming creating separate buffers for separate meshes is how it's usually done)

Now, let's say I want to draw one of the meshes using an index buffer. Looking at the book Practical Rendering and Computation with Direct3D 11 it doesnt seem like the creation of an index buffer in any way references a vertex buffer, so how does the index buffer know (during input assembly) what vertex buffer to act on?

I've done some googling without answers, which leads me to assume there's something obvious about it that I'm missing.

2

2 Answers

2
votes

You are right, index buffers do not reference specific vertex buffers. During DrawIndexed active index buffer is used to supply indices into active vertex buffers (the ones you set using SetIndexBuffer/SetVertexBuffers).

1
votes

Indeed, Index Buffers and Vertex Buffers are completely independent.

Index buffer will know about VertexBuffer at draw time (eg: when both as bound to the pipeline)

You can think of Index Buffer as a "Lookup Table", where you keep a list or elements indices to draw.

That also means you can attach two completely "logically unrelated" buffers to the pipeline and draw it, nothing will prevent you from doing that, but you will of course have some strange visual results.

Decoupling both has many advantages, here are a few examples:

  • You can reuse an index buffer (for example, two displaced grids with identical resolution can share the same index buffer). That can be a decent memory gain.
  • You can draw your Vertex buffer on it's own and do some processing per vertex (draw a point list for sprites for example, or apply skinning/displacement into a Stream Output buffer , then draw the resulting vertex buffer using DrawIndexed)
  • Both Vertex/Index buffers can also be bound as ByteAddressBuffer, so you can also process your geometry in Compute Shader, and build another optimized index buffer, with culled triangles for example, then process the Indexed Draw with the optimized buffer. Applying those culls in with indices instead of vertices is often faster than vertex, as you will move much less memory.
  • This is a niche case, but sometimes I have to draw a mesh as a set of triangles, but then draw as a set of lines (some form of wireframe). If as example, you take a single Box, you will not want to draw the diagonals as lines, so I have a shared Vertexbuffer with box vertices, then one IndexBuffer dedicated to draw triangle list, and another to draw line list. In case of large models, this can also be an effective memory gain.