| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | [System.Serializable] |
| | 13 | | public sealed class SceneMetricsCounter : ISceneMetricsCounter |
| | 14 | | { |
| | 15 | | public static class LimitsConfig |
| | 16 | | { |
| | 17 | | // number of entities |
| | 18 | | public const int entities = 200; |
| | 19 | |
|
| | 20 | | // Number of faces (per parcel) |
| | 21 | | public const int triangles = 10000; |
| | 22 | | public const int bodies = 300; |
| | 23 | | public const int textures = 10; |
| | 24 | | public const int materials = 20; |
| | 25 | | public const int meshes = 200; |
| | 26 | |
|
| | 27 | | public const float height = 20; |
| | 28 | | public const float visibleRadius = 10; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | protected class EntityMetrics |
| | 32 | | { |
| 550 | 33 | | public Dictionary<Material, int> materials = new Dictionary<Material, int>(); |
| | 34 | | public int bodies = 0; |
| | 35 | | public int triangles = 0; |
| | 36 | | public int textures = 0; |
| | 37 | |
|
| | 38 | | public override string ToString() |
| | 39 | | { |
| 550 | 40 | | return $"materials: {materials.Count}, bodies: {bodies}, triangles: {triangles}, textures: {textures}"; |
| | 41 | | } |
| | 42 | | } |
| | 43 | |
|
| 1 | 44 | | private static bool VERBOSE = false; |
| 1 | 45 | | private static ILogger logger = new Logger(Debug.unityLogger.logHandler) { filterLogType = VERBOSE ? LogType.Log |
| | 46 | |
|
| 1420 | 47 | | public IParcelScene scene { get; private set; } |
| | 48 | |
|
| 710 | 49 | | public HashSet<string> excludedEntities = new HashSet<string>(); |
| | 50 | |
|
| | 51 | | SceneMetricsModel cachedModel = null; |
| | 52 | |
|
| | 53 | | private SceneMetricsModel model; |
| | 54 | |
|
| 710 | 55 | | private Dictionary<Material, int> uniqueMaterialsRefCount = new Dictionary<Material, int>(); |
| 710 | 56 | | private Dictionary<Mesh, int> uniqueMeshesRefCount = new Dictionary<Mesh, int>(); |
| 710 | 57 | | private Dictionary<IDCLEntity, EntityMetrics> entitiesMetrics = new Dictionary<IDCLEntity, EntityMetrics>(); |
| | 58 | |
|
| | 59 | | private WorldSceneObjectsTrackingHelper sceneObjectsTrackingHelper; |
| | 60 | | public event Action<ISceneMetricsCounter> OnMetricsUpdated; |
| | 61 | |
|
| 0 | 62 | | public bool isDirty { get; private set; } |
| | 63 | |
|
| 35 | 64 | | public SceneMetricsModel GetModel() { return model.Clone(); } |
| | 65 | |
|
| 710 | 66 | | public SceneMetricsCounter(IParcelScene sceneOwner) |
| | 67 | | { |
| 710 | 68 | | Assert.IsTrue( !string.IsNullOrEmpty(sceneOwner.sceneData.id), "Scene must have an ID!" ); |
| 710 | 69 | | this.scene = sceneOwner; |
| | 70 | |
|
| 710 | 71 | | model = new SceneMetricsModel(); |
| 710 | 72 | | sceneObjectsTrackingHelper = new WorldSceneObjectsTrackingHelper(DataStore.i, scene.sceneData.id); |
| | 73 | |
|
| 710 | 74 | | logger.Log("Start ScenePerformanceLimitsController..."); |
| 710 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Enable() |
| | 78 | | { |
| 710 | 79 | | if (scene == null) |
| 0 | 80 | | return; |
| | 81 | |
|
| 710 | 82 | | sceneObjectsTrackingHelper.OnWillAddRendereable -= OnWillAddRendereable; |
| 710 | 83 | | sceneObjectsTrackingHelper.OnWillRemoveRendereable -= OnWillRemoveRendereable; |
| | 84 | |
|
| 710 | 85 | | sceneObjectsTrackingHelper.OnWillAddRendereable += OnWillAddRendereable; |
| 710 | 86 | | sceneObjectsTrackingHelper.OnWillRemoveRendereable += OnWillRemoveRendereable; |
| | 87 | |
|
| 710 | 88 | | scene.OnEntityAdded -= OnEntityAdded; |
| 710 | 89 | | scene.OnEntityRemoved -= OnEntityRemoved; |
| 710 | 90 | | scene.OnEntityAdded += OnEntityAdded; |
| 710 | 91 | | scene.OnEntityRemoved += OnEntityRemoved; |
| 710 | 92 | | } |
| | 93 | |
|
| | 94 | | public void Disable() |
| | 95 | | { |
| 710 | 96 | | if (scene == null) |
| 0 | 97 | | return; |
| | 98 | |
|
| 710 | 99 | | sceneObjectsTrackingHelper.OnWillAddRendereable -= OnWillAddRendereable; |
| 710 | 100 | | sceneObjectsTrackingHelper.OnWillRemoveRendereable -= OnWillRemoveRendereable; |
| | 101 | |
|
| 710 | 102 | | scene.OnEntityAdded -= OnEntityAdded; |
| 710 | 103 | | scene.OnEntityRemoved -= OnEntityRemoved; |
| 710 | 104 | | } |
| | 105 | |
|
| | 106 | | private void OnWillAddRendereable(Rendereable rendereable) |
| | 107 | | { |
| 278 | 108 | | if (excludedEntities.Contains(rendereable.ownerId)) |
| 0 | 109 | | return; |
| | 110 | |
|
| 278 | 111 | | int trianglesToAdd = rendereable.totalTriangleCount / 3; |
| 278 | 112 | | model.triangles += trianglesToAdd; |
| | 113 | |
|
| 1342 | 114 | | for ( int i = 0; i < rendereable.meshes.Count; i++) |
| | 115 | | { |
| 393 | 116 | | Mesh mesh = rendereable.meshes[i]; |
| | 117 | |
|
| 393 | 118 | | if (uniqueMeshesRefCount.ContainsKey(mesh)) |
| | 119 | | { |
| 57 | 120 | | uniqueMeshesRefCount[mesh]++; |
| 57 | 121 | | } |
| | 122 | | else |
| | 123 | | { |
| 336 | 124 | | uniqueMeshesRefCount.Add(mesh, 1); |
| 336 | 125 | | model.meshes++; |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| 278 | 129 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private void OnWillRemoveRendereable(Rendereable rendereable) |
| | 133 | | { |
| 101 | 134 | | if (excludedEntities.Contains(rendereable.ownerId)) |
| 0 | 135 | | return; |
| | 136 | |
|
| 101 | 137 | | int trianglesToRemove = rendereable.totalTriangleCount / 3; |
| 101 | 138 | | model.triangles -= trianglesToRemove; |
| | 139 | |
|
| 428 | 140 | | for ( int i = 0; i < rendereable.meshes.Count; i++) |
| | 141 | | { |
| 113 | 142 | | Mesh mesh = rendereable.meshes[i]; |
| | 143 | |
|
| 113 | 144 | | if (uniqueMeshesRefCount.ContainsKey(mesh)) |
| | 145 | | { |
| 113 | 146 | | uniqueMeshesRefCount[mesh]--; |
| | 147 | |
|
| 113 | 148 | | if (uniqueMeshesRefCount[mesh] == 0) |
| | 149 | | { |
| 86 | 150 | | model.meshes--; |
| 86 | 151 | | uniqueMeshesRefCount.Remove(mesh); |
| | 152 | | } |
| 86 | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| 0 | 156 | | logger.LogWarning("OnWillRemoveRendereable", "Trying to remove mesh that never was added"); |
| | 157 | | } |
| | 158 | | } |
| | 159 | |
|
| 101 | 160 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void OnEntityAdded(IDCLEntity e) |
| | 164 | | { |
| 546 | 165 | | e.OnMeshesInfoUpdated += OnEntityMeshInfoUpdated; |
| 546 | 166 | | e.OnMeshesInfoCleaned += OnEntityMeshInfoCleaned; |
| 546 | 167 | | model.entities++; |
| 546 | 168 | | isDirty = true; |
| 546 | 169 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | private void OnEntityRemoved(IDCLEntity e) |
| | 173 | | { |
| | 174 | | // TODO(Brian): When all the code is migrated to the Rendereable counting, remove this call |
| 38 | 175 | | SubstractMetrics(e); |
| | 176 | |
|
| 38 | 177 | | e.OnMeshesInfoUpdated -= OnEntityMeshInfoUpdated; |
| 38 | 178 | | e.OnMeshesInfoCleaned -= OnEntityMeshInfoCleaned; |
| 38 | 179 | | model.entities--; |
| 38 | 180 | | isDirty = true; |
| 38 | 181 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | // TODO(Brian): When all the code is migrated to the Rendereable counting, remove this method |
| | 185 | | private void OnEntityMeshInfoUpdated(IDCLEntity entity) |
| | 186 | | { |
| 552 | 187 | | AddOrReplaceMetrics(entity); |
| 552 | 188 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | // TODO(Brian): When all the code is migrated to the Rendereable counting, remove this method |
| | 192 | | private void OnEntityMeshInfoCleaned(IDCLEntity entity) |
| | 193 | | { |
| 213 | 194 | | SubstractMetrics(entity); |
| 213 | 195 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | // TODO(Brian): When all the code is migrated to the Rendereable counting, remove this method |
| | 199 | | private void AddOrReplaceMetrics(IDCLEntity entity) |
| | 200 | | { |
| 552 | 201 | | if (entitiesMetrics.ContainsKey(entity)) |
| | 202 | | { |
| 308 | 203 | | SubstractMetrics(entity); |
| | 204 | | } |
| | 205 | |
|
| 552 | 206 | | AddMetrics(entity); |
| 552 | 207 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | // TODO(Brian): Move all this counting on OnWillRemoveRendereable instead |
| | 211 | | [System.Obsolete] |
| | 212 | | protected void SubstractMetrics(IDCLEntity entity) |
| | 213 | | { |
| 559 | 214 | | if (excludedEntities.Contains(entity.entityId)) |
| 0 | 215 | | return; |
| | 216 | |
|
| 559 | 217 | | if (!entitiesMetrics.ContainsKey(entity)) |
| 22 | 218 | | return; |
| | 219 | |
|
| 537 | 220 | | EntityMetrics entityMetrics = entitiesMetrics[entity]; |
| | 221 | |
|
| 537 | 222 | | RemoveEntitiesMaterial(entityMetrics); |
| | 223 | |
|
| 537 | 224 | | model.materials = uniqueMaterialsRefCount.Count; |
| 537 | 225 | | model.bodies -= entityMetrics.bodies; |
| | 226 | |
|
| 537 | 227 | | if (entitiesMetrics.ContainsKey(entity)) |
| | 228 | | { |
| 537 | 229 | | entitiesMetrics.Remove(entity); |
| | 230 | | } |
| | 231 | |
|
| 537 | 232 | | isDirty = true; |
| 537 | 233 | | } |
| | 234 | |
|
| | 235 | | // TODO(Brian): Move all this counting on OnWillAddRendereable instead |
| | 236 | | [System.Obsolete] |
| | 237 | | protected void AddMetrics(IDCLEntity entity) |
| | 238 | | { |
| 552 | 239 | | if (excludedEntities.Contains(entity.entityId)) |
| 0 | 240 | | return; |
| | 241 | |
|
| 552 | 242 | | if (entity.meshRootGameObject == null) |
| 0 | 243 | | return; |
| | 244 | |
|
| | 245 | | // If the mesh is being loaded we should skip the evaluation (it will be triggered again later when the load |
| 552 | 246 | | if (entity.meshRootGameObject.GetComponent<MaterialTransitionController>()) |
| 2 | 247 | | return; |
| | 248 | |
|
| 550 | 249 | | EntityMetrics entityMetrics = new EntityMetrics(); |
| | 250 | |
|
| 550 | 251 | | CalculateMaterials(entity, entityMetrics); |
| | 252 | |
|
| | 253 | | // TODO(Brian): We should move bodies and materials to DataStore_WorldObjects later |
| 550 | 254 | | entityMetrics.bodies = entity.meshesInfo.meshFilters.Length; |
| | 255 | |
|
| 550 | 256 | | model.materials = uniqueMaterialsRefCount.Count; |
| 550 | 257 | | model.bodies += entityMetrics.bodies; |
| | 258 | |
|
| 550 | 259 | | if (!entitiesMetrics.ContainsKey(entity)) |
| 550 | 260 | | entitiesMetrics.Add(entity, entityMetrics); |
| | 261 | | else |
| 0 | 262 | | entitiesMetrics[entity] = entityMetrics; |
| | 263 | |
|
| 550 | 264 | | logger.Log("SceneMetrics: entity " + entity.entityId + " metrics " + entityMetrics.ToString()); |
| 550 | 265 | | isDirty = true; |
| 550 | 266 | | } |
| | 267 | |
|
| | 268 | | // TODO(Brian): Move all this counting on OnWillAddRendereable instead |
| | 269 | | [System.Obsolete] |
| | 270 | | void CalculateMaterials(IDCLEntity entity, EntityMetrics entityMetrics) |
| | 271 | | { |
| | 272 | | // TODO(Brian): Find a way to remove this dependency |
| 550 | 273 | | var originalMaterials = Environment.i.world.sceneBoundsChecker.GetOriginalMaterials(entity.meshesInfo); |
| | 274 | |
|
| 550 | 275 | | if (originalMaterials == null) |
| | 276 | | { |
| 20 | 277 | | logger.Log($"SceneMetrics: material null of entity {entity.entityId} -- (style: {Environment.i.world.sce |
| 20 | 278 | | return; |
| | 279 | | } |
| | 280 | |
|
| 530 | 281 | | int originalMaterialsCount = originalMaterials.Count; |
| | 282 | |
|
| 2066 | 283 | | for (int i = 0; i < originalMaterialsCount; i++) |
| | 284 | | { |
| 503 | 285 | | AddMaterial(entityMetrics, originalMaterials[i]); |
| 503 | 286 | | logger.Log($"SceneMetrics: material {originalMaterials[i].name} of entity {entity.entityId} -- (style: { |
| | 287 | | } |
| 530 | 288 | | } |
| | 289 | |
|
| | 290 | | // TODO(Brian): Put this into OnWillAddRendereable instead |
| | 291 | | [System.Obsolete] |
| | 292 | | void AddMaterial(EntityMetrics entityMetrics, Material material) |
| | 293 | | { |
| 503 | 294 | | if (material == null) |
| 0 | 295 | | return; |
| | 296 | |
|
| 503 | 297 | | if (!uniqueMaterialsRefCount.ContainsKey(material)) |
| 405 | 298 | | uniqueMaterialsRefCount.Add(material, 1); |
| | 299 | | else |
| 98 | 300 | | uniqueMaterialsRefCount[material] += 1; |
| | 301 | |
|
| 503 | 302 | | if (!entityMetrics.materials.ContainsKey(material)) |
| 465 | 303 | | entityMetrics.materials.Add(material, 1); |
| | 304 | | else |
| 38 | 305 | | entityMetrics.materials[material] += 1; |
| 38 | 306 | | } |
| | 307 | |
|
| | 308 | | // TODO(Brian): Put this into OnWillRemoveRendereable instead |
| | 309 | | [System.Obsolete] |
| | 310 | | void RemoveEntitiesMaterial(EntityMetrics entityMetrics) |
| | 311 | | { |
| 537 | 312 | | var entityMaterials = entityMetrics.materials; |
| 537 | 313 | | using (var iterator = entityMaterials.GetEnumerator()) |
| | 314 | | { |
| 994 | 315 | | while (iterator.MoveNext()) |
| | 316 | | { |
| 457 | 317 | | if (uniqueMaterialsRefCount.ContainsKey(iterator.Current.Key)) |
| | 318 | | { |
| 457 | 319 | | uniqueMaterialsRefCount[iterator.Current.Key] -= iterator.Current.Value; |
| | 320 | |
|
| 457 | 321 | | if (uniqueMaterialsRefCount[iterator.Current.Key] <= 0) |
| 397 | 322 | | uniqueMaterialsRefCount.Remove(iterator.Current.Key); |
| | 323 | | } |
| | 324 | | } |
| 537 | 325 | | } |
| 537 | 326 | | } |
| | 327 | |
|
| | 328 | | public void Dispose() |
| | 329 | | { |
| 710 | 330 | | if (scene == null) |
| 0 | 331 | | return; |
| | 332 | |
|
| 710 | 333 | | sceneObjectsTrackingHelper.Dispose(); |
| | 334 | |
|
| 710 | 335 | | scene.OnEntityAdded -= OnEntityAdded; |
| 710 | 336 | | scene.OnEntityRemoved -= OnEntityRemoved; |
| | 337 | |
|
| 710 | 338 | | logger.Log("Disposing..."); |
| 710 | 339 | | } |
| | 340 | |
|
| | 341 | |
|
| | 342 | | public SceneMetricsModel GetLimits() |
| | 343 | | { |
| 1577 | 344 | | if (cachedModel == null) |
| | 345 | | { |
| 673 | 346 | | cachedModel = new SceneMetricsModel(); |
| | 347 | |
|
| 673 | 348 | | int parcelCount = scene.sceneData.parcels.Length; |
| 673 | 349 | | float log = Mathf.Log(parcelCount + 1, 2); |
| 673 | 350 | | float lineal = parcelCount; |
| | 351 | |
|
| 673 | 352 | | cachedModel.triangles = (int) (lineal * LimitsConfig.triangles); |
| 673 | 353 | | cachedModel.bodies = (int) (lineal * LimitsConfig.bodies); |
| 673 | 354 | | cachedModel.entities = (int) (lineal * LimitsConfig.entities); |
| 673 | 355 | | cachedModel.materials = (int) (log * LimitsConfig.materials); |
| 673 | 356 | | cachedModel.textures = (int) (log * LimitsConfig.textures); |
| 673 | 357 | | cachedModel.meshes = (int) (log * LimitsConfig.meshes); |
| 673 | 358 | | cachedModel.sceneHeight = (int) (log * LimitsConfig.height); |
| | 359 | | } |
| | 360 | |
|
| 1577 | 361 | | return cachedModel; |
| | 362 | | } |
| | 363 | |
|
| | 364 | | public bool IsInsideTheLimits() |
| | 365 | | { |
| 4 | 366 | | SceneMetricsModel limits = GetLimits(); |
| 4 | 367 | | SceneMetricsModel usage = GetModel(); |
| | 368 | |
|
| 4 | 369 | | if (usage.triangles > limits.triangles) |
| 0 | 370 | | return false; |
| | 371 | |
|
| 4 | 372 | | if (usage.bodies > limits.bodies) |
| 0 | 373 | | return false; |
| | 374 | |
|
| 4 | 375 | | if (usage.entities > limits.entities) |
| 1 | 376 | | return false; |
| | 377 | |
|
| 3 | 378 | | if (usage.materials > limits.materials) |
| 0 | 379 | | return false; |
| | 380 | |
|
| 3 | 381 | | if (usage.textures > limits.textures) |
| 0 | 382 | | return false; |
| | 383 | |
|
| 3 | 384 | | if (usage.meshes > limits.meshes) |
| 0 | 385 | | return false; |
| | 386 | |
|
| 3 | 387 | | return true; |
| | 388 | | } |
| | 389 | |
|
| | 390 | | public void AddExcludedEntity(string entityId) |
| | 391 | | { |
| 3 | 392 | | if ( !excludedEntities.Contains(entityId)) |
| 3 | 393 | | excludedEntities.Add(entityId); |
| 3 | 394 | | } |
| | 395 | |
|
| | 396 | | public void RemoveExcludedEntity(string entityId) |
| | 397 | | { |
| 3 | 398 | | if ( excludedEntities.Contains(entityId)) |
| 3 | 399 | | excludedEntities.Remove(entityId); |
| 3 | 400 | | } |
| | 401 | |
|
| | 402 | | public void SendEvent() |
| | 403 | | { |
| 1031 | 404 | | if (!isDirty) |
| 888 | 405 | | return; |
| | 406 | |
|
| 143 | 407 | | isDirty = false; |
| | 408 | |
|
| 143 | 409 | | Interface.WebInterface.ReportOnMetricsUpdate(scene.sceneData.id, |
| | 410 | | model.ToMetricsModel(), GetLimits().ToMetricsModel()); |
| 143 | 411 | | } |
| | 412 | | } |
| | 413 | | } |