Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Multithreaded mesh bounds recalculation?

Discussion in 'Entity Component System' started by keenanwoodall, Sep 20, 2018.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I'm working on a system for deforming meshes and right now the slowest part is the bounds recalculation (I was able to jobify normal recalculation). It would be simple to calculate bounds on a single other thread but I'm curious if it's possible to split the calculation over multiple threads with the job system.

    I'm not sure how to do it because each batch can't access the same bounds. I've never ventured beyond IJob and IJobParallelFor, is there another interface that would make this easier? I thought maybe I could deploy a bunch of batches and then have a Bounds instance encapsulate each batches' bounds on the main thread. That way I'm only encapsulating as many bounds as there are threads (roughly) instead of thousands of points. Is what I'm trying to do silly?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,955
    i'd just try to find minx,miny,minz,maxx,maxy,maz (which probably should work splitted on thread too, haven't used ecs)
    and then adjust/make bounds from that?
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Yea I know how to calculate the bounds, I was just wondering if there was a way to split the calculation across multiple threads or speed it up in some other way. The problem is each thread can't access the same bounds variable at the same time because that will break stuff. [edit] also I'm not using ECS, just the job system.
     
  4. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    345
    Maybe you ll find inspiration looking at MeshInstanceRender in the latest ECS sources :

    AppData\Local\Unity\cache\packages\staging-packages.unity.com\com.unity.entities@0.0.12-preview.13\Unity.Rendering.Hybrid\MeshInstanceRendererSystem.cs
     
  5. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Just split the vertices into X chunks (one per thread), calculate bounds per chunk, and then do the min/max of all chunks?
     
    Antypodish likes this.
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    That's what I was thinking. Is there a way to automate the splitting with the job system? Similarly to how you tell IJobParallelFor how many operations you want in a batch and it splits the native arrays for you behind the scenes?
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    This is where you define. See number 32

    Code (CSharp):
    1. protected override JobHandle OnUpdate ( JobHandle inputDeps )
    2.     {
    3.         return new MyJob
    4.         {
    5.             data = data
    6.         }.Schedule ( data.Length, 32, inputDeps ) ;
    7.     }
     
  8. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I know how to schedule a job and give it a batch size, but I can't get each batches' uniquely calculated bounds. The end result is a single job and a single version of whatever data was in the job. The goal is to use the job system to calculate the bounds of separate chunks of the mesh and then create one final 'Bounds' instance that encapsulates each batches' chunk's bounds on the main thread.
     
  9. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Use use the .NET Task API, it allows you to return values from each batch easily.