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;
60 checkGlError(trace::Call &call) {
61 GLenum error = glGetError();
62 if (error == GL_NO_ERROR) {
66 std::ostream & os = retrace::warning(call);
74 os << "GL_INVALID_ENUM";
76 case GL_INVALID_VALUE:
77 os << "GL_INVALID_VALUE";
79 case GL_INVALID_OPERATION:
80 os << "GL_INVALID_OPERATION";
82 case GL_STACK_OVERFLOW:
83 os << "GL_STACK_OVERFLOW";
85 case GL_STACK_UNDERFLOW:
86 os << "GL_STACK_UNDERFLOW";
88 case GL_OUT_OF_MEMORY:
89 os << "GL_OUT_OF_MEMORY";
91 case GL_INVALID_FRAMEBUFFER_OPERATION:
92 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
94 case GL_TABLE_TOO_LARGE:
95 os << "GL_TABLE_TOO_LARGE";
107 GLuint64 timestamp = 0;
109 if (retrace::profilingGpuTimes && supportsTimestamp) {
110 glGenQueries(1, &query);
111 glQueryCounter(query, GL_TIMESTAMP);
112 glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
113 glDeleteQueries(1, &query);
121 if (retrace::profilingCpuTimes) {
122 return os::getTime() * (1.0E9 / os::timeFrequency);
129 completeCallQuery(CallQuery& query) {
130 /* Get call start and duration */
131 GLuint64 timestamp = 0, duration = 0, samples = 0;
133 if (retrace::profilingGpuTimes) {
134 if (supportsTimestamp) {
135 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
138 if (supportsElapsed) {
139 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
143 if (retrace::profilingPixelsDrawn && supportsOcclusion) {
144 glGetQueryObjectui64vEXT(query.ids[2], GL_QUERY_RESULT, &samples);
147 glDeleteQueries(3, query.ids);
149 /* Add call to profile */
150 retrace::profiler.addCall(query.call, query.sig->name, query.program, samples, timestamp, duration, query.start, query.duration);
155 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
156 completeCallQuery(*itr);
163 beginProfile(trace::Call &call) {
165 /* Check for extension support */
166 const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
169 supportsTimestamp = glws::checkExtension("GL_ARB_timer_query", extensions);
170 supportsElapsed = glws::checkExtension("GL_EXT_timer_query", extensions) || supportsTimestamp;
171 supportsOcclusion = glws::checkExtension("GL_ARB_occlusion_query", extensions);
173 if (retrace::profilingGpuTimes) {
174 if (!supportsTimestamp && !supportsElapsed) {
175 std::cout << "Error: Cannot run profile, GL_EXT_timer_query extension is not supported." << std::endl;
179 glGetQueryiv(GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS, &bits);
182 std::cout << "Error: Cannot run profile, GL_QUERY_COUNTER_BITS == 0." << std::endl;
187 if (retrace::profilingPixelsDrawn && !supportsOcclusion) {
188 std::cout << "Error: Cannot run profile, GL_ARB_occlusion_query extension is not supported." << std::endl;
195 /* Create call query */
197 query.call = call.no;
198 query.sig = call.sig;
199 query.program = glretrace::currentContext ? glretrace::currentContext->activeProgram : 0;
201 glGenQueries(3, query.ids);
203 if (retrace::profilingGpuTimes) {
204 if (supportsTimestamp) {
205 glQueryCounter(query.ids[0], GL_TIMESTAMP);
208 if (supportsElapsed) {
209 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
213 if (retrace::profilingPixelsDrawn && supportsOcclusion) {
214 glBeginQuery(GL_SAMPLES_PASSED, query.ids[2]);
217 if (retrace::profilingCpuTimes) {
218 query.start = os::getTime();
221 callQueries.push_back(query);
225 endProfile(trace::Call &call) {
226 if (retrace::profilingCpuTimes) {
227 CallQuery& query = callQueries.back();
228 query.duration = (os::getTime() - query.start) * (1.0E9 / os::timeFrequency);
231 if (retrace::profilingGpuTimes && supportsElapsed) {
232 glEndQuery(GL_TIME_ELAPSED);
235 if (retrace::profilingPixelsDrawn && supportsOcclusion) {
236 glEndQuery(GL_SAMPLES_PASSED);
244 if (retrace::profiling) {
245 retrace::profiler.addFrameStart(retrace::frameNo, getGpuTimestamp(), getCpuTimestamp());
250 frame_complete(trace::Call &call) {
251 if (retrace::profiling) {
252 /* Complete any remaining queries */
255 /* Indicate end of current frame */
256 retrace::profiler.addFrameEnd(getGpuTimestamp(), getCpuTimestamp());
259 retrace::frameComplete(call);
261 /* Indicate start of next frame */
264 if (!currentDrawable) {
268 if (retrace::debug && !currentDrawable->visible) {
269 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
273 } /* namespace glretrace */
277 retrace::setUp(void) {
283 retrace::addCallbacks(retrace::Retracer &retracer)
285 retracer.addCallbacks(glretrace::gl_callbacks);
286 retracer.addCallbacks(glretrace::glx_callbacks);
287 retracer.addCallbacks(glretrace::wgl_callbacks);
288 retracer.addCallbacks(glretrace::cgl_callbacks);
289 retracer.addCallbacks(glretrace::egl_callbacks);
294 retrace::getSnapshot(void) {
295 if (!glretrace::currentDrawable) {
299 return glstate::getDrawBufferImage();
304 retrace::dumpState(std::ostream &os)
306 if (glretrace::insideGlBeginEnd ||
307 !glretrace::currentDrawable ||
308 !glretrace::currentContext) {
312 glstate::dumpCurrentContext(os);
318 retrace::flushRendering(void) {
319 glretrace::flushQueries();
324 retrace::waitForInput(void) {
325 while (glws::processEvents()) {
330 retrace::cleanUp(void) {