public void UnloadTile(int3 tile)
 {
     m_UnloadInstancesQuery.SetFilter(new ProceduralScatterTile {
         Position = tile
     });
     EntityManager.DestroyEntity(m_UnloadInstancesQuery);
     m_UnloadInstancesQuery.ResetFilter();
 }
Exemplo n.º 2
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityArray boltSpawnerEntityDataArray = boltSpawnerEntityDataGroup.GetEntityArray();

            if (boltSpawnerEntityDataArray.Length == 0)
            {
                return(inputDeps);
            }

            BoltSpawnerEntityData boltSpawnerEntityData = GetComponentDataFromEntity <BoltSpawnerEntityData>()[boltSpawnerEntityDataArray[0]];

            uniqueEntityTypes.Clear();
            EntityManager.GetAllUniqueSharedComponentData(uniqueEntityTypes);

            JobHandle spawnJobHandle     = new JobHandle();
            JobHandle spawnJobDependency = inputDeps;

            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                EntityTypeData entityTypeData = uniqueEntityTypes[i];
                if (entityTypeData.entityType == EntityTypeData.EntityType.EnemyShip ||
                    entityTypeData.entityType == EntityTypeData.EntityType.AllyShip)
                {
                    aiSpawnBoltDataGroup.SetFilter(uniqueEntityTypes[i]);

                    NativeQueue <Entity> .Concurrent spawnBoltEntityQueueToUse = boltSpawnerEntityData.enemyBoltSpawnQueueConcurrent;
                    if (entityTypeData.entityType == EntityTypeData.EntityType.AllyShip)
                    {
                        spawnBoltEntityQueueToUse = boltSpawnerEntityData.allyBoltSpawnQueueConcurrent;
                    }


                    AISpawnBoltJob aiSpawnBoltJob = new AISpawnBoltJob
                    {
                        entityArray          = aiSpawnBoltDataGroup.GetEntityArray(),
                        aiPositionArray      = aiSpawnBoltDataGroup.GetComponentDataArray <Position>(),
                        aiRotationArray      = aiSpawnBoltDataGroup.GetComponentDataArray <Rotation>(),
                        aiSpawnBoltDataArray = aiSpawnBoltDataGroup.GetComponentDataArray <AISpawnBoltData>(),
                        spawnBoltEntityQueue = spawnBoltEntityQueueToUse,
                        deltaTime            = Time.deltaTime
                    };

                    JobHandle tmpJobHandle = aiSpawnBoltJob.Schedule(aiSpawnBoltJob.aiSpawnBoltDataArray.Length,
                                                                     MonoBehaviourECSBridge.Instance.GetJobBatchCount(aiSpawnBoltJob.aiSpawnBoltDataArray.Length),
                                                                     spawnJobDependency);

                    spawnJobHandle     = JobHandle.CombineDependencies(spawnJobHandle, tmpJobHandle);
                    spawnJobDependency = JobHandle.CombineDependencies(spawnJobDependency, tmpJobHandle);
                }
            }
            aiSpawnBoltDataGroup.ResetFilter();

            return(spawnJobHandle);
        }
Exemplo n.º 3
0
        public void UnloadSceneImmediate(Entity scene)
        {
            if (EntityManager.HasComponent <StreamingState>(scene))
            {
                m_SceneFilter.SetFilter(new SceneTag {
                    SceneEntity = scene
                });

                EntityManager.DestroyEntity(m_SceneFilter);

                m_SceneFilter.ResetFilter();

                EntityManager.RemoveComponent <StreamingState>(scene);
            }
        }
