Search Unity

GPU Instancing

Discussion in '5.4 Beta' started by djarcas, Dec 29, 2015.

  1. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    So... some documentation? Limits? Restrictions? Usage? Anything?

    Please PLEASE tell me this isn't going to be like batching again - it works, but kinda, sorta, then stops working with absolutely no clue why and no documentation to help us make it work.
     
  2. AlteredPlanet

    AlteredPlanet

    Joined:
    Aug 12, 2013
    Posts:
    455
    yea , this version has crashes like every 15 minutes when in apllication
     
  3. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    294
    Would instancing work out of the box for the standard shader at the moment?
    Will it recognize same meshes (with same material) and use instancing for them if dynamic batching is enabled?
    Or do we need to write a special shader to use instancing at the moment?

    What are the plans for the release of 5.4? Will instancing be available for standard shader out of the box and applied automatically if dynamic batching is enabled?

    Thanks.
     
  4. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,492
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    7,853
    Yay Instancing! :cool::D

    :confused::(o_OBut I need to write instancing shaders, will the standard shader get an instancing option(please)?;)
     
  6. mgooding

    mgooding

    Joined:
    Mar 6, 2014
    Posts:
    10
    This is a really great addition. Thanks for the holiday surprise!

    Can Unity turn off dynamic batching for meshes that support instancing? The dynamic batching combination negates the benefits of instancing, and in my testing it's the difference between 4MM triangles at 35 FPS and 16MM triangles at 75 FPS.
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,021
    Read the doc, it's disrespectful to just gloss over and ignore the fact Unity will do it...
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,021
    My understanding is that it replaces what would normally be dynamically batched so you don't have to do anything. This is because the shader will be cooked up for it.
     
  9. mgooding

    mgooding

    Joined:
    Mar 6, 2014
    Posts:
    10
    That's what I would like to have happen, but unfortunately it's not how it currently works. If meshes can be dynamically batched, that happens before instancing. The number in my post are the difference between allowing dynamic batching for my test meshes and forcing it off.
     
  10. JonDadley

    JonDadley

    Joined:
    Sep 2, 2013
    Posts:
    139
    Does the standard shader that ships with B1 have the modifications to work with instancing? If not, has anyone made the modifications to the standard shader and could share it? I'm not hugely shader savvy (so can't make the changes myself) but I'd love to be able to start experimenting with GPU instancing.
     
  11. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    We have modified the standard shader to add instancing support, but didn't ship those changes in beta 1. You can expect it in a later beta.
     
    JonDadley likes this.
  12. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    We do realize it's not the best idea to tie instancing with dynamic batching as albeit sharing some similar batching criteria, these are two fundamentally different ways to do draw call batching and people want to use instancing without dynamic batching other stuff.

    We will make them two separate switches before final release.
     
    landon912, mgooding and Peter77 like this.
  13. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    I am mostly worried about this (from the "docs"):
    "Objects affected by multiple lights can’t be instanced efficiently. We are working on an improvement though."

    If this is not solved, and I mean solved entirely, then instancing will be of little use in many real world cases...

     
  14. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    Hey @yuanxing_cai , the switches sounds good but I do think that instancing should just be picked over dynamic batching if it's avaible, even when both are on. Unchecking dynamic batching would disable it too for platforms where instancing isn't supported. I hope that's considered. Thanks!
     
  15. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,021
    In my case it's incredibly useful regardless, and it would be lit perfectly fine as I'd perform my own lighting in shader. But I understand where you're coming from for general use cases... hope support is forthcoming.
     
  16. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    326
    Does 5.4 will support Sprite Renderer too ?
     
  17. mgooding

    mgooding

    Joined:
    Mar 6, 2014
    Posts:
    10
    I agree - I will settle for this switch, but it would be perfect if this could be handled automatically. Thanks!
     
  18. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,021
    Also we would find it terribly useful for skinned meshes :)
     
  19. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,021
    Don't forget dynamic batching does not have the same characteristics at all. Vertex limitations. You can't really be sharing these two settings...

    I'm not sure what hardware supports instancing but as far as I know it would nearly always be better to use Instancing. Is there a scenario where one might want dynamic batching ever, over instancing where hw support is there?
     
  20. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    My point exactly hippo - but right now it seems to be the other way around :) That should be fixed and I'm just afraid unity will say "there's a switch use that" and not fix it - hence the comment. There definitely should be a switch too though.
     
    mgooding likes this.
  21. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    384
    Is it possible to use this feature on skinned meshes?
     
  22. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    Not with our current way of doing GPU skinning and definitely not with CPU skinning.
     
  23. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    326
    @yuanxing_cai GPU instancing will works with sprite renderer ?
     
  24. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,541
    This make me curious how did you guys did the Unite demo where there's a lot of character moving in the middle of the city
     
  25. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,486
    No instancing, just plain old rendering separate characters :)

    We had about 20,000 characters being 'simulated' - just as positions/states/timings in a massive array - and used the CullingGroup API to track which ones were visible to the camera at which times. We had a pool of about 200 actual character GameObjects, and when the CullingGroup API told us that a character had become visible, we took a GameObject from the pool and 'switched over' to it (animation-driven movement, etc). When they were invisible again, we sent them back to the pool.

    I think it'd be theoretically possible for you to implement your own GPU matrix palette skinning shaders (or similar) for instanced characters, but I've not tried it yet...
     
  26. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,541
    What........... i never knew we have culling group API. . . . !!!?????? very usefull, need some sample how to use it :p.Alright CullingGroup API should be in other thread.
    But still 200 skinned mesh in the pool, isn't that gonna make some overhead in the CPU( for example for mid spec laptop )? :D
     
  27. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,486
    You might find the second half of this thread useful.
    Well, we also used some pretty aggressive LOD (the character is 8771 verts at highest detail, but only 443 at the lowest), and things like Optimise Game Objects. For sure, the more vertices you have to skin, the longer it's going to take, which is where GPU skinning and instancing can come in handy.
     
  28. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,541
    Thanks i'll check it out.
    Ah i see, that's make sense. in ACU they even using some very low res tube parented to bone with grey material for the lowes character LOD
     
  29. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    Any news on when instancing is expected to work on the Mac and other platforms?
     
  30. Hyp-X

    Hyp-X

    Joined:
    Jun 24, 2015
    Posts:
    400
    Modified a copy of the SpeedTree shaders to test this on trees.
    Made a simple test scene with just 2 copies of the same tree.
    Seems to work fine except:
    - Instancing doesn't kick in on all parts of tree in the shadow pass. The main camera depth & forward pass is working correctly though.
    - It doesn't work with trees painted on the terrain, only in separate game objects.
     
  31. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    This is known bug and the fix is on its way to the next build.
     
  32. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    We've got it up and running on GLES3. Still needs some finishing touches and testing before we ship.
     
    PeterB likes this.
  33. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    326
    SpriteRenderer will works with GPU instancing ? It can work better than dynamic batching for large scene.
     
  34. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    We're onto b3 now - either I'm really dumb, or this hasn't been changed? I've spent a few hours trying to get instancing working, and it's kiiinda there, but doing shadows is beyond me, and I'm acutely aware that I'm re-inventing the wheel. If it's just a shader you've modified, can you stick it in the doc? Most of want to use the amazing tools you guys write to make games, not write our own tools <3
     
    JonDadley likes this.
  35. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    SpriteRenderer doesn't utilize instancing at the moment, but as you said, it has the potential to benefit from instancing. We'll see what we could do about this after we get MeshRenderer instancing working on all supported platforms.
     
  36. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    Beta3 doesn't have it yet, but shader changes have passed our internal code review and are not far from release. Another good news for you - you will be able to use surface shader to generate the actual instancing code (including the shadow pass) with a simple #pragma directive.
     
  37. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    I'm sure all the excited and eager people on this thread wouldn't mind a copy of the shader before it's passed the internal code review ;-) Is that possible, at all, or is there too much in the way of depdencies?
     
  38. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    Okay then. If you insist. Disclaimer: it's still considered a WIP. Try it with a simple scene before apply the shader to your real game level.
     

    Attached Files:

  39. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    Whilst the drawcalls are indeed impressively and WONDERFULLY low, I seem to be spending 70ms a frame on the GPU inside RenderDeferred.Reflections. Unsure how to disable them right now.
     
  40. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    A few other performance notes:

    Rendering 8,000 objects in Unity 4 takes 22ms with, like, a zillion draw calls.
    Rendering 8,000 non-instanced objects in Unity 5 takes 35ms

    Honestly, this is a pretty piss-poor performance drop between Unity 4 and 5 - I thought U5 was going to be faster!

    Rendering 8,000 instanced objects in Unity 5 takes ~20ms, so that's a minor gain there.

    Rendering itself takes 2.5ms, almost all of which was spent in 'lighting' (1 directional light) and 'reflections' (which is amazing, as I haven't got anything like that switched on)

    Looking at where we're wasting CPU time is impressive; ~4ms is pissed up the wall in culling. As ever, I wish Unity would just let me bypass that, and do my own culling. Even Unreal isn't dumb enough to do culling on every objects, every frame! But I digress.

    Digging into the remaining time, we have:

    ~3ms spent in 'prepare render nodes'
    ~1.5ms spent in 'Render.Prepare'
    ~3ms in Lighting (again, a single directional light in this scene)
    ~3ms in the GBuffer
    ~3ms in PrepareShadowMap.

    This isn't much of a useful technique if the rest of Unity isn't going to keep up - any suggestions to get the performance out that I'm expecting? I was quite happy to whoosh 100,000 (low poly!) objects around on the Xbox 360 at 60 fps, so I'm hoping a modern PC should be able to manage, y'know, a little more! Here I have a GTX 970 managing just about 30fps with 8,000 low poly objects and a single directional light.

    Back to you!
     
    ZJP, Martin_H, landon912 and 2 others like this.
  41. AlteredPlanet

    AlteredPlanet

    Joined:
    Aug 12, 2013
    Posts:
    455
    great points , I so agree with the stats you pulled same here
     
  42. AlteredPlanet

    AlteredPlanet

    Joined:
    Aug 12, 2013
    Posts:
    455
    To be honest, it doesn't seem like telling them to fix a few bugs will solve anything, they will just break others in the process. Its the mindset that needs to change, if they are designing sloppy systems,then we will be in this "please fix, improve this " cycle forever
     
    elias_t and PeterB like this.
  43. JonDadley

    JonDadley

    Joined:
    Sep 2, 2013
    Posts:
    139
    Perhaps I'm out of line here but I think you're being a bit unreasonable - a user asked for a pre-release copy of the shader and we were given one as a favour with the express note that it was WIP. I don't think it's fair to judge something that hasn't even been released and is still being worked on (in a beta build no less) so harshly!
     
  44. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    Absolutely agreed. The Instancing part is doing an absolute bang-up job! The issue is around all the other parts of unity throwing their toys out of the pram when I stick 8,000 low poly objects into a scene, and my question is absolutely 'How do I get everything else to play nicely?'

    It may even be that the information I dug up can help steer things in the right direction (Example, does anyone know HOW to claw back all that wasted time in Reflections? How can I stop Unity wasting so much time culling? What on earth is PrepareRenderNodes, and how can I minimise time spent there?)

    Instancing isn't much use if other parts of Unity explode when you try and use it ;)
     
    elias_t, PeterB and Prodigga like this.
  45. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,748
    When you say Unity 5, which version is that?

    Currently 5.4 has some general performance regressions (compared to 5.3) in render node prepare etc. code. Later 5.4 beta should fix that.

    And a project for us to profile on would be useful too. Currently we use a bunch of our own demos, some other game projects, and synthetic scenes to profile. More is always welcome.

    What exactly is "Reflections"? Are you using deferred shading? If so, then reflections by default are done per pixel in a deferred way, though CPU cost of that should be tiny (but there's a GPU cost to render the probe objects). Deferred reflections can be turned off in graphics settings (set deferred reflections to "no support" there).

    If by "reflections" you mean something else, then more details please.
     
  46. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,384
    hi there,
    will there be support for LOD groups and LOD fading? or is there already?
    right now (beta 5) unity_LODFade seems still to be per draw...
     
  47. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,384
    how did you manage to get it work?
    i tested this with beta5 - no instancing as far as i can say.
     
  48. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    A few updates from 5.4.0.b6:

    I can draw about 6,000 of my test items, non-instanced, with shadows, and use up about 16ms of CPU and 14ms of GPU.

    The same mesh, instances, with shadows, can get up to about 12,000 copies, using up about 16ms of CPU and only 2.2ms of GPU - this is excellent!
     
    elias_t likes this.
  49. djarcas

    djarcas

    Joined:
    Nov 15, 2012
    Posts:
    160
    You want me to make you a project that spawns objects in a scene for you? :|

    You got me. That's just what the profiler says...

    http://i.imgur.com/OwhUIAQ.png

    http://i.imgur.com/Jj7OX4j.png

    That's a grab from 43,000 items. The editor crashed when I stopped running the scene, sadly. (Drawing, say, 50,000 items and then stopping the Editor running seems to be about 100% reliable way of locking it dead)
     
  50. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,021
    Well if wishes were fishes, poor men wouldn't starve. But I'm wishing to throw a similar amount around in Unity (who wouldn't?) - any idea where all the performance is being lost?
     
unityunity