Search Unity

Offset Native array pointer?

Discussion in 'Scripting' started by RaymingW, Aug 19, 2019.

  1. RaymingW

    RaymingW

    Joined:
    Nov 11, 2017
    Posts:
    36
    I've been playing around the Job system mesh generating. One big performance bottleneck I found is native array coping. Using a for loop is just not ideal.

    I found this article there explaining how to copy a float3 native array to mesh.vertices with MemCpy
    https://gist.github.com/LotteMakesStuff/c2f9b764b15f74d14c00ceb4214356b4

    However, I want to take it to the next level, I want to modify a certain section of the native array in one Job based on the index. Is there a way for me to offset the pointer based on the index?
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Tho I'm not familiar with Jobs, presumably you can still use pointer arithmetic?
    Code (CSharp):
    1. var pointer = (byte*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(NativeArray);
    2. var offset = pointer + index * stride;
     
  3. RaymingW

    RaymingW

    Joined:
    Nov 11, 2017
    Posts:
    36
    Ok it worked, in the article the pointer type is void and I can't apply '+' to it, all I need to do is change pointer type to Vector3.
    Thank you!
     
  4. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    The pointer must be an integer type to perform arithmetic.