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 std::map<glws::Context*, GLuint> activePrograms;
57 checkGlError(trace::Call &call) {
58 GLenum error = glGetError();
59 if (error == GL_NO_ERROR) {
63 std::ostream & os = retrace::warning(call);
71 os << "GL_INVALID_ENUM";
73 case GL_INVALID_VALUE:
74 os << "GL_INVALID_VALUE";
76 case GL_INVALID_OPERATION:
77 os << "GL_INVALID_OPERATION";
79 case GL_STACK_OVERFLOW:
80 os << "GL_STACK_OVERFLOW";
82 case GL_STACK_UNDERFLOW:
83 os << "GL_STACK_UNDERFLOW";
85 case GL_OUT_OF_MEMORY:
86 os << "GL_OUT_OF_MEMORY";
88 case GL_INVALID_FRAMEBUFFER_OPERATION:
89 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
91 case GL_TABLE_TOO_LARGE:
92 os << "GL_TABLE_TOO_LARGE";
104 GLuint64 timestamp = 0;
106 if (retrace::profilingGpuTimes) {
107 glGenQueries(1, &query);
108 glQueryCounter(query, GL_TIMESTAMP);
109 glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
110 glDeleteQueries(1, &query);
118 if (retrace::profilingCpuTimes) {
119 return os::getTime() * (1.0E9 / os::timeFrequency);
126 completeCallQuery(CallQuery& query) {
127 /* Get call start and duration */
128 GLuint64 timestamp = 0, duration = 0, samples = 0;
130 if (retrace::profilingGpuTimes) {
131 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
132 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
135 if (retrace::profilingPixelsDrawn) {
136 glGetQueryObjectui64vEXT(query.ids[2], GL_QUERY_RESULT, &samples);
139 glDeleteQueries(3, query.ids);
141 /* Add call to profile */
142 retrace::profiler.addCall(query.call, query.sig->name, query.program, samples, timestamp, duration, query.start, query.duration);
147 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
148 completeCallQuery(*itr);
154 void setActiveProgram(GLuint program)
156 activePrograms[glretrace::currentContext] = program;
162 std::map<glws::Context*, GLuint>::iterator it;
163 it = activePrograms.find(glretrace::currentContext);
164 if (it == activePrograms.end())
171 beginProfile(trace::Call &call) {
173 const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
174 if (!glws::checkExtension("GL_ARB_timer_query", extensions)) {
175 std::cout << "Error: Cannot run profile, GL_ARB_timer_query extension is not supported." << std::endl;
182 /* Create call query */
184 query.call = call.no;
185 query.sig = call.sig;
186 query.program = getActiveProgram();
188 glGenQueries(3, query.ids);
190 if (retrace::profilingGpuTimes) {
191 glQueryCounter(query.ids[0], GL_TIMESTAMP);
192 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
195 if (retrace::profilingPixelsDrawn) {
196 glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
199 if (retrace::profilingCpuTimes) {
200 query.start = os::getTime();
203 callQueries.push_back(query);
207 endProfile(trace::Call &call) {
208 if (retrace::profilingCpuTimes) {
209 CallQuery& query = callQueries.back();
210 query.duration = (os::getTime() - query.start) * (1.0E9 / os::timeFrequency);
213 if (retrace::profilingGpuTimes) {
214 glEndQuery(GL_TIME_ELAPSED);
217 if (retrace::profilingPixelsDrawn) {
218 glEndQuery(GL_SAMPLES_PASSED);
226 if (retrace::profiling) {
227 retrace::profiler.addFrameStart(retrace::frameNo, getGpuTimestamp(), getCpuTimestamp());
232 frame_complete(trace::Call &call) {
233 if (retrace::profiling) {
234 /* Complete any remaining queries */
237 /* Indicate end of current frame */
238 retrace::profiler.addFrameEnd(getGpuTimestamp(), getCpuTimestamp());
241 retrace::frameComplete(call);
243 /* Indicate start of next frame */
246 if (!currentDrawable) {
250 if (retrace::debug && !currentDrawable->visible) {
251 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
255 } /* namespace glretrace */
259 retrace::setUp(void) {
265 retrace::addCallbacks(retrace::Retracer &retracer)
267 retracer.addCallbacks(glretrace::gl_callbacks);
268 retracer.addCallbacks(glretrace::glx_callbacks);
269 retracer.addCallbacks(glretrace::wgl_callbacks);
270 retracer.addCallbacks(glretrace::cgl_callbacks);
271 retracer.addCallbacks(glretrace::egl_callbacks);
276 retrace::getSnapshot(void) {
277 if (!glretrace::currentDrawable) {
281 return glstate::getDrawBufferImage();
286 retrace::dumpState(std::ostream &os)
288 if (glretrace::insideGlBeginEnd ||
289 !glretrace::currentDrawable ||
290 !glretrace::currentContext) {
294 glstate::dumpCurrentContext(os);
300 retrace::flushRendering(void) {
301 glretrace::flushQueries();
306 retrace::waitForInput(void) {
307 while (glws::processEvents()) {
312 retrace::cleanUp(void) {