| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | [System.Serializable] |
| | 10 | | public class SceneMetricsController : ISceneMetricsController |
| | 11 | | { |
| | 12 | | private static bool VERBOSE = false; |
| | 13 | | public IParcelScene scene; |
| | 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 | | { |
| 538 | 33 | | public Dictionary<Material, int> materials = new Dictionary<Material, int>(); |
| 538 | 34 | | public Dictionary<Mesh, int> meshes = new Dictionary<Mesh, int>(); |
| | 35 | | public int bodies = 0; |
| | 36 | | public int triangles = 0; |
| | 37 | | public int textures = 0; |
| | 38 | |
|
| | 39 | | override public string ToString() |
| | 40 | | { |
| 0 | 41 | | return string.Format("materials: {0}, meshes: {1}, bodies: {2}, triangles: {3}, textures: {4}", |
| | 42 | | materials.Count, meshes.Count, bodies, triangles, textures); |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| | 46 | | [SerializeField] |
| | 47 | | protected SceneMetricsModel model; |
| | 48 | |
|
| | 49 | | protected Dictionary<IDCLEntity, EntityMetrics> entitiesMetrics; |
| | 50 | | private Dictionary<Mesh, int> uniqueMeshesRefCount; |
| | 51 | | private Dictionary<Material, int> uniqueMaterialsRefCount; |
| | 52 | |
|
| 0 | 53 | | public bool isDirty { get; protected set; } |
| | 54 | |
|
| 27 | 55 | | public SceneMetricsModel GetModel() { return model.Clone(); } |
| | 56 | |
|
| 729 | 57 | | public SceneMetricsController(IParcelScene sceneOwner) |
| | 58 | | { |
| 729 | 59 | | this.scene = sceneOwner; |
| | 60 | |
|
| 729 | 61 | | uniqueMeshesRefCount = new Dictionary<Mesh, int>(); |
| 729 | 62 | | uniqueMaterialsRefCount = new Dictionary<Material, int>(); |
| 729 | 63 | | entitiesMetrics = new Dictionary<IDCLEntity, EntityMetrics>(); |
| 729 | 64 | | model = new SceneMetricsModel(); |
| | 65 | |
|
| 729 | 66 | | if (VERBOSE) |
| | 67 | | { |
| 0 | 68 | | Debug.Log("Start ScenePerformanceLimitsController..."); |
| | 69 | | } |
| 729 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Enable() |
| | 73 | | { |
| 729 | 74 | | if (scene == null) |
| 0 | 75 | | return; |
| | 76 | |
|
| 729 | 77 | | scene.OnEntityAdded -= OnEntityAdded; |
| 729 | 78 | | scene.OnEntityRemoved -= OnEntityRemoved; |
| 729 | 79 | | scene.OnEntityAdded += OnEntityAdded; |
| 729 | 80 | | scene.OnEntityRemoved += OnEntityRemoved; |
| 729 | 81 | | } |
| | 82 | |
|
| | 83 | | public void Disable() |
| | 84 | | { |
| 726 | 85 | | if (scene == null) |
| 0 | 86 | | return; |
| | 87 | |
|
| 726 | 88 | | scene.OnEntityAdded -= OnEntityAdded; |
| 726 | 89 | | scene.OnEntityRemoved -= OnEntityRemoved; |
| 726 | 90 | | } |
| | 91 | |
|
| | 92 | | SceneMetricsModel cachedModel = null; |
| | 93 | |
|
| | 94 | | public SceneMetricsModel GetLimits() |
| | 95 | | { |
| 1550 | 96 | | if (cachedModel == null) |
| | 97 | | { |
| 673 | 98 | | cachedModel = new SceneMetricsModel(); |
| | 99 | |
|
| 673 | 100 | | int parcelCount = scene.sceneData.parcels.Length; |
| 673 | 101 | | float log = Mathf.Log(parcelCount + 1, 2); |
| 673 | 102 | | float lineal = parcelCount; |
| | 103 | |
|
| 673 | 104 | | cachedModel.triangles = (int) (lineal * LimitsConfig.triangles); |
| 673 | 105 | | cachedModel.bodies = (int) (lineal * LimitsConfig.bodies); |
| 673 | 106 | | cachedModel.entities = (int) (lineal * LimitsConfig.entities); |
| 673 | 107 | | cachedModel.materials = (int) (log * LimitsConfig.materials); |
| 673 | 108 | | cachedModel.textures = (int) (log * LimitsConfig.textures); |
| 673 | 109 | | cachedModel.meshes = (int) (log * LimitsConfig.meshes); |
| 673 | 110 | | cachedModel.sceneHeight = (int) (log * LimitsConfig.height); |
| | 111 | | } |
| | 112 | |
|
| 1550 | 113 | | return cachedModel; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public bool IsInsideTheLimits() |
| | 117 | | { |
| 4 | 118 | | SceneMetricsModel limits = GetLimits(); |
| 4 | 119 | | SceneMetricsModel usage = GetModel(); |
| | 120 | |
|
| 4 | 121 | | if (usage.triangles > limits.triangles) |
| 0 | 122 | | return false; |
| | 123 | |
|
| 4 | 124 | | if (usage.bodies > limits.bodies) |
| 0 | 125 | | return false; |
| | 126 | |
|
| 4 | 127 | | if (usage.entities > limits.entities) |
| 1 | 128 | | return false; |
| | 129 | |
|
| 3 | 130 | | if (usage.materials > limits.materials) |
| 0 | 131 | | return false; |
| | 132 | |
|
| 3 | 133 | | if (usage.textures > limits.textures) |
| 0 | 134 | | return false; |
| | 135 | |
|
| 3 | 136 | | if (usage.meshes > limits.meshes) |
| 0 | 137 | | return false; |
| | 138 | |
|
| 3 | 139 | | return true; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public void SendEvent() |
| | 143 | | { |
| 727 | 144 | | if (isDirty) |
| | 145 | | { |
| 122 | 146 | | isDirty = false; |
| | 147 | |
|
| 122 | 148 | | Interface.WebInterface.ReportOnMetricsUpdate(scene.sceneData.id, |
| | 149 | | model.ToMetricsModel(), GetLimits().ToMetricsModel()); |
| | 150 | | } |
| 727 | 151 | | } |
| | 152 | |
|
| | 153 | | protected virtual void OnEntityAdded(IDCLEntity e) |
| | 154 | | { |
| 542 | 155 | | e.OnMeshesInfoUpdated += OnEntityMeshInfoUpdated; |
| 542 | 156 | | e.OnMeshesInfoCleaned += OnEntityMeshInfoCleaned; |
| 542 | 157 | | model.entities++; |
| 542 | 158 | | isDirty = true; |
| 542 | 159 | | } |
| | 160 | |
|
| | 161 | | protected virtual void OnEntityRemoved(IDCLEntity e) |
| | 162 | | { |
| 34 | 163 | | SubstractMetrics(e); |
| 34 | 164 | | e.OnMeshesInfoUpdated -= OnEntityMeshInfoUpdated; |
| 34 | 165 | | e.OnMeshesInfoCleaned -= OnEntityMeshInfoCleaned; |
| 34 | 166 | | model.entities--; |
| 34 | 167 | | isDirty = true; |
| 34 | 168 | | } |
| | 169 | |
|
| 1076 | 170 | | protected virtual void OnEntityMeshInfoUpdated(IDCLEntity entity) { AddOrReplaceMetrics(entity); } |
| | 171 | |
|
| 438 | 172 | | protected virtual void OnEntityMeshInfoCleaned(IDCLEntity entity) { SubstractMetrics(entity); } |
| | 173 | |
|
| | 174 | | protected void AddOrReplaceMetrics(IDCLEntity entity) |
| | 175 | | { |
| 540 | 176 | | if (entitiesMetrics.ContainsKey(entity)) |
| | 177 | | { |
| 298 | 178 | | SubstractMetrics(entity); |
| | 179 | | } |
| | 180 | |
|
| 540 | 181 | | AddMetrics(entity); |
| 540 | 182 | | } |
| | 183 | |
|
| | 184 | | protected void SubstractMetrics(IDCLEntity entity) |
| | 185 | | { |
| 552 | 186 | | if (!entitiesMetrics.ContainsKey(entity)) |
| | 187 | | { |
| 25 | 188 | | return; |
| | 189 | | } |
| | 190 | |
|
| 527 | 191 | | EntityMetrics entityMetrics = entitiesMetrics[entity]; |
| | 192 | |
|
| 527 | 193 | | RemoveEntitiesMaterial(entityMetrics); |
| 527 | 194 | | RemoveEntityMeshes(entityMetrics); |
| | 195 | |
|
| 527 | 196 | | model.materials = uniqueMaterialsRefCount.Count; |
| 527 | 197 | | model.meshes = uniqueMeshesRefCount.Count; |
| 527 | 198 | | model.triangles -= entityMetrics.triangles; |
| 527 | 199 | | model.bodies -= entityMetrics.bodies; |
| | 200 | |
|
| 527 | 201 | | if (entitiesMetrics.ContainsKey(entity)) |
| | 202 | | { |
| 527 | 203 | | entitiesMetrics.Remove(entity); |
| | 204 | | } |
| | 205 | |
|
| 527 | 206 | | isDirty = true; |
| 527 | 207 | | } |
| | 208 | |
|
| | 209 | | protected void AddMetrics(IDCLEntity entity) |
| | 210 | | { |
| 540 | 211 | | if (entity.meshRootGameObject == null) |
| | 212 | | { |
| 0 | 213 | | return; |
| | 214 | | } |
| | 215 | |
|
| | 216 | | // If the mesh is being loaded we should skip the evaluation (it will be triggered again later when the load |
| 540 | 217 | | if (entity.meshRootGameObject.GetComponent<MaterialTransitionController>()) // the object's MaterialTransiti |
| | 218 | | { |
| 2 | 219 | | return; |
| | 220 | | } |
| | 221 | |
|
| 538 | 222 | | EntityMetrics entityMetrics = new EntityMetrics(); |
| | 223 | |
|
| 538 | 224 | | int visualMeshRawTriangles = 0; |
| | 225 | |
|
| | 226 | | //NOTE(Brian): If this proves to be too slow we can spread it with a Coroutine spooler. |
| 538 | 227 | | MeshFilter[] meshFilters = entity.meshesInfo.meshFilters; |
| | 228 | |
|
| 2088 | 229 | | for (int i = 0; i < meshFilters.Length; i++) |
| | 230 | | { |
| 506 | 231 | | MeshFilter mf = meshFilters[i]; |
| | 232 | |
|
| 506 | 233 | | if (mf != null && mf.sharedMesh != null) |
| | 234 | | { |
| 506 | 235 | | visualMeshRawTriangles += mf.sharedMesh.triangles.Length; |
| 506 | 236 | | AddMesh(entityMetrics, mf.sharedMesh); |
| 506 | 237 | | if (VERBOSE) |
| | 238 | | { |
| 0 | 239 | | Debug.Log("SceneMetrics: tris count " + mf.sharedMesh.triangles.Length + " from mesh " + mf.shar |
| 0 | 240 | | Debug.Log("SceneMetrics: mesh " + mf.sharedMesh.name + " of entity " + entity.entityId); |
| | 241 | | } |
| | 242 | | } |
| | 243 | | } |
| | 244 | |
|
| 538 | 245 | | CalculateMaterials(entity, entityMetrics); |
| | 246 | |
|
| | 247 | | //The array is a list of triangles that contains indices into the vertex array. The size of the triangle arr |
| | 248 | | //Vertices can be shared by simply indexing into the same vertex. |
| 538 | 249 | | entityMetrics.triangles = visualMeshRawTriangles / 3; |
| 538 | 250 | | entityMetrics.bodies = entity.meshesInfo.meshFilters.Length; |
| | 251 | |
|
| 538 | 252 | | model.materials = uniqueMaterialsRefCount.Count; |
| 538 | 253 | | model.meshes = uniqueMeshesRefCount.Count; |
| 538 | 254 | | model.triangles += entityMetrics.triangles; |
| 538 | 255 | | model.bodies += entityMetrics.bodies; |
| | 256 | |
|
| 538 | 257 | | if (!entitiesMetrics.ContainsKey(entity)) |
| | 258 | | { |
| 538 | 259 | | entitiesMetrics.Add(entity, entityMetrics); |
| 538 | 260 | | } |
| | 261 | | else |
| | 262 | | { |
| 0 | 263 | | entitiesMetrics[entity] = entityMetrics; |
| | 264 | | } |
| | 265 | |
|
| 538 | 266 | | if (VERBOSE) |
| | 267 | | { |
| 0 | 268 | | Debug.Log("SceneMetrics: entity " + entity.entityId + " metrics " + entityMetrics.ToString()); |
| | 269 | | } |
| | 270 | |
|
| 538 | 271 | | isDirty = true; |
| 538 | 272 | | } |
| | 273 | |
|
| | 274 | | void CalculateMaterials(IDCLEntity entity, EntityMetrics entityMetrics) |
| | 275 | | { |
| 538 | 276 | | var originalMaterials = Environment.i.world.sceneBoundsChecker.GetOriginalMaterials(entity.meshesInfo); |
| 538 | 277 | | if (originalMaterials == null) |
| 17 | 278 | | return; |
| | 279 | |
|
| 521 | 280 | | int originalMaterialsCount = originalMaterials.Count; |
| | 281 | |
|
| 2110 | 282 | | for (int i = 0; i < originalMaterialsCount; i++) |
| | 283 | | { |
| 534 | 284 | | AddMaterial(entityMetrics, originalMaterials[i]); |
| | 285 | |
|
| 534 | 286 | | if (VERBOSE) |
| 0 | 287 | | Debug.Log($"SceneMetrics: material (style: {Environment.i.world.sceneBoundsChecker.GetFeedbackStyle( |
| | 288 | | } |
| 521 | 289 | | } |
| | 290 | |
|
| | 291 | | void AddMesh(EntityMetrics entityMetrics, Mesh mesh) |
| | 292 | | { |
| 506 | 293 | | if (!uniqueMeshesRefCount.ContainsKey(mesh)) |
| | 294 | | { |
| 388 | 295 | | uniqueMeshesRefCount.Add(mesh, 1); |
| 388 | 296 | | } |
| | 297 | | else |
| | 298 | | { |
| 118 | 299 | | uniqueMeshesRefCount[mesh] += 1; |
| | 300 | | } |
| | 301 | |
|
| 506 | 302 | | if (!entityMetrics.meshes.ContainsKey(mesh)) |
| | 303 | | { |
| 444 | 304 | | entityMetrics.meshes.Add(mesh, 1); |
| 444 | 305 | | } |
| | 306 | | else |
| | 307 | | { |
| 62 | 308 | | entityMetrics.meshes[mesh] += 1; |
| | 309 | | } |
| 62 | 310 | | } |
| | 311 | |
|
| | 312 | | void RemoveEntityMeshes(EntityMetrics entityMetrics) |
| | 313 | | { |
| 527 | 314 | | var entityMeshes = entityMetrics.meshes; |
| 527 | 315 | | using (var iterator = entityMeshes.GetEnumerator()) |
| | 316 | | { |
| 958 | 317 | | while (iterator.MoveNext()) |
| | 318 | | { |
| 431 | 319 | | if (uniqueMeshesRefCount.ContainsKey(iterator.Current.Key)) |
| | 320 | | { |
| 431 | 321 | | uniqueMeshesRefCount[iterator.Current.Key] -= iterator.Current.Value; |
| 431 | 322 | | if (uniqueMeshesRefCount[iterator.Current.Key] <= 0) |
| | 323 | | { |
| 375 | 324 | | uniqueMeshesRefCount.Remove(iterator.Current.Key); |
| | 325 | | } |
| | 326 | | } |
| | 327 | | } |
| 527 | 328 | | } |
| 527 | 329 | | } |
| | 330 | |
|
| | 331 | | void AddMaterial(EntityMetrics entityMetrics, Material material) |
| | 332 | | { |
| 534 | 333 | | if (material == null) |
| 0 | 334 | | return; |
| | 335 | |
|
| 534 | 336 | | if (!uniqueMaterialsRefCount.ContainsKey(material)) |
| | 337 | | { |
| 428 | 338 | | uniqueMaterialsRefCount.Add(material, 1); |
| 428 | 339 | | } |
| | 340 | | else |
| | 341 | | { |
| 106 | 342 | | uniqueMaterialsRefCount[material] += 1; |
| | 343 | | } |
| | 344 | |
|
| 534 | 345 | | if (!entityMetrics.materials.ContainsKey(material)) |
| | 346 | | { |
| 487 | 347 | | entityMetrics.materials.Add(material, 1); |
| 487 | 348 | | } |
| | 349 | | else |
| | 350 | | { |
| 47 | 351 | | entityMetrics.materials[material] += 1; |
| | 352 | | } |
| 47 | 353 | | } |
| | 354 | |
|
| | 355 | | void RemoveEntitiesMaterial(EntityMetrics entityMetrics) |
| | 356 | | { |
| 527 | 357 | | var entityMaterials = entityMetrics.materials; |
| 527 | 358 | | using (var iterator = entityMaterials.GetEnumerator()) |
| | 359 | | { |
| 1007 | 360 | | while (iterator.MoveNext()) |
| | 361 | | { |
| 480 | 362 | | if (uniqueMaterialsRefCount.ContainsKey(iterator.Current.Key)) |
| | 363 | | { |
| 480 | 364 | | uniqueMaterialsRefCount[iterator.Current.Key] -= iterator.Current.Value; |
| 480 | 365 | | if (uniqueMaterialsRefCount[iterator.Current.Key] <= 0) |
| | 366 | | { |
| 421 | 367 | | uniqueMaterialsRefCount.Remove(iterator.Current.Key); |
| | 368 | | } |
| | 369 | | } |
| | 370 | | } |
| 527 | 371 | | } |
| 527 | 372 | | } |
| | 373 | | } |
| | 374 | | } |