public void SortMultipleTimes()
    {
        var length01     = 100;
        var length02     = 1000;
        var parallelSort = new ParallelSort <DataWithIndex <float> >();

        var array01   = SortUtilityTest.GenerateFloatNativeArray(length01, out var inputDeps);
        var counter01 = new NativeCounter(Allocator.TempJob);

        inputDeps = parallelSort.Sort(array01, array01.Length, 8, inputDeps);
        inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter01, array01.Length, inputDeps);

        var array02 = SortUtilityTest.GenerateFloatNativeArray(length02, out var moreFloatsHandle);

        inputDeps = JobHandle.CombineDependencies(inputDeps, moreFloatsHandle);
        var counter02 = new NativeCounter(Allocator.TempJob);

        inputDeps = parallelSort.Sort(array02, array02.Length, 3, inputDeps);
        inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter02, array02.Length, inputDeps);

        inputDeps.Complete();
        var result01 = counter01.Value;
        var result02 = counter02.Value;

        counter01.Dispose();
        array01.Dispose();
        counter02.Dispose();
        array02.Dispose();
        parallelSort.Dispose();

        Assert.True(result01 == length01);
        Assert.True(result02 == length02);
    }
Example #2
0
    static DataWithIndex <T>[] Sort <T>(NativeArray <DataWithIndex <T> > array, int processAmount, int concurrentJobs)
        where T : struct, IComparable <T>
    {
        var length = array.Length;
        var timeSpanParallelSort = new TimeSpanParallelSort <DataWithIndex <T> >();
        var counter = new NativeCounter(Allocator.TempJob);
        var cycles  = 0;

        timeSpanParallelSort.Start(array, processAmount, concurrentJobs).Complete();
        while (!timeSpanParallelSort.IsComplete)
        {
            timeSpanParallelSort.Update().Complete();
            cycles++;
        }

        SortUtilityTest
        .CountSortedData(timeSpanParallelSort.SortedData.AsDeferredJobArray(), counter, length, default)
        .Complete();
        var sortedArray = timeSpanParallelSort.SortedData.ToArray();
        var result      = counter.Value;

        counter.Dispose();
        array.Dispose();
        timeSpanParallelSort.Dispose();
        Assert.True(result == length);
        UnityEngine.Debug.Log($"Sorted array in {cycles} cycles");
        return(sortedArray);
    }
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        EntityManager.GetAllUniqueSharedComponentData <LineRenderer>(lineRenderers);

        NativeCounter      lineCounter = new NativeCounter(Allocator.Temp);
        LineMeshBuilderJob job         = new LineMeshBuilderJob();

        for (int i = 0; i < lineRenderers.Count; ++i)
        {
            LineRenderer renderer = lineRenderers[i];

            group.SetFilter(renderer);
            int groupCount = group.CalculateLength();
            if (groupCount == 0)
            {
                continue;
            }

            lineCounter.Count = 0;
            job.Initialize(group, renderer.Vertices, renderer.Normals, lineCounter);
            inputDeps = job.Schedule(groupCount, 8, inputDeps);
            inputDeps.Complete();
        }
        lineRenderers.Clear();
        lineCounter.Dispose();
        return(inputDeps);
    }
Example #4
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        EntityManager.GetAllUniqueSharedComponentData <LineRendererRef>(lineRendererRefs);
        NativeCounter lineCounter = new NativeCounter(Allocator.TempJob);

        var vertexBuffers = GetBufferFromEntity <BufferableVertex>();

        for (int i = 0; i < lineRendererRefs.Count; ++i)
        {
            Entity          lineRendererEntity = lineRendererRefs[i].Value;
            LineRendererRef rendererRef        = lineRendererRefs[i];

            lineQuery.SetFilter(rendererRef);
            int groupCount = lineQuery.CalculateEntityCount();
            if (groupCount == 0)
            {
                continue;
            }

            lineCounter.Count = 0;
            LineMeshBuilderJob job = new LineMeshBuilderJob {
                _counter      = lineCounter,
                _vertexBuffer = (vertexBuffers[lineRendererEntity]),
            };
            inputDeps = job.Schedule(this, inputDeps);
            inputDeps.Complete();
        }

        lineRendererRefs.Clear();
        lineCounter.Dispose();
        return(inputDeps);
    }
