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