Search code examples
vulkan

Differentiate models when using single draw call


I'm trying to draw geometry for multiple models using a single draw call. All the geometry, thusly, resizes within the same vertex/index buffers. The geometry for the different models share the same vertex format, but the vertex amounts for each model can be different.

In the vertex/fragment shaders, what's a technique that can be used to differentiate between the different models, to access their appropriate transforms/textures/etc ?


Solution

  • Are these static models? For traditional static batching:

    You only need a single transform relative to the batch origin (position the individual models relative to the batch origin as part of the offline data packaging step).

    You can batch your textures in to a single atlas (either a single 2D image with different coordinates for each object, or a texture array with a different layer for each object).

    If you do it this way you don't need to different component models - they are effectively just "one large model". Which has nice performance properties ...

    For more modern methods, you can try indirect draws with multiple "drawCount" values to index the settings you want. This allows variable buffer offsets and triangle counts to be used, but the rest of the state used needs to be the same.

    As an alternative to texture arrays, with bindless texturing you can just programmatically select which texture to use in the shader at runtime. BUT you generally still want it to be at least warp-uniform to avoid a performance hit.