Exemplo n.º 4
0
        void UpdateFrozenRenderBatches()
        {
            var staticChunksOrderVersion = EntityManager.GetComponentOrderVersion <FrozenRenderSceneTag>();

            if (staticChunksOrderVersion == m_LastFrozenChunksOrderVersion)
            {
                return;
            }

            for (int i = 0; i < m_LastKnownSubsceneTagVersion.Length; i++)
            {
                var scene   = m_LastKnownSubsceneTagVersion[i].Scene;
                var version = m_LastKnownSubsceneTagVersion[i].Version;

                if (EntityManager.GetSharedComponentOrderVersion(scene) != version)
                {
                    // Debug.Log($"Removing scene:{scene:X8} batches");
                    Profiler.BeginSample("Remove Subscene");
                    m_SubsceneTagVersion.Remove(scene);
                    m_InstancedRenderMeshBatchGroup.RemoveTag(scene);
                    Profiler.EndSample();
                }
            }

            m_LastKnownSubsceneTagVersion.Clear();

            var loadedSceneTags = new List <FrozenRenderSceneTag>();

            EntityManager.GetAllUniqueSharedComponentData(loadedSceneTags);

            for (var i = 0; i < loadedSceneTags.Count; i++)
            {
                var subsceneTag        = loadedSceneTags[i];
                int subsceneTagVersion = EntityManager.GetSharedComponentOrderVersion(subsceneTag);

                m_LastKnownSubsceneTagVersion.Add(new SubSceneTagOrderVersion
                {
                    Scene   = subsceneTag,
                    Version = subsceneTagVersion
                });

                var alreadyTrackingSubscene = m_SubsceneTagVersion.TryGetValue(subsceneTag, out var _);
                if (alreadyTrackingSubscene)
                {
                    continue;
                }

                m_FrozenGroup.SetFilter(subsceneTag);

                //@TODO - revert to CreateArchetypeChunkArray once it supports filtering
                //var filteredChunks = m_FrozenGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var filteredChunks = m_FrozenGroup.GetAllMatchingChunks(Allocator.TempJob);

                m_FrozenGroup.ResetFilter();

                m_SubsceneTagVersion.TryAdd(subsceneTag, subsceneTagVersion);

                Profiler.BeginSample("CacheMeshBatchRenderGroup");
                CacheMeshBatchRendererGroup(subsceneTag, filteredChunks, filteredChunks.Length);
                Profiler.EndSample();

                filteredChunks.Dispose();
            }

            m_LastFrozenChunksOrderVersion = staticChunksOrderVersion;
        }
