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;
44 const trace::FunctionSig *sig;
47 static bool firstFrame = true;
48 static std::list<CallQuery> callQueries;
49 static const int maxActiveCallQueries = 100;
53 checkGlError(trace::Call &call) {
54 GLenum error = glGetError();
55 if (error == GL_NO_ERROR) {
59 std::ostream & os = retrace::warning(call);
67 os << "GL_INVALID_ENUM";
69 case GL_INVALID_VALUE:
70 os << "GL_INVALID_VALUE";
72 case GL_INVALID_OPERATION:
73 os << "GL_INVALID_OPERATION";
75 case GL_STACK_OVERFLOW:
76 os << "GL_STACK_OVERFLOW";
78 case GL_STACK_UNDERFLOW:
79 os << "GL_STACK_UNDERFLOW";
81 case GL_OUT_OF_MEMORY:
82 os << "GL_OUT_OF_MEMORY";
84 case GL_INVALID_FRAMEBUFFER_OPERATION:
85 os << "GL_INVALID_FRAMEBUFFER_OPERATION";
87 case GL_TABLE_TOO_LARGE:
88 os << "GL_TABLE_TOO_LARGE";
100 GLuint64 timestamp = 0;
102 glGenQueries(1, &query);
103 glQueryCounter(query, GL_TIMESTAMP);
104 glGetQueryObjectui64vEXT(query, GL_QUERY_RESULT, ×tamp);
105 glDeleteQueries(1, &query);
111 completeCallQuery(CallQuery& query) {
112 /* Get call start and duration */
113 GLuint64 timestamp = 0, duration = 0;
114 glGetQueryObjectui64vEXT(query.ids[0], GL_QUERY_RESULT, ×tamp);
115 glGetQueryObjectui64vEXT(query.ids[1], GL_QUERY_RESULT, &duration);
116 glDeleteQueries(2, query.ids);
118 /* Add call to profile */
119 retrace::profiler.addCall(query.call, query.sig->name, timestamp, duration);
124 for (std::list<CallQuery>::iterator itr = callQueries.begin(); itr != callQueries.end(); ++itr) {
125 completeCallQuery(*itr);
132 beginProfileGPU(trace::Call &call) {
137 /* Ensure we don't have TOO many queries waiting for results */
138 if (callQueries.size() >= maxActiveCallQueries) {
139 completeCallQuery(callQueries.front());
140 callQueries.pop_front();
143 /* Create call query */
145 query.call = call.no;
146 query.sig = call.sig;
148 glGenQueries(2, query.ids);
149 glQueryCounter(query.ids[0], GL_TIMESTAMP);
150 glBeginQuery(GL_TIME_ELAPSED, query.ids[1]);
152 callQueries.push_back(query);
156 endProfileGPU(trace::Call &call) {
157 glEndQuery(GL_TIME_ELAPSED);
164 if (retrace::profileGPU) {
165 retrace::profiler.addFrameStart(retrace::frameNo, getTimestamp());
170 frame_complete(trace::Call &call) {
171 if (retrace::profileGPU) {
172 /* Complete any remaining queries */
175 /* Indicate end of current frame */
176 retrace::profiler.addFrameEnd(getTimestamp());
179 retrace::frameComplete(call);
181 /* Indicate start of next frame */
184 if (!currentDrawable) {
188 if (retrace::debug && !currentDrawable->visible) {
189 retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
193 } /* namespace glretrace */
197 retrace::setUp(void) {
203 retrace::addCallbacks(retrace::Retracer &retracer)
205 retracer.addCallbacks(glretrace::gl_callbacks);
206 retracer.addCallbacks(glretrace::glx_callbacks);
207 retracer.addCallbacks(glretrace::wgl_callbacks);
208 retracer.addCallbacks(glretrace::cgl_callbacks);
209 retracer.addCallbacks(glretrace::egl_callbacks);
214 retrace::getSnapshot(void) {
215 if (!glretrace::currentDrawable) {
219 return glstate::getDrawBufferImage();
224 retrace::dumpState(std::ostream &os)
226 if (glretrace::insideGlBeginEnd ||
227 !glretrace::currentDrawable ||
228 !glretrace::currentContext) {
232 glstate::dumpCurrentContext(os);
238 retrace::flushRendering(void) {
239 glretrace::flushQueries();
244 retrace::waitForInput(void) {
245 while (glws::processEvents()) {
250 retrace::cleanUp(void) {