Debian Codegraph @

rtperformance/rtperformance.h

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef RUN_TIME_PROFILING_
20 #define RUN_TIME_PROFILING_
21
22 #include <cstdint>
23
24 // uncomment the following if you want to enable Run-time Profiling
25 #ifndef RELEASE
26 #define USE_RTP
27 #endif
28
29 #if defined(POSIX)
30 // #if defined(POSIX)
31 #ifdef USE_RTP
32 #undef USE_RTP // no rtp for now
33 #endif
34 #endif
35
36 // use INT64 for 64bit integers
37 #ifdef USE_RTP
38 #if defined(POSIX)
39 #define INT64 int64_t
40 #else
41 #define INT64 signed __int64
42 #endif
43 #else
44 #define INT64 uint8_t
45 #endif
46
47
48 // struct of information to be saved per frame (note: use INT64 for timer info)
49 // -----------------------------------------------------------------------------------
50 struct tRTFrameInfo {
51 INT64 frame_num; // the frame number, used internally
52 INT64 renderframe_time;
53 INT64 multiframe_time;
54 INT64 musicframe_time;
55 INT64 ambsound_frame_time;
56 INT64 weatherframe_time;
57 INT64 playerframe_time;
58 INT64 doorframe_time;
59 INT64 levelgoal_time;
60 INT64 matcenframe_time;
61 INT64 objframe_time;
62 INT64 aiframeall_time;
63 INT64 processkeys_time;
64 INT64 normalevent_time;
65 INT64 ct_flying_time;
66 INT64 ct_aidoframe_time;
67 INT64 ct_weaponframe_time;
68 INT64 ct_explosionframe_time;
69 INT64 ct_debrisframe_time;
70 INT64 ct_splinterframe_time;
71 INT64 mt_physicsframe_time;
72 INT64 mt_walkingframe_time;
73 INT64 mt_shockwave_time;
74 INT64 obj_doeffect_time;
75 INT64 obj_move_player_time;
76 INT64 obj_d3xint_time;
77 INT64 obj_objlight_time;
78 INT64 cycle_anim;
79 INT64 vis_eff_move;
80 INT64 phys_link;
81 INT64 obj_do_frm;
82 INT64 fvi_time;
83
84 int texture_uploads;
85 int polys_drawn;
86 int fvi_calls;
87 float frame_time; // how long the frame took. A float because it's already calc'd so we might as well save it
88 };
89
90 // Flags for Runtime Performance Counters (these are 64 bit)
91 // --------------------------------------------------------------------
92 #define RTI_FRAMETIME 0x00000001
93 #define RTI_RENDERFRAMETIME 0x00000002
94 #define RTI_MULTIFRAMETIME 0x00000004
95 #define RTI_MUSICFRAMETIME 0x00000008
96 #define RTI_AMBSOUNDFRAMETIME 0x00000010
97 #define RTI_WEATHERFRAMETIME 0x00000020
98 #define RTI_PLAYERFRAMETIME 0x00000040
99 #define RTI_DOORFRAMETIME 0x00000080
100 #define RTI_LEVELGOALTIME 0x00000100
101 #define RTI_MATCENFRAMETIME 0x00000200
102 #define RTI_OBJFRAMETIME 0x00000400
103 #define RTI_AIFRAMETIME 0x00000800
104 #define RTI_PROCESSKEYTIME 0x00001000
105 #define RTI_TEXTUREUPLOADS 0x00002000
106 #define RTI_POLYSDRAWN 0x00004000
107
108 // Macros to be used internally and externally
109 // --------------------------------------------------
110
111 #ifndef USE_RTP
112
113 #define RTP_CLOCKTOSECONDS(c1, c2, t)
114 #define RTP_CLOCKSECONDS(c, t)
115 #define RTP_RECORDVALUE(member, value)
116 #define RTP_INCRVALUE(member, amount)
117 #define RTP_DECRVALUE(member, amount)
118 #define RTP_GETVALUE(member, value)
119 #define RTP_STARTTIME(member)
120 #define RTP_tSTARTTIME(member, time)
121 #define RTP_ENDTIME(member)
122 #define RTP_tENDTIME(member, time)
123 #define RTP_GETCLOCK(time)
124 #define RTP_STARTINCTIME(member)
125 #define RTP_ENDINCTIME(member)
126 #define RTP_ENABLEFLAGS(flags)
127 #define RTP_DISABLEFLAGS(flags)
128
129 #else
130
131 #define RTP_ENABLEFLAGS(flags) rtp_EnableFlags(flags)
132
133 #define RTP_DISABLEFLAGS(flags) rtp_EnableFlags(flags)
134
135 // given 2 clocks (INT64's) it will return the time in seconds between them (double)
136 // (internal use)
137 // c1 = start time
138 // c2 = end time
139 // t = time in seconds
140 #define RTP_CLOCKTOSECONDS(c1, c2, t) \
141 do { \
142 if (Runtime_performance_enabled) { \
143 INT64 diff = (c2) - (c1); \
144 t = ((double)diff) / ((double)Runtime_performance_clockfreq); \
145 } \
146 } while (0)
147
148 // given a clock time, it will convert it to seconds
149 #define RTP_CLOCKSECONDS(c, t) \
150 do { \
151 if (Runtime_performance_enabled) { \
152 t = ((double)c) / ((double)Runtime_performance_clockfreq); \
153 } \
154 } while (0)
155
156 // sets a value of a member of the current tRTFrameInfo frame
157 // (external use)
158 // member = the member of the tRTFrameInfo to set
159 // value = the value to set it to
160 #define RTP_RECORDVALUE(member, value) \
161 do { \
162 if (Runtime_performance_enabled) { \
163 RTP_SingleFrame.member = (value); \
164 } \
165 } while (0)
166
167 // increments a value of a member of the current tRTFrameInfo frame
168 // (external use)
169 // member = the member of the tRTFrameInfo to increment
170 // amount = how much to increment it
171 #define RTP_INCRVALUE(member, amount) \
172 do { \
173 if (Runtime_performance_enabled) { \
174 RTP_SingleFrame.member += (amount); \
175 } \
176 } while (0)
177
178 // decrements a value of a member of the current tRTFrameInfo frame
179 // (external use)
180 // member = the member of the tRTFrameInfo to decrement
181 // amount = how much to decrement it
182 #define RTP_DECRVALUE(member, amount) \
183 do { \
184 if (Runtime_performance_enabled) { \
185 RTP_SingleFrame.member -= (amount); \
186 } \
187 } while (0)
188
189 // returns the value of a member of the current tRTFrameInfo frame
190 // (external use)
191 // member = the member to retrieve
192 // value = where to store the value
193 #define RTP_GETVALUE(member, value) \
194 do { \
195 if (Runtime_performance_enabled) { \
196 value = RTP_SingleFrame.member; \
197 } \
198 } while (0)
199
200 // starts a frame time calculation for a time member of the tRTFrameInfo frame
201 // this version calls the clock to get the current time
202 // member = member to start recording time on
203 #define RTP_STARTTIME(member) \
204 do { \
205 if (Runtime_performance_enabled) { \
206 RTP_SingleFrame.member = rtp_GetClock(); \
207 } \
208 } while (0)
209
210 // starts a frame time calculation given a time to start it with (similar to the above
211 // except you give it the starting time). This should be used (along with
212 // RTP_tENDTIME(member,time)) in places where you do a bunch of frame times in a row
213 // to reduce the number of calls to rtp_GetClock(), however, this way will give
214 // a few cycles off, compared to the RTP_STARTTIME(member)/RTP_ENDTIME(member) way
215 // member = member to start recording time on
216 #define RTP_tSTARTTIME(member, time) \
217 do { \
218 if (Runtime_performance_enabled) { \
219 RTP_SingleFrame.member = time; \
220 } \
221 } while (0)
222
223 // ends a frame time calculation for a member of tRTFrameInfo frame
224 // member = member to stop recording time for
225 #define RTP_ENDTIME(member) \
226 do { \
227 if (Runtime_performance_enabled) { \
228 RTP_SingleFrame.member = rtp_GetClock() - RTP_SingleFrame.member; \
229 } \
230 } while (0)
231
232 // same as above, but will return the time that it stopped the recording time
233 #define RTP_tENDTIME(member, time) \
234 do { \
235 if (Runtime_performance_enabled) { \
236 time = rtp_GetClock(); \
237 RTP_SingleFrame.member = time - RTP_SingleFrame.member; \
238 } \
239 } while (0)
240
241 // returns the current clock time
242 #define RTP_GETCLOCK(time) \
243 do { \
244 if (Runtime_performance_enabled) { \
245 time = rtp_GetClock(); \
246 } \
247 } while (0)
248
249 // Starts a frame time calculation, but when ending the frame, it will increment the time by
250 // the difference instead of just setting it
251 // RTP_STARTINCTIME and RTP_ENDINCTIME must be in the same scope
252 #define RTP_STARTINCTIME(member) \
253 INT64 __start_time_; \
254 do { \
255 if (Runtime_performance_enabled) { \
256 __start_time_ = rtp_GetClock(); \
257 } \
258 } while (0)
259
260 // Ends a frame time calculation and increments the member by the time for that frame
261 #define RTP_ENDINCTIME(member) \
262 do { \
263 if (Runtime_performance_enabled) { \
264 RTP_SingleFrame.member += (rtp_GetClock() - __start_time_); \
265 } \
266 } while (0)
267
268 #endif
269
270 // Internal Global Vars
271 // ------------------------------
272 extern tRTFrameInfo RTP_SingleFrame;
273 extern INT64 Runtime_performance_clockfreq;
274 extern uint8_t Runtime_performance_enabled;
275
276 /*
277 void rtp_Init
278 This function initilizes the runtime performance system so it's ready to be used
279 */
280 void rtp_Init();
281
282 /*
283 void rtp_EnableFlags
284 This function enables the performance profiling of whatever flags you pass in
285 */
286 void rtp_EnableFlags(INT64 flags);
287
288 /*
289 void rtp_DisableFlags
290 This function disables the performance profiling of whatever flags you pass in
291 */
292 void rtp_DisableFlags(INT64 flags);
293
294 /*
295 void rtp_StartLog
296 Calling this function will reset the log and start a new log, recording immediatly
297 */
298 void rtp_StartLog();
299
300 /*
301 void rtp_StopLog
302 Calling this function will stop the currently processing log and write it out to
303 file.
304 */
305 void rtp_StopLog();
306
307 /*
308 void rtp_PauseLog
309 Calling this function will pause the log recording until rtp_ResumeLog is called
310 */
311 void rtp_PauseLog();
312
313 /*
314 void rtp_ResumeLog
315 Calling this will resume a paused log, starting at where it left off
316 */
317 void rtp_ResumeLog();
318
319 /*
320 void rtp_RecordFrame
321 Calling this will record the data of the frame into the internal log, and prepare for
322 the next frame
323 */
324 void rtp_RecordFrame();
325
326 /*
327 INT64 rtp_GetClock
328 Returns the current hi-resolution clock value...no checking for overflow
329 */
330 INT64 rtp_GetClock();
331
332 /*
333 void rtp_WriteBufferLog
334 Writes the buffer of frames to file...
335 */
336 void rtp_WriteBufferLog();
337
338 #endif