Skip to content

Commit 5ae3764

Browse files
committed
Changed api of multiple datasets to use arrays of arrays
1 parent f4fe399 commit 5ae3764

File tree

2 files changed

+69
-44
lines changed

2 files changed

+69
-44
lines changed

Assets/DebugOverlay/DebugOverlay.cs

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -164,37 +164,41 @@ public static void Write<T0, T1, T2, T3>(float x, float y, string format, T0 arg
164164
instance._Write(x, y, format, new ArgList4<T0, T1, T2, T3>(arg0, arg1, arg2, arg3));
165165
}
166166

167-
// Draw a stacked histogram from numSets of data. Data must contain numSets of interleaved, non-negative datapoints.
168-
public static void DrawHist(float x, float y, float w, float h, float[] data, int startSample, Color[] color, int numSets, float maxRange = -1.0f)
167+
// Draw a histogram of one set of data. Data must contain non-negative datapoints.
168+
public static void DrawHist(float x, float y, float w, float h, float[] data, int startSample, Color color, float maxRange = -1.0f)
169169
{
170170
if (instance == null)
171171
return;
172-
instance._DrawHist(x, y, w, h, data, startSample, color, numSets, maxRange);
172+
s_TempData[0] = data;
173+
s_TempColors[0] = color;
174+
instance._DrawHist(x, y, w, h, s_TempData, startSample, s_TempColors, maxRange);
175+
s_TempData[0] = null;
173176
}
177+
static float[][] s_TempData = new float[1][];
178+
static Color[] s_TempColors = new Color[1];
174179

175-
static Color[] m_Colors = new Color[1];
176-
public static void DrawHist(float x, float y, float w, float h, float[] data, int startSample, Color color, float maxRange = -1.0f)
180+
// Draw a stacked histogram multiple sets of data. Data must contain non-negative datapoints.
181+
public static void DrawHist(float x, float y, float w, float h, float[][] data, int startSample, Color[] color, float maxRange = -1.0f)
177182
{
178183
if (instance == null)
179184
return;
180-
m_Colors[0] = color;
181-
instance._DrawHist(x, y, w, h, data, startSample, m_Colors, 1, maxRange);
185+
instance._DrawHist(x, y, w, h, data, startSample, color, maxRange);
182186
}
183187

184-
public static void DrawGraph(float x, float y, float w, float h, float[] data, int startSample, Color[] color, int numSets, float maxRange = -1.0f)
188+
public static void DrawGraph(float x, float y, float w, float h, float[] data, int startSample, Color color, float maxRange = -1.0f)
185189
{
186190
if (instance == null)
187191
return;
188-
instance._DrawGraph(x, y, w, h, data, startSample, color, numSets, maxRange);
192+
s_TempData[0] = data;
193+
s_TempColors[0] = color;
194+
instance._DrawGraph(x, y, w, h, s_TempData, startSample, s_TempColors, maxRange);
189195
}
190196

191-
192-
public static void DrawGraph(float x, float y, float w, float h, float[] data, int startSample, Color color, float maxRange = -1.0f)
197+
public static void DrawGraph(float x, float y, float w, float h, float[][] data, int startSample, Color[] color, float maxRange = -1.0f)
193198
{
194199
if (instance == null)
195200
return;
196-
m_Colors[0] = color;
197-
instance._DrawGraph(x, y, w, h, data, startSample, m_Colors, 1, maxRange);
201+
instance._DrawGraph(x, y, w, h, data, startSample, color, maxRange);
198202
}
199203

200204
public static void DrawRect(float x, float y, float w, float h, Color col)
@@ -234,22 +238,32 @@ void _DrawText(float x, float y, ref char[] text, int length)
234238
}
235239
}
236240

