Search Unity

[SOLVED] Potentially massive job: How to handle functions and methods?

Discussion in 'Entity Component System' started by Nyanpas, Feb 17, 2020.

  1. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    The greetings,
    I am working on a procedural mesh creation scripture that is currently running on the thread of main in a coroutine so I can split the worklode across as many frames as needed to ease CPU-load in order to make it work during runtime. Now, I have also implemented the job system for other parts of the game to help reduce main thread worklodes, like for checking distances, culling, and NPC logic. Basically a lot of vector math, which is exactly the same I am doing in this system as well.

    However, since what I am generating is different every time, it is a bit hard to predict the outcome (and it is the point as well, since I want to have unpredictability), which is why I am using List<T>[] for now to collect vertices and UVs to generate the meshes from. Now, I also have built an extensive library of methods and functions to help make this as streamlined as possible. The maine creation loop calls these methods and functions which generates tris, quads, strips, ngons, and so forth based on a SegmentCollection-struct.

    So the question is, say I have a massive generation job that for simplicity and ease-of-use would need to call a few generic creation methods and functions, how would this work in the job system? Can I call "non-jobified" methods and functions in a job?

    Please assist.
    Thank you very much for your help. :3
     
  2. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    Sounds like a separate world could be useful here since they can run at different tick rates. Haven't done something like this yet, but the gist would be to package up everything your generation world needs from your main world, send it over, let the generation world work as long as it needs, and when your final generated data is ready send it back to the main world.

    If that's not what your looking for, try to break your massive job into smaller steps. You could then use a simple state machine and cached data to do X number of smaller step job per main world frame and update the data as you go.

    For these steps, run them on the main thread. For example, generate mesh verts, indices, and colors inside a job. Then on the main thread assemble the mesh and assign RenderMesh to an entity.
     
  3. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    if these methods are static, yes you can call them

    And if for some reason currently they are not static and rely on some state data in their class, you can always refactor them in a way where they get fed that data from the job through their params.

    The only rule is that there must not be any class types in any of those methods. It must be structs & Native collections only

    Here you can find examples of static utility functions being called from a job:
    https://github.com/Unity-Technologi...oller/Scripts/CharacterControllerAuthoring.cs

    For example, "CheckSupport" and "CollideAndIntegrate" are big complex functions hidden away in CharacterControllerUtilities
     
    Last edited: Feb 17, 2020
    Nyanpas likes this.
  4. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406