summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/OGL/GPUTimer.h
blob: 2beefa839e5431d3a10a6cfeeafe04db15c9ab5a (plain)
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
// Copyright 2016 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "Common/GL/GLExtensions/GLExtensions.h"

#ifndef GL_TIME_ELAPSED
#define GL_TIME_ELAPSED 0x88BF
#endif

namespace OGL
{
/*
 * This class can be used to measure the time it takes for the GPU to perform a draw call
 * or compute dispatch. To use:
 *
 *   - Create an instance of GPUTimer before issuing the draw call.
 *     (this can be before or after any binding that needs to be done)
 *
 *   - (optionally) call Begin(). This is not needed for a single draw call.
 *
 *   - Issue the draw call or compute dispatch as normal.
 *
 *   - (optionally) call End(). This is not necessary for a single draw call.
 *
 *   - Call GetTime{Seconds,Milliseconds,Nanoseconds} to determine how long the operation
 *     took to execute on the GPU.
 *
 * NOTE: When the timer is read back, this will force a GL flush, so the more often a timer is used,
 * the larger of a performance impact it will have. Only one timer can be active at any time, due to
 * using GL_TIME_ELAPSED. This is not enforced by the class, however.
 *
 */
class GPUTimer final
{
public:
  GPUTimer()
  {
    glGenQueries(1, &m_query_id);
    Begin();
  }

  ~GPUTimer()
  {
    End();
    glDeleteQueries(1, &m_query_id);
  }

  void Begin()
  {
    if (m_started)
      glEndQuery(GL_TIME_ELAPSED);

    glBeginQuery(GL_TIME_ELAPSED, m_query_id);
    m_started = true;
  }

  void End()
  {
    if (!m_started)
      return;

    glEndQuery(GL_TIME_ELAPSED);
    m_started = false;
  }

  double GetTimeSeconds()
  {
    GetResult();
    return static_cast<double>(m_result) / 1000000000.0;
  }

  double GetTimeMilliseconds()
  {
    GetResult();
    return static_cast<double>(m_result) / 1000000.0;
  }

  u32 GetTimeNanoseconds()
  {
    GetResult();
    return m_result;
  }

private:
  void GetResult()
  {
    if (m_has_result)
      return;

    if (m_started)
      End();

    glGetQueryObjectuiv(m_query_id, GL_QUERY_RESULT, &m_result);
    m_has_result = true;
  }

  GLuint m_query_id;
  GLuint m_result = 0;
  bool m_started = false;
  bool m_has_result = false;
};
}  // namespace OGL