Example #5
0
 protected override void OnStopRunning()
 {
     for (var i = 0; i < MAX_QUERIES; ++i)
     {
         batches[i].Dispose();
     }
     waitingEntities.Dispose();
     counter.Dispose();
 }
Example #6
0
 protected override void OnDestroy()
 {
     m_vertices.Dispose();
     m_triangles.Dispose();
     m_vertexCounter.Dispose();
     m_vertexIndexCounter.Dispose();
     m_subMeshCounter.Dispose();
     m_offsets.Dispose();
     m_sharedFontIndices.Dispose();
     m_entityIndexMap.Dispose();
 }
Example #7
0
    public void GetSetCount()
    {
        var counter = new NativeCounter(Allocator.Temp);

        Assert.AreEqual(0, counter.Count);
        counter.Count = 42;
        Assert.AreEqual(42, counter.Count);
        counter.Count = 3;
        Assert.AreEqual(3, counter.Count);
        counter.Dispose();
    }
Example #8
0
    public void Increment()
    {
        var counter = new NativeCounter(Allocator.Temp);

        Assert.AreEqual(0, counter.Count);
        counter.Increment();
        Assert.AreEqual(1, counter.Count);
        counter.Increment();
        counter.Increment();
        Assert.AreEqual(3, counter.Count);
        counter.Dispose();
    }
Example #9
0
    public void ParallelIncrement()
    {
        var counter = new NativeCounter(Allocator.Temp);
        var jobData = new ParallelCountTo();

        jobData.counter = counter;
        // Count every second item
        jobData.countMask = 1;
        jobData.Schedule(1000000, 1).Complete();
        Assert.AreEqual(500000, counter.Count);
        counter.Dispose();
    }
Example #10
0
    public void ParallelDecrementSetCount()
    {
        var counter = new NativeCounter(Allocator.Temp);
        var jobData = new ParallelDecrementCountTo();

        jobData.counter = counter.ToConcurrent();
        counter.Count   = -42;
        // Count every second item
        jobData.countMask = 1;
        jobData.Schedule(1000, 1).Complete();
        Assert.AreEqual(-542, counter.Count);
        counter.Dispose();
    }
Example #11
0
    public void Decrement()
    {
        int count;
        var counter = new NativeCounter(Allocator.Temp);

        Assert.AreEqual(0, counter.Count);
        count = counter.Decrement();
        Assert.AreEqual(-1, count);
        Assert.AreEqual(-1, counter.Count);
        count = counter.Decrement();
        count = counter.Decrement();
        Assert.AreEqual(-3, count);
        Assert.AreEqual(-3, counter.Count);
        counter.Dispose();
    }
Example #12
0
    public void SetCountConcurrentDecrement()
    {
        var counter           = new NativeCounter(Allocator.Temp);
        var concurrentCounter = counter.ToConcurrent();

        Assert.AreEqual(0, counter.Count);
        concurrentCounter.Decrement();
        Assert.AreEqual(-1, counter.Count);
        counter.Count = 40;
        Assert.AreEqual(40, counter.Count);
        concurrentCounter.Decrement();
        concurrentCounter.Decrement();
        Assert.AreEqual(38, counter.Count);
        counter.Dispose();
    }
Example #13
0
    public void SetCountConcurrentIncrement()
    {
        var counter = new NativeCounter(Allocator.Temp);

        NativeCounter.Concurrent concurrentCounter = counter;
        Assert.AreEqual(0, counter.Count);
        concurrentCounter.Increment();
        Assert.AreEqual(1, counter.Count);
        counter.Count = 40;
        Assert.AreEqual(40, counter.Count);
        concurrentCounter.Increment();
        concurrentCounter.Increment();
        Assert.AreEqual(42, counter.Count);
        counter.Dispose();
    }
    static void FloatSort(int length, int concurrentJobs)
    {
        var array        = SortUtilityTest.GenerateFloatNativeArray(length, out var inputDeps);
        var parallelSort = new ParallelSort <DataWithIndex <float> >();
        var counter      = new NativeCounter(Allocator.TempJob);

        inputDeps = parallelSort.Sort(array, array.Length, concurrentJobs, inputDeps: inputDeps);
        inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter, array.Length, inputDeps);
        inputDeps.Complete();
        var result = counter.Value;

        counter.Dispose();
        array.Dispose();
        parallelSort.Dispose();
        Assert.True(result == length);
    }
