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 const int maxActiveCallQueries = 256;
94 static std::list<CallQuery> callQueries;
95 static bool firstFrame = true;
102 glGenQueries(1, &query);
104 glQueryCounter(query, GL_TIMESTAMP);
105 glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
107 glDeleteQueries(1, &query);
113 completeCallQuery(CallQuery& query) {
114 /* Get call start and duration */
115 GLuint64 timestamp, duration;
116 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
117 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
118 glDeleteQueries(2, query.ids);
120 /* Add call to profile */
121 retrace::profiler.addCall(query.call, query.sig->name, timestamp, duration);
125 beginProfileGPU(trace::Call &call) {
130 /* Ensure we don't have TOO many queries waiting for results */
131 if (callQueries.size() >= maxActiveCallQueries) {
132 completeCallQuery(callQueries.front());
133 callQueries.pop_front();
136 /* Create call query */
138 query.call = call.no;
139 query.sig = call.sig;
141 glGenQueries(2, query.ids);
142 glQueryCounter(query.ids[0], GL_TIMESTAMP);
143 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
145 callQueries.push_back(query);
149 endProfileGPU(trace::Call &call) {
150 glEndQuery(GL_TIME_ELAPSED);
157 if (retrace::profileGPU) {
158 retrace::profiler.addFrameStart(retrace::frameNo, getTimestamp());
163 frame_complete(trace::Call &call) {
164 if (retrace::profileGPU) {
165 /* Complete any remaining queries */
166 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
167 completeCallQuery(*itr);
172 /* Indicate end of current frame */
173 retrace::profiler.addFrameEnd(getTimestamp());
176 retrace::frameComplete(call);
178 /* Indicate start of next frame */
181 if (!currentDrawable) {
185 if (retrace::debug && !currentDrawable->visible) {
186 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
190 } /* namespace glretrace */
194 retrace::setUp(void) {
200 retrace::addCallbacks(retrace::Retracer &retracer)
202 retracer.addCallbacks(glretrace::gl_callbacks);
203 retracer.addCallbacks(glretrace::glx_callbacks);
204 retracer.addCallbacks(glretrace::wgl_callbacks);
205 retracer.addCallbacks(glretrace::cgl_callbacks);
206 retracer.addCallbacks(glretrace::egl_callbacks);
211 retrace::getSnapshot(void) {
212 if (!glretrace::currentDrawable) {
216 return glstate::getDrawBufferImage();
221 retrace::dumpState(std::ostream &os)
223 if (glretrace::insideGlBeginEnd ||
224 !glretrace::currentDrawable ||
225 !glretrace::currentContext) {
229 glstate::dumpCurrentContext(os);
235 retrace::flushRendering(void) {
240 retrace::waitForInput(void) {
241 while (glws::processEvents()) {
246 retrace::cleanUp(void) {