Exemplo n.º 5
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            UnityEngine.Profiling.Profiler.BeginSample("System Init");


            EntityArray destroyEntityArray = destroyEntityDataGroup.GetEntityArray();

            if (destroyEntityArray.Length == 0)
            {
                return(inputDeps);
            }

            Entity destroyEntity = destroyEntityArray[0];
            ComponentDataFromEntity <DestroyEntityData> destroyEntityDataFromEntity = GetComponentDataFromEntity <DestroyEntityData>();
            DestroyEntityData destroyEntityData = destroyEntityDataFromEntity[destroyEntity];

            currentCellDictionary++;
            if (currentCellDictionary >= cellEntityTypeDictionaryArray.Length)
            {
                currentCellDictionary = 0;
            }

            //Make sure we cleared all the hash maps
            allClearCellsJobHandle.Complete();

            uniqueEntityTypes.Clear();
            EntityManager.GetAllUniqueSharedComponentData(uniqueEntityTypes);

            entityTypeList.Clear();
            subsetEntityDictionary.Clear();
            subsetMinMaxDataDictionary.Clear();
            fillCellJobHandleDictionary.Clear();

            JobHandle allBoundGroupDependencies = boundDataGroup.GetDependency();

            UnityEngine.Profiling.Profiler.EndSample();

            UnityEngine.Profiling.Profiler.BeginSample("FillJobSetup");

            JobHandle allocateCellJobDependency = allClearCellsJobHandle;
            JobHandle allocateCellJobHandle     = new JobHandle();

            EntityArray[] subsetEntityArrayArray = new EntityArray[uniqueEntityTypes.Count];
            ComponentDataArray <EntityBoundMinMaxData>[] subsetMinMaxDataArrayArray = new ComponentDataArray <EntityBoundMinMaxData> [uniqueEntityTypes.Count];

            //create the hashMaps if needed and get the subset arrays we will use
            UnityEngine.Profiling.Profiler.BeginSample("GetEntityArray");


            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                boundDataGroup.SetFilter(uniqueEntityTypes[i]);
                subsetEntityArrayArray[i]     = boundDataGroup.GetEntityArray();
                subsetMinMaxDataArrayArray[i] = boundDataGroup.GetComponentDataArray <EntityBoundMinMaxData>();

                if (subsetEntityArrayArray[i].Length != 0)
                {
                    CreateCellHashMap(uniqueEntityTypes[i].entityType);
                }
            }

            boundDataGroup.ResetFilter();
            UnityEngine.Profiling.Profiler.EndSample();


            //set the cells capacity now
            UnityEngine.Profiling.Profiler.BeginSample("Resize Hash Map");
            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                EntityTypeData entityTypeData    = uniqueEntityTypes[i];
                EntityArray    subsetEntityArray = subsetEntityArrayArray[i];

                if (subsetEntityArray.Length == 0)
                {
                    continue;
                }

                NativeMultiHashMap <int, HashMapData> tmpOutputCell = cellEntityTypeDictionary[entityTypeData.entityType];

                //TODO: Test the memory usage
                //We are setting the capacity really high to not run out of space while running our jobs
                if (tmpOutputCell.Capacity < subsetEntityArray.Length * 10)
                {
                    AllocateCellsJob allocateCellJob = new AllocateCellsJob
                    {
                        outputCells    = tmpOutputCell,
                        capacityWanted = subsetEntityArray.Length * 20,
                    };

                    allocateCellJobHandle = JobHandle.CombineDependencies(allocateCellJob.Schedule(allocateCellJobDependency), allocateCellJobHandle);
                }
            }
            UnityEngine.Profiling.Profiler.EndSample();

            JobHandle fillCellJobDependency = JobHandle.CombineDependencies(inputDeps, allBoundGroupDependencies, allocateCellJobHandle);

            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                EntityTypeData entityTypeData    = uniqueEntityTypes[i];
                EntityArray    subsetEntityArray = subsetEntityArrayArray[i];
                ComponentDataArray <EntityBoundMinMaxData> subsetMinMaxDataArray = subsetMinMaxDataArrayArray[i];

                if (subsetEntityArray.Length == 0)
                {
                    continue;
                }

                NativeMultiHashMap <int, HashMapData> .Concurrent tmpOutputCell = cellEntityTypeDictionary[entityTypeData.entityType].ToConcurrent();
                float3 tmpOutputCellSize = cellSizeEntityDictionary[entityTypeData.entityType];

                UnityEngine.Profiling.Profiler.BeginSample("Allocate tmp Array");
                NativeArray <Entity> subsetEntityArrayOutput = new NativeArray <Entity>(subsetEntityArray.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                NativeArray <EntityBoundMinMaxData> subsetMinMaxDataArrayOutput = new NativeArray <EntityBoundMinMaxData>(subsetEntityArray.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                UnityEngine.Profiling.Profiler.EndSample();

                FillCellJob fillCellJob = new FillCellJob
                {
                    entityArray = subsetEntityArray,
                    entityBoundMinMaxDataArray       = subsetMinMaxDataArray,
                    entityArrayOutput                = subsetEntityArrayOutput,
                    entityBoundMinMaxDataArrayOutput = subsetMinMaxDataArrayOutput,
                    outputCells = tmpOutputCell,
                    cellSizes   = tmpOutputCellSize,
                };

                JobHandle previousFillJobDependency;

                boundDataGroup.SetFilter(entityTypeData);
                JobHandle jobDependency = JobHandle.CombineDependencies(fillCellJobDependency, boundDataGroup.GetDependency());
                if (fillCellJobHandleDictionary.TryGetValue(entityTypeData.entityType, out previousFillJobDependency))
                {
                    jobDependency = JobHandle.CombineDependencies(jobDependency, previousFillJobDependency);
                }


                JobHandle fillCellJobHandle = fillCellJob.Schedule(subsetEntityArray.Length,
                                                                   MonoBehaviourECSBridge.Instance.GetJobBatchCount(subsetEntityArray.Length),
                                                                   jobDependency);


                entityTypeList.Add(entityTypeData.entityType);
                subsetEntityDictionary.Add(entityTypeData.entityType, subsetEntityArrayOutput);
                subsetMinMaxDataDictionary.Add(entityTypeData.entityType, subsetMinMaxDataArrayOutput);
                fillCellJobHandleDictionary.Add(entityTypeData.entityType, fillCellJobHandle);
            }
            UnityEngine.Profiling.Profiler.EndSample();

            if (fillCellJobHandleDictionary.Count == 0)
            {
                return(inputDeps);
            }

            UnityEngine.Profiling.Profiler.BeginSample("CollisionJobSetup");

            JobHandle previousCollisionJobHandle = new JobHandle();

            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.PlayerBolt, destroyEntityData, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.EnemyShip, destroyEntityData, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.AllyShip, destroyEntityData, previousCollisionJobHandle);

            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.AllyBolt, destroyEntityData, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.PlayerBolt, destroyEntityData, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.AllyShip, destroyEntityData, previousCollisionJobHandle);

            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.AllyShip, EntityTypeData.EntityType.EnemyBolt, destroyEntityData, previousCollisionJobHandle);

            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.Asteroid, destroyEntityData, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.EnemyBolt, destroyEntityData, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.EnemyShip, destroyEntityData, previousCollisionJobHandle);


            UnityEngine.Profiling.Profiler.EndSample();

            JobHandle.ScheduleBatchedJobs();

            UnityEngine.Profiling.Profiler.BeginSample("DisposeJobSetup");

            JobHandle jobHandleToReturn = new JobHandle();

            List <JobHandle> clearCellJobHandleList = new List <JobHandle>(entityTypeList.Count);

            for (int i = 0; i < entityTypeList.Count; i++)
            {
                if (subsetEntityDictionary.ContainsKey(entityTypeList[i]))
                {
                    jobHandleToReturn = JobHandle.CombineDependencies(fillCellJobHandleDictionary[entityTypeList[i]], jobHandleToReturn);

                    ClearCellsJob clearCellsJob = new ClearCellsJob
                    {
                        entityArray = subsetEntityDictionary[entityTypeList[i]],
                        entityBoundMinMaxDataArray = subsetMinMaxDataDictionary[entityTypeList[i]],
                        outputCells = cellEntityTypeDictionary[entityTypeList[i]],
                    };

                    JobHandle clearCellsJobHandle = clearCellsJob.Schedule(JobHandle.CombineDependencies(fillCellJobHandleDictionary[entityTypeList[i]], previousCollisionJobHandle));
                    clearCellJobHandleList.Add(clearCellsJobHandle);
                }
            }

            jobHandleToReturn = JobHandle.CombineDependencies(previousCollisionJobHandle, jobHandleToReturn);

            UnityEngine.Profiling.Profiler.EndSample();

            NativeArray <JobHandle> clearCellsJobHandleArray = new NativeArray <JobHandle>(clearCellJobHandleList.ToArray(), Allocator.Temp);

            allClearCellsJobHandle = JobHandle.CombineDependencies(clearCellsJobHandleArray);
            clearCellsJobHandleArray.Dispose();


            return(jobHandleToReturn);
        }