Example #15
0
    public void ConcurrentIncrement()
    {
        int count;
        var counter           = new NativeCounter(Allocator.Temp);
        var concurrentCounter = counter.ToConcurrent();

        Assert.AreEqual(0, counter.Count);
        count = concurrentCounter.Increment();
        Assert.AreEqual(1, count);
        Assert.AreEqual(1, counter.Count);
        count = concurrentCounter.Increment();
        count = concurrentCounter.Increment();
        Assert.AreEqual(3, count);
        Assert.AreEqual(3, counter.Count);
        counter.Dispose();
    }
    static DataWithIndex <int>[] IntSort(NativeArray <DataWithIndex <int> > array, int concurrentJobs, JobHandle inputDeps = default)
    {
        var length       = array.Length;
        var parallelSort = new ParallelSort <DataWithIndex <int> >();
        var counter      = new NativeCounter(Allocator.TempJob);

        inputDeps = parallelSort.Sort(array, length, concurrentJobs, inputDeps);
        inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter, length, inputDeps);
        inputDeps.Complete();
        var sortedArray = parallelSort.SortedData.ToArray();
        var result      = counter.Value;

        counter.Dispose();
        array.Dispose();
        parallelSort.Dispose();
        Assert.True(result == length);
        return(sortedArray);
    }
