fix build
[gltest.git] / gltest.cpp
1 /* gltest - small OpenGL tearing test program
2  * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 // stdlib includes
20 #include <iostream>
21 #include <time.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <boost/program_options.hpp>
29 #include <X11/keysym.h>
30
31 namespace po = boost::program_options;
32
33 // my GL utility functions and the GL window class
34 #include "glutil.h"
35 #include "glwindow.h"
36 // include proper GL backend (windowing system)
37 #if defined(WIN_GLX)
38 #include "glxbackend.h"
39 static GLBackend *createGLBackend()
40 {
41         return new GLXBackend();
42 }
43 #elif defined(WIN_EGL)
44 #include "eglbackend.h"
45 static GLBackend *createGLBackend()
46 {
47         return new EGLBackend();
48 }
49 #else
50 #error "No GL window type selected"
51 #endif
52
53 // configuration
54 static const GLfloat boxWidth = 0.045f;
55 static const GLfloat boxSpeed = 1.25f; // per second
56
57 // profiler
58 enum ProfilerState { StatePreRender, StateClear, StateDraw, StatePresent, StatePostRender, StateOutsideRender, NumProfilerStates };
59 static const char *profilerStateNames[NumProfilerStates] = { "Pre-Render", "Clearing", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
60
61 // utility functions
62 static double getTime()
63 {
64         struct timespec tp;
65         clock_gettime(CLOCK_MONOTONIC, &tp);
66         return tp.tv_sec + 1e-9 * tp.tv_nsec;
67 }
68
69 // the window
70 class TearTestWindow : public GLWindow {
71 public:
72         TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()),
73                 overdraw(false), sleep_time(0), boxPos(0), boxDirection(1)
74         {}
75
76         void setOverdraw(bool overdraw) { this->overdraw = overdraw; }
77         void setSleepTime(int sleep_time) { this->sleep_time = sleep_time; }
78
79         void setSwapInterval(int i) { getBackend()->setSwapInterval(i); }
80
81 private:
82         bool overdraw;
83         int sleep_time;
84         // animation control
85         double lastFrame;
86         GLfloat boxPos, boxDirection;
87         // FPS, profiler
88         double lastDisplay, lastProfile;                 
89         int framect;
90         ProfilerState curState;
91         double stateTime[NumProfilerStates];
92
93 protected:
94         virtual void initGL()
95         {
96                 // initialize GL proper
97                 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
98                 glDisable(GL_DEPTH_TEST);
99                 // initialize clocks
100                 lastFrame = getTime();
101                 // initailize profiler
102                 framect = 0;
103                 memset(stateTime, 0, sizeof(stateTime));
104                 curState = NumProfilerStates;
105                 lastDisplay = lastProfile = getTime();
106         }
107         
108         virtual void resizeGL(unsigned int width, unsigned int height)
109         {
110                 glViewport(0, 0, width, height);
111                 initialise2dProjection();
112                 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
113         }
114         
115         void profilerTick(ProfilerState nextState)
116         {
117                 assert (nextState >= 0 && nextState < NumProfilerStates);
118                 double time = getTime();
119                 if (curState >= 0 && curState < NumProfilerStates)
120                         stateTime[curState] += time-lastProfile;
121                 curState = nextState;
122                 lastProfile = time;
123                 // display?
124                 const double elapsed = time-lastDisplay;
125                 if (elapsed >= 3) {
126                         printf("%.1f fps, time spent: ", framect/elapsed);
127                         for (int i = 0; i < NumProfilerStates; ++i) {
128                                 if (i != 0) printf(", ");
129                                 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
130                         }
131                         printf("\n");
132                         lastDisplay = time;
133                         framect = 0;
134                         memset(stateTime, 0, sizeof(stateTime));
135                 }
136         }
137         
138         void renderGL()
139         {              
140                 //////////////////////////////////////////////
141                 profilerTick(StatePreRender);
142                 double time = getTime();
143                 // anim
144                 double passedTime = time-lastFrame;
145                 boxPos += boxSpeed*passedTime*boxDirection;
146                 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
147                         if (boxPos < 0) {
148                                 boxPos = -boxPos;
149                                 boxDirection = -boxDirection;
150                         }
151                         else {
152                                 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
153                                 boxDirection = -boxDirection;
154                         }
155                 }
156                 lastFrame = time;
157                 //////////////////////////////////////////////
158                 profilerTick(StateClear);
159                 if (overdraw) {
160                         // clear manually
161                         drawQuad(/*color*/0.0f, 0.0f, 0.0f, /*coordinates*/0, 0, 1, 1);
162                 }
163                 else {
164                         glClear(GL_COLOR_BUFFER_BIT);
165                 }
166                 //////////////////////////////////////////////
167                 profilerTick(StateDraw);
168                 drawQuad(/*color*/0.8f, 1.0f, 0.75f, /*coordinates*/boxPos, 0, boxPos+boxWidth, 1);
169                 usleep(sleep_time*1000);
170                 //////////////////////////////////////////////
171                 profilerTick(StatePresent);
172                 getBackend()->swapBuffers();
173                 //////////////////////////////////////////////
174                 profilerTick(StatePostRender);
175                 glFinish();
176                 ++framect;
177                 //////////////////////////////////////////////
178                 profilerTick(StateOutsideRender);
179         }
180         
181         virtual void handleKeyPress(KeySym key)
182         {
183                 switch (key) {
184                         case XK_Escape: close(); break;
185                         case XK_F1: setFullscreen(!getFullscreen()); break;
186                         default: break;
187                 }
188         }
189 };
190
191 int main(int argc, char ** argv)
192 {
193         // program options handling
194         po::options_description desc("Allowed options");
195         desc.add_options()
196                 ("help,h", "produce help message")
197                 ("swap-interval,i", po::value<int>(), "set swap interval")
198                 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
199                 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleep in each frame (in the drawing phase)")
200         ;
201         po::variables_map vm;
202         po::store(po::parse_command_line(argc, argv, desc), vm);
203         po::notify(vm);
204
205         if (vm.count("help")) {
206                 std::cout << desc << "\n";
207                 return 1;
208         }
209
210         // actual program
211         TearTestWindow w;
212         w.setOverdraw(vm.count("overdraw"));
213         w.setSleepTime(vm["sleep"].as<int>());
214         w.open(800, 600);
215         if (vm.count("swap-interval"))
216                 w.setSwapInterval(vm["swap-interval"].as<int>());
217         w.exec();
218 }