protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var commandBuffer = m_Barrier.CreateCommandBuffer().ToConcurrent();


        ///create the job that needs to be scheduled
        var job = new ResetJob
        {
            CommandBuffer = commandBuffer
        }.Schedule(this, inputDeps);

        m_Barrier.AddJobHandleForProducer(job);

        return(job);
    }
Beispiel #2
0
        protected override void OnUpdate()
        {
            bool needsStatsUpdate = false;

            while (m_sampleCounter >= kFramesPerStat)
            {
                m_sampleCounter -= kFramesPerStat;
                needsStatsUpdate = true;
            }
            m_sampleCounter++;

            var profilingData = worldGlobalEntity.GetCollectionComponent <ProfilingData>(false);

            CompleteDependency();
            if (m_firstFrame)
            {
                m_firstFrame = false;
                profilingData.frameStopwatch.Start();
                return;
            }

            profilingData.frameStopwatch.Stop();
#if UNITY_EDITOR
            if (!m_firstFrame)
            {
                profilingData.gpuStopwatch.Stop();
            }
#endif

            var frameTime = profilingData.frameStopwatch.Elapsed;
            profilingData.frameStopwatch.Reset();
            profilingData.frameStopwatch.Start();

            float frameDuration = (float)((double)frameTime.Ticks / TimeSpan.TicksPerMillisecond);

            var   gpuTime     = profilingData.gpuStopwatch.Elapsed;
            float gpuDuration = (float)((double)gpuTime.Ticks / TimeSpan.TicksPerMillisecond);

            var jh = new AppendStatAndShiftJob
            {
                array       = profilingData.cpuShort,
                appendValue = frameDuration - gpuDuration
            }.Schedule();
            jh = new AppendStatAndShiftJob
            {
                array       = profilingData.gpuShort,
                appendValue = frameDuration
            }.Schedule(jh);

            if (Keyboard.current.leftBracketKey.wasPressedThisFrame)
            {
                jh = new ResetJob
                {
                    cpuMin     = profilingData.cpuMin,
                    cpuAverage = profilingData.cpuAverage,
                    cpuMax     = profilingData.cpuMax,
                    gpuMin     = profilingData.gpuMin,
                    gpuAverage = profilingData.gpuAverage,
                    gpuMax     = profilingData.gpuMax,
                }.Schedule(jh);
            }

            if (needsStatsUpdate)
            {
                jh = new ComputeStatsJob
                {
                    array    = profilingData.cpuShort,
                    minArray = profilingData.cpuMin,
                    avgArray = profilingData.cpuAverage,
                    maxArray = profilingData.cpuMax
                }.Schedule(jh);

                jh = new ComputeStatsJob
                {
                    array    = profilingData.gpuShort,
                    minArray = profilingData.gpuMin,
                    avgArray = profilingData.gpuAverage,
                    maxArray = profilingData.gpuMax
                }.Schedule(jh);
            }

            jh = new GenerateImageJob
            {
                cpuShort   = profilingData.cpuShort,
                cpuMin     = profilingData.cpuMin,
                cpuAverage = profilingData.cpuAverage,
                cpuMax     = profilingData.cpuMax,
                gpuShort   = profilingData.gpuShort,
                gpuMin     = profilingData.gpuMin,
                gpuAverage = profilingData.gpuAverage,
                gpuMax     = profilingData.gpuMax,
                image      = profilingData.image,
                barValues  = profilingData.barValues,
            }.Schedule(jh);

            worldGlobalEntity.UpdateJobDependency <ProfilingData>(jh, false);
        }
Beispiel #3
0
    protected override JobHandle OnUpdate(JobHandle jobHandle)
    {
        if (!initialized)
        {
            initializePersistentArrays(numberWorkerThreads, gridParams);
            initialized = true;
        }

        //QuadTree.ClearAndBulkInsert(obstacleArray);

        //NativeList<ObstacleStruct> queryResult = new NativeList<ObstacleStruct>(Allocator.Temp);

        //QuadTree.RangeQuery(new NativeQuadTree.AABB2D(new float2(500, 500), new float2(1000, 1000)), queryResult);

        //Debug.Log(queryResult);



        EntityCommandBuffer concurrentCommandBuffer = entityCommandBufferSystem.CreateCommandBuffer();

        EntityQuery entityQuery = GetEntityQuery(ComponentType.ReadOnly <PathfindingParams>());

        NativeArray <PathfindingParams> pathFindingArray = entityQuery.ToComponentDataArray <PathfindingParams>(Allocator.TempJob);
        NativeArray <Entity>            entityArray      = entityQuery.ToEntityArray(Allocator.TempJob);



        NativeArray <JobHandle> jobHandles = new NativeArray <JobHandle>(jobCollections.Count, Allocator.TempJob);

        for (int index = 0; index < jobCollections.Count; index++)
        {
            jobHandles[index] = jobHandle;
        }

        for (int r = 0; r < entityArray.Length; r++)
        {
            int               index             = r % jobCollections.Count;
            jobCollection     data              = jobCollections[index];
            PathfindingParams pathfindingParams = pathFindingArray[r];

            FindPathJob findPathJob = new FindPathJob
            {
                DimX            = gridParams.x,
                DimY            = gridParams.y,
                Neighbours      = neighbourOffsetArray,
                startPosition   = pathfindingParams.startPosition,
                endPosition     = pathfindingParams.endPosition,
                itterationLimit = itterationLimit,
                Grid            = ObstacleController.instance.getNativeMap(),
                CostSoFar       = data.CostSoFar,
                CameFrom        = data.CameFrom,
                entity          = entityArray[r],
                OpenSet         = data.OpenSet
            };

            JobHandle findPathHandle = findPathJob.Schedule(jobHandles[index]);



            ResetJob resetJob = new ResetJob
            {
                CostSoFar = data.CostSoFar,
                OpenSet   = data.OpenSet,
                CameFrom  = data.CameFrom,
                DimX      = gridParams.x,
                entity    = entityArray[r],
                pathFound = data.foundPath,
                pathfindingParamsComponentDataFromEntity = GetComponentDataFromEntity <PathfindingParams>(),
                pathFollowComponentDataFromEntity        = GetComponentDataFromEntity <PathFollow>(),
                pathPositionBufferFromEntity             = GetBufferFromEntity <PathPosition>(),
                startPosition = pathfindingParams.startPosition,
                endPosition   = pathfindingParams.endPosition,
            };

            jobHandles[index] = resetJob.Schedule(findPathHandle);
            //PostUpdateCommands.RemoveComponent<PathfindingParams>(entity);
            concurrentCommandBuffer.RemoveComponent <PathfindingParams>(entityArray[r]);
        }

        jobHandle = JobHandle.CombineDependencies(jobHandles);
        entityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        entityArray.Dispose();
        pathFindingArray.Dispose();

        jobHandles.Dispose();


        return(jobHandle);
    }