Question

As a unity user, i'm confused about how i could implement something similar to their static batching. Multiple meshes (diferent meshes but same material) display as being rendered in 1 draw call, however i'm not finding how i could do that in DirectX.

I can see how to draw in DirectX using Draw(). I know you can draw the same meshes multiple times in a single call (DrawiInstanced()?) however i don't get how you can draw multiple different meshes in a single draw call. I would love to be pointed into the right direction or given a sample showing say a cube & a triangle being rendered in a single draw call sharing a single material.

I'm using DirectX11.1 & doing so through SharpDX, a SharpDX example is prefered but a DX one is fine, however no DX9/10 examples if the correct way to do this changed, i'm strictly only interested in DX11.

Thanks.

Was it helpful?

Solution

There's no mechanism to do this automatically, but if you have multiple meshes that share input layout and "material" (shaders, textures, etc.), you can simply concatenate the vertex and index buffers (with indices patched to the base vertex offset as necessary).

As an example that draws a cube and triangle, just set up your VB to contain both:

float cubeAndTriangleVertices[] = 
{
  -1, -1, -1, // cube vertices
  -1, -1,  1,
  -1,  1, -1,
  -1,  1,  1,
   1, -1, -1,
   1, -1,  1,
   1,  1, -1,
   1,  1,  1,
   2,  0,  0, // triangle vertices
   2,  1,  0,
   3,  0,  0,
};
D3D11_SUBRESOURCE_DATA cubeAndTriangleVData = { cubeAndTriangleVertices };
device->CreateVertexBuffer(&cubeAndTriangleVData, &vb);

unsigned short cubeAndTriangleIndices[] =
{
  0, 2, 1, 2, 3, 1, // -x face
  7, 6, 5, 6, 4, 5, // +x face
  0, 1, 4, 4, 1, 5, // -y face
  2, 6, 3, 6, 7, 3, // +y face
  6, 2, 0, 4, 6, 0, // -z face
  3, 7, 1, 7, 5, 1, // +z face
  8, 9, 10, // triangle
}

D3D11_SUBRESOURCE_DATA cubeAndTriangleIData = { cubeAndTriangleIndices };
device->CreateIndexBuffer(&cubeAndTriangleIData, &ib);

// ...pipeline setup

context->DrawIndexed(6 * 6 + 3, 0, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top