f5f341e1410a2a77060986138f47252ccae28697
[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
30 namespace po = boost::program_options;
31
32 // my GL utility functions and the GL window class
33 #include "glutil.h"
34 #include "glwindow.h"
35 // include proper GL backend (windowing system)
36 #if defined(WIN_GLX)
37 #include "glxbackend.h"
38 static GLBackend *createGLBackend()
39 {
40         return new GLXBackend();
41 }
42 #elif defined(WIN_EGL)
43 #include "eglbackend.h"
44 static GLBackend *createGLBackend()
45 {
46         return new EGLBackend();
47 }
48 #else
49 #error "No GL window type selected"
50 #endif
51
52 // configuration
53 static const GLfloat boxWidth = 0.045f;
54 static const GLfloat boxSpeed = 1.25f; // per second
55
56 // profiler
57 enum ProfilerState { StatePreRender, StateClear, StateDraw, StatePresent, StatePostRender, StateOutsideRender, NumProfilerStates };
58 static const char *profilerStateNames[NumProfilerStates] = { "Pre-Render", "Clearing", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
59
60 // utility functions
61 static double getTime()
62 {
63         struct timespec tp;
64         clock_gettime(CLOCK_MONOTONIC, &tp);
65         return tp.tv_sec + 1e-9 * tp.tv_nsec;
66 }
67
68 // the window
69 class TearTestWindow : public GLWindow {
70 public:
71         TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()),
72                 overdraw(false), sleep_time(0), boxPos(0), boxDirection(1)
73         {}
74
75         void setOverdraw(bool overdraw) { this->overdraw = overdraw; }
76         void setSleepTime(int sleep_time) { this->sleep_time = sleep_time; }
77
78         void setSwapInterval(int i) { getBackend()->setSwapInterval(i); }
79
80 private:
81         bool overdraw;
82         int sleep_time;
83         // animation control
84         double lastFrame;
85         GLfloat boxPos, boxDirection;
86         // FPS, profiler
87         double lastDisplay, lastProfile;                 
88         int framect;
89         ProfilerState curState;
90         double stateTime[NumProfilerStates];
91
92 protected:
93         virtual void initGL()
94         {
95                 // initialize GL proper
96                 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
97                 glDisable(GL_DEPTH_TEST);
98                 // initialize clocks
99                 lastFrame = getTime();
100                 // initailize profiler
101                 framect = 0;
102                 memset(stateTime, 0, sizeof(stateTime));
103                 curState = NumProfilerStates;
104                 lastDisplay = lastProfile = getTime();
105         }
106         
107         virtual void resizeGL(unsigned int width, unsigned int height)
108         {
109                 glViewport(0, 0, width, height);
110                 initialise2dProjection();
111                 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
112         }
113         
114         void profilerTick(ProfilerState nextState)
115         {
116                 assert (nextState >= 0 && nextState < NumProfilerStates);
117                 double time = getTime();
118                 if (curState >= 0 && curState < NumProfilerStates)
119                         stateTime[curState] += time-lastProfile;
120                 curState = nextState;
121                 lastProfile = time;
122                 // display?
123                 const double elapsed = time-lastDisplay;
124                 if (elapsed >= 3) {
125                         printf("%.1f fps, time spent: ", framect/elapsed);
126                         for (int i = 0; i < NumProfilerStates; ++i) {
127                                 if (i != 0) printf(", ");
128                                 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
129                         }
130                         printf("\n");
131                         lastDisplay = time;
132                         framect = 0;
133                         memset(stateTime, 0, sizeof(stateTime));
134                 }
135         }
136         
137         void renderGL()
138         {              
139                 //////////////////////////////////////////////
140                 profilerTick(StatePreRender);
141                 double time = getTime();
142                 // anim
143                 double passedTime = time-lastFrame;
144                 boxPos += boxSpeed*passedTime*boxDirection;
145                 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
146                         if (boxPos < 0) {
147                                 boxPos = -boxPos;
148                                 boxDirection = -boxDirection;
149                         }
150                         else {
151                                 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
152                                 boxDirection = -boxDirection;
153                         }
154                 }
155                 lastFrame = time;
156                 //////////////////////////////////////////////
157                 profilerTick(StateClear);
158                 if (overdraw) {
159                         // clear manually
160                         drawQuad(/*color*/0.0f, 0.0f, 0.0f, /*coordinates*/0, 0, 1, 1);
161                 }
162                 else {
163                         glClear(GL_COLOR_BUFFER_BIT);
164                 }
165                 //////////////////////////////////////////////
166                 profilerTick(StateDraw);
167                 drawQuad(/*color*/0.8f, 1.0f, 0.75f, /*coordinates*/boxPos, 0, boxPos+boxWidth, 1);
168                 usleep(sleep_time*1000);
169                 //////////////////////////////////////////////
170                 profilerTick(StatePresent);
171                 getBackend()->swapBuffers();
172                 //////////////////////////////////////////////
173                 profilerTick(StatePostRender);
174                 glFinish();
175                 ++framect;
176                 //////////////////////////////////////////////
177                 profilerTick(StateOutsideRender);
178         }
179         
180         virtual void handleKeyPress(KeySym key)
181         {
182                 switch (key) {
183                         case XK_Escape: close(); break;
184                         case XK_F1: setFullscreen(!getFullscreen()); break;
185                         default: break;
186                 }
187         }
188 };
189
190 int main(int argc, char ** argv)
191 {
192         // program options handling
193         po::options_description desc("Allowed options");
194         desc.add_options()
195                 ("help,h", "produce help message")
196                 ("swap-interval,i", po::value<int>(), "set swap interval")
197                 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
198                 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleep in each frame (in the drawing phase)")
199         ;
200         po::variables_map vm;
201         po::store(po::parse_command_line(argc, argv, desc), vm);
202         po::notify(vm);
203
204         if (vm.count("help")) {
205                 std::cout << desc << "\n";
206                 return 1;
207         }
208
209         // actual program
210         TearTestWindow w;
211         w.setOverdraw(vm.count("overdraw"));
212         w.setSleepTime(vm["sleep"].as<int>());
213         w.open(800, 600);
214         if (vm.count("swap-interval"))
215                 w.setSwapInterval(vm["swap-interval"].as<int>());
216         w.exec();
217 }