feb794fcc08948f3d386f61e21200c073a6d2003
[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 <GL/gl.h>
28 #include <boost/program_options.hpp>
29
30 namespace po = boost::program_options;
31
32 // include proper GL connector
33 #include "glwindow.h"
34 #if defined(USE_GLX)
35 #include "glxbackend.h"
36 static GLBackend *createGLBackend()
37 {
38         return new GLXBackend();
39 }
40 #elif defined(USE_EGL)
41 #include "eglbackend.h"
42 static GLBackend *createGLBackend()
43 {
44         return new EGLBackend();
45 }
46 #else
47 #error "No GL window type selected"
48 #endif
49
50 // configuration
51 static const GLfloat boxWidth = 0.045f;
52 static const GLfloat boxSpeed = 1.25f; // per second
53
54 // profiler
55 enum ProfilerState { StatePreRender, StateClear, StateDraw, StatePresent, StatePostRender, StateOutsideRender, NumProfilerStates };
56 static const char *profilerStateNames[NumProfilerStates] = { "Pre-Render", "Clearing", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
57
58 // utility functions
59 static double getTime()
60 {
61         struct timespec tp;
62         clock_gettime(CLOCK_MONOTONIC, &tp);
63         return tp.tv_sec + 1e-9 * tp.tv_nsec;
64 }
65
66 static void rectQuad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
67 {
68         glVertex2f(x1, y1);
69         glVertex2f(x2, y1);
70         glVertex2f(x2, y2);
71         glVertex2f(x1, y2);
72 }
73
74 // the window
75 class TearTestWindow : public GLWindow {
76 public:
77         TearTestWindow(bool overdraw, bool copy, int sleep_time) : GLWindow(XOpenDisplay(0), createGLBackend()),
78                 overdraw(overdraw), copy(copy), sleep_time(sleep_time), boxPos(0), boxDirection(1)
79         {}
80
81         void setSwapInterval(int i) {
82                 getBackend()->setSwapInterval(i);
83         }
84
85 protected:
86         virtual void initGL()
87         {
88                 // initialize GL proper
89                 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
90                 glDisable(GL_DEPTH_TEST);
91                 // initialize clocks
92                 lastFrame = getTime();
93                 // initailize profiler
94                 framect = 0;
95                 memset(stateTime, 0, sizeof(stateTime));
96                 curState = NumProfilerStates;
97                 lastDisplay = lastProfile = getTime();
98         }
99         
100         virtual void resizeGL(unsigned int width, unsigned int height)
101         {
102                 // prevent divide-by-zero
103                 if (height == 0)
104                         height = 1;
105                 glViewport(0, 0, width, height);
106                 glMatrixMode(GL_PROJECTION);
107                 glLoadIdentity();
108                 glOrtho (0, 1, 1, 0, 0, 1);
109                 glMatrixMode(GL_MODELVIEW);
110                 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
111                 glClear(GL_COLOR_BUFFER_BIT);
112                 glFlush();
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                         glBegin(GL_QUADS);
162                         glColor3f(0.0f, 0.0f, 0.0f);
163                         rectQuad(0, 0, 1, 1);
164                         glEnd();
165                 }
166                 else {
167                         glClear(GL_COLOR_BUFFER_BIT);
168                 }
169                 //////////////////////////////////////////////
170                 profilerTick(StateDraw);
171                 glBegin(GL_QUADS);
172                 glColor3f(0.8f, 1.0f, 0.75f);
173                 rectQuad(boxPos, 0, boxPos+boxWidth, 1);
174                 glEnd();
175                 usleep(sleep_time*1000);
176                 //////////////////////////////////////////////
177                 profilerTick(StatePresent);
178                 if (copy) {
179                         glDrawBuffer(GL_FRONT);
180                         glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
181                         glDrawBuffer(GL_BACK);
182                 }
183                 else {
184                         getBackend()->swapBuffers();
185                 }
186                 //////////////////////////////////////////////
187                 profilerTick(StatePostRender);
188                 glFlush();
189                 ++framect;
190                 //////////////////////////////////////////////
191                 profilerTick(StateOutsideRender);
192         }
193         
194         virtual void handleKeyPress(KeySym key)
195         {
196                 switch (key) {
197                         case XK_Escape: close(); break;
198                         case XK_F1: setFullscreen(!getFullscreen()); break;
199                         default: break;
200                 }
201         }
202
203 private:
204         bool overdraw, copy;
205         int sleep_time;
206         // animation control
207         double lastFrame;
208         GLfloat boxPos, boxDirection;
209         // FPS, profiler
210         double lastDisplay, lastProfile;                 
211         int framect;
212         ProfilerState curState;
213         double stateTime[NumProfilerStates];
214 };
215
216 int main(int argc, char ** argv)
217 {
218         // program options handling
219         po::options_description desc("Allowed options");
220         desc.add_options()
221                 ("help,h", "produce help message")
222                 ("swap-interval,i", po::value<int>(), "set swap interval")
223                 ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
224                 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
225                 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
226         ;
227         po::variables_map vm;
228         po::store(po::parse_command_line(argc, argv, desc), vm);
229         po::notify(vm);
230
231         if (vm.count("help")) {
232                 std::cout << desc << "\n";
233                 return 1;
234         }
235
236         // actual program
237         TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
238         w.open(800, 600);
239         if (vm.count("swap-interval"))
240                 w.setSwapInterval(vm["swap-interval"].as<int>());
241         w.exec();
242 }