OVRProfiler.cs 5.1 KB

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