Example #17
0
            public void Dispose()
            {
                if (nativeVertices.IsCreated)
                {
                    nativeVertices.Dispose();
                }

                if (nativeNormals.IsCreated)
                {
                    nativeNormals.Dispose();
                }

                if (nativeIndices.IsCreated)
                {
                    nativeIndices.Dispose();
                }

                if (counter.IsCreated)
                {
                    counter.Dispose();
                }

                if (nativeUVs.IsCreated)
                {
                    nativeUVs.Dispose();
                }

                if (nativeColors.IsCreated)
                {
                    nativeColors.Dispose();
                }

                if (nativeLightData.IsCreated)
                {
                    nativeLightData.Dispose();
                }
            }
    public static void GenerateMarchingCubesWithJob(Voxel[,,] voxels, Vector3Int cellSize, Vector3 chunkScale, bool triangleIndexing, List <Vector3> vertices, List <int> triangles, List <Color> colors)
    {
        NativeArray <Vector3> nativeVertices  = new NativeArray <Vector3>(15 * cellSize.x * cellSize.y * cellSize.z, Allocator.TempJob);
        NativeArray <int>     nativeTriangles = new NativeArray <int>(15 * cellSize.x * cellSize.y * cellSize.z, Allocator.TempJob);
        NativeArray <Color>   nativeColors    = new NativeArray <Color>(15 * cellSize.x * cellSize.y * cellSize.z, Allocator.TempJob);
        NativeCounter         counter         = new NativeCounter(Allocator.TempJob);
        NativeArray <Voxel>   nativeVoxels    = new NativeArray <Voxel>(voxels.Length, Allocator.TempJob);

        nativeVoxels.ManagedToNative(voxels);

        MarchingCubesJob marchingCubesJob = new MarchingCubesJob
        {
            vertices   = nativeVertices,
            counter    = counter.ToConcurrent(),
            voxels     = nativeVoxels,
            colors     = nativeColors,
            cellSize   = cellSize,
            gridSize   = cellSize + Vector3Int.one,
            chunkScale = chunkScale,
        };

        JobHandle marchingCubesJobHandle = marchingCubesJob.Schedule(voxels.Length, 32);

        marchingCubesJobHandle.Complete();

        if (counter.Count > 0)
        {
            int verticeSize  = counter.Count * 3;
            int triangleSize = verticeSize;

            if (triangleIndexing)
            {
                verticeSize = TriangleIndexingForJob(nativeVertices, nativeTriangles, nativeColors, verticeSize);
            }
            else
            {
                AddTriangleIndexForNoIndexing triangleJob = new AddTriangleIndexForNoIndexing
                {
                    triangles = nativeTriangles
                };

                JobHandle triangleJobHandle = triangleJob.Schedule(verticeSize, 32);
                triangleJobHandle.Complete();
            }

            NativeSlice <Vector3> nativeSliceVertices  = new NativeSlice <Vector3>(nativeVertices, 0, verticeSize);
            NativeSlice <Color>   nativeSliceColors    = new NativeSlice <Color>(nativeColors, 0, verticeSize);
            NativeSlice <int>     nativeSliceTriangles = new NativeSlice <int>(nativeTriangles, 0, triangleSize);

            colors.Clear();
            vertices.Clear();
            triangles.Clear();

            vertices.NativeAddRange(nativeSliceVertices);
            triangles.NativeAddRange(nativeSliceTriangles);
            colors.NativeAddRange(nativeSliceColors);
        }

        counter.Dispose();
        nativeVertices.Dispose();
        nativeTriangles.Dispose();
        nativeVoxels.Dispose();
        nativeColors.Dispose();
    }
    public static void GenerateMarchingSquaresWithJob(NativeArray <Voxel> nativeVoxels, Vector2Int chunkSize, Vector2 chunkScale, bool interpolate, bool triangleIndexing, bool greedyMeshing, List <Vector3> vertices, List <int> triangles)
    {
        NativeArray <Vector3> nativeVertices  = new NativeArray <Vector3>(9 * chunkSize.x * chunkSize.y, Allocator.TempJob);
        NativeArray <int>     nativeTriangles = new NativeArray <int>(9 * chunkSize.x * chunkSize.y, Allocator.TempJob);
        NativeArray <bool>    nativeQuadMap   = new NativeArray <bool> (chunkSize.x * chunkSize.y, Allocator.TempJob);
        NativeCounter         counter         = new NativeCounter(Allocator.TempJob);

        MarchingSquaresJob marchingSquaresJob = new MarchingSquaresJob
        {
            vertices         = nativeVertices,
            quadMap          = nativeQuadMap,
            counter          = counter.ToConcurrent(),
            voxels           = nativeVoxels,
            chunkSize        = chunkSize,
            chunkScale       = chunkScale,
            interpolate      = interpolate,
            triangleIndexing = triangleIndexing,
            greedyMeshing    = greedyMeshing
        };

        JobHandle marchingSquaresJobHandle = marchingSquaresJob.Schedule(chunkSize.x * chunkSize.y, 32);

        marchingSquaresJobHandle.Complete();

        if (greedyMeshing)
        {
            GreedyMeshingForJob(chunkSize, chunkScale, nativeVertices, counter, nativeQuadMap);
        }
        else
        {
            MakeQuadJob quadJob = new MakeQuadJob
            {
                chunkSize  = chunkSize,
                chunkScale = chunkScale,
                vertices   = nativeVertices,
                triangles  = nativeTriangles,
                quadMap    = nativeQuadMap,
                counter    = counter.ToConcurrent()
            };

            JobHandle quadJobHandle = quadJob.Schedule(chunkSize.x * chunkSize.y, 32);
            quadJobHandle.Complete();
        }

        int verticeSize  = counter.Count * 3;
        int triangleSize = verticeSize;

        if (triangleIndexing)
        {
            verticeSize = TriangleIndexingForJob(nativeVertices, nativeTriangles, verticeSize);
        }
        else
        {
            AddTriangleIndexForNoIndexing triangleJob = new AddTriangleIndexForNoIndexing
            {
                triangles = nativeTriangles
            };

            JobHandle triangleJobHandle = triangleJob.Schedule(verticeSize, 32);
            triangleJobHandle.Complete();
        }

        NativeSlice <Vector3> nativeSliceVertices  = new NativeSlice <Vector3>(nativeVertices, 0, verticeSize);
        NativeSlice <int>     nativeSliceTriangles = new NativeSlice <int>(nativeTriangles, 0, triangleSize);

        vertices.Clear();
        triangles.Clear();

        vertices.NativeAddRange(nativeSliceVertices);
        triangles.NativeAddRange(nativeSliceTriangles);

        counter.Dispose();
        nativeQuadMap.Dispose();
        nativeVertices.Dispose();
        nativeTriangles.Dispose();
    }
Example #20
0
 public void TeardownCounterTest()
 {
     counter.Dispose();
 }