237-
void _DrawGraph(float x, float y, float w, float h, float[] data, int startSample, Color[] color, int numSets, float maxRange = -1.0f)
241+
void _DrawGraph(float x, float y, float w, float h, float[][] data, int startSample, Color[] color, float maxRange = -1.0f)
238242
{
239-
if (data.Length % numSets != 0)
240-
throw new System.ArgumentException("Length of data must be a multiple of numSets");
241-
if (color.Length != numSets)
242-
throw new System.ArgumentException("Length of colors must be numSets");
243+
if(data == null || data.Length == 0 || data[0] == null)
244+
throw new System.ArgumentException("Invalid data argument (data must contain at least one non null array");
243245

244-
var dataLength = data.Length;
245-
var numSamples = dataLength / numSets;
246+
var numSamples = data[0].Length;
247+
/*
248+
for(int i = 1; i < data.Length; ++i)
249+
{
250+
if(data[i] == null || data[i].Length != numSamples)
251+
throw new System.ArgumentException("Length of data of all arrays must be the same");
252+
}
253+
*/
254+
255+
if (color.Length != data.Length)
256+
throw new System.ArgumentException("Length of colors must match number of datasets");
246257

247258
float maxData = float.MinValue;
248259

249-
for (var i = 0; i < dataLength; i++)
260+
foreach (var dataset in data)
250261
{
251-
if (data[i] > maxData)
252-
maxData = data[i];
262+
for (var i = 0; i < numSamples; i++)
263+
{
264+
if (dataset[i] > maxData)
265+
maxData = dataset[i];
266+
}
253267
}
254268

255269
if (maxData > maxRange)
@@ -258,14 +272,14 @@ void _DrawGraph(float x, float y, float w, float h, float[] data, int startSampl
258272
float dx = w / numSamples;
259273
float scale = maxRange > 0 ? h / maxRange : 1.0f;
260274

261-
for (var j = 0; j < numSets; j++)
275+
for (var j = 0; j < data.Length; j++)
262276
{
263277
float old_pos_x = 0;
264278
float old_pos_y = 0;
265279
Vector4 col = color[j];
266280
for (var i = 0; i < numSamples; i++)
267281
{
268-
float d = data[((i + startSample) % numSamples) * numSets + j];
282+
float d = data[j][(i + startSample) % numSamples];
269283
var pos_x = m_OriginX + x + dx * i;
270284
var pos_y = m_OriginY + y + h - d * scale;
271285
if (i > 0)
@@ -274,19 +288,29 @@ void _DrawGraph(float x, float y, float w, float h, float[] data, int startSampl
274288
old_pos_y = pos_y;
275289
}
276290
}
291+
277292
AddLine(x, y + h, x + w, y + h, color[0]);
278293
AddLine(x, y, x, y + h, color[0]);
279294
}
280295

281-
void _DrawHist(float x, float y, float w, float h, float[] data, int startSample, Color[] color, int numSets, float maxRange = -1.0f)
296+
void _DrawHist(float x, float y, float w, float h, float[][] data, int startSample, Color[] color, float maxRange = -1.0f)
282297
{
283-
if (data.Length % numSets != 0)
284-
throw new System.ArgumentException("Length of data must be a multiple of numSets");
285-
if (color.Length != numSets)
286-
throw new System.ArgumentException("Length of colors must be numSets");
298+
if (data == null || data.Length == 0 || data[0] == null)
299+
throw new System.ArgumentException("Invalid data argument (data must contain at least one non null array");
300+
301+
var numSamples = data[0].Length;
302+
/*
303+
for (int i = 1; i < data.Length; ++i)
304+
{
305+
if (data[i] == null || data[i].Length != numSamples)
306+
throw new System.ArgumentException("Length of data of all arrays must be the same");
307+
}
308+
*/
309+
310+
if (color.Length != data.Length)
311+
throw new System.ArgumentException("Length of colors must match number of datasets");
287312

288313
var dataLength = data.Length;
289-
var numSamples = dataLength / numSets;
290314

291315
float maxData = float.MinValue;
292316

@@ -295,8 +319,8 @@ void _DrawHist(float x, float y, float w, float h, float[] data, int startSample
295319
{
296320
float sum = 0;
297321

298-
for (var j = 0; j < numSets; j++)
299-
sum += data[i * numSets + j];
322+
foreach(var dataset in data)
323+
sum += dataset[i];
300324

301325
if (sum > maxData)
302326
maxData = sum;
@@ -312,10 +336,10 @@ void _DrawHist(float x, float y, float w, float h, float[] data, int startSample
312336
for (var i = 0; i < numSamples; i++)
313337
{
314338
stackOffset = 0;
315-
for (var j = 0; j < numSets; j++)
339+
for (var j = 0; j < data.Length; j++)
316340
{
317341
var c = color[j];
318-
float d = data[((i + startSample) % numSamples) * numSets + j];
342+
float d = data[j][(i + startSample) % numSamples];
319343
float barHeight = d * scale; // now in [0, h]
320344
var pos_x = m_OriginX + x + dx * i;
321345
var pos_y = m_OriginY + y + h - barHeight - stackOffset;

Assets/Game/Stats.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class Stats : IGameSystem
66
{
77

8-
float[] fpsArray = new float[200];
8+
float[][] fpsArray = new float[2][] { new float[100], new float[100] };
99
float[] frameTimeArray = new float[100];
1010

1111
System.Diagnostics.Stopwatch m_StopWatch;
@@ -29,11 +29,12 @@ private void CmdShowstats(string[] args)
2929
m_ShowStats = (m_ShowStats + 1) % 3;
3030
}
3131

32-
void CalcStatistics(float[] data, int count, out float mean, out float variance, out float minValue, out float maxValue)
32+
void CalcStatistics(float[] data, out float mean, out float variance, out float minValue, out float maxValue)
3333
{
3434
float sum = 0, sum2 = 0;
3535
minValue = float.MaxValue;
3636
maxValue = float.MinValue;
37+
int count = data.Length;
3738
for (var i = 0; i < count; i++)
3839
{
3940
var x = data[i];
@@ -78,23 +79,23 @@ public void TickUpdate()
7879

7980
/// Graphing difference between deltaTime and actual passed time
8081
float fps = Time.deltaTime * 1000.0f;
81-
var idx = (Time.frameCount * 2) % fpsArray.Length; ;
82-
fpsArray[idx] = -Mathf.Min(0, frameDurationMs - fps);
83-
fpsArray[idx + 1] = Mathf.Max(0, frameDurationMs - fps);
82+
var idx = Time.frameCount % fpsArray[0].Length; ;
83+
fpsArray[0][idx] = -Mathf.Min(0, frameDurationMs - fps);
84+
fpsArray[1][idx] = Mathf.Max(0, frameDurationMs - fps);
8485
float variance, mean, min, max;
85-
CalcStatistics(fpsArray, fpsArray.Length, out mean, out variance, out min, out max);
86+
CalcStatistics(fpsArray[0], out mean, out variance, out min, out max);
8687

8788
// Draw histogram over time differences
88-
DebugOverlay.DrawHist(20, 10, 20, 3, fpsArray, Time.frameCount, colors, 2, max);
89+
DebugOverlay.DrawHist(20, 10, 20, 3, fpsArray, Time.frameCount, colors, max);
8990
DebugOverlay.SetColor(new Color(1.0f,0.3f,0.0f));
9091
DebugOverlay.Write(20, 14, "{0,4:#.###} ({1,4:##.#} +/- {2,4:#.##})", frameDurationMs - fps, mean, Mathf.Sqrt(variance));
9192

92-
DebugOverlay.DrawGraph(45, 10, 40, 3, fpsArray, Time.frameCount, colors, 2, max);
93+
DebugOverlay.DrawGraph(45, 10, 40, 3, fpsArray, Time.frameCount, colors, max);
9394

9495
/// Graphing frametime
9596
var idx2 = Time.frameCount % frameTimeArray.Length;
9697
frameTimeArray[idx2] = frameDurationMs;
97-
CalcStatistics(frameTimeArray, frameTimeArray.Length, out mean, out variance, out min, out max);
98+
CalcStatistics(frameTimeArray, out mean, out variance, out min, out max);
9899
DebugOverlay.DrawHist(20, 15, 20, 3, frameTimeArray, Time.frameCount, Color.red, max);
99100

100101
// Draw legend

0 commit comments

Comments
 (0)