Make the state names capital-first
[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                 glClear(GL_COLOR_BUFFER_BIT);                                                 
111                 glFlush();
112         }
113         
114         void profilerTick(ProfilerState nextState)
115         {
116                 assert (nextState >= 0 && nextState < NumProfilerStates);
117                 double time = getTime();
118                 if (curState >= 0 && curState < NumProfilerStates)
119                         stateTime[curState] += time-lastProfile;
120                 curState = nextState;
121                 lastProfile = time;
122                 // display?
123                 const double elapsed = time-lastDisplay;
124                 if (elapsed >= 3) {
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);
129                         }
130                         printf("\n");
131                         lastDisplay = time;
132                         framect = 0;
133                         memset(stateTime, 0, sizeof(stateTime));
134                 }
135         }
136         
137         void renderGL()
138         {              
139                 //////////////////////////////////////////////
140                 profilerTick(StatePreRender);
141                 double time = getTime();
142                 // anim
143                 double passedTime = time-lastFrame;
144                 boxPos += boxSpeed*passedTime*boxDirection;
145                 while (boxPos < 0 || boxPos+boxWidth > 1) { // wrapover
146                         if (boxPos < 0) {
147                                 boxPos = -boxPos;
148                                 boxDirection = -boxDirection;
149                         }
150                         else {
151                                 boxPos = 1.0-boxWidth-(boxPos+boxWidth-1.0);
152                                 boxDirection = -boxDirection;
153                         }
154                 }
155                 lastFrame = time;
156                 //////////////////////////////////////////////
157                 profilerTick(StateClear);
158                 if (overdraw) {
159                         // clear manually
160                         glBegin(GL_QUADS);
161                         glColor3f(0.0f, 0.0f, 0.0f);
162                         rectQuad(0, 0, 1, 1);
163                         glEnd();
164                 }
165                 else {
166                         glClear(GL_COLOR_BUFFER_BIT);
167                 }
168                 //////////////////////////////////////////////
169                 profilerTick(StateDraw);
170                 glBegin(GL_QUADS);
171                 glColor3f(0.8f, 1.0f, 0.75f);
172                 rectQuad(boxPos, 0, boxPos+boxWidth, 1);
173                 glEnd();
174                 usleep(sleep_time*1000);
175                 //////////////////////////////////////////////
176                 profilerTick(StatePresent);
177                 if (copy) {
178                         glDrawBuffer(GL_FRONT);
179                         glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
180                         glDrawBuffer(GL_BACK);
181                 }
182                 else {
183                         getBackend()->swapBuffers();
184                 }
185                 //////////////////////////////////////////////
186                 profilerTick(StatePostRender);
187                 glFlush();
188                 ++framect;
189                 //////////////////////////////////////////////
190                 profilerTick(StateOutsideRender);
191         }
192         
193         virtual void handleKeyPress(KeySym key)
194         {
195                 switch (key) {
196                         case XK_Escape: close(); break;
197                         case XK_F1: setFullscreen(!getFullscreen()); break;
198                         default: break;
199                 }
200         }
201
202 private:
203         bool overdraw, copy;
204         int sleep_time;
205         // animation control
206         double lastFrame;
207         GLfloat boxPos, boxDirection;
208         // FPS, profiler
209         double lastDisplay, lastProfile;                 
210         int framect;
211         ProfilerState curState;
212         double stateTime[NumProfilerStates];
213 };
214
215 int main(int argc, char ** argv)
216 {
217         // program options handling
218         po::options_description desc("Allowed options");
219         desc.add_options()
220                 ("help,h", "produce help message")
221                 ("swap-interval,i", po::value<int>(), "set swap interval")
222                 ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
223                 ("overdraw,o", "overdraw previous image (instead of calling glClear)")
224                 ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
225         ;
226         po::variables_map vm;
227         po::store(po::parse_command_line(argc, argv, desc), vm);
228         po::notify(vm);
229
230         if (vm.count("help")) {
231                 std::cout << desc << "\n";
232                 return 1;
233         }
234
235         // actual program
236         TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
237         w.open(800, 600);
238         if (vm.count("swap-interval"))
239                 w.setSwapInterval(vm["swap-interval"].as<int>());
240         w.exec();
241 }