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