| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | [RequireComponent(typeof(StatsPanel))] |
| | 12 | | public class UserBenchmarkController : MonoBehaviour, IBenchmarkController |
| | 13 | | { |
| | 14 | | public enum Columns |
| | 15 | | { |
| | 16 | | LABEL, |
| | 17 | | VALUE, |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public enum Rows |
| | 21 | | { |
| | 22 | | NONE, |
| | 23 | | PROCESSED_MESSAGES, |
| | 24 | | PENDING_MESSAGES, |
| | 25 | | BREAK_0, |
| | 26 | | CURRENT_SCENE, |
| | 27 | | POLYGONS_VS_LIMIT, |
| | 28 | | TEXTURES_VS_LIMIT, |
| | 29 | | MATERIALS_VS_LIMIT, |
| | 30 | | ENTITIES_VS_LIMIT, |
| | 31 | | MESHES_VS_LIMIT, |
| | 32 | | BODIES_VS_LIMIT, |
| | 33 | | COMPONENT_OBJECTS_COUNT |
| | 34 | | } |
| | 35 | |
|
| | 36 | | const string BREAK_0_TEXT = ""; |
| | 37 | |
|
| | 38 | | const string PROCESSED_MESSAGES_TEXT = "Processed Messages"; |
| | 39 | | const string PENDING_MESSAGES_TEXT = "Pending on Queue"; |
| | 40 | |
|
| | 41 | | const string CURRENT_SCENE_TEST = "Scene Location:"; |
| | 42 | | const string POLYGON_VS_LIMIT_TEXT = "Poly Count"; |
| | 43 | | const string TEXTURES_VS_LIMIT_TEXT = "Textures Count"; |
| | 44 | | const string MATERIALS_VS_LIMIT_TEXT = "Materials Count"; |
| | 45 | | const string ENTITIES_VS_LIMIT_TEXT = "Entities Count"; |
| | 46 | | const string MESHES_VS_LIMIT_TEXT = "Meshes Count"; |
| | 47 | | const string BODIES_VS_LIMIT_TEXT = "Bodies Count"; |
| | 48 | | const string COMPONENT_OBJECTS_COUNT_TEXT = "Components Count"; |
| | 49 | |
|
| | 50 | | int totalMessagesCurrent; |
| | 51 | | int totalMessagesGlobal; |
| | 52 | |
|
| | 53 | | StatsPanel statsPanel; |
| | 54 | | List<Columns> columnsList; |
| | 55 | | List<Rows> rowsList; |
| | 56 | |
|
| 0 | 57 | | private void Awake() { StartProfiling(); } |
| | 58 | |
|
| 0 | 59 | | private void OnDestroy() { StopProfiling(); } |
| | 60 | |
|
| | 61 | | public void Init() |
| | 62 | | { |
| 0 | 63 | | this.statsPanel = GetComponent<StatsPanel>(); |
| | 64 | |
|
| 0 | 65 | | columnsList = Enum.GetValues(typeof(Columns)).Cast<Columns>().ToList(); |
| 0 | 66 | | rowsList = Enum.GetValues(typeof(Rows)).Cast<Rows>().ToList(); |
| | 67 | |
|
| 0 | 68 | | statsPanel.PopulateTable(columnsList.Count, rowsList.Count, 240, 240); |
| | 69 | |
|
| 0 | 70 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Columns.LABEL, ""); |
| | 71 | |
|
| | 72 | | //Init Labels |
| 0 | 73 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PROCESSED_MESSAGES, PROCESSED_MESSAGES_TEXT); |
| 0 | 74 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.PENDING_MESSAGES, PENDING_MESSAGES_TEXT); |
| | 75 | |
|
| 0 | 76 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BREAK_0, BREAK_0_TEXT); |
| | 77 | |
|
| 0 | 78 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.CURRENT_SCENE, CURRENT_SCENE_TEST); |
| 0 | 79 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.POLYGONS_VS_LIMIT, POLYGON_VS_LIMIT_TEXT); |
| 0 | 80 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.TEXTURES_VS_LIMIT, TEXTURES_VS_LIMIT_TEXT); |
| 0 | 81 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MATERIALS_VS_LIMIT, MATERIALS_VS_LIMIT_TEXT); |
| 0 | 82 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.ENTITIES_VS_LIMIT, ENTITIES_VS_LIMIT_TEXT); |
| 0 | 83 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.MESHES_VS_LIMIT, MESHES_VS_LIMIT_TEXT); |
| 0 | 84 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.BODIES_VS_LIMIT, BODIES_VS_LIMIT_TEXT); |
| 0 | 85 | | statsPanel.SetCellText((int) Columns.LABEL, (int) Rows.COMPONENT_OBJECTS_COUNT, COMPONENT_OBJECTS_COUNT_TEXT |
| | 86 | |
|
| | 87 | | //Init Values |
| | 88 | |
|
| 0 | 89 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, "=/="); |
| 0 | 90 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, "=/="); |
| | 91 | |
|
| 0 | 92 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, "=/="); |
| 0 | 93 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, "=/="); |
| 0 | 94 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, "=/="); |
| 0 | 95 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, "=/="); |
| 0 | 96 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, "=/="); |
| 0 | 97 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, "=/="); |
| 0 | 98 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, "=/="); |
| 0 | 99 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, "=/="); |
| | 100 | |
|
| 0 | 101 | | Canvas.ForceUpdateCanvases(); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void StartProfiling() |
| | 105 | | { |
| 0 | 106 | | if (statsPanel == null) |
| | 107 | | { |
| 0 | 108 | | Init(); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | profilingCoroutine = CoroutineStarter.Start(RefreshProfilingData()); |
| 0 | 112 | | ProfilingEvents.OnMessageWillQueue += OnMessageWillQueue; |
| 0 | 113 | | ProfilingEvents.OnMessageWillDequeue += OnMessageWillDequeue; |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | private void OnMessageWillDequeue(string obj) |
| | 117 | | { |
| 0 | 118 | | totalMessagesCurrent = Math.Min(totalMessagesCurrent + 1, totalMessagesGlobal); |
| 0 | 119 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, $"{totalMessagesCurrent} of {tota |
| 0 | 120 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, $"{totalMessagesGlobal - totalMessa |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void OnMessageWillQueue(string obj) |
| | 124 | | { |
| 0 | 125 | | totalMessagesGlobal++; |
| 0 | 126 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PROCESSED_MESSAGES, $"{totalMessagesCurrent}:{totalMe |
| 0 | 127 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.PENDING_MESSAGES, $"{totalMessagesGlobal - totalMessa |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | private Coroutine profilingCoroutine; |
| | 131 | |
|
| | 132 | | public void StopProfiling() |
| | 133 | | { |
| 0 | 134 | | if (profilingCoroutine != null) |
| 0 | 135 | | CoroutineStarter.Stop(profilingCoroutine); |
| | 136 | |
|
| 0 | 137 | | ProfilingEvents.OnMessageWillQueue -= OnMessageWillQueue; |
| 0 | 138 | | ProfilingEvents.OnMessageWillDequeue -= OnMessageWillDequeue; |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private IEnumerator RefreshProfilingData() |
| | 142 | | { |
| 0 | 143 | | while (true) |
| | 144 | | { |
| 0 | 145 | | IParcelScene activeScene = GetActiveScene(); |
| | 146 | |
|
| 0 | 147 | | if (activeScene != null && activeScene.metricsCounter != null) |
| | 148 | | { |
| 0 | 149 | | var metrics = activeScene.metricsCounter.currentCount; |
| 0 | 150 | | var limits = activeScene.metricsCounter.maxCount; |
| 0 | 151 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.CURRENT_SCENE, $"{activeScene.sceneData.id}") |
| 0 | 152 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.POLYGONS_VS_LIMIT, $"{metrics.triangles} of { |
| 0 | 153 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.TEXTURES_VS_LIMIT, $"{metrics.textures} of {l |
| 0 | 154 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MATERIALS_VS_LIMIT, $"{metrics.materials} of |
| 0 | 155 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.ENTITIES_VS_LIMIT, $"{metrics.entities} of {l |
| 0 | 156 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.MESHES_VS_LIMIT, $"{metrics.meshes} of {limit |
| 0 | 157 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.BODIES_VS_LIMIT, $"{metrics.bodies} of {limit |
| 0 | 158 | | statsPanel.SetCellText((int) Columns.VALUE, (int) Rows.COMPONENT_OBJECTS_COUNT, activeScene.disposab |
| | 159 | | } |
| | 160 | |
|
| 0 | 161 | | yield return WaitForSecondsCache.Get(0.2f); |
| | 162 | | } |
| | 163 | | } |
| | 164 | |
|
| | 165 | | private IParcelScene GetActiveScene() |
| | 166 | | { |
| 0 | 167 | | IWorldState worldState = Environment.i.world.state; |
| 0 | 168 | | string debugSceneId = KernelConfig.i.Get().debugConfig.sceneDebugPanelTargetSceneId; |
| | 169 | |
|
| 0 | 170 | | if (!string.IsNullOrEmpty(debugSceneId)) |
| | 171 | | { |
| 0 | 172 | | if (worldState.loadedScenes.TryGetValue(debugSceneId, out IParcelScene scene)) |
| 0 | 173 | | return scene; |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | var currentPos = Utils.WorldToGridPosition(DCLCharacterController.i.characterPosition.worldPosition); |
| 0 | 177 | | return worldState.loadedScenes.Values.FirstOrDefault( |
| 0 | 178 | | x => x.sceneData.parcels != null |
| 0 | 179 | | && x.sceneData.parcels.Any(y => y == currentPos)); |
| | 180 | | } |
| | 181 | | } |
| | 182 | | } |