1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
29 #include "retrace.hpp"
31 #include "glstate.hpp"
32 #include "glretrace.hpp"
33 #include "os_time.hpp"
38 bool insideList = false;
39 bool insideGlBeginEnd = false;
46 const trace::FunctionSig *sig;
51 static bool firstFrame = true;
52 static std::list<CallQuery> callQueries;
53 static const int maxActiveCallQueries = 128;
54 static std::map<glws::Context*, GLuint> activePrograms;
58 checkGlError(trace::Call &call) {
59 GLenum error = glGetError();
60 if (error == GL_NO_ERROR) {
64 std::ostream & os = retrace::warning(call);
72 os << "GL_INVALID_ENUM";
74 case GL_INVALID_VALUE:
75 os << "GL_INVALID_VALUE";
77 case GL_INVALID_OPERATION:
78 os << "GL_INVALID_OPERATION";
80 case GL_STACK_OVERFLOW:
81 os << "GL_STACK_OVERFLOW";
83 case GL_STACK_UNDERFLOW:
84 os << "GL_STACK_UNDERFLOW";
86 case GL_OUT_OF_MEMORY:
87 os << "GL_OUT_OF_MEMORY";
89 case GL_INVALID_FRAMEBUFFER_OPERATION:
90 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
92 case GL_TABLE_TOO_LARGE:
93 os << "GL_TABLE_TOO_LARGE";
105 GLuint64 timestamp = 0;
107 if (retrace::profilingGpuTimes) {
108 glGenQueries(1, &query);
109 glQueryCounter(query, GL_TIMESTAMP);
110 glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
111 glDeleteQueries(1, &query);
119 if (retrace::profilingCpuTimes) {
120 return os::getTime() * (1.0E9 / os::timeFrequency);
127 completeCallQuery(CallQuery& query) {
128 /* Get call start and duration */
129 GLuint64 timestamp = 0, duration = 0, samples = 0;
131 if (retrace::profilingGpuTimes) {
132 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
133 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
136 if (retrace::profilingPixelsDrawn) {
137 glGetQueryObjectui64vEXT(query.ids[2], GL_QUERY_RESULT, &samples);
140 glDeleteQueries(3, query.ids);
142 /* Add call to profile */
143 retrace::profiler.addCall(query.call, query.sig->name, query.program, samples, timestamp, duration, query.start, query.duration);
148 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
149 completeCallQuery(*itr);
155 void setActiveProgram(GLuint program)
157 activePrograms[glretrace::currentContext] = program;
163 std::map<glws::Context*, GLuint>::iterator it;
164 it = activePrograms.find(glretrace::currentContext);
165 if (it == activePrograms.end())
172 beginProfile(trace::Call &call) {
174 const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
175 if (!glws::checkExtension("GL_ARB_timer_query", extensions)) {
176 std::cout << "Error: Cannot run profile, GL_ARB_timer_query extension is not supported." << std::endl;
183 /* Ensure we don't have TOO many queries waiting for results */
184 if (callQueries.size() >= maxActiveCallQueries) {
185 completeCallQuery(callQueries.front());
186 callQueries.pop_front();
189 /* Create call query */
191 query.call = call.no;
192 query.sig = call.sig;
193 query.program = getActiveProgram();
195 glGenQueries(3, query.ids);
197 if (retrace::profilingGpuTimes) {
198 glQueryCounter(query.ids[0], GL_TIMESTAMP);
199 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
202 if (retrace::profilingPixelsDrawn) {
203 glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
206 callQueries.push_back(query);
208 if (retrace::profilingCpuTimes) {
209 query.start = os::getTime();
214 endProfile(trace::Call &call) {
215 if (retrace::profilingCpuTimes) {
216 CallQuery& query = callQueries.back();
217 query.duration = (os::getTime() - query.start) * (1.0E9 / os::timeFrequency);
220 if (retrace::profilingGpuTimes) {
221 glEndQuery(GL_TIME_ELAPSED);
224 if (retrace::profilingPixelsDrawn) {
225 glEndQuery(GL_SAMPLES_PASSED);
233 if (retrace::profiling) {
234 retrace::profiler.addFrameStart(retrace::frameNo, getGpuTimestamp(), getCpuTimestamp());
239 frame_complete(trace::Call &call) {
240 if (retrace::profiling) {
241 /* Complete any remaining queries */
244 /* Indicate end of current frame */
245 retrace::profiler.addFrameEnd(getGpuTimestamp(), getCpuTimestamp());
248 retrace::frameComplete(call);
250 /* Indicate start of next frame */
253 if (!currentDrawable) {
257 if (retrace::debug && !currentDrawable->visible) {
258 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
262 } /* namespace glretrace */
266 retrace::setUp(void) {
272 retrace::addCallbacks(retrace::Retracer &retracer)
274 retracer.addCallbacks(glretrace::gl_callbacks);
275 retracer.addCallbacks(glretrace::glx_callbacks);
276 retracer.addCallbacks(glretrace::wgl_callbacks);
277 retracer.addCallbacks(glretrace::cgl_callbacks);
278 retracer.addCallbacks(glretrace::egl_callbacks);
283 retrace::getSnapshot(void) {
284 if (!glretrace::currentDrawable) {
288 return glstate::getDrawBufferImage();
293 retrace::dumpState(std::ostream &os)
295 if (glretrace::insideGlBeginEnd ||
296 !glretrace::currentDrawable ||
297 !glretrace::currentContext) {
301 glstate::dumpCurrentContext(os);
307 retrace::flushRendering(void) {
308 glretrace::flushQueries();
313 retrace::waitForInput(void) {
314 while (glws::processEvents()) {
319 retrace::cleanUp(void) {