X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=glretrace.py;h=b6dac924ef5b4e350f2ee0321c81fae96696eb15;hb=081e59db6cf60b3b39d6b6ce58b4a1b2ff96020e;hp=a66d5d2bbc86a4a20e55d71ecef252054a7e7870;hpb=7fe1dc57d2249236b83aa1a78001a9245d6e1681;p=apitrace diff --git a/glretrace.py b/glretrace.py index a66d5d2..b6dac92 100644 --- a/glretrace.py +++ b/glretrace.py @@ -37,6 +37,38 @@ class GlRetracer(Retracer): def retrace_function(self, function): Retracer.retrace_function(self, function) + array_pointer_function_names = set(( + "glVertexPointer", + "glNormalPointer", + "glColorPointer", + "glIndexPointer", + "glTexCoordPointer", + "glEdgeFlagPointer", + "glFogCoordPointer", + "glSecondaryColorPointer", + + "glInterleavedArrays", + + "glVertexPointerEXT", + "glNormalPointerEXT", + "glColorPointerEXT", + "glIndexPointerEXT", + "glTexCoordPointerEXT", + "glEdgeFlagPointerEXT", + "glFogCoordPointerEXT", + "glSecondaryColorPointerEXT", + + "glVertexAttribPointer", + "glVertexAttribPointerARB", + "glVertexAttribPointerNV", + "glVertexAttribIPointer", + "glVertexAttribIPointerEXT", + "glVertexAttribLPointer", + "glVertexAttribLPointerEXT", + + #"glMatrixIndexPointerARB", + )) + draw_array_function_names = set([ "glDrawArrays", "glDrawArraysEXT", @@ -67,320 +99,256 @@ class GlRetracer(Retracer): "glMultiModeDrawElementsIBM", ]) - def call_function(self, function): - if (function.name in self.draw_array_function_names or - function.name in self.draw_elements_function_names): - print ' GLint __array_buffer = 0;' - print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);' - print ' if (!__array_buffer) {' - self.fail_function(function) + misc_draw_function_names = set([ + "glClear", + "glEnd", + "glDrawPixels", + "glBlitFramebuffer", + "glBlitFramebufferEXT", + ]) + + bind_framebuffer_function_names = set([ + "glBindFramebuffer", + "glBindFramebufferARB", + "glBindFramebufferEXT", + ]) + + # Names of the functions that can pack into the current pixel buffer + # object. See also the ARB_pixel_buffer_object specification. + pack_function_names = set([ + 'glGetCompressedTexImage', + 'glGetConvolutionFilter', + 'glGetHistogram', + 'glGetMinmax', + 'glGetPixelMapfv', + 'glGetPixelMapuiv', + 'glGetPixelMapusv', + 'glGetPolygonStipple', + 'glGetSeparableFilter,', + 'glGetTexImage', + 'glReadPixels', + 'glGetnCompressedTexImageARB', + 'glGetnConvolutionFilterARB', + 'glGetnHistogramARB', + 'glGetnMinmaxARB', + 'glGetnPixelMapfvARB', + 'glGetnPixelMapuivARB', + 'glGetnPixelMapusvARB', + 'glGetnPolygonStippleARB', + 'glGetnSeparableFilterARB', + 'glGetnTexImageARB', + 'glReadnPixelsARB', + ]) + + def retrace_function_body(self, function): + is_array_pointer = function.name in self.array_pointer_function_names + is_draw_array = function.name in self.draw_array_function_names + is_draw_elements = function.name in self.draw_elements_function_names + is_misc_draw = function.name in self.misc_draw_function_names + + if is_array_pointer or is_draw_array or is_draw_elements: + print ' if (glretrace::parser.version < 1) {' + + if is_array_pointer or is_draw_array: + print ' GLint __array_buffer = 0;' + print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);' + print ' if (!__array_buffer) {' + self.fail_function(function) + print ' }' + + if is_draw_elements: + print ' GLint __element_array_buffer = 0;' + print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);' + print ' if (!__element_array_buffer) {' + self.fail_function(function) + print ' }' + print ' }' - if function.name in self.draw_elements_function_names: - print ' GLint __element_array_buffer = 0;' - print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);' - print ' if (!__element_array_buffer) {' - self.fail_function(function) + # When no pack buffer object is bound, the pack functions are no-ops. + if function.name in self.pack_function_names: + print ' GLint __pack_buffer = 0;' + print ' glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &__pack_buffer);' + print ' if (!__pack_buffer) {' + if function.name == 'glReadPixels': + print ' glFinish();' + print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAME ||' + print ' glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAMEBUFFER) {' + print ' glretrace::snapshot(call.no);' + print ' }' + print ' return;' print ' }' - if function.name == "glViewport": - print ' if (x + width > __window_width) {' - print ' __window_width = x + width;' - print ' __reshape_window = true;' + # Pre-snapshots + if function.name in self.bind_framebuffer_function_names: + print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAMEBUFFER) {' + print ' glretrace::snapshot(call.no - 1);' print ' }' - print ' if (y + height > __window_height) {' - print ' __window_height = y + height;' - print ' __reshape_window = true;' + if function.name == 'glFrameTerminatorGREMEDY': + print ' glretrace::frame_complete(call.no);' + return + + Retracer.retrace_function_body(self, function) + + # Post-snapshots + if function.name in ('glFlush', 'glFinish'): + print ' if (!glretrace::double_buffer) {' + print ' glretrace::frame_complete(call.no);' + print ' }' + if is_draw_array or is_draw_elements or is_misc_draw: + print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_DRAW) {' + print ' glretrace::snapshot(call.no);' + print ' }' + + + def call_function(self, function): + if function.name == "glViewport": + print ' GLint draw_framebuffer = 0;' + print ' glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);' + print ' if (draw_framebuffer == 0) {' + print ' if (glretrace::drawable) {' + print ' int drawable_width = x + width;' + print ' int drawable_height = y + height;' + print ' if (drawable_width > (int)glretrace::drawable->width ||' + print ' drawable_height > (int)glretrace::drawable->height) {' + print ' glretrace::drawable->resize(drawable_width, drawable_height);' + print ' if (!glretrace::drawable->visible) {' + print ' glretrace::drawable->show();' + print ' }' + print ' glScissor(0, 0, drawable_width, drawable_height);' + print ' }' + print ' }' print ' }' if function.name == "glEnd": - print ' insideGlBeginEnd = false;' + print ' glretrace::insideGlBeginEnd = false;' + + if function.name == 'memcpy': + print ' if (!dest || !src || !n) return;' Retracer.call_function(self, function) + # Error checking if function.name == "glBegin": - print ' insideGlBeginEnd = true;' - else: + print ' glretrace::insideGlBeginEnd = true;' + elif function.name.startswith('gl'): # glGetError is not allowed inside glBegin/glEnd - print ' checkGlError();' - - pointer_function_names = set([ - "glColorPointer", - "glColorPointerEXT", - "glEdgeFlagPointer", - "glEdgeFlagPointerEXT", - "glFogCoordPointer", - "glFogCoordPointerEXT", - "glIndexPointer", - "glIndexPointerEXT", - "glMatrixIndexPointerARB", - "glNormalPointer", - "glNormalPointerEXT", - "glSecondaryColorPointer", - "glSecondaryColorPointerEXT", - "glTexCoordPointer", - "glTexCoordPointerEXT", - "glVertexAttribLPointer", - "glVertexAttribPointer", - "glVertexAttribPointerARB", - "glVertexAttribPointerNV", - "glVertexPointer", - "glVertexPointerEXT", - ]) + print ' if (!glretrace::benchmark && !glretrace::insideGlBeginEnd) {' + print ' glretrace::checkGlError(call);' + if function.name in ('glProgramStringARB', 'glProgramStringNV'): + print r' GLint error_position = -1;' + print r' glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_position);' + print r' if (error_position != -1) {' + print r' const char *error_string = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);' + print r' std::cerr << call.no << ": warning: " << error_string << "\n";' + print r' }' + if function.name == 'glCompileShader': + print r' GLint compile_status = 0;' + print r' glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);' + print r' if (!compile_status) {' + print r' GLint info_log_length = 0;' + print r' glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);' + print r' GLchar *infoLog = new GLchar[info_log_length];' + print r' glGetShaderInfoLog(shader, info_log_length, NULL, infoLog);' + print r' std::cerr << call.no << ": warning: " << infoLog << "\n";' + print r' delete [] infoLog;' + print r' }' + if function.name == 'glLinkProgram': + print r' GLint link_status = 0;' + print r' glGetProgramiv(program, GL_LINK_STATUS, &link_status);' + print r' if (!link_status) {' + print r' GLint info_log_length = 0;' + print r' glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);' + print r' GLchar *infoLog = new GLchar[info_log_length];' + print r' glGetProgramInfoLog(program, info_log_length, NULL, infoLog);' + print r' std::cerr << call.no << ": warning: " << infoLog << "\n";' + print r' delete [] infoLog;' + print r' }' + if function.name == 'glCompileShaderARB': + print r' GLint compile_status = 0;' + print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_COMPILE_STATUS_ARB, &compile_status);' + print r' if (!compile_status) {' + print r' GLint info_log_length = 0;' + print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);' + print r' GLchar *infoLog = new GLchar[info_log_length];' + print r' glGetInfoLogARB(shaderObj, info_log_length, NULL, infoLog);' + print r' std::cerr << call.no << ": warning: " << infoLog << "\n";' + print r' delete [] infoLog;' + print r' }' + if function.name == 'glLinkProgramARB': + print r' GLint link_status = 0;' + print r' glGetObjectParameterivARB(programObj, GL_OBJECT_LINK_STATUS_ARB, &link_status);' + print r' if (!link_status) {' + print r' GLint info_log_length = 0;' + print r' glGetObjectParameterivARB(programObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);' + print r' GLchar *infoLog = new GLchar[info_log_length];' + print r' glGetInfoLogARB(programObj, info_log_length, NULL, infoLog);' + print r' std::cerr << call.no << ": warning: " << infoLog << "\n";' + print r' delete [] infoLog;' + print r' }' + if function.name in ('glMapBuffer', 'glMapBufferARB', 'glMapBufferRange', 'glMapNamedBufferEXT', 'glMapNamedBufferRangeEXT'): + print r' if (!__result) {' + print r' std::cerr << call.no << ": warning: failed to map buffer\n";' + print r' }' + if function.name in ('glGetAttribLocation', 'glGetAttribLocationARB'): + print r' GLint __orig_result = call.ret->toSInt();' + print r' if (__result != __orig_result) {' + print r' std::cerr << call.no << ": warning vertex attrib location mismatch " << __orig_result << " -> " << __result << "\n";' + print r' }' + if function.name in ('glCheckFramebufferStatus', 'glCheckFramebufferStatusEXT', 'glCheckNamedFramebufferStatusEXT'): + print r' GLint __orig_result = call.ret->toSInt();' + print r' if (__orig_result == GL_FRAMEBUFFER_COMPLETE &&' + print r' __result != GL_FRAMEBUFFER_COMPLETE) {' + print r' std::cerr << call.no << ": incomplete framebuffer (" << __result << ")\n";' + print r' }' + print ' }' def extract_arg(self, function, arg, arg_type, lvalue, rvalue): - if (function.name in self.pointer_function_names and arg.name == 'pointer' or - function.name in self.draw_elements_function_names and arg.name == 'indices'): - print ' if (dynamic_cast(&%s)) {' % rvalue - print ' %s = 0;' % (lvalue) - print ' } else {' - print ' %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue) - print ' }' + if function.name in self.array_pointer_function_names and arg.name == 'pointer': + print ' %s = static_cast<%s>(%s.toPointer(true));' % (lvalue, arg_type, rvalue) return - if function.name.startswith('glUniform') and function.args[0].name == arg.name == 'location': + if function.name in self.draw_elements_function_names and arg.name == 'indices': + self.extract_opaque_arg(function, arg, arg_type, lvalue, rvalue) + return + + # Handle pointer with offsets into the current pack pixel buffer + # object. + if function.name in self.pack_function_names and arg.output: + self.extract_opaque_arg(function, arg, arg_type, lvalue, rvalue) + return + + if arg.type is glapi.GLlocation \ + and 'program' not in [arg.name for arg in function.args]: print ' GLint program = -1;' print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);' + + if arg.type is glapi.GLlocationARB \ + and 'programObj' not in [arg.name for arg in function.args]: + print ' GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);' Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue) + # Don't try to use more samples than the implementation supports + if arg.name == 'samples': + assert arg.type is glapi.GLsizei + print ' GLint max_samples = 0;' + print ' glGetIntegerv(GL_MAX_SAMPLES, &max_samples);' + print ' if (samples > max_samples) {' + print ' samples = max_samples;' + print ' }' + if __name__ == '__main__': print r''' #include -#include -#include #include "glproc.hpp" -#include - -static bool double_buffer = false; -static bool insideGlBeginEnd = false; - -static int __window_width = 256, __window_height = 256; -bool __reshape_window = false; - -unsigned __frame = 0; -long long __startTime = 0; -static enum { - MODE_DISPLAY = 0, - MODE_SNAPSHOT, - MODE_COMPARE, -} __mode = MODE_DISPLAY; - -const char *__snapshot_prefix = ""; - - -static void -checkGlError(void) { - if (insideGlBeginEnd) { - return; - } - - GLenum error = glGetError(); - if (error == GL_NO_ERROR) { - return; - } - - std::cerr << "warning: glGetError() = "; - switch (error) { - case GL_INVALID_ENUM: - std::cerr << "GL_INVALID_ENUM"; - break; - case GL_INVALID_VALUE: - std::cerr << "GL_INVALID_VALUE"; - break; - case GL_INVALID_OPERATION: - std::cerr << "GL_INVALID_OPERATION"; - break; - case GL_STACK_OVERFLOW: - std::cerr << "GL_STACK_OVERFLOW"; - break; - case GL_STACK_UNDERFLOW: - std::cerr << "GL_STACK_UNDERFLOW"; - break; - case GL_OUT_OF_MEMORY: - std::cerr << "GL_OUT_OF_MEMORY"; - break; - case GL_INVALID_FRAMEBUFFER_OPERATION: - std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION"; - break; - case GL_TABLE_TOO_LARGE: - std::cerr << "GL_TABLE_TOO_LARGE"; - break; - default: - std::cerr << error; - break; - } - std::cerr << "\n"; -} +#include "glretrace.hpp" + + ''' api = glapi.glapi + api.add_function(glapi.memcpy) retracer = GlRetracer() - retracer.retrace_api(glapi.glapi) - print r''' - -static Trace::Parser parser; - -static void display_noop(void) { -} - -#include "image.hpp" - -static void frame_complete(void) { - ++__frame; - - if (!__reshape_window && (__mode == MODE_SNAPSHOT || __mode == MODE_COMPARE)) { - char filename[PATH_MAX]; - - snprintf(filename, sizeof filename, "%s%04u.png", __snapshot_prefix, __frame); - - Image::Image *ref; - if (__mode == MODE_COMPARE) { - ref = Image::readPNG(filename); - if (!ref) { - return; - } - if (verbosity) - std::cout << "Read " << filename << "\n"; - } - - Image::Image src(__window_width, __window_height, true); - glReadPixels(0, 0, __window_width, __window_height, GL_RGBA, GL_UNSIGNED_BYTE, src.pixels); - - if (__mode == MODE_SNAPSHOT) { - if (src.writePNG(filename) && verbosity) { - std::cout << "Wrote " << filename << "\n"; - } - } - - if (__mode == MODE_COMPARE) { - std::cout << "Frame " << __frame << " average precision of " << src.compare(*ref) << " bits\n"; - delete ref; - } - } - -} - -static void display(void) { - Trace::Call *call; - - while ((call = parser.parse_call())) { - if (call->name() == "glFlush") { - glFlush(); - if (!double_buffer) { - frame_complete(); - } - } - - if (!retrace_call(*call)) { - if (call->name() == "glXSwapBuffers" || - call->name() == "wglSwapBuffers") { - if (double_buffer) - glutSwapBuffers(); - else - glFlush(); - frame_complete(); - return; - } - } - } - - // Reached the end of trace - glFlush(); - - long long endTime = OS::GetTime(); - float timeInterval = (endTime - __startTime) * 1.0E-6; - - std::cout << - "Rendered " << __frame << " frames" - " in " << timeInterval << " secs," - " average of " << (__frame/timeInterval) << " fps\n"; - - if (__mode == MODE_DISPLAY) { - glutDisplayFunc(&display_noop); - glutIdleFunc(NULL); - } else { - exit(0); - } -} - -static void idle(void) { - if (__reshape_window) { - // XXX: doesn't quite work - glutReshapeWindow(__window_width, __window_height); - __reshape_window = false; - } - glutPostRedisplay(); -} - -static void usage(void) { - std::cout << - "Usage: glretrace [OPTION] TRACE\n" - "Replay TRACE.\n" - "\n" - " -c compare against snapshots\n" - " -db use a double buffer visual\n" - " -p PREFIX snapshot prefix\n" - " -s take snapshots\n" - " -v verbose output\n"; -} - -int main(int argc, char **argv) -{ - - int i; - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (!strcmp(arg, "--")) { - break; - } else if (!strcmp(arg, "-c")) { - __mode = MODE_COMPARE; - } else if (!strcmp(arg, "-db")) { - double_buffer = true; - } else if (!strcmp(arg, "--help")) { - usage(); - return 0; - } else if (!strcmp(arg, "-p")) { - __snapshot_prefix = argv[++i]; - } else if (!strcmp(arg, "-s")) { - __mode = MODE_SNAPSHOT; - } else if (!strcmp(arg, "-v")) { - ++verbosity; - } else { - std::cerr << "error: unknown option " << arg << "\n"; - usage(); - return 1; - } - } - - glutInit(&argc, argv); - glutInitWindowPosition(0, 0); - glutInitWindowSize(__window_width, __window_height); - glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE)); - glutCreateWindow(argv[0]); - - glutDisplayFunc(&display); - glutIdleFunc(&idle); - - for (GLuint h = 0; h < 1024; ++h) { - __list_map[h] = h; - } - - for ( ; i < argc; ++i) { - if (parser.open(argv[i])) { - __startTime = OS::GetTime(); - glutMainLoop(); - parser.close(); - } - } - - return 0; -} - -''' + retracer.retrace_api(api)