Search Unity

GPU instancing shader questions

Discussion in 'Shaders' started by bpears, Nov 30, 2016.

  1. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Posted this in unity answers but maybe it's more appropriate here since there are multiple questions I have.


    EDITOR

    Do instances work in the editor or only in real time?

    PLAYMODE

    If I instantiate more objects over time, will they still instance together? Or do all instances have to be ready upon start?

    If you can do so, would adding/removing a few thousand instances rapidly and constantly bog down the performance, or is it rather seemless?

    How will vertex animation effect instances? Will it work at all? The same?

    Can you instance skinned meshes such as characters and animate independently?
     
  2. zeroyao

    zeroyao

    Unity Technologies

    Joined:
    Mar 28, 2013
    Posts:
    169
    Hi,

    > Do instances work in the editor or only in real time?
    Instancing works both in Editor and player. Make sure your player runs on a capable platform.

    > If I instantiate more objects over time, will they still instance together?
    Yes. Instancing doesn't require any preprocessing of the objects. So it applies to runtime-created objects.

    > If you can do so, would adding/removing a few thousand instances rapidly and constantly bog down the performance, or is it rather seemless?
    Generally adding/removing gameobjects in large amount has significant overheads (things may have been optimized in recent Unity releases).

    > How will vertex animation effect instances? Will it work at all? The same?
    That depends if the animation changes the data stored in the mesh object. If you achieve the effect by e.g. using a vertex shader, the mesh object will stay intact thus still be instanced among objects. Instancing only works on the same Mesh object.

    > Can you instance skinned meshes such as characters and animate independently?
    We don't have built-in skinnedmesh instancing support.

    Cheers,
    Yao
     
  3. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Awesome, thanks.
     
  4. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    So, it seems unclear to me. Do I need to instantiate these objects
    + the instanced shader, or do I use Graphics.DrawMeshInstance + instanced shader? Or can it be done either way?

    And how can I debug that they are being instanced? Is there something in the profiler?

    The graprahics instancing docs don't really go into much depth of how instancing is used.
     
    Last edited: Dec 3, 2016
  5. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    Trying it by just solely using the shader, without calling drawmeshinstance, results in apparently no instancing. Am I doing something wrong or do we have to call drawmeshinstance for everything we want to use it?

    i.JPG
     
  6. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    It is showing instances now. Each batch is like 6 draw calls though but maybe thats normal since it has realtime shadows. And I also noticed you can just use the shader or you can pass Matrix4x4s into Graphics.DrawMeshInstanced (which seems to run much much better). In both cases the bottleneck is on the graphics obviously, but using the shader alone is really performance heavy, which is somewhat expected, but doesnt seem quite right. The 1023 instances per batch is kind of annoying but I suppose we can segment them out.
     
  7. bpears

    bpears

    Joined:
    Aug 23, 2012
    Posts:
    249
    I would like to know, once we get to the oddball remainder amount of instances after segmenting out batches of 1023, if we tell Graphics.DrawMeshInstanced to draw 1023 still, even though there are only say half that in the remaining batch of instances, is this ok to do? Would it still try to draw 1023?
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,350
    In forward using a single directional light with cascaded shadows will be 6 draw calls per object / instance set. One for each cascade, one for the forward camera depth texture pass, and one for finally rendering to the screen.
     
    Last edited: Dec 11, 2016
    Martin_H likes this.