protected override void OnCreate()
        {
            m_vertices           = new NativeList <Vertex>(10000, Allocator.Persistent);
            m_triangles          = new NativeList <VertexIndex>(10000, Allocator.Persistent);
            m_vertexCounter      = new NativeCounter(Allocator.Persistent);
            m_vertexIndexCounter = new NativeCounter(Allocator.Persistent);
            m_subMeshCounter     = new NativeCounter(Allocator.Persistent);
            m_offsets            = new NativeList <OffsetInfo>(0, Allocator.Persistent);
            m_entityIndexMap     = new NativeHashMap <Entity, int>(1000, Allocator.Persistent);
            m_sharedFontIndices  = new NativeList <int>(0, Allocator.Persistent);

            m_canvasdRootQuery = GetEntityQuery(
                ComponentType.ReadOnly <CanvasRoot>(),
                ComponentType.ReadWrite <Vertex>(),
                ComponentType.ReadWrite <VertexIndex>(),
                ComponentType.ReadWrite <SubMeshInfo>());
            m_vertexDataQuery = GetEntityQuery(
                ComponentType.ReadOnly <FontMaterial>(),
                ComponentType.ReadOnly <TextRenderer>(),
                ComponentType.ReadOnly <Vertex>(),
                ComponentType.ReadOnly <VertexIndex>());
            m_filterChanged = new ComponentType[] {
                ComponentType.ReadOnly <Vertex>(),
                ComponentType.ReadOnly <VertexIndex>()
            };
        }
    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 #3
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);
    }
        /// <summary>
        /// Starts a mesh generation job
        /// </summary>
        /// <param name="voxelDataStore">The store where to retrieve the voxel data from</param>
        /// <param name="chunkCoordinate">The coordinate of the chunk that will be generated</param>
        /// <returns>The job handle and the actual mesh generation job</returns>
        public override JobHandleWithData <IMesherJob> CreateMesh(VoxelDataStore voxelDataStore, int3 chunkCoordinate)
        {
            if (!voxelDataStore.TryGetVoxelDataChunk(chunkCoordinate, out VoxelDataVolume boundsVoxelData))
            {
                return(null);
            }

            NativeCounter vertexCountCounter = new NativeCounter(Allocator.TempJob);

            int voxelCount = (boundsVoxelData.Width - 1) * (boundsVoxelData.Height - 1) * (boundsVoxelData.Depth - 1);
            int maxLength  = 15 * voxelCount;

            NativeArray <MeshingVertexData> outputVertices  = new NativeArray <MeshingVertexData>(maxLength, Allocator.TempJob);
            NativeArray <ushort>            outputTriangles = new NativeArray <ushort>(maxLength, Allocator.TempJob);

            MarchingCubesJob marchingCubesJob = new MarchingCubesJob
            {
                VoxelData          = boundsVoxelData,
                Isolevel           = Isolevel,
                VertexCountCounter = vertexCountCounter,

                OutputVertices  = outputVertices,
                OutputTriangles = outputTriangles
            };

            JobHandle jobHandle = marchingCubesJob.Schedule(voxelCount, 128);

            JobHandleWithData <IMesherJob> jobHandleWithData = new JobHandleWithData <IMesherJob>();

            jobHandleWithData.JobHandle = jobHandle;
            jobHandleWithData.JobData   = marchingCubesJob;

            return(jobHandleWithData);
        }
    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 #6
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 #7
0
        protected override void OnCreate()
        {
            m_rootsQuery = GetEntityQuery(new EntityQueryDesc {
                All = new ComponentType[]
                {
                    ComponentType.ReadOnly <LocalToWorld>(),
                    ComponentType.ReadOnly <Child>()
                },
                None = new ComponentType[]
                {
                    typeof(Parent)
                },
                Options = EntityQueryOptions.FilterWriteGroup
            });

            m_allChildrenQuery = GetEntityQuery(new EntityQueryDesc {
                All = new ComponentType[]
                {
                    ComponentType.ReadOnly <LocalToWorld>(),
                    ComponentType.ReadOnly <Parent>()
                },
                Options = EntityQueryOptions.IncludeDisabled
            });

            m_childrenEntities        = new NativeList <ChildInfo>(Allocator.Persistent);
            m_topLevelChildrenCounter = new NativeCounter(Allocator.Persistent);
        }
 public static JobHandle CountSortedData <T>(NativeArray <T> input, NativeCounter output, int length, JobHandle inputDeps)
     where T : struct, IComparable <T>
 {
     return(new ValidateSortedArray <T> {
         Source = input,
         Counter = output
     }.Schedule(length, 128, inputDeps));
 }
 public static JobHandle CountBufferElements <TBufferElementData>(
     this EntityQuery entityQuery,
     ref NativeCounter counter,
     JobHandle inputDeps)
     where TBufferElementData : struct, IBufferElementData
 {
     return(new CountBufferElements <TBufferElementData> {
         Counter = counter
     }.Schedule(entityQuery, inputDeps));
 }
