Line data Source code
1 : /* rendertimers.cpp: renderer functionality used for displaying rendering stats
2 : * while the program is running
3 : *
4 : * timers can be created with designated start/stop points in the code; sub-ms
5 : * times needed for accurate diagnosis possible (each frame is ~16.6ms @ 60Hz)
6 : *
7 : * used in rendergl.cpp
8 : */
9 : #include "../libprimis-headers/cube.h"
10 : #include "../../shared/geomexts.h"
11 : #include "../../shared/glexts.h"
12 :
13 : #include "rendergl.h"
14 : #include "rendertext.h"
15 : #include "renderttf.h"
16 : #include "renderva.h"
17 :
18 : #include "interface/control.h"
19 :
20 : void cleanuptimers(); //needed for timer script gvar
21 : VAR(frametimer, 0, 0, 1); //toggles timing how long each frame takes (and rendering it to timer ui), used in hud
22 :
23 : //declared and used as pointer in header
24 : struct timer final
25 : {
26 : enum
27 : {
28 : Timer_MaxQuery = 4 //max number of gl queries
29 : };
30 : const char *name; //name the timer reports as
31 : bool gpu; //whether the timer is for gpu time (true) or cpu time
32 : std::array<GLuint, Timer_MaxQuery> query; //gpu query information
33 : int waiting; //internal bitmask for queries
34 : size_t starttime; //time the timer was started (in terms of ms since game started)
35 : float result, //raw value of the timer, -1 if no info available
36 : print; //the time the timer displays: ms per frame for whatever object
37 : };
38 :
39 : //locally relevant functionality
40 : namespace
41 : {
42 : std::vector<timer> timers;
43 : std::vector<size_t> timerorder;
44 : size_t timercycle = 0;
45 :
46 0 : VARFN(timer, usetimers, 0, 0, 1, cleanuptimers()); //toggles logging timer information & rendering it
47 :
48 0 : timer *findtimer(const char *name, bool gpu) //also creates a new timer if none found
49 : {
50 0 : for(size_t i = 0; i < timers.size(); i++)
51 : {
52 0 : if(!std::strcmp(timers[i].name, name) && timers[i].gpu == gpu)
53 : {
54 0 : timerorder.erase(std::find(timerorder.begin(), timerorder.end(), i));
55 0 : timerorder.push_back(i);
56 0 : return &timers[i];
57 : }
58 : }
59 0 : timerorder.push_back(timers.size());
60 0 : timers.emplace_back();
61 0 : timer &t = timers.back();
62 0 : t.name = name;
63 0 : t.gpu = gpu;
64 0 : t.query.fill(0);
65 0 : if(gpu)
66 : {
67 0 : glGenQueries(timer::Timer_MaxQuery, t.query.data());
68 : }
69 0 : t.waiting = 0;
70 0 : t.starttime = 0;
71 0 : t.result = -1;
72 0 : t.print = -1;
73 0 : return &t;
74 : }
75 : }
76 :
77 : //externally relevant functionality
78 :
79 : //used to start a timer in some part of the code, cannot be used outside of rendering part
80 :
81 0 : timer *begintimer(const char *name, bool gpu)
82 : {
83 0 : if(!usetimers || inbetweenframes || (gpu && (!hasTQ || deferquery)))
84 : {
85 0 : return nullptr;
86 : }
87 0 : timer *t = findtimer(name, gpu);
88 0 : if(t->gpu)
89 : {
90 0 : deferquery++;
91 0 : glBeginQuery(GL_TIME_ELAPSED_EXT, t->query[timercycle]);
92 0 : t->waiting |= 1<<timercycle;
93 : }
94 : else
95 : {
96 0 : t->starttime = getclockmillis();
97 : }
98 0 : return t;
99 : }
100 :
101 : //used to end a timer started by begintimer(), needs to be included sometime after begintimer
102 : //the part between begintimer() and endtimer() is what gets timed
103 0 : void endtimer(timer *t)
104 : {
105 0 : if(!t)
106 : {
107 0 : return;
108 : }
109 0 : if(t->gpu)
110 : {
111 0 : glEndQuery(GL_TIME_ELAPSED_EXT);
112 0 : deferquery--;
113 : }
114 : else
115 : {
116 0 : t->result = std::max(static_cast<float>(getclockmillis() - t->starttime), 0.0f);
117 : }
118 : }
119 :
120 : //foreach timer, query what time has passed since last update
121 0 : void synctimers()
122 : {
123 0 : timercycle = (timercycle + 1) % timer::Timer_MaxQuery;
124 :
125 0 : for(timer& t : timers)
126 : {
127 0 : if(t.waiting&(1<<timercycle))
128 : {
129 0 : GLint available = 0;
130 0 : while(!available)
131 : {
132 0 : glGetQueryObjectiv(t.query[timercycle], GL_QUERY_RESULT_AVAILABLE, &available);
133 : }
134 0 : GLuint64EXT result = 0;
135 0 : glGetQueryObjectui64v(t.query[timercycle], GL_QUERY_RESULT, &result);
136 0 : t.result = std::max(static_cast<float>(result) * 1e-6f, 0.0f);
137 0 : t.waiting &= ~(1<<timercycle);
138 : }
139 : else
140 : {
141 0 : t.result = -1;
142 : }
143 : }
144 0 : }
145 :
146 0 : void cleanuptimers()
147 : {
148 0 : for(const timer& t : timers)
149 : {
150 0 : if(t.gpu)
151 : {
152 0 : glDeleteQueries(timer::Timer_MaxQuery, t.query.data());
153 : }
154 : }
155 0 : timers.clear();
156 0 : timerorder.clear();
157 0 : }
158 :
159 0 : void printtimers(int conw, int framemillis)
160 : {
161 0 : if(!frametimer && !usetimers)
162 : {
163 0 : return;
164 : }
165 : static int lastprint = 0;
166 0 : int offset = 0;
167 0 : if(frametimer)
168 : {
169 : static int printmillis = 0;
170 0 : if(totalmillis - lastprint >= 200)
171 : {
172 0 : printmillis = framemillis;
173 : }
174 : std::array<char, 200> framestring;
175 0 : constexpr int size = 42;
176 0 : std::sprintf(framestring.data(), "frame time %i ms", printmillis);
177 0 : ttr.fontsize(size);
178 0 : ttr.renderttf(framestring.data(), {0xFF, 0xFF, 0xFF, 0}, conw-20*size, size*3/2+offset*9*size/8);
179 : //draw_textf("frame time %i ms", conw-20*FONTH, conh-FONTH*3/2-offset*9*FONTH/8, printmillis);
180 0 : offset++;
181 : }
182 0 : if(usetimers)
183 : {
184 0 : for(int i : timerorder)
185 : {
186 0 : timer &t = timers[i];
187 0 : if(t.print < 0 ? t.result >= 0 : totalmillis - lastprint >= 200)
188 : {
189 0 : t.print = t.result;
190 : }
191 0 : if(t.print < 0 || (t.gpu && !(t.waiting&(1<<timercycle))))
192 : {
193 0 : continue;
194 : }
195 : std::array<char, 200> framestring;
196 0 : constexpr int size = 42;
197 0 : std::sprintf(framestring.data(), "%s%s %5.2f ms", t.name, t.gpu ? "" : " (cpu)", t.print);
198 0 : ttr.fontsize(size);
199 0 : ttr.renderttf(framestring.data(), {0xFF, 0xFF, 0xFF, 0}, conw-20*size, size*3/2+offset*9*size/8);
200 :
201 : //draw_textf("%s%s %5.2f ms", conw-20*FONTH, conh-FONTH*3/2-offset*9*FONTH/8, t.name, t.gpu ? "" : " (cpu)", t.print);
202 0 : offset++;
203 : }
204 : }
205 0 : if(totalmillis - lastprint >= 200)
206 : {
207 0 : lastprint = totalmillis;
208 : }
209 : }
|