| | 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 | | using UnityEngine.Profiling; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 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 | |
|
| 1 | 31 | | private static bool VERBOSE = false; |
| 1 | 32 | | private static Logger logger = new Logger("SceneMetricsCounter") { verboseEnabled = VERBOSE }; |
| | 33 | | public event Action<ISceneMetricsCounter> OnMetricsUpdated; |
| | 34 | |
|
| 443 | 35 | | private SceneMetricsModel maxCountValue = new SceneMetricsModel(); |
| 443 | 36 | | private SceneMetricsModel currentCountValue = new SceneMetricsModel(); |
| 443 | 37 | | private SceneMetricsModel lastCountValue = new SceneMetricsModel(); |
| | 38 | |
|
| | 39 | | // TODO: We should handle this better, right now if we get the current amount of limits, we update the metrics |
| | 40 | | // So if someone check the current amount when subscribed to the OnMetricsUpdated, we will try to Update the met |
| | 41 | | public SceneMetricsModel currentCount |
| | 42 | | { |
| | 43 | | get |
| | 44 | | { |
| 79 | 45 | | UpdateMetrics(); |
| 79 | 46 | | return currentCountValue.Clone(); |
| | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| 361 | 50 | | public SceneMetricsModel maxCount => maxCountValue.Clone(); |
| | 51 | |
|
| 4578 | 52 | | public bool dirty { get; private set; } |
| | 53 | |
|
| | 54 | | private int sceneNumber; |
| | 55 | |
|
| | 56 | | private Vector2Int scenePosition; |
| | 57 | |
|
| | 58 | | private int sceneParcelCount; |
| | 59 | |
|
| | 60 | | private DataStore_WorldObjects data; |
| | 61 | | private bool enabled = false; |
| | 62 | |
|
| 10 | 63 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore, int sceneNumber, Vector2Int scenePosition, int scen |
| | 64 | | { |
| 10 | 65 | | this.data = dataStore; |
| 10 | 66 | | Configure(sceneNumber, scenePosition, sceneParcelCount); |
| 10 | 67 | | } |
| | 68 | |
|
| 433 | 69 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore) |
| | 70 | | { |
| 433 | 71 | | this.data = dataStore; |
| 433 | 72 | | } |
| | 73 | |
|
| | 74 | | public void Configure(int sceneNumber, Vector2Int scenePosition, int sceneParcelCount) |
| | 75 | | { |
| 436 | 76 | | this.sceneNumber = sceneNumber; |
| 436 | 77 | | this.scenePosition = scenePosition; |
| 436 | 78 | | this.sceneParcelCount = sceneParcelCount; |
| | 79 | |
|
| 436 | 80 | | Assert.IsTrue( sceneNumber > 0, "Scene must have a scene number!" ); |
| 436 | 81 | | maxCountValue = ComputeMaxCount(); |
| 436 | 82 | | } |
| | 83 | |
|
| | 84 | | public void Dispose() |
| | 85 | | { |
| 443 | 86 | | } |
| | 87 | |
|
| | 88 | | public void Enable() |
| | 89 | | { |
| 436 | 90 | | if ( enabled ) |
| 0 | 91 | | return; |
| | 92 | |
|
| 436 | 93 | | var sceneData = data.sceneData[sceneNumber]; |
| | 94 | |
|
| 436 | 95 | | sceneData.materials.OnAdded += OnDataChanged; |
| 436 | 96 | | sceneData.materials.OnRemoved += OnDataChanged; |
| | 97 | |
|
| 436 | 98 | | sceneData.textures.OnAdded += OnTextureAdded; |
| 436 | 99 | | sceneData.textures.OnRemoved += OnTextureRemoved; |
| | 100 | |
|
| 436 | 101 | | sceneData.meshes.OnAdded += OnMeshAdded; |
| 436 | 102 | | sceneData.meshes.OnRemoved += OnMeshRemoved; |
| | 103 | |
|
| 436 | 104 | | sceneData.animationClips.OnAdded += OnAnimationClipAdded; |
| 436 | 105 | | sceneData.animationClips.OnRemoved += OnAnimationClipRemoved; |
| | 106 | |
|
| 436 | 107 | | sceneData.animationClipSize.OnChange += OnAnimationClipSizeChange; |
| 436 | 108 | | sceneData.meshDataSize.OnChange += OnMeshDataSizeChange; |
| | 109 | |
|
| 436 | 110 | | sceneData.audioClips.OnAdded += OnAudioClipAdded; |
| 436 | 111 | | sceneData.audioClips.OnRemoved += OnAudioClipRemoved; |
| | 112 | |
|
| 436 | 113 | | sceneData.renderers.OnAdded += OnDataChanged; |
| 436 | 114 | | sceneData.renderers.OnRemoved += OnDataChanged; |
| | 115 | |
|
| 436 | 116 | | sceneData.owners.OnAdded += OnDataChanged; |
| 436 | 117 | | sceneData.owners.OnRemoved += OnDataChanged; |
| | 118 | |
|
| 436 | 119 | | sceneData.triangles.OnChange += OnDataChanged; |
| | 120 | |
|
| 436 | 121 | | enabled = true; |
| 436 | 122 | | } |
| | 123 | |
|
| | 124 | | public void Disable() |
| | 125 | | { |
| 433 | 126 | | if ( !enabled ) |
| 7 | 127 | | return; |
| | 128 | |
|
| 426 | 129 | | var sceneData = data.sceneData[sceneNumber]; |
| | 130 | |
|
| 426 | 131 | | sceneData.materials.OnAdded -= OnDataChanged; |
| 426 | 132 | | sceneData.materials.OnRemoved -= OnDataChanged; |
| | 133 | |
|
| 426 | 134 | | sceneData.textures.OnAdded -= OnTextureAdded; |
| 426 | 135 | | sceneData.textures.OnRemoved -= OnTextureRemoved; |
| | 136 | |
|
| 426 | 137 | | sceneData.meshes.OnAdded -= OnMeshAdded; |
| 426 | 138 | | sceneData.meshes.OnRemoved -= OnMeshRemoved; |
| | 139 | |
|
| 426 | 140 | | sceneData.animationClipSize.OnChange -= OnAnimationClipSizeChange; |
| 426 | 141 | | sceneData.meshDataSize.OnChange -= OnMeshDataSizeChange; |
| | 142 | |
|
| 426 | 143 | | sceneData.audioClips.OnAdded -= OnAudioClipAdded; |
| 426 | 144 | | sceneData.audioClips.OnRemoved -= OnAudioClipRemoved; |
| | 145 | |
|
| 426 | 146 | | sceneData.animationClips.OnAdded -= OnDataChanged; |
| 426 | 147 | | sceneData.animationClips.OnRemoved -= OnDataChanged; |
| | 148 | |
|
| 426 | 149 | | sceneData.renderers.OnAdded -= OnDataChanged; |
| 426 | 150 | | sceneData.renderers.OnRemoved -= OnDataChanged; |
| | 151 | |
|
| 426 | 152 | | sceneData.owners.OnAdded -= OnDataChanged; |
| 426 | 153 | | sceneData.owners.OnRemoved -= OnDataChanged; |
| | 154 | |
|
| 426 | 155 | | sceneData.triangles.OnChange -= OnDataChanged; |
| | 156 | |
|
| 426 | 157 | | enabled = false; |
| 426 | 158 | | } |
| | 159 | |
|
| | 160 | | private SceneMetricsModel ComputeMaxCount() |
| | 161 | | { |
| 436 | 162 | | var result = new SceneMetricsModel(); |
| | 163 | |
|
| 436 | 164 | | float log = Mathf.Log(sceneParcelCount + 1, 2); |
| 436 | 165 | | float lineal = sceneParcelCount; |
| | 166 | |
|
| 436 | 167 | | result.triangles = (int) (lineal * LimitsConfig.triangles); |
| 436 | 168 | | result.bodies = (int) (lineal * LimitsConfig.bodies); |
| 436 | 169 | | result.entities = (int) (lineal * LimitsConfig.entities); |
| 436 | 170 | | result.materials = (int) (log * LimitsConfig.materials); |
| 436 | 171 | | result.textures = (int) (log * LimitsConfig.textures); |
| 436 | 172 | | result.meshes = (int) (log * LimitsConfig.meshes); |
| 436 | 173 | | result.sceneHeight = (int) (log * LimitsConfig.height); |
| | 174 | |
|
| 436 | 175 | | return result; |
| | 176 | | } |
| | 177 | |
|
| | 178 | | public bool IsInsideTheLimits() |
| | 179 | | { |
| 0 | 180 | | UpdateMetrics(); |
| 0 | 181 | | SceneMetricsModel limits = ComputeMaxCount(); |
| 0 | 182 | | SceneMetricsModel usage = currentCountValue; |
| | 183 | |
|
| 0 | 184 | | if (usage.triangles > limits.triangles) |
| 0 | 185 | | return false; |
| | 186 | |
|
| 0 | 187 | | if (usage.bodies > limits.bodies) |
| 0 | 188 | | return false; |
| | 189 | |
|
| 0 | 190 | | if (usage.entities > limits.entities) |
| 0 | 191 | | return false; |
| | 192 | |
|
| 0 | 193 | | if (usage.materials > limits.materials) |
| 0 | 194 | | return false; |
| | 195 | |
|
| 0 | 196 | | if (usage.textures > limits.textures) |
| 0 | 197 | | return false; |
| | 198 | |
|
| 0 | 199 | | if (usage.meshes > limits.meshes) |
| 0 | 200 | | return false; |
| | 201 | |
|
| 0 | 202 | | return true; |
| | 203 | | } |
| | 204 | |
|
| | 205 | | private void MarkDirty() |
| | 206 | | { |
| 3084 | 207 | | dirty = true; |
| 3084 | 208 | | } |
| | 209 | |
|
| | 210 | | public void SendEvent() |
| | 211 | | { |
| 1317 | 212 | | if (!dirty) |
| 1140 | 213 | | return; |
| | 214 | |
|
| 177 | 215 | | dirty = false; |
| | 216 | |
|
| 177 | 217 | | UpdateMetrics(); |
| | 218 | |
|
| 177 | 219 | | Interface.WebInterface.ReportOnMetricsUpdate(sceneNumber, currentCountValue.ToMetricsModel(), maxCount.ToMet |
| 177 | 220 | | } |
| | 221 | |
|
| | 222 | | void OnMeshAdded(Mesh mesh) |
| | 223 | | { |
| 347 | 224 | | if (mesh == null) |
| 0 | 225 | | return; |
| | 226 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 347 | 227 | | currentCountValue.meshMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(mesh); |
| | 228 | | #endif |
| 347 | 229 | | MarkDirty(); |
| 347 | 230 | | } |
| | 231 | |
|
| | 232 | | void OnMeshRemoved(Mesh mesh) |
| | 233 | | { |
| 127 | 234 | | if (mesh == null) |
| 0 | 235 | | return; |
| | 236 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 127 | 237 | | currentCountValue.meshMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(mesh); |
| | 238 | | #endif |
| 127 | 239 | | MarkDirty(); |
| 127 | 240 | | } |
| | 241 | |
|
| | 242 | | void OnAnimationClipAdded(AnimationClip animationClip) |
| | 243 | | { |
| 12 | 244 | | if (animationClip == null) |
| 0 | 245 | | return; |
| | 246 | |
|
| | 247 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 12 | 248 | | currentCountValue.animationClipMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(animationClip); |
| | 249 | | #endif |
| 12 | 250 | | MarkDirty(); |
| 12 | 251 | | } |
| | 252 | |
|
| | 253 | | void OnAnimationClipRemoved(AnimationClip animationClip) |
| | 254 | | { |
| 2 | 255 | | if (animationClip == null) |
| 0 | 256 | | return; |
| | 257 | |
|
| | 258 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 2 | 259 | | currentCountValue.animationClipMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(animationClip); |
| | 260 | | #endif |
| 2 | 261 | | MarkDirty(); |
| 2 | 262 | | } |
| | 263 | |
|
| | 264 | | void OnAudioClipAdded(AudioClip audioClip) |
| | 265 | | { |
| 18 | 266 | | if (audioClip == null) |
| 0 | 267 | | return; |
| | 268 | |
|
| 18 | 269 | | MarkDirty(); |
| | 270 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 18 | 271 | | currentCountValue.audioClipMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(audioClip); |
| | 272 | | #endif |
| 18 | 273 | | currentCountValue.audioClipMemoryScore += MetricsScoreUtils.ComputeAudioClipScore(audioClip); |
| 18 | 274 | | } |
| | 275 | |
|
| | 276 | | void OnAudioClipRemoved(AudioClip audioClip) |
| | 277 | | { |
| 3 | 278 | | if (audioClip == null) |
| 0 | 279 | | return; |
| | 280 | |
|
| 3 | 281 | | MarkDirty(); |
| | 282 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 3 | 283 | | currentCountValue.audioClipMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(audioClip); |
| | 284 | | #endif |
| 3 | 285 | | currentCountValue.audioClipMemoryScore -= MetricsScoreUtils.ComputeAudioClipScore(audioClip); |
| 3 | 286 | | } |
| | 287 | |
|
| | 288 | |
|
| | 289 | | private void OnMeshDataSizeChange(long current, long previous) |
| | 290 | | { |
| 4 | 291 | | MarkDirty(); |
| 4 | 292 | | currentCountValue.meshMemoryScore = current; |
| 4 | 293 | | } |
| | 294 | |
|
| | 295 | | void OnAnimationClipSizeChange(long animationClipSize, long previous) |
| | 296 | | { |
| 4 | 297 | | MarkDirty(); |
| 4 | 298 | | currentCountValue.animationClipMemoryScore = animationClipSize; |
| 4 | 299 | | } |
| | 300 | |
|
| | 301 | | void OnTextureAdded(Texture texture) |
| | 302 | | { |
| 193 | 303 | | if (texture == null) |
| 0 | 304 | | return; |
| | 305 | |
|
| 193 | 306 | | MarkDirty(); |
| | 307 | |
|
| 193 | 308 | | if (texture is Texture2D tex2D) |
| | 309 | | { |
| | 310 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 193 | 311 | | currentCountValue.textureMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(tex2D); |
| | 312 | | #endif |
| 193 | 313 | | currentCountValue.textureMemoryScore += MetricsScoreUtils.ComputeTextureScore(tex2D); |
| | 314 | | } |
| 193 | 315 | | } |
| | 316 | |
|
| | 317 | | void OnTextureRemoved(Texture texture) |
| | 318 | | { |
| 60 | 319 | | if (texture == null) |
| 0 | 320 | | return; |
| | 321 | |
|
| 60 | 322 | | MarkDirty(); |
| | 323 | |
|
| 60 | 324 | | if (texture is Texture2D tex2D) |
| | 325 | | { |
| | 326 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 60 | 327 | | currentCountValue.textureMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(tex2D); |
| | 328 | | #endif |
| 60 | 329 | | currentCountValue.textureMemoryScore -= MetricsScoreUtils.ComputeTextureScore(tex2D); |
| | 330 | | } |
| 60 | 331 | | } |
| | 332 | |
|
| | 333 | | void OnDataChanged<T>(T obj) |
| | 334 | | where T : class |
| | 335 | | { |
| 794 | 336 | | MarkDirty(); |
| 794 | 337 | | } |
| | 338 | |
|
| | 339 | | void OnDataChanged(int obj1, int obj2) |
| | 340 | | { |
| 472 | 341 | | MarkDirty(); |
| 472 | 342 | | } |
| | 343 | |
|
| | 344 | | void OnDataChanged(long obj1) |
| | 345 | | { |
| 1048 | 346 | | MarkDirty(); |
| 1048 | 347 | | } |
| | 348 | |
|
| | 349 | | private void UpdateMetrics() |
| | 350 | | { |
| 256 | 351 | | if (sceneNumber <= 0 || data == null || !data.sceneData.ContainsKey(sceneNumber)) |
| 0 | 352 | | return; |
| | 353 | |
|
| 256 | 354 | | if (data != null && data.sceneData.ContainsKey(sceneNumber)) |
| | 355 | | { |
| 256 | 356 | | var sceneData = data.sceneData[sceneNumber]; |
| | 357 | |
|
| 256 | 358 | | if (sceneData != null) |
| | 359 | | { |
| 256 | 360 | | currentCountValue.materials = sceneData.materials.Count(); |
| 256 | 361 | | currentCountValue.textures = sceneData.textures.Count(); |
| 256 | 362 | | currentCountValue.meshes = sceneData.meshes.Count(); |
| 256 | 363 | | currentCountValue.entities = sceneData.owners.Count(); |
| 256 | 364 | | currentCountValue.bodies = sceneData.renderers.Count(); |
| 256 | 365 | | currentCountValue.triangles = sceneData.triangles.Get() / 3; |
| | 366 | | } |
| | 367 | | } |
| | 368 | |
|
| 256 | 369 | | logger.Verbose($"Current metrics: {currentCountValue}"); |
| 256 | 370 | | RaiseMetricsUpdate(); |
| 256 | 371 | | } |
| | 372 | |
|
| | 373 | | private void UpdateWorstMetricsOffense() |
| | 374 | | { |
| 256 | 375 | | DataStore_SceneMetrics metricsData = DataStore.i.Get<DataStore_SceneMetrics>(); |
| | 376 | |
|
| 256 | 377 | | if ( maxCountValue != null && metricsData.worstMetricOffenseComputeEnabled.Get() ) |
| | 378 | | { |
| 256 | 379 | | bool isOffending = currentCountValue > maxCountValue; |
| | 380 | |
|
| 256 | 381 | | if ( !isOffending ) |
| 255 | 382 | | return; |
| | 383 | |
|
| 1 | 384 | | bool firstOffense = false; |
| | 385 | |
|
| 1 | 386 | | if (!metricsData.worstMetricOffenses.ContainsKey(sceneNumber)) |
| | 387 | | { |
| 1 | 388 | | firstOffense = true; |
| 1 | 389 | | metricsData.worstMetricOffenses[sceneNumber] = currentCountValue.Clone(); |
| | 390 | | } |
| | 391 | |
|
| 1 | 392 | | SceneMetricsModel worstOffense = metricsData.worstMetricOffenses[sceneNumber]; |
| 1 | 393 | | SceneMetricsModel currentOffense = maxCountValue - currentCountValue; |
| | 394 | |
|
| 1 | 395 | | if ( firstOffense ) |
| 1 | 396 | | logger.Verbose($"New offending scene with scene number {sceneNumber} ({scenePosition})!\n{currentCou |
| | 397 | |
|
| 1 | 398 | | if ( currentOffense < worstOffense ) |
| 1 | 399 | | return; |
| | 400 | |
|
| 0 | 401 | | metricsData.worstMetricOffenses[sceneNumber] = currentOffense; |
| 0 | 402 | | logger.Verbose($"New offending scene with scene number {sceneNumber} {scenePosition}!\nmetrics: {current |
| | 403 | | } |
| 0 | 404 | | } |
| | 405 | |
|
| | 406 | | private void RaiseMetricsUpdate() |
| | 407 | | { |
| 256 | 408 | | UpdateWorstMetricsOffense(); |
| 256 | 409 | | if (!currentCountValue.Equals(lastCountValue)) |
| | 410 | | { |
| 150 | 411 | | lastCountValue = currentCountValue; |
| 150 | 412 | | OnMetricsUpdated?.Invoke(this); |
| | 413 | | } |
| 106 | 414 | | } |
| | 415 | | } |
| | 416 | | } |