Exemplo n.º 6
0
        bool ExtractEntityRemapRefs(EntityManager srcManager, out NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping)
        {
            // External entity references are "virtual" entities. If we don't have any, only real entities need remapping
            int remapTableSize = srcManager.EntityCapacity;

            using (var externalRefEntities = GetExternalRefEntities(srcManager, Allocator.TempJob))
            {
                // We can potentially have several external entity reference arrays, each one pointing to a different scene
                var externalEntityRefInfos = new NativeArray <ExternalEntityRefInfo>(externalRefEntities.Length, Allocator.Temp);
                for (int i = 0; i < externalRefEntities.Length; ++i)
                {
                    // External references point to indices beyond the range used by the entities in this scene
                    // The highest index used by all those references defines how big the remap table has to be
                    externalEntityRefInfos[i] = srcManager.GetComponentData <ExternalEntityRefInfo>(externalRefEntities[i]);
                    var extRefs = srcManager.GetBuffer <ExternalEntityRef>(externalRefEntities[i]);
                    remapTableSize = math.max(remapTableSize, externalEntityRefInfos[i].EntityIndexStart + extRefs.Length);
                }

                // Within a scene, external scenes are identified by some ID
                // In the destination world, scenes are identified by an entity
                // Every entity coming from a scene needs to have a SceneTag that references the scene entity
                using (var sceneTags = ExternalRefToSceneTag(externalEntityRefInfos, Allocator.TempJob))
                {
                    entityRemapping = new NativeArray <EntityRemapUtility.EntityRemapInfo>(remapTableSize, Allocator.TempJob);

                    for (int i = 0; i < externalRefEntities.Length; ++i)
                    {
                        var extRefs    = srcManager.GetBuffer <ExternalEntityRef>(externalRefEntities[i]);
                        var extRefInfo = srcManager.GetComponentData <ExternalEntityRefInfo>(externalRefEntities[i]);

                        // A scene that external references point to is expected to have a single public reference array
                        m_PublicRefFilter.SetFilter(sceneTags[i]);
                        using (var pubRefEntities = m_PublicRefFilter.ToEntityArray(Allocator.TempJob))
                        {
                            if (pubRefEntities.Length == 0)
                            {
                                // If the array is missing, the external scene isn't loaded, we have to wait.
                                entityRemapping.Dispose();
                                return(false);
                            }

                            var pubRefs = EntityManager.GetBuffer <PublicEntityRef>(pubRefEntities[0]);

                            // Proper mapping from external reference in section to entity in main world
                            for (int k = 0; k < extRefs.Length; ++k)
                            {
                                var srcIdx = extRefInfo.EntityIndexStart + k;
                                var target = pubRefs[extRefs[k].entityIndex].targetEntity;

                                // External references always have a version number of 1
                                entityRemapping[srcIdx] = new EntityRemapUtility.EntityRemapInfo
                                {
                                    SourceVersion = 1,
                                    Target        = target
                                };
                            }
                        }

                        m_PublicRefFilter.ResetFilter();
                    }
                }
            }

            return(true);
        }
