##########################################################################/
-import base
-import glapi
-
-
+"""GL retracer generator."""
-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)
+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<const Trace::Array *>(&%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_pointer(self, pointer, lvalue, rvalue):
- # FIXME
- raise NotImplementedError
-
- def visit_handle(self, handle, lvalue, rvalue):
- self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue));
- print ' if (verbosity >= 2)'
- print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
-
- 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)((%s).string());' % (lvalue, string.expr, rvalue)
-
-
-
-class ValueWrapper(base.Visitor):
-
- def visit_literal(self, literal, lvalue, rvalue):
- pass
-
- def visit_alias(self, alias, lvalue, rvalue):
- self.visit(alias.type, lvalue, rvalue)
-
- def visit_enum(self, enum, lvalue, rvalue):
- pass
-
- def visit_bitmask(self, bitmask, lvalue, rvalue):
- pass
-
- def visit_array(self, array, lvalue, rvalue):
- print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
- print ' if (__a%s) {' % (array.id)
- length = '__a%s->values.size()' % array.id
- 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:
- print ' }'
- print ' }'
-
- def visit_pointer(self, pointer, lvalue, rvalue):
- # FIXME
- raise NotImplementedError
-
- def visit_handle(self, handle, lvalue, rvalue):
- print " __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
- print ' if (verbosity >= 2)'
- print ' std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
-
- def visit_blob(self, blob, lvalue, rvalue):
- pass
-
- def visit_string(self, string, lvalue, rvalue):
- pass
-
-
-
-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_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])
- if function.type is not base.Void:
- print ' %s __result;' % (function.type)
- print ' __result = %s(%s);' % (function.name, arg_names)
- else:
- print ' %s(%s);' % (function.name, arg_names)
- for arg in function.args:
- if arg.output:
- arg_type = ConstRemover().visit(arg.type)
- rvalue = 'call.arg("%s")' % (arg.name,)
- lvalue = arg.name
- try:
- ValueWrapper().visit(arg_type, lvalue, rvalue)
- except NotImplementedError:
- print ' // FIXME: %s' % arg.name
- if function.type is not base.Void:
- rvalue = '*call.ret'
- lvalue = '__result'
- try:
- ValueWrapper().visit(function.type, lvalue, rvalue)
- except NotImplementedError:
- print ' // FIXME: result'
- 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;'
print ' }'
- print
- print ' if (verbosity >=1 ) {'
- print ' std::cout << call;'
- print ' std::cout.flush();'
- print ' };'
- print
- for function in functions:
- if function.sideeffects:
- print ' if (call.name == "%s") {' % function.name
- print ' retrace_%s(call);' % 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 ' std::cerr << "warning: unknown call " << call.name << "\\n";'
- print ' return false;'
- print '}'
- print
+ 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(true));' % (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 ' }'
-def retrace_api(api):
- types = api.all_types()
- handles = [type for type in types if isinstance(type, base.Handle)]
- for handle in handles:
- print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
- print
+if __name__ == '__main__':
+ print r'''
+#include <string.h>
- retrace_functions(api.functions)
+#include "glproc.hpp"
+#include "glretrace.hpp"
-if __name__ == '__main__':
- print
- print '#include <stdlib.h>'
- print '#include <string.h>'
- print
- print '#ifdef WIN32'
- print '#include <windows.h>'
- print '#endif'
- print
- print '#include <GL/glew.h>'
- print '#include <GL/glut.h>'
- print
- print '#include "trace_parser.hpp"'
- print
- print 'unsigned verbosity = 0;'
- print
- retrace_api(glapi.glapi)
- print '''
-
-Trace::Parser parser;
-
-static bool insideGlBeginEnd;
-
-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);
-
- if (call->name == "glBegin") {
- insideGlBeginEnd = true;
- }
-
- if (call->name == "glEnd") {
- insideGlBeginEnd = false;
- }
-
- if (!insideGlBeginEnd) {
- 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_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";
- }
- }
- }
-
- 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);
-
- 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, "-v")) {
- ++verbosity;
- } else {
- std::cerr << "error: unknown option " << arg << "\\n";
- return 1;
- }
- }
-
- for ( ; i < argc; ++i) {
- if (parser.open(argv[i])) {
- glutMainLoop();
- parser.close();
- }
- }
-
- return 0;
-}
-
-'''
+'''
+ api = glapi.glapi
+ api.add_function(glapi.memcpy)
+ retracer = GlRetracer()
+ retracer.retrace_api(api)