5 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
16 [DllImport(
"GpuTiming")]
17 private static extern IntPtr GetRenderEventFunc();
19 [DllImport(
"GpuTiming")]
20 private static extern double GetLastFrameGPUTime(
int eventId);
22 private const int BaseBeginEventId = 1000;
23 private const int BaseEndEventId = 2000;
25 private static int nextAvailableEventId = 0;
26 private static Stack<int> currentEventId =
new Stack<int>();
27 private static Dictionary<string, int> eventIds =
new Dictionary<string, int>();
33 public static double GetTime(
string eventId)
36 if (eventIds.TryGetValue(eventId, out eventValue))
38 return GetLastFrameGPUTime(eventValue);
51 if (!eventIds.TryGetValue(eventId, out eventValue))
53 if (nextAvailableEventId == BaseEndEventId)
58 eventValue = nextAvailableEventId;
59 eventIds.Add(eventId, nextAvailableEventId++);
62 if (currentEventId.Contains(eventValue))
64 Debug.LogWarning(
"BeginSample() is being called without a corresponding EndSample() call.");
68 currentEventId.Push(eventValue);
71 int eventFunctionId = eventValue + BaseBeginEventId;
72 GL.IssuePluginEvent(GetRenderEventFunc(), eventFunctionId);
80 if (currentEventId.Count > 0)
83 int eventId = currentEventId.Pop() + BaseEndEventId;
84 GL.IssuePluginEvent(GetRenderEventFunc(), eventId);