10 #include <boost/program_options.hpp>
12 namespace po = boost::program_options;
14 // include proper GL connector
17 #include "glxbackend.h"
18 static GLBackend *createGLBackend()
20 return new GLXBackend();
22 #elif defined(USE_EGL)
23 #include "eglbackend.h"
24 static GLBackend *createGLBackend()
26 return new EGLBackend();
29 #error "No GL window type selected"
33 static const GLfloat boxWidth = 0.045f;
34 static const GLfloat boxSpeed = 1.25f; // per second
37 enum ProfilerState { statePreRender, stateDraw, statePresent, statePostRender, stateOutsideRender, numProfilerStates };
38 static const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
41 static double getTime()
44 clock_gettime(CLOCK_MONOTONIC, &tp);
45 return tp.tv_sec + 1e-9 * tp.tv_nsec;
48 static void drawRect(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
50 glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x1, y2);
54 class TearTestWindow : public GLWindow {
56 TearTestWindow(bool overdraw, bool copy) : GLWindow(XOpenDisplay(0), createGLBackend()), overdraw(overdraw), copy(copy), boxPos(0), boxDirection(1)
59 void setSwapInterval(int i) {
60 getBackend()->setSwapInterval(i);
66 // initialize GL proper
67 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
68 glDisable(GL_DEPTH_TEST);
70 lastFrame = getTime();
71 // initailize profiler
73 memset(stateTime, 0, sizeof(stateTime));
75 lastDisplay = lastProfile = getTime();
78 virtual void resizeGL(unsigned int width, unsigned int height)
80 /* prevent divide-by-zero */
83 glViewport(0, 0, width, height);
84 glMatrixMode(GL_PROJECTION);
86 glOrtho (0, 1, 1, 0, 0, 1);
87 glMatrixMode(GL_MODELVIEW);
88 glClear(GL_COLOR_BUFFER_BIT);
92 void profilerTick(ProfilerState nextState)
94 assert (nextState >= 0 && nextState < numProfilerStates);
95 double time = getTime();
97 stateTime[curState] += time-lastProfile;
101 const double elapsed = time-lastDisplay;
103 printf("%.1f fps, time spent: ", framect/elapsed);
104 for (int i = 0; i < numProfilerStates; ++i) {
105 if (i != 0) printf(", ");
106 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
111 memset(stateTime, 0, sizeof(stateTime));
117 //////////////////////////////////////////////
118 profilerTick(statePreRender);
119 double time = getTime();
121 double passedTime = time-lastFrame;
122 boxPos += boxSpeed*passedTime*boxDirection;
123 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
126 boxDirection = -boxDirection;
129 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
130 boxDirection = -boxDirection;
134 //////////////////////////////////////////////
135 profilerTick(stateDraw);
139 glColor3f(0.0f, 0.0f, 0.0f);
140 drawRect(0, 0, 1, 1);
143 glClear(GL_COLOR_BUFFER_BIT);
146 glColor3f(0.8f, 1.0f, 0.75f);
147 drawRect(boxPos, 0, boxPos+boxWidth, 1);
149 //////////////////////////////////////////////
150 profilerTick(statePresent);
152 glDrawBuffer(GL_FRONT);
153 glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
154 glDrawBuffer(GL_BACK);
157 getBackend()->swapBuffers();
159 //////////////////////////////////////////////
160 profilerTick(statePostRender);
163 //////////////////////////////////////////////
164 profilerTick(stateOutsideRender);
167 virtual void handleKeyPress(KeySym key)
170 case XK_Escape: close(); break;
171 case XK_F1: setFullscreen(!getFullscreen()); break;
179 GLfloat boxPos, boxDirection;
181 double lastDisplay, lastProfile;
182 int framect, curState;
183 double stateTime[numProfilerStates];
188 int main(int argc, char ** argv)
190 // program options handling
191 po::options_description desc("Allowed options");
193 ("help,h", "produce help message")
194 ("swap-interval,i", po::value<int>(), "set swap interval")
195 ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
196 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
198 po::variables_map vm;
199 po::store(po::parse_command_line(argc, argv, desc), vm);
202 if (vm.count("help")) {
203 std::cout << desc << "\n";
208 TearTestWindow w(vm.count("overdraw"), vm.count("copy"));
210 if (vm.count("swap-interval"))
211 w.setSwapInterval(vm["swap-interval"].as<int>());