Exemplo n.º 7
0
    private JobHandle ScheduleCounting(JobHandle inputDeps)
    {
        switch (_config.Method)
        {
        case TestMethod.InstancedByteComponent:
            if (_config.InterestingStateCount == 1)
            {
                return(new CountSpecificInstancedByteStateJob
                {
                    Components = _groupWithInstancedByteComponent.Components,
                    StateToCheckFor = 0
                }.Schedule(_groupWithInstancedByteComponent.Length, 64, inputDeps));
            }
            else
            {
                FillStatesToCheckFor();
                return(new CountMultipleInstancedByteStatesJob
                {
                    Components = _groupWithInstancedByteComponent.Components,
                    StatesToCheckFor = _statesToCheckFor
                }.Schedule(_groupWithInstancedByteComponent.Length, 64, inputDeps));
            }

        case TestMethod.AddRemoveInstancedComponent:
            return(new CountExistingComponentsJob
            {
            }.Schedule(_groupWithInstancedBinaryComponent.Length, 64, inputDeps));

        case TestMethod.AddRemoveSharedComponent:
            return(new CountExistingComponentsJob
            {
            }.Schedule(_groupWithSharedBinaryComponent.Length, 64, inputDeps));

        case TestMethod.SetValueSharedComponentForEachFilter:
            var uniques = new List <SharedByteState>();
            EntityManager.GetAllUniqueSharedComponentDatas(uniques);
            var filter = _byteStateGroup.CreateForEachFilter(uniques);
            for (int i = 0; i < filter.Length; i++)
            {
                if (_config.InterestingStateCount > uniques[i].State)
                {
                    var interestingCount = _byteStateGroup.GetEntityArray(filter, i).Length;
                    var forEachHandle    = new CountExistingComponentsJob
                    {
                    }.Schedule(interestingCount, 64, inputDeps);
                    filter.Dispose();
                    return(forEachHandle);
                }
            }
            filter.Dispose();
            return(inputDeps);

        case TestMethod.SetValueSharedComponentSetFilter:
            if (_config.InterestingStateCount == 1)
            {
                var handle = ScheduleCountForFilterWithState(0, inputDeps);
                _byteStateGroup.ResetFilter();
                return(handle);
            }
            else
            {
                var filterHandles = new NativeList <JobHandle>(_config.InterestingStateCount, Allocator.Temp);
                for (int i = 0; i < _config.InterestingStateCount; i++)
                {
                    filterHandles.Add(ScheduleCountForFilterWithState(i, inputDeps));
                }
                var combinedFilterHandles = JobHandle.CombineDependencies(filterHandles);
                _byteStateGroup.ResetFilter();
                filterHandles.Dispose();
            }
            return(inputDeps);

        case TestMethod.SetValueSharedComponentNoFilter:
            var length = _byteStateGroup.CalculateLength();
            if (length > 0)
            {
                if (_config.InterestingStateCount == 1)
                {
                    return(new CountSpecificSharedByteStateJob
                    {
                        Components = _byteStateGroup.GetSharedComponentDataArray <SharedByteState>(),
                        StateToCheckFor = 0
                    }.Schedule(length, 64, inputDeps));
                }
                else
                {
                    FillStatesToCheckFor();
                    return(new CountMultipleSharedByteStatesJob
                    {
                        Components = _byteStateGroup.GetSharedComponentDataArray <SharedByteState>(),
                        StatesToCheckFor = _statesToCheckFor
                    }.Schedule(length, 64, inputDeps));
                }
            }
            return(inputDeps);

        default:
            return(inputDeps);
        }
    }