X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=glretrace.py;h=b775609aa0eb79ca6f4daf538ed258febfcc54f6;hb=124cd34846abf59579fe7cbd819a3a719a07a29f;hp=e73561ddd8bbeb87626672b6e9f70306e72e3162;hpb=6f51d3b71cd318f8337e7f5fe791867fc2713a5b;p=apitrace diff --git a/glretrace.py b/glretrace.py index e73561d..b775609 100644 --- a/glretrace.py +++ b/glretrace.py @@ -24,200 +24,328 @@ ##########################################################################/ -import base -import gl - - - -class ConstRemover(base.Rebuilder): - - def visit_const(self, const): - return const.type - - def visit_opaque(self, opaque): - expr = opaque.expr - if expr.startswith('const '): - expr = expr[6:] - return base.Opaque(expr) +"""GL retracer generator.""" + + +import stdapi +import glapi +from retrace import Retracer + + +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", + "glDrawArraysIndirect", + "glDrawArraysInstanced", + "glDrawArraysInstancedARB", + "glDrawArraysInstancedEXT", + "glDrawMeshArraysSUN", + "glMultiDrawArrays", + "glMultiDrawArraysEXT", + "glMultiModeDrawArraysIBM", + ]) + + draw_elements_function_names = set([ + "glDrawElements", + "glDrawElementsBaseVertex", + "glDrawElementsIndirect", + "glDrawElementsInstanced", + "glDrawElementsInstancedARB", + "glDrawElementsInstancedBaseVertex", + "glDrawElementsInstancedEXT", + "glDrawRangeElements", + "glDrawRangeElementsBaseVertex", + "glDrawRangeElementsEXT", + "glMultiDrawElements", + "glMultiDrawElementsBaseVertex", + "glMultiDrawElementsEXT", + "glMultiModeDrawElementsIBM", + ]) + + 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 ' }' + # 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) {' + print ' return;' + print ' }' -class ValueExtractor(base.Visitor): + # 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 ' }' - def visit_literal(self, literal, lvalue, rvalue): - print ' %s = %s;' % (lvalue, rvalue) + Retracer.retrace_function_body(self, function) - def visit_alias(self, alias, lvalue, rvalue): - self.visit(alias.type, lvalue, rvalue) - - def visit_enum(self, enum, lvalue, rvalue): - print ' %s = %s;' % (lvalue, rvalue) + # Post-snapshots + if function.name in ('glFlush', 'glFinish'): + print ' if (!glretrace::double_buffer) {' + print ' glretrace::frame_complete(call.no);' + print ' }' + 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 ' }' + 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 visit_bitmask(self, bitmask, lvalue, rvalue): - self.visit(bitmask.type, lvalue, rvalue) - def visit_array(self, array, lvalue, rvalue): - print ' const Trace::Array *__a%s = dynamic_cast(&%s);' % (array.id, rvalue) - print ' if (__a%s) {' % (array.id) - length = '__a%s->values.size()' % array.id - print ' %s = new %s[%s];' % (lvalue, array.type, length) - index = '__i' + array.id - print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length) - try: - self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index)) - finally: + 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 ' } else {' - print ' %s = NULL;' % lvalue print ' }' - def visit_blob(self, blob, lvalue, rvalue): - print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue) - - def visit_string(self, string, lvalue, rvalue): - print ' %s = (%s).string();' % (lvalue, rvalue) - - - -def retrace_function(function): - print 'static void retrace_%s(Trace::Call &call) {' % function.name - success = True - for arg in function.args: - arg.type = ConstRemover().visit(arg.type) - print ' %s %s;' % (arg.type, arg.name) - rvalue = 'call.arg("%s")' % (arg.name,) - lvalue = arg.name - try: - ValueExtractor().visit(arg.type, lvalue, rvalue) - except NotImplementedError: - success = False - print ' %s = 0; // FIXME' % arg.name - if not success: - print ' std::cerr << "warning: unsupported call %s\\n";' % function.name - print ' return;' - arg_names = ", ".join([arg.name for arg in function.args]) - print ' %s(%s);' % (function.name, arg_names) - print '}' - print - - -def retrace_functions(functions): - for function in functions: - if function.sideeffects: - retrace_function(function) - - print 'static bool retrace_call(Trace::Call &call) {' - for function in functions: - if not function.sideeffects: - print ' if (call.name == "%s") {' % function.name - print ' return true;' + if function.name == "glEnd": + 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 ' glretrace::insideGlBeginEnd = true;' + elif function.name.startswith('gl'): + # glGetError is not allowed inside glBegin/glEnd + 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 ' }' - print - print ' std::cout << call;' - print ' std::cout.flush();' - print - for function in functions: - if function.sideeffects: - print ' if (call.name == "%s") {' % function.name - print ' retrace_%s(call);' % function.name - print ' return true;' + + def extract_arg(self, function, arg, arg_type, lvalue, rvalue): + if function.name in self.array_pointer_function_names and arg.name == 'pointer': + print ' %s = static_cast<%s>(%s.toPointer());' % (lvalue, arg_type, rvalue) + return + + 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 ' }' - print ' std::cerr << "warning: unsupported call " << call.name << "\\n";' - print ' return false;' - print '}' - print if __name__ == '__main__': - print - print '#include ' - print '#include ' - print '#include ' - print '#include ' - print - print '#include "trace_parser.hpp"' - print - - functions = gl.basic_functions(base.Function) + gl.extended_functions(base.Function) - retrace_functions(functions) - - print ''' - -Trace::Parser parser; - -static void display(void) { - Trace::Call *call; - - while ((call = parser.parse_call())) { - if (call->name == "glFlush()" || - call->name == "glXSwapBuffers" || - call->name == "wglSwapBuffers") { - glFlush(); - return; - } - - retrace_call(*call); - - GLenum error = glGetError(); - if (error != GL_NO_ERROR) { - 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_TABLE_TOO_LARGE: - std::cerr << "GL_TABLE_TOO_LARGE"; - break; - default: - std::cerr << error; - break; - } - std::cerr << "\\n"; - } - } - - glFlush(); - glutIdleFunc(NULL); -} - -static void idle(void) { - glutPostRedisplay(); -} - -int main(int argc, char **argv) -{ - glutInit(&argc, argv); - glutInitWindowPosition(0, 0); - glutInitWindowSize(800, 600); - glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE); - glutCreateWindow(argv[0]); - glewInit(); - - glutDisplayFunc(&display); - glutIdleFunc(&idle); - - for (int i = 1; i < argc; ++i) { - if (parser.open(argv[i])) { - glutMainLoop(); - parser.close(); - } - } - - return 0; -} - -''' + print r''' +#include + +#include "glproc.hpp" +#include "glretrace.hpp" + + +''' + api = glapi.glapi + api.add_function(glapi.memcpy) + retracer = GlRetracer() + retracer.retrace_api(api)