| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | #if UNITY_EDITOR |
| 18 | |
| 19 | using UnityEngine; |
| 20 | using UnityEditor; |
| 21 | using System.Collections.Generic; |
| 22 | using Assets.OVR.Scripts; |
| 23 | |
| 24 | public class OVRProfiler : EditorWindow |
| 25 | { |
| 26 | enum TargetPlatform |
| 27 | { |
| 28 | OculusGo, |
| 29 | GearVR, |
| 30 | Quest, |
| 31 | OculusRift |
| 32 | }; |
| 33 | |
| 34 | private static List<RangedRecord> mRecords = new List<RangedRecord>(); |
| 35 | private Vector2 mScrollPosition; |
| 36 | static private TargetPlatform mTargetPlatform; |
| 37 | |
| 38 | [MenuItem("Oculus/Tools/OVR Profiler")] |
| 39 | static void Init() |
| 40 | { |
| 41 | // Get existing open window or if none, make a new one: |
| 42 | EditorWindow.GetWindow(typeof(OVRProfiler)); |
| 43 | #if UNITY_ANDROID |
| 44 | mTargetPlatform = TargetPlatform.OculusGo; |
| 45 | #else |
| 46 | mTargetPlatform = TargetPlatform.OculusRift; |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | void OnGUI() |
| 51 | { |
| 52 | GUILayout.Label("OVR Profiler", EditorStyles.boldLabel); |
| 53 | string[] options = new string[] |
| 54 | { |
| 55 | "Oculus Go", "Gear VR", "Oculus Quest", "Oculus Rift", |
| 56 | }; |
| 57 | mTargetPlatform = (TargetPlatform)EditorGUILayout.Popup("Target Oculus Platform", (int)mTargetPlatform, options); |
| 58 | |
| 59 | if (EditorApplication.isPlaying) |
| 60 | { |
| 61 | UpdateRecords(); |
| 62 | DrawResults(); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | ShowCenterAlignedMessageLabel("Click Run in Unity to view stats."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void OnInspectorUpdate() |
| 71 | { |
| 72 | Repaint(); |
| 73 | } |
| 74 | |
| 75 | void DrawResults() |
| 76 | { |
| 77 | string lastCategory = ""; |
| 78 | |
| 79 | mScrollPosition = EditorGUILayout.BeginScrollView(mScrollPosition); |
| 80 | |
| 81 | foreach (RangedRecord record in mRecords) |
| 82 | { |
| 83 | // Add separator and label for new category |
| 84 | if (!record.category.Equals(lastCategory)) |
| 85 | { |
| 86 | lastCategory = record.category; |
| 87 | EditorGUILayout.Separator(); |
| 88 | EditorGUILayout.BeginHorizontal(); |
| 89 | GUILayout.Label(lastCategory, EditorStyles.label, GUILayout.Width(200)); |
| 90 | EditorGUILayout.EndHorizontal(); |
| 91 | } |
| 92 | |
| 93 | // Draw records |
| 94 | EditorGUILayout.BeginHorizontal(); |
| 95 | Rect r = EditorGUILayout.BeginVertical(); |
| 96 | EditorGUI.ProgressBar(r, record.value / (record.max * 2), record.category + " " + record.value.ToString()); |
| 97 | GUILayout.Space(16); |
| 98 | EditorGUILayout.EndVertical(); |
| 99 | EditorGUILayout.EndHorizontal(); |
| 100 | |
| 101 | EditorGUILayout.BeginHorizontal(); |
| 102 | GUILayout.Label(record.message); |
| 103 | EditorGUILayout.EndHorizontal(); |
| 104 | |
| 105 | GUI.enabled = true; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | EditorGUILayout.EndScrollView(); |
| 110 | } |
| 111 | |
| 112 | private void UpdateRecords() |
| 113 | { |
| 114 | mRecords.Clear(); |
| 115 | |
| 116 | if (mTargetPlatform == TargetPlatform.OculusRift) |
| 117 | { |
| 118 | AddRecord("Client Frame CPU Time (ms)", "", UnityStats.frameTime * 1000, 0, 11); |
| 119 | AddRecord("Render Frame CPU Time (ms)", "", UnityStats.renderTime * 1000, 0, 11); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | // Graphics memory |
| 124 | long memSizeByte = UnityStats.usedTextureMemorySize + UnityStats.vboTotalBytes; |
| 125 | AddRecord("Graphics Memory (MB)", "Please use less than 1024 MB of vertex and texture memory.", ConvertBytes(memSizeByte, "MB"), 0, 1024); |
| 126 | } |
| 127 | |
| 128 | float triVertRec = mTargetPlatform == TargetPlatform.OculusRift ? 1000000 : 100000; |
| 129 | // Triangle count |
| 130 | AddRecord("Triangles", "Please use less than 100000 triangles.", UnityStats.triangles, 0, triVertRec); |
| 131 | |
| 132 | // Vertices count |
| 133 | AddRecord("Vertices", "Please use less than 100000 vertices.", UnityStats.vertices, 0, triVertRec); |
| 134 | |
| 135 | float dcRec = mTargetPlatform == TargetPlatform.OculusRift ? 1000 : 100; |
| 136 | // Draw call count |
| 137 | AddRecord("Draw Call", "Please use less than 100 draw calls.", UnityStats.drawCalls, 0, dcRec); |
| 138 | } |
| 139 | |
| 140 | private string FormatBytes(long bytes, string target) |
| 141 | { |
| 142 | return System.String.Format("{0:0.##} {1}", ConvertBytes(bytes, target), target); |
| 143 | } |
| 144 | |
| 145 | private float ConvertBytes(long bytes, string target) |
| 146 | { |
| 147 | string[] Suffix = { "B", "KB", "MB", "GB", "TB" }; |
| 148 | int i; |
| 149 | double dblSByte = bytes; |
| 150 | for (i = 0; i < Suffix.Length; i++, bytes /= 1024) |
| 151 | { |
| 152 | if (Suffix[i] == target) |
| 153 | return (float)dblSByte; |
| 154 | dblSByte = bytes / 1024.0; |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | private void ShowCenterAlignedMessageLabel(string message) |
| 160 | { |
| 161 | GUILayout.BeginVertical(); |
| 162 | GUILayout.FlexibleSpace(); |
| 163 | GUILayout.BeginHorizontal(); |
| 164 | GUILayout.FlexibleSpace(); |
| 165 | GUILayout.Label(message, EditorStyles.boldLabel); |
| 166 | GUILayout.FlexibleSpace(); |
| 167 | GUILayout.EndHorizontal(); |
| 168 | GUILayout.FlexibleSpace(); |
| 169 | GUILayout.EndVertical(); |
| 170 | } |
| 171 | |
| 172 | private void AddRecord(string category, string message, float value, float min, float max) |
| 173 | { |
| 174 | RangedRecord record = new RangedRecord(category, message, value, min, max); |
| 175 | mRecords.Add(record); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | #endif |