Example #10
0
            protected sealed override bool Filter(NativeCounter c)
            {
                if ((c.Section & mask) != 0)
                {
                    Console.Write("{0,10} {1,10} {2,10} ", c.Section, c.Type, c.Unit);
                    return(true);
                }

                return(false);
            }
Example #11
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 #12
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 #13
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 #14
0
            public NativeMeshData(int chunkSize)
            {
                int maxVertices = 12 * chunkSize * chunkSize * chunkSize;
                int maxIndices  = 18 * chunkSize * chunkSize * chunkSize;

                nativeVertices = new NativeArray <float3>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeNormals  = new NativeArray <float3>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeUVs      = new NativeArray <float4>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeColors   = new NativeArray <Color>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeIndices  = new NativeArray <int>(maxIndices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                counter        = new NativeCounter(Allocator.TempJob);
            }
Example #15
0
 public static JobHandle ResizeBufferDeferred <TBufferElementData>(
     this EntityQuery entityQuery,
     ComponentSystemBase system,
     NativeCounter length,
     JobHandle inputDeps)
     where TBufferElementData : struct, IBufferElementData
 {
     return(new ResizeBufferDeferred <TBufferElementData> {
         BufferType = system.GetArchetypeChunkBufferType <TBufferElementData>(false),
         Length = length
     }.Schedule(entityQuery, inputDeps));
 }
Example #16
0
 public static JobHandle CountBufferElements <TBufferElementData>(
     this EntityQuery entityQuery,
     EntityManager entityManager,
     ref NativeCounter counter,
     JobHandle inputDeps)
     where TBufferElementData : struct, IBufferElementData
 {
     return(new CountBufferElements <TBufferElementData> {
         ChunkBufferType = entityManager.GetArchetypeChunkBufferType <TBufferElementData>(true),
         Counter = counter
     }.Schedule(entityQuery, inputDeps));
 }
Example #17
0
    protected override void OnStartRunning()
    {
        batches = new RequestBatch[MAX_QUERIES];
        for (var i = 0; i < MAX_QUERIES; ++i)
        {
            batches[i] = new RequestBatch(2000);
        }
        waitingEntities = new NativeQueue <Entity>(Allocator.Persistent);
        counter         = new NativeCounter(Allocator.Persistent);

        endFence = new JobHandle();
    }
Example #18
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 #19
0
            public NativeMeshData(int3 chunkSize)
            {
                int numVoxels   = chunkSize.x * chunkSize.y * chunkSize.z;
                int maxVertices = 12 * numVoxels;
                int maxIndices  = 18 * numVoxels;

                nativeVoxels   = new NativeArray <Voxel>(numVoxels, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeVertices = new NativeArray <float3>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeNormals  = new NativeArray <float3>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeUVs      = new NativeArray <float4>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeColors   = new NativeArray <Color>(maxVertices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                nativeIndices  = new NativeArray <int>(maxIndices, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                counter        = new NativeCounter(Allocator.TempJob);
            }
Example #20
0
        void CreateOffsets(
            NativeHashMap <Entity, int> entitiesIndexMap,
            NativeArray <SortedEntity> sortedEntities,
            NativeArray <int> sharedComponentIndices,
            NativeArray <OffsetInfo> offsets,
            NativeCounter vertexCounter,
            NativeCounter vertexIndexCounter,
            NativeCounter subMeshCounter)
        {
            var vertices      = GetBufferFromEntity <Vertex>(true);
            var vertexIndices = GetBufferFromEntity <VertexIndex>(true);

            Job
            .WithReadOnly(entitiesIndexMap)
            .WithReadOnly(sortedEntities)
            .WithReadOnly(sharedComponentIndices)
            .WithReadOnly(vertices)
            .WithReadOnly(vertexIndices)
            .WithCode(() => {
                var prevIndex = -1;
                for (var i = sortedEntities.Length - 1; i >= 0; --i)
                {
                    var entity = sortedEntities[i].Value;
                    if (!entitiesIndexMap.TryGetValue(entity, out var index))
                    {
                        continue;
                    }
                    var vertexData               = vertices[entity];
                    var vertexIndexData          = vertexIndices[entity];
                    var sharedComponentIndex     = sharedComponentIndices[index];
                    var prevSharedComponentIndex = prevIndex >= 0 ? sharedComponentIndices[prevIndex] : -1;
                    offsets[index]               = new OffsetInfo {
                        Vertex            = vertexCounter.Value,
                        VertexCount       = vertexData.Length,
                        Indices           = vertexIndexCounter.Value,
                        SubMeshIndex      = sharedComponentIndex != prevSharedComponentIndex ? subMeshCounter.Value : -1,
                        SubMesh           = sharedComponentIndex != prevSharedComponentIndex ? vertexIndexCounter.Value : -1,
                        SubMeshMaterialId = sharedComponentIndex != prevSharedComponentIndex ? sharedComponentIndex : -1
                    };
                    if (sharedComponentIndex != prevSharedComponentIndex)
                    {
                        subMeshCounter.Increment(1);
                    }
                    vertexCounter.Increment(vertexData.Length);
                    vertexIndexCounter.Increment(vertexIndexData.Length);
                    prevIndex = index;
                }
            }).Schedule();
        }
Example #21
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 #22
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 #23
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();
    }
Example #24
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 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);
    }
    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 #27
0
        /// <inheritdoc/>
        public override JobHandleWithData <IMesherJob> CreateMesh(VoxelDataStore voxelDataStore, VoxelColorStore voxelColorStore, int3 chunkCoordinate)
        {
            if (!voxelDataStore.TryGetDataChunk(chunkCoordinate, out VoxelDataVolume <byte> boundsVoxelData))
            {
                return(null);
            }

            if (!voxelColorStore.TryGetDataChunk(chunkCoordinate, out VoxelDataVolume <Color32> boundsVoxelColors))
            {
                return(null);
            }

            NativeCounter vertexCountCounter = new NativeCounter(Allocator.TempJob);

            int voxelCount = VoxelWorld.WorldSettings.ChunkSize.x * VoxelWorld.WorldSettings.ChunkSize.y * VoxelWorld.WorldSettings.ChunkSize.z;
            int maxLength  = 15 * voxelCount;

            NativeArray <MeshingVertexData> outputVertices  = new NativeArray <MeshingVertexData>(maxLength, Allocator.TempJob);
            NativeArray <ushort>            outputTriangles = new NativeArray <ushort>(maxLength, Allocator.TempJob);

            MarchingCubesJob marchingCubesJob = new MarchingCubesJob
            {
                VoxelData          = boundsVoxelData,
                VoxelColors        = boundsVoxelColors,
                Isolevel           = Isolevel,
                VertexCountCounter = vertexCountCounter,

                OutputVertices  = outputVertices,
                OutputTriangles = outputTriangles
            };

            JobHandle jobHandle = marchingCubesJob.Schedule();

            JobHandleWithData <IMesherJob> jobHandleWithData = new JobHandleWithData <IMesherJob>
            {
                JobHandle = jobHandle,
                JobData   = marchingCubesJob
            };

            return(jobHandleWithData);
        }
 protected override bool Filter(NativeCounter c) => c.Section == CounterSection.Gc;
 public DirectivesTest1(Allocator type)
 {
     Kek = new NativeCounter(type);
 }
Example #30
0
 public void SetupCounterTest()
 {
     counter = new NativeCounter(Allocator.Temp);
 }