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) {
177 /* Ensure we don't have TOO many queries waiting for results */
178 if (callQueries.size() >= maxActiveCallQueries) {
179 completeCallQuery(callQueries.front());
180 callQueries.pop_front();
183 /* Create call query */
185 query.call = call.no;
186 query.sig = call.sig;
187 query.program = getActiveProgram();
189 glGenQueries(3, query.ids);
191 if (retrace::profilingGpuTimes) {
192 glQueryCounter(query.ids[0], GL_TIMESTAMP);
193 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
196 if (retrace::profilingPixelsDrawn) {
197 glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
200 callQueries.push_back(query);
202 if (retrace::profilingCpuTimes) {
203 query.start = os::getTime();
208 endProfile(trace::Call &call) {
209 if (retrace::profilingCpuTimes) {
210 CallQuery& query = callQueries.back();
211 query.duration = (os::getTime() - query.start) * (1.0E9 / os::timeFrequency);
214 if (retrace::profilingGpuTimes) {
215 glEndQuery(GL_TIME_ELAPSED);
218 if (retrace::profilingPixelsDrawn) {
219 glEndQuery(GL_SAMPLES_PASSED);
227 if (retrace::profiling) {
228 retrace::profiler.addFrameStart(retrace::frameNo, getGpuTimestamp(), getCpuTimestamp());
233 frame_complete(trace::Call &call) {
234 if (retrace::profiling) {
235 /* Complete any remaining queries */
238 /* Indicate end of current frame */
239 retrace::profiler.addFrameEnd(getGpuTimestamp(), getCpuTimestamp());
242 retrace::frameComplete(call);
244 /* Indicate start of next frame */
247 if (!currentDrawable) {
251 if (retrace::debug && !currentDrawable->visible) {
252 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
256 } /* namespace glretrace */
260 retrace::setUp(void) {
266 retrace::addCallbacks(retrace::Retracer &retracer)
268 retracer.addCallbacks(glretrace::gl_callbacks);
269 retracer.addCallbacks(glretrace::glx_callbacks);
270 retracer.addCallbacks(glretrace::wgl_callbacks);
271 retracer.addCallbacks(glretrace::cgl_callbacks);
272 retracer.addCallbacks(glretrace::egl_callbacks);
277 retrace::getSnapshot(void) {
278 if (!glretrace::currentDrawable) {
282 return glstate::getDrawBufferImage();
287 retrace::dumpState(std::ostream &os)
289 if (glretrace::insideGlBeginEnd ||
290 !glretrace::currentDrawable ||
291 !glretrace::currentContext) {
295 glstate::dumpCurrentContext(os);
301 retrace::flushRendering(void) {
302 glretrace::flushQueries();
307 retrace::waitForInput(void) {
308 while (glws::processEvents()) {
313 retrace::cleanUp(void) {