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"
37 bool insideList = false;
38 bool insideGlBeginEnd = false;
42 checkGlError(trace::Call &call) {
43 GLenum error = glGetError();
44 if (error == GL_NO_ERROR) {
48 std::ostream & os = retrace::warning(call);
56 os << "GL_INVALID_ENUM";
58 case GL_INVALID_VALUE:
59 os << "GL_INVALID_VALUE";
61 case GL_INVALID_OPERATION:
62 os << "GL_INVALID_OPERATION";
64 case GL_STACK_OVERFLOW:
65 os << "GL_STACK_OVERFLOW";
67 case GL_STACK_UNDERFLOW:
68 os << "GL_STACK_UNDERFLOW";
70 case GL_OUT_OF_MEMORY:
71 os << "GL_OUT_OF_MEMORY";
73 case GL_INVALID_FRAMEBUFFER_OPERATION:
74 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
76 case GL_TABLE_TOO_LARGE:
77 os << "GL_TABLE_TOO_LARGE";
90 const trace::FunctionSig *sig;
93 static std::vector<CallQuery> callQueries;
94 static GLuint frameQueries[2] = { 0, 0 };
97 if (retrace::profileGPU) {
98 glGenQueries(2, frameQueries);
100 /* Query frame start time */
101 glQueryCounter(frameQueries[0], GL_TIMESTAMP);
105 void frame_complete(trace::Call &call) {
106 if (retrace::profileGPU) {
107 /* Query frame end time */
108 glQueryCounter(frameQueries[1], GL_TIMESTAMP);
113 retrace::frameComplete(call);
115 /* Indicate start of next frame */
118 if (!currentDrawable) {
122 if (retrace::debug && !currentDrawable->visible) {
123 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
130 if (callQueries.size() == 0)
134 GLuint64 frameBegin, frameEnd;
136 /* Wait for frame to finish */
138 glGetQueryObjectiv(frameQueries[1], GL_QUERY_RESULT_AVAILABLE, &available);
141 /* Get frame start and end */
142 glGetQueryObjectui64vEXT(frameQueries[0], GL_QUERY_RESULT, &frameBegin);
143 glGetQueryObjectui64vEXT(frameQueries[1], GL_QUERY_RESULT, &frameEnd);
144 glDeleteQueries(2, frameQueries);
146 /* Add frame to profile */
147 retrace::profiler.addFrame(trace::Profiler::Frame(retrace::frameNo, frameBegin, frameEnd - frameBegin));
149 /* Loop through all active call queries */
150 for (std::vector<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
151 CallQuery& query = *itr;
152 GLuint64 timestamp, duration;
154 /* Get queue start and duration */
155 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
156 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
157 glDeleteQueries(2, query.ids);
159 /* Add call to profile */
160 retrace::profiler.addCall(trace::Profiler::Call(query.call, query.sig->name, timestamp, duration));
167 beginProfileGPU(trace::Call &call) {
168 if (frameQueries[0] == 0) {
173 query.call = call.no;
174 query.sig = call.sig;
176 /* Create start and duration queries */
177 glGenQueries(2, query.ids);
178 glQueryCounter(query.ids[0], GL_TIMESTAMP);
179 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
181 callQueries.push_back(query);
185 endProfileGPU(trace::Call &call) {
186 glEndQuery(GL_TIME_ELAPSED);
189 } /* namespace glretrace */
193 retrace::setUp(void) {
199 retrace::addCallbacks(retrace::Retracer &retracer)
201 retracer.addCallbacks(glretrace::gl_callbacks);
202 retracer.addCallbacks(glretrace::glx_callbacks);
203 retracer.addCallbacks(glretrace::wgl_callbacks);
204 retracer.addCallbacks(glretrace::cgl_callbacks);
205 retracer.addCallbacks(glretrace::egl_callbacks);
210 retrace::getSnapshot(void) {
211 if (!glretrace::currentDrawable) {
215 return glstate::getDrawBufferImage();
220 retrace::dumpState(std::ostream &os)
222 if (glretrace::insideGlBeginEnd ||
223 !glretrace::currentDrawable ||
224 !glretrace::currentContext) {
228 glstate::dumpCurrentContext(os);
234 retrace::flushRendering(void) {
239 retrace::waitForInput(void) {
240 while (glws::processEvents()) {
245 retrace::cleanUp(void) {