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