| | 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 | |
|
| 514 | 35 | | private SceneMetricsModel maxCountValue = new SceneMetricsModel(); |
| 514 | 36 | | private SceneMetricsModel currentCountValue = new SceneMetricsModel(); |
| | 37 | |
|
| | 38 | | public SceneMetricsModel currentCount |
| | 39 | | { |
| | 40 | | get |
| | 41 | | { |
| 96 | 42 | | UpdateMetrics(); |
| 96 | 43 | | return currentCountValue.Clone(); |
| | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| 739 | 47 | | public SceneMetricsModel maxCount => maxCountValue.Clone(); |
| | 48 | |
|
| 0 | 49 | | public bool dirty { get; private set; } |
| | 50 | |
|
| | 51 | | private string sceneId; |
| | 52 | |
|
| | 53 | | private Vector2Int scenePosition; |
| | 54 | |
|
| | 55 | | private int sceneParcelCount; |
| | 56 | |
|
| | 57 | | private DataStore_WorldObjects data; |
| | 58 | | private bool enabled = false; |
| | 59 | |
|
| 10 | 60 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore, string sceneId, Vector2Int scenePosition, int scene |
| | 61 | | { |
| 10 | 62 | | this.data = dataStore; |
| 10 | 63 | | Configure(sceneId, scenePosition, sceneParcelCount); |
| 10 | 64 | | } |
| | 65 | |
|
| 504 | 66 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore) |
| | 67 | | { |
| 504 | 68 | | this.data = dataStore; |
| 504 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Configure(string sceneId, Vector2Int scenePosition, int sceneParcelCount) |
| | 72 | | { |
| 511 | 73 | | this.sceneId = sceneId; |
| 511 | 74 | | this.scenePosition = scenePosition; |
| 511 | 75 | | this.sceneParcelCount = sceneParcelCount; |
| | 76 | |
|
| 511 | 77 | | Assert.IsTrue( !string.IsNullOrEmpty(sceneId), "Scene must have an ID!" ); |
| 511 | 78 | | maxCountValue = ComputeMaxCount(); |
| 511 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Dispose() |
| | 82 | | { |
| 504 | 83 | | } |
| | 84 | |
|
| | 85 | |
|
| | 86 | | public void Enable() |
| | 87 | | { |
| 511 | 88 | | if ( enabled ) |
| 0 | 89 | | return; |
| | 90 | |
|
| 511 | 91 | | var sceneData = data.sceneData[sceneId]; |
| | 92 | |
|
| 511 | 93 | | sceneData.materials.OnAdded += OnDataChanged; |
| 511 | 94 | | sceneData.materials.OnRemoved += OnDataChanged; |
| | 95 | |
|
| 511 | 96 | | sceneData.textures.OnAdded += OnTextureAdded; |
| 511 | 97 | | sceneData.textures.OnRemoved += OnTextureRemoved; |
| | 98 | |
|
| 511 | 99 | | sceneData.meshes.OnAdded += OnDataChanged; |
| 511 | 100 | | sceneData.meshes.OnRemoved += OnDataChanged; |
| | 101 | |
|
| 511 | 102 | | sceneData.animationClipSize.OnChange += OnAnimationClipSizeChange; |
| 511 | 103 | | sceneData.meshDataSize.OnChange += OnMeshDataSizeChange; |
| | 104 | |
|
| 511 | 105 | | sceneData.audioClips.OnAdded += OnAudioClipAdded; |
| 511 | 106 | | sceneData.audioClips.OnRemoved += OnAudioClipRemoved; |
| | 107 | |
|
| 511 | 108 | | sceneData.renderers.OnAdded += OnDataChanged; |
| 511 | 109 | | sceneData.renderers.OnRemoved += OnDataChanged; |
| | 110 | |
|
| 511 | 111 | | sceneData.owners.OnAdded += OnDataChanged; |
| 511 | 112 | | sceneData.owners.OnRemoved += OnDataChanged; |
| | 113 | |
|
| 511 | 114 | | sceneData.triangles.OnChange += OnDataChanged; |
| | 115 | |
|
| 511 | 116 | | enabled = true; |
| 511 | 117 | | } |
| | 118 | |
|
| | 119 | | public void Disable() |
| | 120 | | { |
| 504 | 121 | | if ( !enabled ) |
| 3 | 122 | | return; |
| | 123 | |
|
| 501 | 124 | | var sceneData = data.sceneData[sceneId]; |
| | 125 | |
|
| 501 | 126 | | sceneData.materials.OnAdded -= OnDataChanged; |
| 501 | 127 | | sceneData.materials.OnRemoved -= OnDataChanged; |
| | 128 | |
|
| 501 | 129 | | sceneData.textures.OnAdded -= OnTextureAdded; |
| 501 | 130 | | sceneData.textures.OnRemoved -= OnTextureRemoved; |
| | 131 | |
|
| 501 | 132 | | sceneData.meshes.OnAdded -= OnDataChanged; |
| 501 | 133 | | sceneData.meshes.OnRemoved -= OnDataChanged; |
| | 134 | |
|
| 501 | 135 | | sceneData.animationClipSize.OnChange -= OnAnimationClipSizeChange; |
| 501 | 136 | | sceneData.meshDataSize.OnChange -= OnMeshDataSizeChange; |
| | 137 | |
|
| 501 | 138 | | sceneData.audioClips.OnAdded -= OnAudioClipAdded; |
| 501 | 139 | | sceneData.audioClips.OnRemoved -= OnAudioClipRemoved; |
| | 140 | |
|
| 501 | 141 | | sceneData.renderers.OnAdded -= OnDataChanged; |
| 501 | 142 | | sceneData.renderers.OnRemoved -= OnDataChanged; |
| | 143 | |
|
| 501 | 144 | | sceneData.owners.OnAdded -= OnDataChanged; |
| 501 | 145 | | sceneData.owners.OnRemoved -= OnDataChanged; |
| | 146 | |
|
| 501 | 147 | | sceneData.triangles.OnChange -= OnDataChanged; |
| | 148 | |
|
| 501 | 149 | | enabled = false; |
| 501 | 150 | | } |
| | 151 | |
|
| | 152 | | private SceneMetricsModel ComputeMaxCount() |
| | 153 | | { |
| 516 | 154 | | var result = new SceneMetricsModel(); |
| | 155 | |
|
| 516 | 156 | | float log = Mathf.Log(sceneParcelCount + 1, 2); |
| 516 | 157 | | float lineal = sceneParcelCount; |
| | 158 | |
|
| 516 | 159 | | result.triangles = (int) (lineal * LimitsConfig.triangles); |
| 516 | 160 | | result.bodies = (int) (lineal * LimitsConfig.bodies); |
| 516 | 161 | | result.entities = (int) (lineal * LimitsConfig.entities); |
| 516 | 162 | | result.materials = (int) (log * LimitsConfig.materials); |
| 516 | 163 | | result.textures = (int) (log * LimitsConfig.textures); |
| 516 | 164 | | result.meshes = (int) (log * LimitsConfig.meshes); |
| 516 | 165 | | result.sceneHeight = (int) (log * LimitsConfig.height); |
| | 166 | |
|
| 516 | 167 | | return result; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | public bool IsInsideTheLimits() |
| | 171 | | { |
| 5 | 172 | | UpdateMetrics(); |
| 5 | 173 | | SceneMetricsModel limits = ComputeMaxCount(); |
| 5 | 174 | | SceneMetricsModel usage = currentCountValue; |
| | 175 | |
|
| 5 | 176 | | if (usage.triangles > limits.triangles) |
| 0 | 177 | | return false; |
| | 178 | |
|
| 5 | 179 | | if (usage.bodies > limits.bodies) |
| 0 | 180 | | return false; |
| | 181 | |
|
| 5 | 182 | | if (usage.entities > limits.entities) |
| 1 | 183 | | return false; |
| | 184 | |
|
| 4 | 185 | | if (usage.materials > limits.materials) |
| 0 | 186 | | return false; |
| | 187 | |
|
| 4 | 188 | | if (usage.textures > limits.textures) |
| 0 | 189 | | return false; |
| | 190 | |
|
| 4 | 191 | | if (usage.meshes > limits.meshes) |
| 0 | 192 | | return false; |
| | 193 | |
|
| 4 | 194 | | return true; |
| | 195 | | } |
| | 196 | |
|
| | 197 | | private void MarkDirty() |
| | 198 | | { |
| 0 | 199 | | dirty = true; |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | public void SendEvent() |
| | 203 | | { |
| 678 | 204 | | if (!dirty) |
| 482 | 205 | | return; |
| | 206 | |
|
| 196 | 207 | | dirty = false; |
| | 208 | |
|
| 196 | 209 | | UpdateMetrics(); |
| | 210 | |
|
| 196 | 211 | | Interface.WebInterface.ReportOnMetricsUpdate(sceneId, currentCountValue.ToMetricsModel(), maxCount.ToMetrics |
| 196 | 212 | | } |
| | 213 | |
|
| | 214 | | void OnAudioClipAdded(AudioClip audioClip) |
| | 215 | | { |
| 17 | 216 | | MarkDirty(); |
| 17 | 217 | | currentCountValue.audioClipMemory += MetricsScoreUtils.ComputeAudioClipScore(audioClip); |
| 17 | 218 | | } |
| | 219 | |
|
| | 220 | | void OnAudioClipRemoved(AudioClip audioClip) |
| | 221 | | { |
| 3 | 222 | | MarkDirty(); |
| 3 | 223 | | currentCountValue.audioClipMemory -= MetricsScoreUtils.ComputeAudioClipScore(audioClip); |
| 3 | 224 | | } |
| | 225 | |
|
| | 226 | |
|
| | 227 | | private void OnMeshDataSizeChange(long current, long previous) |
| | 228 | | { |
| 85 | 229 | | MarkDirty(); |
| 85 | 230 | | currentCountValue.meshMemory = current; |
| 85 | 231 | | } |
| | 232 | |
|
| | 233 | | void OnAnimationClipSizeChange(long animationClipSize, long previous) |
| | 234 | | { |
| 13 | 235 | | MarkDirty(); |
| 13 | 236 | | currentCountValue.animationClipMemory = animationClipSize; |
| 13 | 237 | | } |
| | 238 | |
|
| | 239 | | void OnTextureAdded(Texture texture) |
| | 240 | | { |
| 175 | 241 | | MarkDirty(); |
| | 242 | |
|
| 175 | 243 | | if (texture is Texture2D tex2D) |
| | 244 | | { |
| 175 | 245 | | currentCountValue.textureMemory += MetricsScoreUtils.ComputeTextureScore(tex2D); |
| | 246 | | } |
| 175 | 247 | | } |
| | 248 | |
|
| | 249 | | void OnTextureRemoved(Texture texture) |
| | 250 | | { |
| 45 | 251 | | MarkDirty(); |
| | 252 | |
|
| 45 | 253 | | if (texture is Texture2D tex2D) |
| | 254 | | { |
| 45 | 255 | | currentCountValue.textureMemory -= MetricsScoreUtils.ComputeTextureScore(tex2D); |
| | 256 | | } |
| 45 | 257 | | } |
| | 258 | |
|
| | 259 | | void OnDataChanged<T>(T obj) |
| | 260 | | where T : class |
| | 261 | | { |
| 2486 | 262 | | MarkDirty(); |
| 2486 | 263 | | } |
| | 264 | |
|
| | 265 | | void OnDataChanged(int obj1, int obj2) |
| | 266 | | { |
| 449 | 267 | | MarkDirty(); |
| 449 | 268 | | } |
| | 269 | |
|
| | 270 | | private void UpdateMetrics() |
| | 271 | | { |
| 297 | 272 | | if (string.IsNullOrEmpty(sceneId)) |
| 0 | 273 | | return; |
| | 274 | |
|
| 297 | 275 | | if (data != null && data.sceneData.ContainsKey(sceneId)) |
| | 276 | | { |
| 297 | 277 | | var sceneData = data.sceneData[sceneId]; |
| | 278 | |
|
| 297 | 279 | | if (sceneData != null) |
| | 280 | | { |
| 297 | 281 | | currentCountValue.materials = sceneData.materials.Count(); |
| 297 | 282 | | currentCountValue.textures = sceneData.textures.Count(); |
| 297 | 283 | | currentCountValue.meshes = sceneData.meshes.Count(); |
| 297 | 284 | | currentCountValue.entities = sceneData.owners.Count(); |
| 297 | 285 | | currentCountValue.bodies = sceneData.renderers.Count(); |
| 297 | 286 | | currentCountValue.triangles = sceneData.triangles.Get() / 3; |
| | 287 | | } |
| | 288 | | } |
| | 289 | |
|
| 297 | 290 | | logger.Verbose($"Current metrics: {currentCountValue}"); |
| 297 | 291 | | RaiseMetricsUpdate(); |
| 297 | 292 | | } |
| | 293 | |
|
| | 294 | | private void UpdateWorstMetricsOffense() |
| | 295 | | { |
| 297 | 296 | | DataStore_SceneMetrics metricsData = DataStore.i.Get<DataStore_SceneMetrics>(); |
| | 297 | |
|
| 297 | 298 | | if ( maxCountValue != null && metricsData.worstMetricOffenseComputeEnabled.Get() ) |
| | 299 | | { |
| 297 | 300 | | bool isOffending = currentCountValue > maxCountValue; |
| | 301 | |
|
| 297 | 302 | | if ( !isOffending ) |
| 296 | 303 | | return; |
| | 304 | |
|
| 1 | 305 | | bool firstOffense = false; |
| | 306 | |
|
| 1 | 307 | | if (!metricsData.worstMetricOffenses.ContainsKey(sceneId)) |
| | 308 | | { |
| 1 | 309 | | firstOffense = true; |
| 1 | 310 | | metricsData.worstMetricOffenses[sceneId] = currentCountValue.Clone(); |
| | 311 | | } |
| | 312 | |
|
| 1 | 313 | | SceneMetricsModel worstOffense = metricsData.worstMetricOffenses[sceneId]; |
| 1 | 314 | | SceneMetricsModel currentOffense = maxCountValue - currentCountValue; |
| | 315 | |
|
| 1 | 316 | | if ( firstOffense ) |
| 1 | 317 | | logger.Verbose($"New offending scene {sceneId} ({scenePosition})!\n{currentCountValue}"); |
| | 318 | |
|
| 1 | 319 | | if ( currentOffense < worstOffense ) |
| 1 | 320 | | return; |
| | 321 | |
|
| 0 | 322 | | metricsData.worstMetricOffenses[sceneId] = currentOffense; |
| 0 | 323 | | logger.Verbose($"New offending scene {sceneId} {scenePosition}!\nmetrics: {currentCountValue}\nlimits: { |
| | 324 | | } |
| 0 | 325 | | } |
| | 326 | |
|
| | 327 | | private void RaiseMetricsUpdate() |
| | 328 | | { |
| 297 | 329 | | UpdateWorstMetricsOffense(); |
| 297 | 330 | | OnMetricsUpdated?.Invoke(this); |
| 0 | 331 | | } |
| | 332 | | } |
| | 333 | | } |