Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Build Meshes with the Job System?

Discussion in 'Data Oriented Technology Stack' started by z000z, Feb 13, 2018.

  1. z000z

    z000z

    Joined:
    Dec 31, 2014
    Posts:
    73
    Is it on the radar possibly to use the job system for mesh building? Currently one of the biggest slowdowns I have at the moment is creating meshes, I've already got the actual vertices/normals/etc being created in background threads but just simply assigning the vertices to a mesh is a pretty intensive act and currently has to happen on the main thread.

    Any chance we might see some way of doing this in jobs?
     
    SiriusRU and kayb14 like this.
  2. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,738
    Sure but you could do it right now using compute shader
     
  3. z000z

    z000z

    Joined:
    Dec 31, 2014
    Posts:
    73
    The problem with the compute shader, is my target ATM includes iOS/Android devices that don't support the compute shader.
     
  4. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,487
  5. z000z

    z000z

    Joined:
    Dec 31, 2014
    Posts:
    73
    @LeonhardP That thread is about drawing meshes, this is for building a mesh.
     
  6. MartinGram

    MartinGram

    Unity Technologies

    Joined:
    Feb 24, 2017
    Posts:
    39
    Hello z000z,

    This is a feature that is on our list to do, but it will not be ready for the 2018.1 release.
     
    joshcamas, SiriusRU, kayb14 and 3 others like this.
  7. z000z

    z000z

    Joined:
    Dec 31, 2014
    Posts:
    73
  8. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,738
    iOS supports compute, only android does not ;)
     
  9. supron

    supron

    Joined:
    Aug 24, 2013
    Posts:
    43
    What about animations? Making AnimationClip is even slower than creating meshes. I hope this is also on your list.
     
  10. kayb14

    kayb14

    Joined:
    Jun 12, 2016
    Posts:
    8
    What about the 2019 release? Please make a list of currently available job types.
    It's really hard to find them scattered around forums, github and the official api.

    anyways so much thanks for the year 2018 with unity!!!
     
    Bas-Smit likes this.
  11. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    100
    @MartinGram any update on this? Should I be able to use GetNativeVertexBufferPtr? I tried for a while but only got editor crashes for my troubles.
     
    learc83 likes this.
  12. LiamMitchell

    LiamMitchell

    Joined:
    Oct 30, 2015
    Posts:
    1
    Interested in this, any update?
     
    Jes28 likes this.
  13. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    818
    Mesh building via Jobs would also be at the top of my wishlist regarding ECS API.

    Any ETA?
     
    Jes28 likes this.
  14. mohydineName

    mohydineName

    Joined:
    Aug 30, 2009
    Posts:
    301
    Yes, ETA?
     
    Ivan-Pestrikov, Bas-Smit and Jes28 like this.
  15. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    240
    Will we have a way to write mesh vertex data directly without copying whole array? Especially writing just some parts of vertices, not all of it
     
  16. Piefayth

    Piefayth

    Joined:
    Feb 7, 2017
    Posts:
    58
    Huh, I was experimenting with this a while ago. I don't really know if this approach is valid, but you can certainly write to a managed array in a bursted job if you're willing to give up all the safety restrictions.

    Code (CSharp):
    1. /* system */
    2. Vector3[] vertices = mesh.vertices;
    3. void* pVertices = UnsafeUtility.AddressOf(ref vertices[0]);
    4. int length = vertices.Length;
    5.  
    6. new SomeJob { pVertices = pVertices }.Schedule(length, 1, inputDeps);
    7.  
    8. /* job */
    9. [BurstCompile]
    10. unsafe struct SomeJob {
    11.     [NativeDisableUnsafePtrRestriction] public void* pVertices;
    12.  
    13.     public void Execute(int index) {
    14.         UnsafeUtility.WriteArrayElement(pVertices, index, new float3(1, 1, 1));
    15.     }
    16. }
    The only weakness of this is that after the job completes you need to reassign the vertices array you wrote to to the mesh. I can't figure out a way to write directly. This appears to duck the copy, but it weirds me out that this approach requires the reassignment, cause that gives me the impression there are two instances of the underlying array... I am posting with very incomplete knowledge, so discussion and corrections are welcome!
     
  17. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    100
    the first line copies the native array to managed, which is why you need to write it back, we want to write the native copy directly
     
    Thaina and Piefayth like this.
  18. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    240
    There would be no point to use unsafe pointer if you use
    mesh.vertices


    There was
    mesh.GetNativeVertexBufferPtr
    that could return IntPtr to vertex stream. But it was not safe and we hardly know the layout of that struct

    What I wish is that unity should have a function that could partially update the vertex buffer. Maybe like this

    Code (CSharp):
    1. mesh.SetVertexPositionAt(0,new Vector3(1,0,0));
    2. mesh.SetVertexPositionAt(1,new Vector3(0,1,0));
    3. mesh.SetVertexPositionAt(2,new Vector3(0,0,1));
    4.  
    5. Or maybe
    6.  
    7. // IEnumerable<ValueTuple<int,Vector3>>
    8. mesh.SetVertexPositions(new (int,Vector3)[]{
    9.     (0,new Vector3(1,0,0)),
    10.     (1,new Vector3(0,1,0)),
    11.     (2,new Vector3(0,0,1)),
    12. });
    13.  
    14. Or maybe
    15.  
    16. mesh.SetVertexPositions(1,new Vector3[]{ // apply from offset 1 of buffer with all 3 elements of array
    17.     new Vector3(1,0,0),
    18.     new Vector3(0,1,0),
    19.     new Vector3(0,0,1),
    20. });
    21.  
    22. and maybe
    23.  
    24. mesh.SetVertexPositions(1,0,2,new Vector3[]{ // apply from offset 1 of buffer since index 0 of array by 2 element
    25.     new Vector3(1,0,0),
    26.     new Vector3(0,1,0),
    27.     new Vector3(0,0,1),
    28. });
    29.  
    30.  
     
    Last edited: Apr 30, 2019
    Jes28 likes this.
  19. TheJavierD

    TheJavierD

    Joined:
    Jan 6, 2017
    Posts:
    16
    Yes please, we need this as well.
     
  20. s3vv4

    s3vv4

    Joined:
    Nov 22, 2017
    Posts:
    1
    Was there any progress towards this feature in the recent Unity versions?
     
  21. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    564
    What’s the current workarounds people are using. Do we just used ToArray() on native arrays?