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, stateClear, stateDraw, statePresent, statePostRender, stateOutsideRender, numProfilerStates };
38 static const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Clearing", "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 rectVertices(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
57 class TearTestWindow : public GLWindow {
59 TearTestWindow(bool overdraw, bool copy, int sleep_time) : GLWindow(XOpenDisplay(0), createGLBackend()),
60 overdraw(overdraw), copy(copy), sleep_time(sleep_time), boxPos(0), boxDirection(1)
63 void setSwapInterval(int i) {
64 getBackend()->setSwapInterval(i);
70 // initialize GL proper
71 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
72 glDisable(GL_DEPTH_TEST);
74 lastFrame = getTime();
75 // initailize profiler
77 memset(stateTime, 0, sizeof(stateTime));
79 lastDisplay = lastProfile = getTime();
82 virtual void resizeGL(unsigned int width, unsigned int height)
84 /* prevent divide-by-zero */
87 glViewport(0, 0, width, height);
88 glMatrixMode(GL_PROJECTION);
90 glOrtho (0, 1, 1, 0, 0, 1);
91 glMatrixMode(GL_MODELVIEW);
92 glClear(GL_COLOR_BUFFER_BIT);
96 void profilerTick(ProfilerState nextState)
98 assert (nextState >= 0 && nextState < numProfilerStates);
99 double time = getTime();
101 stateTime[curState] += time-lastProfile;
102 curState = nextState;
105 const double elapsed = time-lastDisplay;
107 printf("%.1f fps, time spent: ", framect/elapsed);
108 for (int i = 0; i < numProfilerStates; ++i) {
109 if (i != 0) printf(", ");
110 printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
115 memset(stateTime, 0, sizeof(stateTime));
121 //////////////////////////////////////////////
122 profilerTick(statePreRender);
123 double time = getTime();
125 double passedTime = time-lastFrame;
126 boxPos += boxSpeed*passedTime*boxDirection;
127 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
130 boxDirection = -boxDirection;
133 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
134 boxDirection = -boxDirection;
138 //////////////////////////////////////////////
139 profilerTick(stateClear);
143 glColor3f(0.0f, 0.0f, 0.0f);
144 rectVertices(0, 0, 1, 1);
148 glClear(GL_COLOR_BUFFER_BIT);
150 //////////////////////////////////////////////
151 profilerTick(stateDraw);
153 glColor3f(0.8f, 1.0f, 0.75f);
154 rectVertices(boxPos, 0, boxPos+boxWidth, 1);
156 usleep(sleep_time*1000);
157 //////////////////////////////////////////////
158 profilerTick(statePresent);
160 glDrawBuffer(GL_FRONT);
161 glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
162 glDrawBuffer(GL_BACK);
165 getBackend()->swapBuffers();
167 //////////////////////////////////////////////
168 profilerTick(statePostRender);
171 //////////////////////////////////////////////
172 profilerTick(stateOutsideRender);
175 virtual void handleKeyPress(KeySym key)
178 case XK_Escape: close(); break;
179 case XK_F1: setFullscreen(!getFullscreen()); break;
189 GLfloat boxPos, boxDirection;
191 double lastDisplay, lastProfile;
192 int framect, curState;
193 double stateTime[numProfilerStates];
196 int main(int argc, char ** argv)
198 // program options handling
199 po::options_description desc("Allowed options");
201 ("help,h", "produce help message")
202 ("swap-interval,i", po::value<int>(), "set swap interval")
203 ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
204 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
205 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
207 po::variables_map vm;
208 po::store(po::parse_command_line(argc, argv, desc), vm);
211 if (vm.count("help")) {
212 std::cout << desc << "\n";
217 TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
219 if (vm.count("swap-interval"))
220 w.setSwapInterval(vm["swap-interval"].as<int>());