Search Unity

Chain constraint parallelisation

Discussion in 'Animation Rigging' started by shamsfk, Apr 24, 2020.

  1. shamsfk

    shamsfk

    Joined:
    Nov 21, 2014
    Posts:
    307
    Hi!

    I'm making a jiggling chain constraint and I need it to be as performant as possible.

    Is it beneficial performance-wise to make a separate constraint on each bone?

    My thoughts: separate constraints => parallel computations on bones => long chains are more performant
     
  2. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hi,
    For a jiggle chain constraint, the most performant would be to limit the implementation to a single constraint. Constraints in Animation Rigging are not evaluated in parallel to one another, but rather in parallel to other animated characters. Also, since a chain constraint always inherits positions from its parent transform, it wouldn't be possible to parallelize efficiently because of the inherent constraints dependencies.
     
    shamsfk likes this.
  3. shamsfk

    shamsfk

    Joined:
    Nov 21, 2014
    Posts:
    307
    Thank you for an answer! Really appreciate it.

    Is there any info/document/faq/list of hints on a topic of Animation Rigging performance practices and considerations?

    Currently, I did a series of cartoony constraints that squash and jiggle and wiggle characters, and as I target mobile VR performance is the main driving factor here.

    I have 2 types of jiggling - simple and dynamic, for dynamic it is true until you have 2 or more parallel chains (yet as I understood there is no performance reason to keep them as separate constraints), and a simple one is just a bunch of randomized sine rotations that are 1) way cheaper (yet still not free on my target and every bit of optimization wisdom like the one above is very welcome) and 2) doesn't need any initial motion to play (it actually can give a lot of juice to a cartoony character)
     
    Last edited: May 13, 2020
  4. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    There is no specific document dedicated to performances best practices, but we've covered most of the implementation details in the talks/blogs/projects we've shared in the past year. Everything is bundled on the following thread: https://forum.unity.com/threads/compilation-of-resources-for-animation-rigging.852973/

    There are a couple things you need to watch for performance wise when building constraints with Animation Rigging:

    - Use burst whenever you can. Adding the [BurstCompile] tag to your constraint job will ensure it's converted to optimized byte code and will greatly reduce execution time.

    - On top of burst, consider using the Unity.Mathematics package. Even though the constraints in the Animation Rigging package don't make use of it (they've been implemented before Unity.Mathematics hit primetime), the Unity.Mathematics package makes use of SIMD instructions and will improve parallel execution of math operations.

    - Limit the number of constraints per character. Every constraint adds a Playable node to the animation graph, and there's a cost to this even if the node weight is zero. Since the animation system is native, we need to invoke managed C# code for every constraint, which can get costly for too many constraints.

    I hope this helps!
     
    shamsfk likes this.
  5. shamsfk

    shamsfk

    Joined:
    Nov 21, 2014
    Posts:
    307
    Thank you!

    One more question regarding performance.

    I've read that there is (or was?) a huge cost associated with enabling a rig. Something alongside 18ms was mentioned for the ninja rig.

    I've tried to test it but got no visible lag, yet I will have a lot of actors, up to 50 (mobile VR) with varying complexity of constraints and their GO will be enabled and disabled frequently (pooling system). Should I be worried or enabling/disabling game objects with rigs on em doesn't cause lags (anymore, maybe it got fixed?)?
     
    Last edited: May 19, 2020
  6. simonbz

    simonbz

    Unity Technologies

    Joined:
    Sep 28, 2015
    Posts:
    295
    Hi,

    Toggling a game object with an animator component will cause a rebind operation on the animator. When doing that, the Animation Rigging constraints also need to rebuild their PlayableGraph. This is certainly a costly operation as in something you don't want to do every frame, but this is common practice in managing animation memory.

    For the Animator state machine, you can enable `Animator.keepAnimatorControllerStateOnDisable` to avoid clearing the state machine playable graph when disabling the Animator. In the end, it's a tradeoff of memory for performance.
     
    shamsfk likes this.