AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GpuTiming.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
7 using UnityEngine;
8 
9 namespace HoloToolkit.Unity
10 {
14  public static class GpuTiming
15  {
16  [DllImport("GpuTiming")]
17  private static extern IntPtr GetRenderEventFunc();
18 
19  [DllImport("GpuTiming")]
20  private static extern double GetLastFrameGPUTime(int eventId);
21 
22  private const int BaseBeginEventId = 1000;
23  private const int BaseEndEventId = 2000;
24 
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)
34  {
35  int eventValue;
36  if (eventIds.TryGetValue(eventId, out eventValue))
37  {
38  return GetLastFrameGPUTime(eventValue);
39  }
40 
41  return -1;
42  }
48  public static bool BeginSample(string eventId)
49  {
50  int eventValue;
51  if (!eventIds.TryGetValue(eventId, out eventValue))
52  {
53  if (nextAvailableEventId == BaseEndEventId)
54  {
55  return false;
56  }
57 
58  eventValue = nextAvailableEventId;
59  eventIds.Add(eventId, nextAvailableEventId++);
60  }
61 
62  if (currentEventId.Contains(eventValue))
63  {
64  Debug.LogWarning("BeginSample() is being called without a corresponding EndSample() call.");
65  return false;
66  }
67 
68  currentEventId.Push(eventValue);
69 
70  // Begin measuring GPU time
71  int eventFunctionId = eventValue + BaseBeginEventId;
72  GL.IssuePluginEvent(GetRenderEventFunc(), eventFunctionId);
73  return true;
74  }
78  public static void EndSample()
79  {
80  if (currentEventId.Count > 0)
81  {
82  // End measuring GPU frame time
83  int eventId = currentEventId.Pop() + BaseEndEventId;
84  GL.IssuePluginEvent(GetRenderEventFunc(), eventId);
85  }
86  }
87  }
88 }
static void EndSample()
Ends the GPU sample currently in flight.
Definition: GpuTiming.cs:78
static bool BeginSample(string eventId)
Begins sampling GPU time.
Definition: GpuTiming.cs:48
Encapsulates access to GPU timing methods.
Definition: GpuTiming.cs:14
static double GetTime(string eventId)
Gets the latest available sample time for the given event.
Definition: GpuTiming.cs:33