1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
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.
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.
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.
28 #include <boost/program_options.hpp>
29 #include <X11/keysym.h>
31 namespace po = boost::program_options;
33 // my GL utility functions and the GL window class
36 // include proper GL backend (windowing system)
38 #include "glxbackend.h"
39 static GLBackend *createGLBackend()
41 return new GLXBackend();
43 #elif defined(WIN_EGL)
44 #include "eglbackend.h"
45 static GLBackend *createGLBackend()
47 return new EGLBackend();
50 #error "No GL window type selected"
54 static const GLfloat boxWidth = 0.045f;
55 static const GLfloat boxSpeed = 1.25f; // per second
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"};
62 static double getTime()
65 clock_gettime(CLOCK_MONOTONIC, &tp);
66 return tp.tv_sec + 1e-9 * tp.tv_nsec;
70 class TearTestWindow : public GLWindow {
72 TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()),
73 overdraw(false), sleep_time(0), boxPos(0), boxDirection(1)
76 void setOverdraw(bool overdraw) { this->overdraw = overdraw; }
77 void setSleepTime(int sleep_time) { this->sleep_time = sleep_time; }
79 void setSwapInterval(int i) { getBackend()->setSwapInterval(i); }
86 GLfloat boxPos, boxDirection;
88 double lastDisplay, lastProfile;
90 ProfilerState curState;
91 double stateTime[NumProfilerStates];
96 // initialize GL proper
97 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
98 glDisable(GL_DEPTH_TEST);
100 lastFrame = getTime();
101 // initailize profiler
103 memset(stateTime, 0, sizeof(stateTime));
104 curState = NumProfilerStates;
105 lastDisplay = lastProfile = getTime();
108 virtual void resizeGL(unsigned int width, unsigned int height)
110 glViewport(0, 0, width, height);
111 initialise2dProjection();
112 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
115 void profilerTick(ProfilerState nextState)
117 assert (nextState >= 0 && nextState < NumProfilerStates);
118 double time = getTime();
119 if (curState >= 0 && curState < NumProfilerStates)
120 stateTime[curState] += time-lastProfile;
121 curState = nextState;
124 const double elapsed = time-lastDisplay;
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);
134 memset(stateTime, 0, sizeof(stateTime));
140 //////////////////////////////////////////////
141 profilerTick(StatePreRender);
142 double time = getTime();
144 double passedTime = time-lastFrame;
145 boxPos += boxSpeed*passedTime*boxDirection;
146 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
149 boxDirection = -boxDirection;
152 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
153 boxDirection = -boxDirection;
157 //////////////////////////////////////////////
158 profilerTick(StateClear);
161 drawQuad(/*color*/0.0f, 0.0f, 0.0f, /*coordinates*/0, 0, 1, 1);
164 glClear(GL_COLOR_BUFFER_BIT);
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);
177 //////////////////////////////////////////////
178 profilerTick(StateOutsideRender);
181 virtual void handleKeyPress(KeySym key)
184 case XK_Escape: close(); break;
185 case XK_F1: setFullscreen(!getFullscreen()); break;
191 int main(int argc, char ** argv)
193 // program options handling
194 po::options_description desc("Allowed 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)")
201 po::variables_map vm;
202 po::store(po::parse_command_line(argc, argv, desc), vm);
205 if (vm.count("help")) {
206 std::cout << desc << "\n";
212 w.setOverdraw(vm.count("overdraw"));
213 w.setSleepTime(vm["sleep"].as<int>());
215 if (vm.count("swap-interval"))
216 w.setSwapInterval(vm["swap-interval"].as<int>());