Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Multi draw support when using RenderMeshIndirect

Discussion in 'General Graphics' started by TJHeuvel-net, Mar 29, 2023.

  1. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    834
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    193
    There is no SV_DrawID in D3D11/D3D12. I think this is some cross reference from gl_DrawId and its multi-draw rendering. Corresponding functionality exists in D3D12 by using indirect drawing. But approach taken by D3D API will get correct data in buffers based on resurce views used during ID3D12CommandSignature creation. So it is mystery how to correctly use InitIndirectDrawArgs(drawId) function in shader with non-zero drawId parameter.
     
  3. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    834
    I think i did figure it out. Im implementing this tutorial in Unity: https://vkguide.dev/docs/gpudriven

    In the case of doing gpu driven rendering, and culling in a compute shader. The compute shader passes data to the vertex shader, e.g. the visible indices. It also needs to know the drawID.

    We can therefore also pass along the drawid. If you get your vertex data by using the instanceId, you can make another structuredbuffer with drawids.
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    834
  5. Kasperrr

    Kasperrr

    Joined:
    Feb 23, 2019
    Posts:
    13
    Intresting. @TJHeuvel-net thanks for links. Did you manage to implement this with RenderMeshIndirect ?
     
  6. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    834
    No, sadly.

    DirectX11 has DrawIndexedInstancedIndirect, as you can see this is only for a single draw command. When you call RenderMeshIndirect in dx11 with a count of 4 Unity will simply do 4 of these draw commands. Thats a limitation by the API, not much they can do there.

    What they do offer is a draw-id for those draw commands, they fill in that data for us. If you follow what they do in UnityIndirect.cginc you can read out that variable, its called `unity_BaseCommandID`. I'd highly recommend seeing what happens using something like RenderDoc or PIX.

    The sad part is that DirectX12 does support this construction of a single API call for ExecuteIndirect that contains multiple draw commands, see the count arguments. This isnt exposed by Unity though, for compatability(?) with dx11 they still do the same workaround. That wouldnt even be necessary in the future, lets hope they update their apis!
     
    Kasperrr likes this.