private bool TriangleContainsVertexInList(int indexPrev, int indexCurr, int indexNext, NativeLinkedList <int> list)
        {
            for (NativeLinkedList <int> .Enumerator currIndexNode = list.Head; currIndexNode.IsValid; currIndexNode.MoveNext())
            {
                NativeLinkedList <int> .Enumerator prevIndexNode = (currIndexNode.Prev.IsValid) ? currIndexNode.Prev : list.Tail;
                NativeLinkedList <int> .Enumerator nextIndexNode = (currIndexNode.Next.IsValid) ? currIndexNode.Next : list.Head;

                bool isCurrentConvex = Math2DUtils.IsVertexConvex(Polygon[prevIndexNode.Value], Polygon[currIndexNode.Value], Polygon[nextIndexNode.Value], true);
                if (isCurrentConvex)
                {
                    continue;
                }

                int currIndexToCheck = currIndexNode.Value;
                if (currIndexToCheck == indexPrev || currIndexToCheck == indexCurr || currIndexToCheck == indexNext)
                {
                    continue;
                }

                if (Math2DUtils.IsInsideTriangle(Polygon[currIndexToCheck], Polygon[indexPrev], Polygon[indexCurr], Polygon[indexNext]))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        private NativeLinkedList <T> GetList(int index)
        {
            NativeLinkedList <T> value = new NativeLinkedList <T>();

            UnsafeUtility.MemCpy(UnsafeUtility.AddressOf(ref value), (mainArray + index * UnsafeUtility.SizeOf <NativeLinkedList <T> >()).ToPointer(), UnsafeUtility.SizeOf <NativeLinkedList <T> >());
            return(value);
        }
Exemplo n.º 3
0
        public VirtualTexture(int resolution, RenderTextureFormat format)
        {
            this.resolution = resolution;
            this.format     = format;
            allSparse       = new ArrayOfNativeLink <TextureSparse>((int)(0.01f + log2(resolution)), Allocator.Persistent, (a, b) =>
            {
                bool2 offset = a.offset == b.offset;
                bool2 size   = a.size == b.size;
                return(offset.x && offset.y && size.x && size.y);
            });
            NativeLinkedList <TextureSparse> originSparse = allSparse[allSparse.Length - 1];

            originSparse.AddLast(new TextureSparse
            {
                offset = 0,
                size   = resolution / 2,
            });
            originSparse.AddLast(new TextureSparse
            {
                offset = int2(0, resolution / 2),
                size   = resolution / 2,
            });
            originSparse.AddLast(new TextureSparse
            {
                offset = int2(resolution / 2, 0),
                size   = resolution / 2,
            });
            originSparse.AddLast(new TextureSparse
            {
                offset = resolution / 2,
                size   = resolution / 2,
            });
        }
Exemplo n.º 4
0
 public void Execute()
 {
     NativeLinkedList <int> .Enumerator e = List.Tail;
     for (int i = 0; i < NumNodesToInsert; ++i)
     {
         e = List.InsertAfter(e, i);
     }
 }
Exemplo n.º 5
0
 public NativeLinkedList <T> this[int index]
 {
     get
     {
         NativeLinkedList <T> value = new NativeLinkedList <T>();
         UnsafeUtility.MemCpy(UnsafeUtility.AddressOf(ref value), (mainArray + index * UnsafeUtility.SizeOf <NativeLinkedList <T> >()).ToPointer(), UnsafeUtility.SizeOf <NativeLinkedList <T> >());
         return(value);
     }
 }
Exemplo n.º 6
0
 public bool MoveNext()
 {
     ptr = next;
     if (ptr.ToPointer() == null)
     {
         return(false);
     }
     next = new UIntPtr(*NativeLinkedList <T> .GetNextPtr(next));
     return(true);
 }
        private NativeLinkedList <int> StorePolygonContourAsLinkedList()
        {
            int totNumVerts = Polygon.HolesNum * 2 + Polygon.VerticesNum;
            NativeLinkedList <int> linkedList = new NativeLinkedList <int>(totNumVerts, Allocator.Temp);

            //add contour points to the vertices linked list and set the max ray length
            for (int i = 0; i < Polygon.ContourPointsNum; ++i)
            {
                linkedList.InsertAfter(linkedList.Tail, i);
            }
            return(linkedList);
        }
        private void Triangulate(int polygonIndex, NativeLinkedList <int> list)
        {
            int trisIndex = 0;

            for (int pi = 0; pi < polygonIndex; ++pi)
            {
                trisIndex += Polygons.GetPolygonHolesNum(pi) * 2 + Polygons.GetPolygonNumVertices(pi) - 2;
            }
            trisIndex *= 3;

            while (list.Length > 2)
            {
                bool hasRemovedEar = false;

                int currListIndex = 0;
                NativeLinkedList <int> .Enumerator currIndexNode = list.Head;
                for (int i = 0; i < list.Length; ++i)
                {
                    NativeLinkedList <int> .Enumerator prevIndexNode = (currIndexNode.Prev.IsValid) ? currIndexNode.Prev : list.Tail;
                    NativeLinkedList <int> .Enumerator nextIndexNode = (currIndexNode.Next.IsValid) ? currIndexNode.Next : list.Head;

                    bool isCurrentConvex = Math2DUtils.IsVertexConvex(Polygons[prevIndexNode.Value], Polygons[currIndexNode.Value], Polygons[nextIndexNode.Value], true);
                    if (isCurrentConvex)
                    {
                        bool triangleContainsAVertex = TriangleContainsVertexInList(prevIndexNode.Value, currIndexNode.Value, nextIndexNode.Value, list);
                        if (!triangleContainsAVertex)
                        {
                            OutTriangles[trisIndex]     = nextIndexNode.Value;
                            OutTriangles[trisIndex + 1] = currIndexNode.Value;
                            OutTriangles[trisIndex + 2] = prevIndexNode.Value;
                            trisIndex += 3;

                            list.Remove(currIndexNode);

                            hasRemovedEar = true;
                            break;
                        }
                    }

                    currListIndex = (currListIndex + 1) % list.Length;
                    currIndexNode = list.GetEnumeratorAtIndex(currListIndex);
                }

                if (!hasRemovedEar)
                {
                    return;
                }
            }
        }
        private NativeLinkedList <int> StorePolygonContourAsLinkedList(int polygonIndex)
        {
            int polygonNumHoles               = Polygons.GetPolygonHolesNum(polygonIndex);
            int polygonNumVertices            = Polygons.GetPolygonNumVertices(polygonIndex);
            int totNumVerts                   = polygonNumHoles * 2 + polygonNumVertices;
            NativeLinkedList <int> linkedList = new NativeLinkedList <int>(totNumVerts, Allocator.Temp);
            //add contour points to the vertices linked list and set the max ray length
            int polygonStartIndex       = Polygons.GetPolygonStartIndex(polygonIndex);
            int polygonContourLastIndex = polygonStartIndex + Polygons.GetContourPointsNum(polygonIndex);

            for (int i = polygonStartIndex; i < polygonContourLastIndex; ++i)
            {
                linkedList.InsertAfter(linkedList.Tail, i);
            }
            return(linkedList);
        }
Exemplo n.º 10
0
 private static void TestLinkedList <T>(NativeLinkedList <T> llist) where T : unmanaged
 {
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.Write("Test: ");
     Console.ForegroundColor = ConsoleColor.Blue;
     Console.Write(nameof(NativeLinkedList <T>));
     Console.ForegroundColor = ConsoleColor.Yellow;
     Console.Write('<');
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.Write(typeof(T).Name);
     Console.ForegroundColor = ConsoleColor.Yellow;
     Console.Write('>');
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.Write(" --> ");
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine(llist.ToString());
 }
Exemplo n.º 11
0
 public void CombineTexture()
 {
     for (int i = 0; i < allSparse.Length - 1; ++i)
     {
         NativeLinkedList <TextureSparse> sparse  = allSparse[i];
         NativeList <TextureSparse>       zeroPos = new NativeList <TextureSparse>(sparse.Length, Allocator.Temp);
         foreach (var j in sparse)
         {
             bool2 ald = IsSparseAligned(j);
             if (ald.x && ald.y)
             {
                 zeroPos.Add(j);
             }
         }
         foreach (var j in zeroPos)
         {
             TextureSparse *array = stackalloc TextureSparse[] {
                 new TextureSparse
                 {
                     offset = j.offset + int2(j.size, 0),
                     size   = j.size,
                 },
                 new TextureSparse
                 {
                     offset = j.offset + int2(0, j.size),
                     size   = j.size,
                 },
                 new TextureSparse
                 {
                     offset = j.offset + j.size,
                     size   = j.size,
                 },
                 j
             };
             if (sparse.ContainsDatas(array, 3))
             {
                 sparse.RemoveData(array, 4);
                 allSparse[i + 1].AddLast(new TextureSparse
                 {
                     offset = j.offset,
                     size   = j.size * 2,
                 });
             }
         }
     }
 }
Exemplo n.º 12
0
        private TextureSparse GetNewSparse(int index)
        {
            if (index >= allSparse.Length)
            {
                throw new Exception("Out of Range!");
            }
            NativeLinkedList <TextureSparse> currentSparseList = allSparse[index];

            if (currentSparseList.Length > 0)
            {
                TextureSparse sparse = *currentSparseList.GetLast();
                currentSparseList.RemoveLast();
                return(sparse);
            }
            TextureSparse nextSparse = GetNewSparse(index + 1);
            int           size       = nextSparse.size / 2;

            currentSparseList.AddLast(new TextureSparse
            {
                offset = nextSparse.offset,
                size   = size,
            });
            currentSparseList.AddLast(new TextureSparse
            {
                offset = nextSparse.offset + int2(0, size),
                size   = size,
            });
            currentSparseList.AddLast(new TextureSparse
            {
                offset = nextSparse.offset + int2(size, 0),
                size   = size,
            });
            return(new TextureSparse
            {
                offset = nextSparse.offset + size,
                size = size,
            });
        }
Exemplo n.º 13
0
    private static void TestLinkedList()
    {
        const int count = 10;

        using var llist = new NativeLinkedList <LCRange>();
        for (int i = 0; i < count; i++)
        {
            llist.AddAfter(new LCRange(i, (i + 7) * 3));
        }

        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("-- foreach supported --");
        Console.ForegroundColor = ConsoleColor.Blue;
        foreach (var item in llist)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine($"Peek -> {llist.Peek()} -> Count: {llist.Count}\n");
        Debug.Assert(llist.Count == count);
        // Pops Nodes
        Console.WriteLine($"Pop  -> {llist.Pop()} -> Count: {llist.Count}");
        Debug.Assert(llist.Count == count - 1);
        Console.WriteLine($"Pop  -> {llist.Pop()} -> Count: {llist.Count}");
        Debug.Assert(llist.Count == count - 2);
        Console.WriteLine($"Pop  -> {llist.Pop()} -> Count: {llist.Count}");
        Debug.Assert(llist.Count == count - 3);
        // Clear All Nodes and free (Count-1)*size memory spaces
        llist.Clear();
        Debug.Assert(llist.IsEmpty);
        Console.WriteLine($"Clear -> Count: {llist.Count}");
        Console.WriteLine();

        TestLinkedList(llist);
    }
        public void Execute()
        {
            NativeLinkedList <int> hullVertices = StorePolygonContourAsLinkedList();

            #region Removing Holes
            //create the array containing the holes data
            NativeArray <ECHoleData> holes = GetHolesDataSortedByMaxX();
            //remove holes
            for (int hi = 0; hi < holes.Length; ++hi)
            {
                ECHoleData hole = holes[hi];
                var        intersectionEdgeP0 = hullVertices.GetEnumerator();
                var        intersectionEdgeP1 = hullVertices.GetEnumerator();
                float2     intersectionPoint  = new float2(float.MaxValue, hole.BridgePoint.y);

                for (var currentHullVertex = hullVertices.Head; currentHullVertex.IsValid; currentHullVertex.MoveNext())
                {
                    var nextHullVertex = (currentHullVertex.Next.IsValid) ? currentHullVertex.Next : hullVertices.Head;

                    float2 currPoint = Polygon[currentHullVertex.Value];
                    float2 nextPoint = Polygon[nextHullVertex.Value];

                    //M is to the left of the line containing the edge (M is inside the outer polygon)
                    bool isMOnLeftOfEdgeLine = (Math2DUtils.LineSide(hole.BridgePoint, currPoint, nextPoint) < 0f);
                    if (isMOnLeftOfEdgeLine)
                    {
                        continue;
                    }

                    // at least one point must be to right of the hole bridge point for intersection with ray to be possible
                    if (currPoint.x < hole.BridgePoint.x && nextPoint.x < hole.BridgePoint.x)
                    {
                        continue;
                    }

                    if (currPoint.y > hole.BridgePoint.y == nextPoint.y > hole.BridgePoint.y)
                    {
                        continue;
                    }

                    float intersectionX = nextPoint.x; // if line p0,p1 is vertical
                    if (math.abs(currPoint.x - nextPoint.x) > float.Epsilon)
                    {
                        float intersectY = hole.BridgePoint.y;
                        float gradient   = (currPoint.y - nextPoint.y) / (currPoint.x - nextPoint.x);
                        float c          = nextPoint.y - gradient * nextPoint.x;
                        intersectionX = (intersectY - c) / gradient;
                    }

                    if (intersectionX < intersectionPoint.x)
                    {
                        intersectionPoint.x = intersectionX;
                        intersectionEdgeP0  = currentHullVertex;
                        intersectionEdgeP1  = nextHullVertex;
                    }
                }

                var selectedHullBridgePoint = hullVertices.GetEnumerator();
                //If I is a vertex of the outer polygon, then M and I are mutually visible
                if (Math2DUtils.SamePoints(intersectionPoint, Polygon[intersectionEdgeP0.Value]))
                {
                    selectedHullBridgePoint = intersectionEdgeP0;
                }
                else if (Math2DUtils.SamePoints(intersectionPoint, Polygon[intersectionEdgeP1.Value]))
                {
                    selectedHullBridgePoint = intersectionEdgeP1;
                }
                else
                {
                    //Select P to be the endpoint of maximum x-value for this edge
                    var P = (Polygon[intersectionEdgeP0.Value].x > Polygon[intersectionEdgeP1.Value].x) ? intersectionEdgeP0 : intersectionEdgeP1;

                    bool  existReflexVertexInsideMIP = false;
                    float minAngle = float.MaxValue;
                    float minDist  = float.MaxValue;
                    for (var currOuterPolygonVertex = hullVertices.Head; currOuterPolygonVertex.IsValid; currOuterPolygonVertex.MoveNext())
                    {
                        if (currOuterPolygonVertex.Value == P.Value)
                        {
                            continue;
                        }

                        var nextOuterPolygonVertex = (currOuterPolygonVertex.Next.IsValid) ? currOuterPolygonVertex.Next : hullVertices.Head;
                        var prevOuterPolygonVertex = (currOuterPolygonVertex.Prev.IsValid) ? currOuterPolygonVertex.Prev : hullVertices.Tail;

                        if (Math2DUtils.IsVertexReflex(
                                Polygon[prevOuterPolygonVertex.Value],
                                Polygon[currOuterPolygonVertex.Value],
                                Polygon[nextOuterPolygonVertex.Value],
                                true))
                        {
                            bool isInsideMIPTriangle = Math2DUtils.IsInsideTriangle(Polygon[currOuterPolygonVertex.Value],
                                                                                    hole.BridgePoint,
                                                                                    intersectionPoint,
                                                                                    Polygon[P.Value]);
                            existReflexVertexInsideMIP |= isInsideMIPTriangle;

                            if (isInsideMIPTriangle)
                            {
                                //search for the reflex vertex R that minimizes the angle between (1,0) and the line segment M-R
                                float2 MR       = Polygon[currOuterPolygonVertex.Value] - hole.BridgePoint;
                                float  angleMRI = math.atan2(MR.y, MR.x);
                                if (angleMRI < minAngle)
                                {
                                    selectedHullBridgePoint = currOuterPolygonVertex;
                                    minAngle = angleMRI;
                                }
                                else if (math.abs(angleMRI - minAngle) <= float.Epsilon)
                                {
                                    //same angle
                                    float lengthMR = math.length(MR);
                                    if (lengthMR < minDist)
                                    {
                                        selectedHullBridgePoint = currOuterPolygonVertex;
                                        minDist = lengthMR;
                                    }
                                }
                            }
                        }

                        if (!existReflexVertexInsideMIP)
                        {
                            selectedHullBridgePoint = P;
                        }
                    }
                }

                hullVertices.InsertAfter(selectedHullBridgePoint, selectedHullBridgePoint.Value);
                for (int i = hole.BridgePointIndex - hole.HoleFirstIndex, count = 0;
                     count < hole.HoleLength;
                     i = (i + hole.HoleLength - 1) % hole.HoleLength, ++count)
                {
                    hullVertices.InsertAfter(selectedHullBridgePoint, i + hole.HoleFirstIndex);
                }
                hullVertices.InsertAfter(selectedHullBridgePoint, hole.BridgePointIndex);
            }
            holes.Dispose();
            #endregion

            Triangulate(hullVertices);
            hullVertices.Dispose();
        }
Exemplo n.º 15
0
 private void SetList(int index, NativeLinkedList <T> list)
 {
     UnsafeUtility.MemCpy((mainArray + index * UnsafeUtility.SizeOf <NativeLinkedList <T> >()).ToPointer(), UnsafeUtility.AddressOf(ref list), UnsafeUtility.SizeOf <NativeLinkedList <T> >());
 }
Exemplo n.º 16
0
    // Run the test

    void Start()
    {
        WarmUpJobSystem();

        const int size =
#if UNITY_EDITOR
            1000
#else
            10000
#endif
        ;

        const int chunkSize           = 1024;
        const int numElementsPerChunk = chunkSize / sizeof(int);

        // Create native collections

        NativeArray <int> sum   = new NativeArray <int>(1, Allocator.TempJob);
        NativeArray <int> array = new NativeArray <int>(
            size,
            Allocator.TempJob);
        NativeList <int> list = new NativeList <int>(
            0,
            Allocator.TempJob);
        NativeLinkedList <int> linkedList = new NativeLinkedList <int>(
            0,
            Allocator.TempJob);
        NativeChunkedList <int> chunkedList = new NativeChunkedList <int>(
            numElementsPerChunk,
            0,
            Allocator.TempJob);
        NativeHashSet <int> hashSet = new NativeHashSet <int>(
            0,
            Allocator.TempJob);
        NativeIntPtr             nativeIntPtr             = new NativeIntPtr(Allocator.TempJob);
        NativePerJobThreadIntPtr nativePerJobThreadIntPtr = new NativePerJobThreadIntPtr(
            Allocator.TempJob);

        // Run add jobs

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

        ListAddJob listAddJob = new ListAddJob
        {
            List             = list,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        listAddJob.Run();
        long listAddTicks = sw.ElapsedTicks;

        LinkedListAddJob linkedListAddJob = new LinkedListAddJob
        {
            List             = linkedList,
            NumNodesToInsert = size
        };

        sw.Reset();
        sw.Start();
        linkedListAddJob.Run();
        long linkedListAddTicks = sw.ElapsedTicks;

        ChunkedListAddJob chunkedListAddJob = new ChunkedListAddJob
        {
            List             = chunkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        chunkedListAddJob.Run();
        long chunkedListAddTicks = sw.ElapsedTicks;

        HashSetAddJob hashSetAddJob = new HashSetAddJob
        {
            Set = hashSet,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        hashSetAddJob.Run();
        long hashSetAddTicks = sw.ElapsedTicks;

        // Run iterate jobs

        ArrayIterateJob arrayIterateJob = new ArrayIterateJob
        {
            Array = array,
            Sum   = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        arrayIterateJob.Run();
        long arrayIterateTicks = sw.ElapsedTicks;

        ListIterateJob listIterateJob = new ListIterateJob
        {
            List = list,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        listIterateJob.Run();
        long listIterateTicks = sw.ElapsedTicks;

        LinkedListIterateJob linkedListIterateJob = new LinkedListIterateJob
        {
            List = linkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        linkedListIterateJob.Run();
        long linkedListIterateTicks = sw.ElapsedTicks;

        ChunkedListIterateJob chunkedListIterateJob = new ChunkedListIterateJob
        {
            List = chunkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        chunkedListIterateJob.Run();
        long chunkedListIterateTicks = sw.ElapsedTicks;

        ArrayIterateJobParallelFor arrayIterateJobParallelFor = new ArrayIterateJobParallelFor
        {
            Array = array,
            Sum   = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        arrayIterateJobParallelFor.Run(size);
        long arrayIterateParallelForTicks = sw.ElapsedTicks;

        linkedList.PrepareForParallelForJob();
        LinkedListIterateJobParallelFor linkedListIterateJobParallelFor = new LinkedListIterateJobParallelFor
        {
            List = linkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        linkedListIterateJobParallelFor.Run(size);
        long linkedListIterateParallelForTicks = sw.ElapsedTicks;

        chunkedList.PrepareForParallelForJob();
        ChunkedListIterateJobParallelFor chunkedListIterateJobParallelFor = new ChunkedListIterateJobParallelFor
        {
            List = chunkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        chunkedListIterateJobParallelFor.RunRanged(size);
        long chunkedListIterateParallelForTicks = sw.ElapsedTicks;

        // Clear native collections

        list.Clear();
        linkedList.Clear();
        chunkedList.Clear();

        // Run insert jobs

        LinkedListInsertJob linkedListInsertJob = new LinkedListInsertJob
        {
            LinkedList       = linkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        linkedListInsertJob.Run();
        long linkedListInsertTicks = sw.ElapsedTicks;

        ChunkedListInsertJob chunkedListInsertJob = new ChunkedListInsertJob
        {
            List             = chunkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        chunkedListInsertJob.Run();
        long chunkedListInsertTicks = sw.ElapsedTicks;

        // Run remove jobs

        LinkedListRemoveJob linkedListRemoveJob = new LinkedListRemoveJob
        {
            List = linkedList,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        linkedListRemoveJob.Run();
        long linkedListRemoveTicks = sw.ElapsedTicks;

        ChunkedListRemoveJob chunkedListRemoveJob = new ChunkedListRemoveJob
        {
            List = chunkedList,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        chunkedListRemoveJob.Run();
        long chunkedListRemoveTicks = sw.ElapsedTicks;

        HashSetRemoveJob hashSetRemoveJob = new HashSetRemoveJob
        {
            Set = hashSet,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        hashSetRemoveJob.Run();
        long hashSetRemoveTicks = sw.ElapsedTicks;

        // Run NativeIntPtr and NativePerJobThreadIntPtr jobs

        NativeIntPtrParallelJob nativeIntPtrParallelJob = new NativeIntPtrParallelJob
        {
            Array = array,
            Sum   = nativeIntPtr.GetParallel()
        };

        sw.Reset();
        sw.Start();
        nativeIntPtrParallelJob.Run(size);
        long nativeIntPtrTicks = sw.ElapsedTicks;

        NativePerJobThreadIntPtrParallelJob nativePerJobThreadIntPtrParallelJob = new NativePerJobThreadIntPtrParallelJob
        {
            Array = array,
            Sum   = nativePerJobThreadIntPtr.GetParallel()
        };

        sw.Reset();
        sw.Start();
        nativePerJobThreadIntPtrParallelJob.Run(size);
        long nativePerJobThreadIntPtrTicks = sw.ElapsedTicks;

        // Report results
        Debug.Log(
            "Operation,Job Type,NativeArray,NativeList,NativeLinkedList,NativeChunkedList,NativeHashSet\n" +
            "Add,Single," + "n/a" + "," + listAddTicks + "," + linkedListAddTicks + "," + chunkedListAddTicks + "," + hashSetAddTicks + "\n" +
            "Iterate,Single," + arrayIterateTicks + "," + listIterateTicks + "," + linkedListIterateTicks + "," + chunkedListIterateTicks + "," + "n/a" + "\n" +
            "Iterate,ParallelFor," + arrayIterateParallelForTicks + "," + "n/a" + "," + linkedListIterateParallelForTicks + "," + chunkedListIterateParallelForTicks + "," + "n/a" + "\n" +
            "Insert,Single," + "n/a" + "," + "n/a" + "," + linkedListInsertTicks + "," + chunkedListInsertTicks + "," + "n/a" + "\n" +
            "Remove,Single," + "n/a" + "," + "n/a" + "," + linkedListRemoveTicks + "," + chunkedListRemoveTicks + "," + hashSetRemoveTicks);
        Debug.Log(
            "Operation,Job Type,NativeIntPtr,NativePerJobThreadIntPtr\n" +
            "Sum,ParallelFor," + nativeIntPtrTicks + "," + nativePerJobThreadIntPtrTicks);

        // Dispose native collections
        sum.Dispose();
        array.Dispose();
        list.Dispose();
        linkedList.Dispose();
        chunkedList.Dispose();
        hashSet.Dispose();
        nativeIntPtr.Dispose();
        nativePerJobThreadIntPtr.Dispose();

        // Quit
#if UNITY_EDITOR
        EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }
Exemplo n.º 17
0
    // Warm up the job system

    static void WarmUpJobSystem()
    {
        // Create native collections

        NativeArray <int> sum   = new NativeArray <int>(1, Allocator.TempJob);
        NativeArray <int> array = new NativeArray <int>(
            4,
            Allocator.TempJob);
        NativeList <int> list = new NativeList <int>(
            4,
            Allocator.TempJob);
        NativeLinkedList <int> linkedList = new NativeLinkedList <int>(
            4,
            Allocator.TempJob);
        NativeChunkedList <int> chunkedList = new NativeChunkedList <int>(
            4,
            4,
            Allocator.TempJob);
        NativeHashSet <int> hashSet = new NativeHashSet <int>(
            4,
            Allocator.TempJob);
        NativeIntPtr             nativeIntPtr             = new NativeIntPtr(Allocator.TempJob);
        NativePerJobThreadIntPtr nativePerJobThreadIntPtr = new NativePerJobThreadIntPtr(
            Allocator.TempJob);

        // Create jobs

        ListAddJob listAddJob = new ListAddJob
        {
            List = list
        };
        LinkedListAddJob linkedListAddJob = new LinkedListAddJob
        {
            List = linkedList
        };
        ChunkedListAddJob chunkedListAddJob = new ChunkedListAddJob
        {
            List = chunkedList
        };
        HashSetAddJob hashSetAddJob = new HashSetAddJob
        {
            Set = hashSet
        };
        ArrayIterateJob arrayIterateJob = new ArrayIterateJob
        {
            Array = array,
            Sum   = sum
        };
        ListIterateJob listIterateJob = new ListIterateJob
        {
            List = list,
            Sum  = sum
        };
        LinkedListIterateJob linkedListIterateJob = new LinkedListIterateJob
        {
            List = linkedList,
            Sum  = sum
        };
        ChunkedListIterateJob chunkedListIterateJob = new ChunkedListIterateJob
        {
            List = chunkedList,
            Sum  = sum
        };
        ArrayIterateJobParallelFor arrayIterateJobParallelFor = new ArrayIterateJobParallelFor
        {
            Array = array,
            Sum   = sum
        };
        LinkedListIterateJobParallelFor linkedListIterateJobParallelFor = new LinkedListIterateJobParallelFor
        {
            List = linkedList,
            Sum  = sum
        };
        ChunkedListIterateJobParallelFor chunkedListIterateJobParallelFor = new ChunkedListIterateJobParallelFor
        {
            List = chunkedList,
            Sum  = sum
        };
        LinkedListInsertJob linkedListInsertJob = new LinkedListInsertJob
        {
            LinkedList = linkedList
        };
        ChunkedListInsertJob chunkedListInsertJob = new ChunkedListInsertJob
        {
            List = chunkedList
        };
        LinkedListRemoveJob linkedListRemoveJob = new LinkedListRemoveJob
        {
            List = linkedList
        };
        ChunkedListRemoveJob chunkedListRemoveJob = new ChunkedListRemoveJob
        {
            List = chunkedList
        };
        HashSetRemoveJob hashSetRemoveJob = new HashSetRemoveJob
        {
            Set = hashSet
        };
        NativeIntPtrParallelJob nativeIntPtrParallelJob = new NativeIntPtrParallelJob
        {
            Array = array,
            Sum   = nativeIntPtr.GetParallel()
        };
        NativePerJobThreadIntPtrParallelJob nativePerJobThreadIntPtrParallelJob = new NativePerJobThreadIntPtrParallelJob
        {
            Array = array,
            Sum   = nativePerJobThreadIntPtr.GetParallel()
        };

        // Run jobs

        listAddJob.Run();
        linkedListAddJob.Run();
        chunkedListAddJob.Run();
        hashSetAddJob.Run();
        arrayIterateJob.Run();
        listIterateJob.Run();
        linkedListIterateJob.Run();
        chunkedListIterateJob.Run();
        arrayIterateJobParallelFor.Run(array.Length);
        linkedListIterateJobParallelFor.Run(linkedList.Length);
        chunkedListIterateJobParallelFor.RunRanged(chunkedList.Length);
        list.Clear();
        linkedList.Clear();
        chunkedList.Clear();
        hashSet.Clear();
        linkedListInsertJob.Run();
        chunkedListInsertJob.Run();
        linkedListRemoveJob.Run();
        chunkedListRemoveJob.Run();
        hashSetRemoveJob.Run();
        nativeIntPtrParallelJob.Run(array.Length);
        nativePerJobThreadIntPtrParallelJob.Run(array.Length);

        // Dispose native collections

        sum.Dispose();
        array.Dispose();
        list.Dispose();
        linkedList.Dispose();
        chunkedList.Dispose();
        hashSet.Dispose();
        nativeIntPtr.Dispose();
        nativePerJobThreadIntPtr.Dispose();
    }