]> git.cworth.org Git - vogl/blob - src/voglbench/voglbench.cpp
- Adding "-msaa X" command line option to voglreplay tool. We don't fully support...
[vogl] / src / voglbench / voglbench.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 // File: voglbench.cpp
27 #include "vogl_common.h"
28 #include "vogl_gl_replayer.h"
29 #include "vogl_texture_format.h"
30 #include "vogl_trace_file_writer.h"
31
32 #include "vogl_colorized_console.h"
33 #include "vogl_command_line_params.h"
34 #include "vogl_cfile_stream.h"
35 #include "vogl_value.h"
36 #include "vogl_dynamic_stream.h"
37 #include "vogl_file_utils.h"
38 #include "vogl_mergesort.h"
39 #include "vogl_unique_ptr.h"
40 #include "vogl_find_files.h"
41 #include "vogl_bigint128.h"
42 #include "vogl_regex.h"
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46
47 #include "vogl_json.h"
48 #include "vogl_blob_manager.h"
49
50 #include "libtelemetry.h"
51
52 #include <X11/Xlib.h>
53 #include <X11/Xutil.h>
54 #include <X11/Xmd.h>
55
56 //$ TODO: investigate using SDL for windows and any keyboard controls.
57 //$ Run clang-format on everything.
58 //$ Use Telemetry to speed this bugger up.
59
60 //----------------------------------------------------------------------------------------------------------------------
61 // globals
62 //----------------------------------------------------------------------------------------------------------------------
63 static void *g_actual_libgl_module_handle;
64 static cfile_stream *g_vogl_pLog_stream;
65
66 //----------------------------------------------------------------------------------------------------------------------
67 // command line params
68 //----------------------------------------------------------------------------------------------------------------------
69 static command_line_param_desc g_command_line_param_descs[] =
70     {
71         { "width", 1, false, "Replay: Set initial window width (default is 1024)" },
72         { "height", 1, false, "Replay: Set initial window height (default is 768)" },
73         { "msaa", 1, false, "Replay: Set initial window multisamples (default is 0)" },
74         { "lock_window_dimensions", 0, false, "Replay: Don't automatically change window's dimensions during replay" },
75         { "endless", 0, false, "Replay: Loop replay endlessly instead of exiting" },
76         { "force_debug_context", 0, false, "Replay: Force GL debug contexts" },
77 #ifdef USE_TELEMETRY
78         { "telemetry_level", 1, false, "Set Telemetry level." },
79 #endif
80         { "loop_frame", 1, false, "Replay: loop mode's start frame" },
81         { "loop_len", 1, false, "Replay: loop mode's loop length" },
82         { "loop_count", 1, false, "Replay: loop mode's loop count" },
83         { "logfile", 1, false, "Create logfile" },
84         { "logfile_append", 1, false, "Append output to logfile" },
85         { "help", 0, false, "Display this help" },
86         { "?", 0, false, "Display this help" },
87         { "pause", 0, false, "Wait for a key at startup (so a debugger can be attached)" },
88         { "verbose", 0, false, "Verbose debug output" },
89         { "quiet", 0, false, "Disable all console output" },
90     };
91
92 //----------------------------------------------------------------------------------------------------------------------
93 // init_logfile
94 //----------------------------------------------------------------------------------------------------------------------
95 static bool init_logfile()
96 {
97     VOGL_FUNC_TRACER
98
99     dynamic_string log_file(g_command_line_params.get_value_as_string_or_empty("logfile"));
100     dynamic_string log_file_append(g_command_line_params.get_value_as_string_or_empty("logfile_append"));
101     if (log_file.is_empty() && log_file_append.is_empty())
102         return true;
103
104     dynamic_string filename(log_file_append.is_empty() ? log_file : log_file_append);
105
106     // This purposely leaks, don't care
107     g_vogl_pLog_stream = vogl_new(cfile_stream);
108
109     if (!g_vogl_pLog_stream->open(filename.get_ptr(), cDataStreamWritable, !log_file_append.is_empty()))
110     {
111         vogl_error_printf("%s: Failed opening log file \"%s\"\n", VOGL_FUNCTION_NAME, filename.get_ptr());
112         return false;
113     }
114     else
115     {
116         vogl_message_printf("Opened log file \"%s\"\n", filename.get_ptr());
117         console::set_log_stream(g_vogl_pLog_stream);
118     }
119
120     return true;
121 }
122
123 //----------------------------------------------------------------------------------------------------------------------
124 // tool_print_title
125 //----------------------------------------------------------------------------------------------------------------------
126 static void tool_print_title()
127 {
128     VOGL_FUNC_TRACER
129
130     vogl_printf("voglbench ");
131     if (sizeof(void *) > 4)
132         vogl_printf("64-bit ");
133     else
134         vogl_printf("32-bit ");
135 #ifdef VOGL_BUILD_DEBUG
136     vogl_printf("Debug ");
137 #else
138     vogl_printf("Release ");
139 #endif
140     vogl_printf("Built %s %s\n", __DATE__, __TIME__);
141 }
142
143 //----------------------------------------------------------------------------------------------------------------------
144 // tool_print_help
145 //----------------------------------------------------------------------------------------------------------------------
146 static void tool_print_help()
147 {
148     VOGL_FUNC_TRACER
149
150     vogl_printf("Usage: voglbench [ -option ... ] input_file optional_output_file [ -option ... ]\n");
151     vogl_printf("Command line options may begin with single minus \"-\" or double minus \"--\"\n");
152
153     vogl_printf("\nCommand line options:\n");
154
155     dump_command_line_info(VOGL_ARRAY_SIZE(g_command_line_param_descs), g_command_line_param_descs, "--");
156 }
157
158 //----------------------------------------------------------------------------------------------------------------------
159 // init_command_line_params
160 //----------------------------------------------------------------------------------------------------------------------
161 static bool init_command_line_params(int argc, char *argv[])
162 {
163     VOGL_FUNC_TRACER
164
165     command_line_params::parse_config parse_cfg;
166     parse_cfg.m_single_minus_params = true;
167     parse_cfg.m_double_minus_params = true;
168
169     if (!g_command_line_params.parse(get_command_line_params(argc, argv), VOGL_ARRAY_SIZE(g_command_line_param_descs), g_command_line_param_descs, parse_cfg))
170     {
171         vogl_error_printf("%s: Failed parsing command line parameters!\n", VOGL_FUNCTION_NAME);
172         return false;
173     }
174
175     if (!init_logfile())
176         return false;
177
178     if (g_command_line_params.get_value_as_bool("help") || g_command_line_params.get_value_as_bool("?"))
179     {
180         tool_print_help();
181         return false;
182     }
183
184     return true;
185 }
186
187 //----------------------------------------------------------------------------------------------------------------------
188 // load_gl
189 //----------------------------------------------------------------------------------------------------------------------
190 static bool load_gl()
191 {
192     VOGL_FUNC_TRACER
193
194     g_actual_libgl_module_handle = dlopen("libGL.so.1", RTLD_LAZY);
195     if (!g_actual_libgl_module_handle)
196     {
197         vogl_error_printf("%s: Failed loading libGL.so.1!\n", VOGL_FUNCTION_NAME);
198         return false;
199     }
200
201     GL_ENTRYPOINT(glXGetProcAddress) = reinterpret_cast<glXGetProcAddress_func_ptr_t>(dlsym(g_actual_libgl_module_handle, "glXGetProcAddress"));
202     if (!GL_ENTRYPOINT(glXGetProcAddress))
203     {
204         vogl_error_printf("%s: Failed getting address of glXGetProcAddress() from libGL.so.1!\n", VOGL_FUNCTION_NAME);
205         return false;
206     }
207
208     return true;
209 }
210
211 //----------------------------------------------------------------------------------------------------------------------
212 // vogl_get_proc_address_helper
213 //----------------------------------------------------------------------------------------------------------------------
214 static vogl_void_func_ptr_t vogl_get_proc_address_helper(const char *pName)
215 {
216     VOGL_FUNC_TRACER
217
218     vogl_void_func_ptr_t pFunc = g_actual_libgl_module_handle ? reinterpret_cast<vogl_void_func_ptr_t>(dlsym(g_actual_libgl_module_handle, pName)) : NULL;
219
220     if ((!pFunc) && (GL_ENTRYPOINT(glXGetProcAddress)))
221         pFunc = reinterpret_cast<vogl_void_func_ptr_t>(GL_ENTRYPOINT(glXGetProcAddress)(reinterpret_cast<const GLubyte *>(pName)));
222
223     return pFunc;
224 }
225
226 //----------------------------------------------------------------------------------------------------------------------
227 // vogl_direct_gl_func_prolog - This function is called before EVERY single GL/GLX function call we make.
228 //----------------------------------------------------------------------------------------------------------------------
229 static void vogl_direct_gl_func_prolog(gl_entrypoint_id_t entrypoint_id, void *pUser_data, void **ppStack_data)
230 {
231     VOGL_NOTE_UNUSED(entrypoint_id);
232     VOGL_NOTE_UNUSED(pUser_data);
233     VOGL_NOTE_UNUSED(ppStack_data);
234
235     vogl_printf("* GLPROLOG %s\n", g_vogl_entrypoint_descs[entrypoint_id].m_pName);
236 }
237
238 //----------------------------------------------------------------------------------------------------------------------
239 // vogl_direct_gl_func_epilog - This function is called immediately after EVERY single GL/GLX function call we make.
240 //----------------------------------------------------------------------------------------------------------------------
241 static void vogl_direct_gl_func_epilog(gl_entrypoint_id_t entrypoint_id, void *pUser_data, void **ppStack_data)
242 {
243     VOGL_NOTE_UNUSED(entrypoint_id);
244     VOGL_NOTE_UNUSED(pUser_data);
245     VOGL_NOTE_UNUSED(ppStack_data);
246
247     vogl_printf("* GLEPILOG %s\n", g_vogl_entrypoint_descs[entrypoint_id].m_pName);
248 }
249
250 //----------------------------------------------------------------------------------------------------------------------
251 // voglbench_init
252 //----------------------------------------------------------------------------------------------------------------------
253 static bool voglbench_init(int argc, char *argv[])
254 {
255     VOGL_FUNC_TRACER
256
257     g_thread_safe_random.seed_from_urandom();
258
259     colorized_console::init();
260     colorized_console::set_exception_callback();
261     //console::set_tool_prefix("(voglbench) ");
262
263     tool_print_title();
264
265     if (!init_command_line_params(argc, argv))
266         return false;
267
268 #ifdef USE_TELEMETRY
269     int telemetry_level = g_command_line_params.get_value_as_int("telemetry_level", 0,
270                                                                  TELEMETRY_LEVEL_MIN + 1, TELEMETRY_LEVEL_MIN, TELEMETRY_LEVEL_MAX);
271     telemetry_set_level(telemetry_level);
272     telemetry_tick();
273 #endif
274
275     vogl_common_lib_early_init();
276     vogl_common_lib_global_init();
277
278     if (g_command_line_params.get_value_as_bool("quiet"))
279         console::disable_output();
280
281     if (g_command_line_params.get_value_as_bool("gl_debug_log"))
282     {
283         vogl_set_direct_gl_func_prolog(vogl_direct_gl_func_prolog, NULL);
284         vogl_set_direct_gl_func_epilog(vogl_direct_gl_func_epilog, NULL);
285     }
286
287     if (!load_gl())
288         return false;
289
290     bool wrap_all_gl_calls = false;
291     vogl_init_actual_gl_entrypoints(vogl_get_proc_address_helper, wrap_all_gl_calls);
292     return true;
293 }
294
295 //----------------------------------------------------------------------------------------------------------------------
296 // voglbench_deinit
297 //----------------------------------------------------------------------------------------------------------------------
298 static void voglbench_deinit()
299 {
300     VOGL_FUNC_TRACER
301
302     colorized_console::deinit();
303 }
304
305 //----------------------------------------------------------------------------------------------------------------------
306 // X11_Pending - from SDL
307 //----------------------------------------------------------------------------------------------------------------------
308 static int X11_Pending(Display *display)
309 {
310     VOGL_FUNC_TRACER
311
312     /* Flush the display connection and look to see if events are queued */
313     XFlush(display);
314     if (XEventsQueued(display, QueuedAlready))
315     {
316         return (1);
317     }
318
319     /* More drastic measures are required -- see if X is ready to talk */
320     {
321         static struct timeval zero_time; /* static == 0 */
322         int x11_fd;
323         fd_set fdset;
324
325         x11_fd = ConnectionNumber(display);
326         FD_ZERO(&fdset);
327         FD_SET(x11_fd, &fdset);
328         if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1)
329         {
330             return (XPending(display));
331         }
332     }
333
334     /* Oh well, nothing is ready .. */
335     return (0);
336 }
337
338 //----------------------------------------------------------------------------------------------------------------------
339 // get_replayer_flags_from_command_line_params
340 //----------------------------------------------------------------------------------------------------------------------
341 static uint get_replayer_flags_from_command_line_params()
342 {
343     uint replayer_flags = cGLReplayerBenchmarkMode;
344
345     static struct
346     {
347         const char *m_pCommand;
348         uint m_flag;
349     } s_replayer_command_line_params[] =
350     {
351         { "verbose", cGLReplayerVerboseMode },
352         { "force_debug_context", cGLReplayerForceDebugContexts },
353         { "debug", cGLReplayerDebugMode },
354         { "lock_window_dimensions", cGLReplayerLockWindowDimensions },
355     };
356
357     for (uint i = 0; i < sizeof(s_replayer_command_line_params) / sizeof(s_replayer_command_line_params[0]); i++)
358     {
359         if (g_command_line_params.get_value_as_bool(s_replayer_command_line_params[i].m_pCommand))
360             replayer_flags |= s_replayer_command_line_params[i].m_flag;
361     }
362
363     return replayer_flags;
364 }
365
366 //----------------------------------------------------------------------------------------------------------------------
367 // tool_replay_mode
368 //----------------------------------------------------------------------------------------------------------------------
369 static bool tool_replay_mode()
370 {
371     VOGL_FUNC_TRACER
372
373     dynamic_string trace_filename(g_command_line_params.get_value_as_string_or_empty("", 1));
374     if (trace_filename.is_empty())
375     {
376         vogl_error_printf("%s: No trace file specified!\n", VOGL_FUNCTION_NAME);
377         return false;
378     }
379
380     dynamic_string actual_trace_filename;
381     vogl_unique_ptr<vogl_trace_file_reader> pTrace_reader(vogl_open_trace_file(
382                 trace_filename,
383                 actual_trace_filename,
384                 g_command_line_params.get_value_as_string_or_empty("loose_file_path").get_ptr()));
385     if (!pTrace_reader.get())
386     {
387         vogl_error_printf("%s: File not found, or unable to determine file type of trace file \"%s\"\n", VOGL_FUNCTION_NAME, trace_filename.get_ptr());
388         return false;
389     }
390
391     vogl_printf("Reading trace file %s\n", actual_trace_filename.get_ptr());
392
393     vogl_gl_replayer replayer;
394     vogl_replay_window window;
395
396     uint replayer_flags = get_replayer_flags_from_command_line_params();
397
398     // TODO: This will create a window with default attributes, which seems fine for the majority of traces.
399     // Unfortunately, some GL call streams *don't* want an alpha channel, or depth, or stencil etc. in the default framebuffer so this may become a problem.
400     // Also, this design only supports a single window, which is going to be a problem with multiple window traces.
401     if (!window.open(g_command_line_params.get_value_as_int("width", 0, 1024, 1, 65535), g_command_line_params.get_value_as_int("height", 0, 768, 1, 65535), g_command_line_params.get_value_as_int("msaa", 0, 0, 0, 65535)))
402     {
403         vogl_error_printf("%s: Failed initializing replay window\n", VOGL_FUNCTION_NAME);
404         return false;
405     }
406
407     if (!replayer.init(replayer_flags, &window, pTrace_reader->get_sof_packet(), pTrace_reader->get_multi_blob_manager()))
408     {
409         vogl_error_printf("%s: Failed initializing GL replayer\n", VOGL_FUNCTION_NAME);
410         return false;
411     }
412
413     // Disable all glGetError() calls in vogl_utils.cpp.
414     vogl_disable_gl_get_error();
415
416     XSelectInput(window.get_display(), window.get_xwindow(),
417                  EnterWindowMask | LeaveWindowMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask | PropertyChangeMask | StructureNotifyMask | KeymapStateMask);
418
419     Atom wmDeleteMessage = XInternAtom(window.get_display(), "WM_DELETE_WINDOW", False);
420     XSetWMProtocols(window.get_display(), window.get_xwindow(), &wmDeleteMessage, 1);
421
422     Bool win_mapped = false;
423
424     vogl_gl_state_snapshot *pSnapshot = NULL;
425     int64_t snapshot_loop_start_frame = -1;
426     int64_t snapshot_loop_end_frame = -1;
427
428     vogl::hash_map<uint64_t> keys_pressed, keys_down;
429
430     int loop_frame = g_command_line_params.get_value_as_int("loop_frame", 0, -1);
431     int loop_len = math::maximum<int>(g_command_line_params.get_value_as_int("loop_len", 0, 1), 1);
432     int loop_count = math::maximum<int>(g_command_line_params.get_value_as_int("loop_count", 0, cINT32_MAX), 1);
433     bool endless_mode = g_command_line_params.get_value_as_bool("endless");
434
435     timer tm;
436     tm.start();
437
438     for (;;)
439     {
440         tmZone(TELEMETRY_LEVEL0, TMZF_NONE, "Main Loop");
441
442         while (X11_Pending(window.get_display()))
443         {
444             XEvent newEvent;
445
446             // Watch for new X eventsn
447             XNextEvent(window.get_display(), &newEvent);
448
449             switch (newEvent.type)
450             {
451                 case KeyPress:
452                 {
453                     KeySym xsym = XLookupKeysym(&newEvent.xkey, 0);
454
455                     //printf("KeyPress 0%04llX %" PRIu64 "\n", (uint64_t)xsym, (uint64_t)xsym);
456
457                     keys_down.insert(xsym);
458                     keys_pressed.insert(xsym);
459
460                     break;
461                 }
462                 case KeyRelease:
463                 {
464                     KeySym xsym = XLookupKeysym(&newEvent.xkey, 0);
465
466                     //printf("KeyRelease 0x%04llX %" PRIu64 "\n", (uint64_t)xsym, (uint64_t)xsym);
467
468                     keys_down.erase(xsym);
469
470                     break;
471                 }
472                 case FocusIn:
473                 case FocusOut:
474                 {
475                     //printf("FocusIn/FocusOut\n");
476
477                     keys_down.reset();
478
479                     break;
480                 }
481                 case MappingNotify:
482                 {
483                     //XRefreshKeyboardMapping(&newEvent);
484                     break;
485                 }
486                 case UnmapNotify:
487                 {
488                     //printf("UnmapNotify\n");
489
490                     win_mapped = false;
491
492                     keys_down.reset();
493
494                     break;
495                 }
496                 case MapNotify:
497                 {
498                     //printf("MapNotify\n");
499
500                     win_mapped = true;
501
502                     keys_down.reset();
503
504                     if (!replayer.update_window_dimensions())
505                         goto error_exit;
506
507                     break;
508                 }
509                 case ConfigureNotify:
510                 {
511                     if (!replayer.update_window_dimensions())
512                         goto error_exit;
513
514                     break;
515                 }
516                 case DestroyNotify:
517                 {
518                     vogl_message_printf("Exiting\n");
519                     goto normal_exit;
520                 }
521                 case ClientMessage:
522                 {
523                     if (newEvent.xclient.data.l[0] == (int)wmDeleteMessage)
524                     {
525                         vogl_message_printf("Exiting\n");
526                         goto normal_exit;
527                     }
528
529                     break;
530                 }
531                 default:
532                     break;
533             }
534         }
535
536         if (replayer.get_at_frame_boundary())
537         {
538             if ((!pSnapshot) && (loop_frame != -1) && (static_cast<int64_t>(replayer.get_frame_index()) == loop_frame))
539             {
540                 vogl_debug_printf("%s: Capturing replayer state at start of frame %u\n", VOGL_FUNCTION_NAME, replayer.get_frame_index());
541
542                 pSnapshot = replayer.snapshot_state();
543
544                 if (pSnapshot)
545                 {
546                     vogl_printf("Snapshot succeeded\n");
547
548                     snapshot_loop_start_frame = pTrace_reader->get_cur_frame();
549                     snapshot_loop_end_frame = pTrace_reader->get_cur_frame() + loop_len;
550
551                     vogl_debug_printf("%s: Loop start: %" PRIi64 " Loop end: %" PRIi64 "\n", VOGL_FUNCTION_NAME, snapshot_loop_start_frame, snapshot_loop_end_frame);
552                 }
553                 else
554                 {
555                     vogl_error_printf("Snapshot failed!\n");
556                     loop_frame = -1;
557                 }
558             }
559         }
560
561         vogl_gl_replayer::status_t status = replayer.process_pending_window_resize();
562         if (status == vogl_gl_replayer::cStatusOK)
563         {
564             for (;;)
565             {
566                 status = replayer.process_next_packet(*pTrace_reader);
567
568                 if ((status == vogl_gl_replayer::cStatusNextFrame) ||
569                     (status == vogl_gl_replayer::cStatusResizeWindow) ||
570                     (status == vogl_gl_replayer::cStatusAtEOF) ||
571                     (status == vogl_gl_replayer::cStatusHardFailure))
572                 {
573                     break;
574                 }
575             }
576         }
577
578         if (status == vogl_gl_replayer::cStatusHardFailure)
579             break;
580
581         if (status == vogl_gl_replayer::cStatusAtEOF)
582         {
583             vogl_message_printf("%s: At trace EOF, frame index %u\n", VOGL_FUNCTION_NAME, replayer.get_frame_index());
584         }
585
586         if (replayer.get_at_frame_boundary() &&
587                 pSnapshot && 
588                 (loop_count > 0) &&
589                 ((pTrace_reader->get_cur_frame() == snapshot_loop_end_frame) || (status == vogl_gl_replayer::cStatusAtEOF)))
590         {
591             status = replayer.begin_applying_snapshot(pSnapshot, false);
592             if ((status != vogl_gl_replayer::cStatusOK) && (status != vogl_gl_replayer::cStatusResizeWindow))
593                 goto error_exit;
594
595             pTrace_reader->seek_to_frame(static_cast<uint>(snapshot_loop_start_frame));
596
597             vogl_debug_printf("%s: Applying snapshot and seeking back to frame %" PRIi64 "\n", VOGL_FUNCTION_NAME, snapshot_loop_start_frame);
598             loop_count--;
599         }
600         else
601         {
602             bool print_progress = (status == vogl_gl_replayer::cStatusAtEOF) ||
603                     ((replayer.get_at_frame_boundary()) && ((replayer.get_frame_index() % 100) == 0));
604             if (print_progress)
605             {
606                 if (pTrace_reader->get_type() == cBINARY_TRACE_FILE_READER)
607                 {
608                     vogl_binary_trace_file_reader &binary_trace_reader = *static_cast<vogl_binary_trace_file_reader *>(pTrace_reader.get());
609
610                     vogl_printf("Replay now at frame index %u, trace file offet %" PRIu64 ", GL call counter %" PRIu64 ", %3.2f%% percent complete\n",
611                                replayer.get_frame_index(),
612                                binary_trace_reader.get_cur_file_ofs(),
613                                replayer.get_last_parsed_call_counter(),
614                                binary_trace_reader.get_trace_file_size() ? (binary_trace_reader.get_cur_file_ofs() * 100.0f) / binary_trace_reader.get_trace_file_size() : 0);
615                 }
616             }
617
618             if (status == vogl_gl_replayer::cStatusAtEOF)
619             {
620                 if (!endless_mode)
621                 {
622                     double time_since_start = tm.get_elapsed_secs();
623
624                     vogl_printf("%u total swaps, %.3f secs, %3.3f avg fps\n", replayer.get_total_swaps(), time_since_start, replayer.get_frame_index() / time_since_start);
625                     break;
626                 }
627
628                 vogl_printf("Resetting state and rewinding back to frame 0\n");
629
630                 replayer.reset_state();
631
632                 if (!pTrace_reader->seek_to_frame(0))
633                 {
634                     vogl_error_printf("%s: Failed rewinding trace reader!\n", VOGL_FUNCTION_NAME);
635                     goto error_exit;
636                 }
637             }
638         }
639
640         telemetry_tick();
641     }
642
643 normal_exit:
644     return true;
645
646 error_exit:
647     return false;
648 }
649
650 //----------------------------------------------------------------------------------------------------------------------
651 // xerror_handler
652 //----------------------------------------------------------------------------------------------------------------------
653 static int xerror_handler(Display *dsp, XErrorEvent *error)
654 {
655     char error_string[256];
656     XGetErrorText(dsp, error->error_code, error_string, sizeof(error_string));
657
658     fprintf(stderr, "voglbench: Fatal X Windows Error: %s\n", error_string);
659     abort();
660 }
661
662 //----------------------------------------------------------------------------------------------------------------------
663 // main
664 //----------------------------------------------------------------------------------------------------------------------
665 int main(int argc, char *argv[])
666 {
667 #if VOGL_FUNCTION_TRACING
668     fflush(stdout);
669     fflush(stderr);
670     setvbuf(stdout, NULL, _IONBF, 0);
671     setvbuf(stderr, NULL, _IONBF, 0);
672 #endif
673
674     VOGL_FUNC_TRACER
675
676     XSetErrorHandler(xerror_handler);
677
678     if (!voglbench_init(argc, argv))
679     {
680         voglbench_deinit();
681         return EXIT_FAILURE;
682     }
683
684     if (g_command_line_params.get_count("") < 2)
685     {
686         vogl_error_printf("No trace file specified!\n");
687
688         tool_print_help();
689
690         voglbench_deinit();
691         return EXIT_FAILURE;
692     }
693
694     if (g_command_line_params.get_value_as_bool("pause"))
695     {
696         vogl_message_printf("Press key to continue\n");
697         vogl_sleep(1000);
698         getchar();
699     }
700
701     bool success = tool_replay_mode();
702
703     vogl_printf("%u warning(s), %u error(s)\n",
704                     console::get_total_messages(cWarningConsoleMessage),
705                     console::get_total_messages(cErrorConsoleMessage));
706
707     voglbench_deinit();
708
709     return success ? EXIT_SUCCESS : EXIT_FAILURE;
710 }