Is it possible to have one buffer for all instance data across draw calls?

Simple example, I have 2 models I want to draw:

  • tree
  • rock

In order to use GPUIndexedIndirectDrawCommand, I put their MVP matrices into a storage buffer.

I’d like to put all of them in one buffer, like:

[treeMat1, treeMat2, treeMat3, bushMat1, bushMat2]

Then, render it with 2 separate SDL_DrawGPUIndexedPrimitivesIndirect draw commands.

In order to do that, you’d have to say:
“draw 3 trees, starting at instance 0” and “draw 2 bushes, starting at instance 3”

That’s why I’d like to set first_instance in SDL_GPUIndexedIndirectDrawCommand.

However, the docs say that the first_instance parameter is not compatible with the InstanceID in the shader, so in the shader I would not be able to get the correct MVP.

struct Input {
    uint instanceID : SV_InstanceID;
};

struct DefaultInstanceData {
    float4x4 mvp;
};

StructuredBuffer<DefaultInstanceData> instanceData : register(t0, space0);


float4x4 mvp = instanceData[input.instanceID].mvp;

I just wanted to ask if this kind of usage is really impossible and if I have to create separate buffers for the instance data for each type of object I need to draw?

It seems having one buffer and indexing into it is the proper way from what I’ve read so please let me know if I’m missing something or if you have any ideas on how it can be done / would be done by an expert.

Thanks!

Nevermind, figured it out.
Leaving the answer here for posterity:

You can still bind another vertexbuffer with input rate by instance, where you provide all the instance ids. This way you could even refer to instance indices that are not in a contiguous block, if you so choose.

So for example, my testvertex shader input looks like this now:

struct Input {
    float3 position : TEXCOORD0;
    float2 uv0 : TEXCOORD1;
    float3 color : TEXCOORD2;
    float3 normal : TEXCOORD3;
    uint instanceID2 : TEXCOORD4; // <-- provide by vertex buffer
    uint instanceID : SV_InstanceID; // don't use this
};

If anyone has more input on this, feedback is ofc still appreciated! :slight_smile: