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>
30 namespace po = boost::program_options;
32 // my GL utility functions and the GL window class
35 // include proper GL backend (windowing system)
37 #include "glxbackend.h"
38 static GLBackend *createGLBackend()
40 return new GLXBackend();
42 #elif defined(WIN_EGL)
43 #include "eglbackend.h"
44 static GLBackend *createGLBackend()
46 return new EGLBackend();
49 #error "No GL window type selected"
53 static const GLfloat boxWidth = 0.045f;
54 static const GLfloat boxSpeed = 1.25f; // per second
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"};
61 static double getTime()
64 clock_gettime(CLOCK_MONOTONIC, &tp);
65 return tp.tv_sec + 1e-9 * tp.tv_nsec;
69 class TearTestWindow : public GLWindow {
71 TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()),
72 overdraw(false), sleep_time(0), boxPos(0), boxDirection(1)
75 void setOverdraw(bool overdraw) { this->overdraw = overdraw; }
76 void setSleepTime(int sleep_time) { this->sleep_time = sleep_time; }
78 void setSwapInterval(int i) { getBackend()->setSwapInterval(i); }
85 GLfloat boxPos, boxDirection;
87 double lastDisplay, lastProfile;
89 ProfilerState curState;
90 double stateTime[NumProfilerStates];
95 // initialize GL proper
96 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
97 glDisable(GL_DEPTH_TEST);
99 lastFrame = getTime();
100 // initailize profiler
102 memset(stateTime, 0, sizeof(stateTime));
103 curState = NumProfilerStates;
104 lastDisplay = lastProfile = getTime();
107 virtual void resizeGL(unsigned int width, unsigned int height)
109 glViewport(0, 0, width, height);
110 initialise2dProjection();
111 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
114 void profilerTick(ProfilerState nextState)
116 assert (nextState >= 0 && nextState < NumProfilerStates);
117 double time = getTime();
118 if (curState >= 0 && curState < NumProfilerStates)
119 stateTime[curState] += time-lastProfile;
120 curState = nextState;
123 const double elapsed = time-lastDisplay;
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);
133 memset(stateTime, 0, sizeof(stateTime));
139 //////////////////////////////////////////////
140 profilerTick(StatePreRender);
141 double time = getTime();
143 double passedTime = time-lastFrame;
144 boxPos += boxSpeed*passedTime*boxDirection;
145 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
148 boxDirection = -boxDirection;
151 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
152 boxDirection = -boxDirection;
156 //////////////////////////////////////////////
157 profilerTick(StateClear);
160 drawQuad(/*color*/0.0f, 0.0f, 0.0f, /*coordinates*/0, 0, 1, 1);
163 glClear(GL_COLOR_BUFFER_BIT);
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);
176 //////////////////////////////////////////////
177 profilerTick(StateOutsideRender);
180 virtual void handleKeyPress(KeySym key)
183 case XK_Escape: close(); break;
184 case XK_F1: setFullscreen(!getFullscreen()); break;
190 int main(int argc, char ** argv)
192 // program options handling
193 po::options_description desc("Allowed 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)")
200 po::variables_map vm;
201 po::store(po::parse_command_line(argc, argv, desc), vm);
204 if (vm.count("help")) {
205 std::cout << desc << "\n";
211 w.setOverdraw(vm.count("overdraw"));
212 w.setSleepTime(vm["sleep"].as<int>());
214 if (vm.count("swap-interval"))
215 w.setSwapInterval(vm["swap-interval"].as<int>());