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 supportsElapsed = true;
52 static bool supportsTimestamp = true;
53 static bool supportsOcclusion = true;
55 static bool firstFrame = true;
56 static std::list<CallQuery> callQueries;
57 static std::map<glws::Context*, GLuint> activePrograms;
61 checkGlError(trace::Call &call) {
62 GLenum error = glGetError();
63 if (error == GL_NO_ERROR) {
67 std::ostream & os = retrace::warning(call);
75 os << "GL_INVALID_ENUM";
77 case GL_INVALID_VALUE:
78 os << "GL_INVALID_VALUE";
80 case GL_INVALID_OPERATION:
81 os << "GL_INVALID_OPERATION";
83 case GL_STACK_OVERFLOW:
84 os << "GL_STACK_OVERFLOW";
86 case GL_STACK_UNDERFLOW:
87 os << "GL_STACK_UNDERFLOW";
89 case GL_OUT_OF_MEMORY:
90 os << "GL_OUT_OF_MEMORY";
92 case GL_INVALID_FRAMEBUFFER_OPERATION:
93 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
95 case GL_TABLE_TOO_LARGE:
96 os << "GL_TABLE_TOO_LARGE";
108 GLuint64 timestamp = 0;
110 if (retrace::profilingGpuTimes && supportsTimestamp) {
111 glGenQueries(1, &query);
112 glQueryCounter(query, GL_TIMESTAMP);
113 glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
114 glDeleteQueries(1, &query);
122 if (retrace::profilingCpuTimes) {
123 return os::getTime() * (1.0E9 / os::timeFrequency);
130 completeCallQuery(CallQuery& query) {
131 /* Get call start and duration */
132 GLuint64 timestamp = 0, duration = 0, samples = 0;
134 if (retrace::profilingGpuTimes) {
135 if (supportsTimestamp) {
136 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
139 if (supportsElapsed) {
140 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
144 if (retrace::profilingPixelsDrawn && supportsOcclusion) {
145 glGetQueryObjectui64vEXT(query.ids[2], GL_QUERY_RESULT, &samples);
148 glDeleteQueries(3, query.ids);
150 /* Add call to profile */
151 retrace::profiler.addCall(query.call, query.sig->name, query.program, samples, timestamp, duration, query.start, query.duration);
156 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
157 completeCallQuery(*itr);
163 void setActiveProgram(GLuint program)
165 activePrograms[glretrace::currentContext] = program;
171 std::map<glws::Context*, GLuint>::iterator it;
172 it = activePrograms.find(glretrace::currentContext);
173 if (it == activePrograms.end())
180 beginProfile(trace::Call &call) {
182 /* Check for extension support */
183 const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
186 supportsTimestamp = glws::checkExtension("GL_ARB_timer_query", extensions);
187 supportsElapsed = glws::checkExtension("GL_EXT_timer_query", extensions) || supportsTimestamp;
188 supportsOcclusion = glws::checkExtension("GL_ARB_occlusion_query", extensions);
190 if (retrace::profilingGpuTimes) {
191 if (!supportsTimestamp && !supportsElapsed) {
192 std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
196 glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
199 std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
204 if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
205 std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
212 /* Create call query */
214 query.call = call.no;
215 query.sig = call.sig;
216 query.program = getActiveProgram();
218 glGenQueries(3, query.ids);
220 if (retrace::profilingGpuTimes) {
221 if (supportsTimestamp) {
222 glQueryCounter(query.ids[0], GL_TIMESTAMP);
225 if (supportsElapsed) {
226 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
230 if (retrace::profilingPixelsDrawn && supportsOcclusion) {
231 glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
234 if (retrace::profilingCpuTimes) {
235 query.start = os::getTime();
238 callQueries.push_back(query);
242 endProfile(trace::Call &call) {
243 if (retrace::profilingCpuTimes) {
244 CallQuery& query = callQueries.back();
245 query.duration = (os::getTime() - query.start) * (1.0E9 / os::timeFrequency);
248 if (retrace::profilingGpuTimes && supportsElapsed) {
249 glEndQuery(GL_TIME_ELAPSED);
252 if (retrace::profilingPixelsDrawn && supportsOcclusion) {
253 glEndQuery(GL_SAMPLES_PASSED);
261 if (retrace::profiling) {
262 retrace::profiler.addFrameStart(retrace::frameNo, getGpuTimestamp(), getCpuTimestamp());
267 frame_complete(trace::Call &call) {
268 if (retrace::profiling) {
269 /* Complete any remaining queries */
272 /* Indicate end of current frame */
273 retrace::profiler.addFrameEnd(getGpuTimestamp(), getCpuTimestamp());
276 retrace::frameComplete(call);
278 /* Indicate start of next frame */
281 if (!currentDrawable) {
285 if (retrace::debug && !currentDrawable->visible) {
286 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
290 } /* namespace glretrace */
294 retrace::setUp(void) {
300 retrace::addCallbacks(retrace::Retracer &retracer)
302 retracer.addCallbacks(glretrace::gl_callbacks);
303 retracer.addCallbacks(glretrace::glx_callbacks);
304 retracer.addCallbacks(glretrace::wgl_callbacks);
305 retracer.addCallbacks(glretrace::cgl_callbacks);
306 retracer.addCallbacks(glretrace::egl_callbacks);
311 retrace::getSnapshot(void) {
312 if (!glretrace::currentDrawable) {
316 return glstate::getDrawBufferImage();
321 retrace::dumpState(std::ostream &os)
323 if (glretrace::insideGlBeginEnd ||
324 !glretrace::currentDrawable ||
325 !glretrace::currentContext) {
329 glstate::dumpCurrentContext(os);
335 retrace::flushRendering(void) {
336 glretrace::flushQueries();
341 retrace::waitForInput(void) {
342 while (glws::processEvents()) {
347 retrace::cleanUp(void) {