]> git.cworth.org Git - apitrace/blobdiff - gltrace.py
Generate a .dmg package on Mac OS X.
[apitrace] / gltrace.py
index 3f6311671a730a3f646b5029a76eeb846f9deb63..dc9c0978a8ec41e07394af55574057feefc37624 100644 (file)
@@ -29,6 +29,7 @@
 
 import stdapi
 import glapi
+import glparams
 from glxapi import glxapi
 from trace import Tracer, dump_instance
 
@@ -80,14 +81,6 @@ class TypeGetter(stdapi.Visitor):
 
 class GlTracer(Tracer):
 
-    def header(self, api):
-        Tracer.header(self, api)
-        self.state_tracker_decl(api)
-
-    def footer(self, api):
-        Tracer.footer(self, api)
-        self.state_tracker_impl(api)
-
     arrays = [
         ("Vertex", "VERTEX"),
         ("Normal", "NORMAL"),
@@ -100,10 +93,19 @@ class GlTracer(Tracer):
     ]
     arrays.reverse()
 
-    def state_tracker_decl(self, api):
+    def header(self, api):
+        Tracer.header(self, api)
+
+        print '// Whether user arrays were used'
+        print 'static bool __user_arrays = false;'
+        print
         # Whether we need user arrays
         print 'static inline bool __need_user_arrays(void)'
         print '{'
+        print '    if (!__user_arrays) {'
+        print '        return false;'
+        print '    }'
+        print
 
         for camelcase_name, uppercase_name in self.arrays:
             function_name = 'gl%sPointer' % camelcase_name
@@ -144,7 +146,77 @@ class GlTracer(Tracer):
 
         print 'static void __trace_user_arrays(GLuint maxindex);'
         print
-    
+
+        print 'struct buffer_mapping {'
+        print '    void *map;'
+        print '    GLint length;'
+        print '    bool write;'
+        print '    bool explicit_flush;'
+        print '};'
+        print
+        for target in self.buffer_targets:
+            print 'struct buffer_mapping __%s_mapping;' % target.lower();
+        print
+        print 'static inline struct buffer_mapping *'
+        print 'get_buffer_mapping(GLenum target) {'
+        print '    switch(target) {'
+        for target in self.buffer_targets:
+            print '    case GL_%s:' % target
+            print '        return & __%s_mapping;' % target.lower()
+        print '    default:'
+        print '        OS::DebugMessage("warning: unknown buffer target 0x%04X\\n", target);'
+        print '        return NULL;'
+        print '    }'
+        print '}'
+        print
+
+        # Generate memcpy's signature
+        self.trace_function_decl(glapi.memcpy)
+
+        # Generate a helper function to determine whether a parameter name
+        # refers to a symbolic value or not
+        print 'static bool'
+        print 'is_symbolic_pname(GLenum pname) {'
+        print '    switch(pname) {'
+        for function, type, count, name in glparams.parameters:
+            if type is glapi.GLenum:
+                print '    case %s:' % name
+        print '        return true;'
+        print '    default:'
+        print '        return false;'
+        print '    }'
+        print '}'
+        print
+        
+        # Generate a helper function to determine whether a parameter value is
+        # potentially symbolic or not; i.e., if the value can be represented in
+        # an enum or not
+        print 'template<class T>'
+        print 'static inline bool'
+        print 'is_symbolic_param(T param) {'
+        print '    return static_cast<T>(static_cast<GLenum>(param)) == param;'
+        print '}'
+        print
+
+        # Generate a helper function to know how many elements a parameter has
+        print 'static size_t'
+        print '__gl_param_size(GLenum pname) {'
+        print '    switch(pname) {'
+        for function, type, count, name in glparams.parameters:
+            if type is not None:
+                print '    case %s: return %u;' % (name, count)
+        print '    case GL_COMPRESSED_TEXTURE_FORMATS: {'
+        print '            GLint num_compressed_texture_formats = 0;'
+        print '            __glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_compressed_texture_formats);'
+        print '            return num_compressed_texture_formats;'
+        print '        }'
+        print '    default:'
+        print r'        OS::DebugMessage("warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);'
+        print '        return 1;'
+        print '    }'
+        print '}'
+        print
+
     array_pointer_function_names = set((
         "glVertexPointer",
         "glNormalPointer",
@@ -155,18 +227,22 @@ class GlTracer(Tracer):
         "glFogCoordPointer",
         "glSecondaryColorPointer",
         
-        #"glVertexPointerEXT",
-        #"glNormalPointerEXT",
-        #"glColorPointerEXT",
-        #"glIndexPointerEXT",
-        #"glTexCoordPointerEXT",
-        #"glEdgeFlagPointerEXT",
-        #"glFogCoordPointerEXT",
-        #"glSecondaryColorPointerEXT",
+        "glInterleavedArrays",
+
+        "glVertexPointerEXT",
+        "glNormalPointerEXT",
+        "glColorPointerEXT",
+        "glIndexPointerEXT",
+        "glTexCoordPointerEXT",
+        "glEdgeFlagPointerEXT",
+        "glFogCoordPointerEXT",
+        "glSecondaryColorPointerEXT",
 
         "glVertexAttribPointer",
         "glVertexAttribPointerARB",
         "glVertexAttribPointerNV",
+        "glVertexAttribIPointer",
+        "glVertexAttribIPointerEXT",
         "glVertexAttribLPointer",
         "glVertexAttribLPointerEXT",
         
@@ -177,6 +253,27 @@ class GlTracer(Tracer):
         'glDrawArrays',
         'glDrawElements',
         'glDrawRangeElements',
+        'glMultiDrawArrays',
+        'glMultiDrawElements',
+        'glDrawArraysInstanced',
+        'glDrawElementsInstanced',
+        'glDrawArraysInstancedARB',
+        'glDrawElementsInstancedARB',
+        'glDrawElementsBaseVertex',
+        'glDrawRangeElementsBaseVertex',
+        'glDrawElementsInstancedBaseVertex',
+        'glMultiDrawElementsBaseVertex',
+        'glDrawArraysIndirect',
+        'glDrawElementsIndirect',
+        'glDrawArraysEXT',
+        'glDrawRangeElementsEXT',
+        'glDrawRangeElementsEXT_size',
+        'glMultiDrawArraysEXT',
+        'glMultiDrawElementsEXT',
+        'glMultiModeDrawArraysIBM',
+        'glMultiModeDrawElementsIBM',
+        'glDrawArraysInstancedEXT',
+        'glDrawElementsInstancedEXT',
     ))
 
     interleaved_formats = [
@@ -202,7 +299,50 @@ class GlTracer(Tracer):
             print '    GLint __array_buffer = 0;'
             print '    __glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
             print '    if (!__array_buffer) {'
+            print '        __user_arrays = true;'
             self.dispatch_function(function)
+
+            # And also break down glInterleavedArrays into the individual calls
+            if function.name == 'glInterleavedArrays':
+                print
+
+                # Initialize the enable flags
+                for camelcase_name, uppercase_name in self.arrays:
+                    flag_name = '__' + uppercase_name.lower()
+                    print '        GLboolean %s = GL_FALSE;' % flag_name
+                print
+
+                # Switch for the interleaved formats
+                print '        switch (format) {'
+                for format in self.interleaved_formats:
+                    print '            case %s:' % format
+                    for camelcase_name, uppercase_name in self.arrays:
+                        flag_name = '__' + uppercase_name.lower()
+                        if format.find('_' + uppercase_name[0]) >= 0:
+                            print '                %s = GL_TRUE;' % flag_name
+                    print '                break;'
+                print '            default:'
+                print '               return;'
+                print '        }'
+                print
+
+                # Emit fake glEnableClientState/glDisableClientState flags
+                for camelcase_name, uppercase_name in self.arrays:
+                    flag_name = '__' + uppercase_name.lower()
+                    enable_name = 'GL_%s_ARRAY' % uppercase_name
+
+                    # Emit a fake function
+                    print '        {'
+                    print '            static const Trace::FunctionSig &__sig = %s ? __glEnableClientState_sig : __glDisableClientState_sig;' % flag_name
+                    print '            unsigned __call = __writer.beginEnter(&__sig);'
+                    print '            __writer.beginArg(0);'
+                    dump_instance(glapi.GLenum, enable_name)
+                    print '            __writer.endArg();'
+                    print '            __writer.endEnter();'
+                    print '            __writer.beginLeave(__call);'
+                    print '            __writer.endLeave();'
+                    print '        }'
+
             print '        return;'
             print '    }'
 
@@ -214,55 +354,118 @@ class GlTracer(Tracer):
             print '        __trace_user_arrays(maxindex);'
             print '    }'
         
-        # And also break down glInterleavedArrays into the individual calls
-        if function.name == 'glInterleavedArrays':
-            print '    GLint __array_buffer = 0;'
-            print '    __glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
-            print '    if (!__array_buffer) {'
-            self.dispatch_function(function)
-            print
+        # Emit a fake memcpy on buffer uploads
+        if function.name in ('glUnmapBuffer', 'glUnmapBufferARB', ):
+            print '    struct buffer_mapping *mapping = get_buffer_mapping(target);'
+            print '    if (mapping && mapping->write && !mapping->explicit_flush) {'
+            self.emit_memcpy('mapping->map', 'mapping->map', 'mapping->length')
+            print '    }'
+        if function.name in ('glFlushMappedBufferRange', 'glFlushMappedBufferRangeAPPLE'):
+            # TODO: avoid copying [0, offset] bytes
+            print '    struct buffer_mapping *mapping = get_buffer_mapping(target);'
+            print '    if (mapping) {'
+            if function.name.endswith('APPLE'):
+                 print '        GLsizeiptr length = size;'
+                 print '        mapping->explicit_flush = true;'
+            print '        //assert(offset + length <= mapping->length);'
+            self.emit_memcpy('mapping->map', 'mapping->map', 'offset + length')
+            print '    }'
+        # FIXME: glFlushMappedNamedBufferRangeEXT
+
+        # Don't leave vertex attrib locations to chance.  Instead emit fake
+        # glBindAttribLocation calls to ensure that the same locations will be
+        # used when retracing.  Trying to remap locations after the fact would
+        # be an herculian task given that vertex attrib locations appear in
+        # many entry-points, including non-shader related ones.
+        if function.name == 'glLinkProgram':
+            Tracer.dispatch_function(self, function)
+            print '    GLint active_attributes = 0;'
+            print '    __glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active_attributes);'
+            print '    for (GLuint attrib = 0; attrib < active_attributes; ++attrib) {'
+            print '        GLint size = 0;'
+            print '        GLenum type = 0;'
+            print '        GLchar name[256];'
+            # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
+            print '        __glGetActiveAttrib(program, attrib, sizeof name, NULL, &size, &type, name);'
+            print "        if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
+            print '            GLint location = __glGetAttribLocation(program, name);'
+            print '            if (location >= 0) {'
+            bind_function = glapi.glapi.get_function_by_name('glBindAttribLocation')
+            self.fake_call(bind_function, ['program', 'location', 'name'])
+            print '            }'
+            print '        }'
+            print '    }'
+        if function.name == 'glLinkProgramARB':
+            Tracer.dispatch_function(self, function)
+            print '    GLint active_attributes = 0;'
+            print '    __glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &active_attributes);'
+            print '    for (GLuint attrib = 0; attrib < active_attributes; ++attrib) {'
+            print '        GLint size = 0;'
+            print '        GLenum type = 0;'
+            print '        GLcharARB name[256];'
+            # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
+            print '        __glGetActiveAttribARB(programObj, attrib, sizeof name, NULL, &size, &type, name);'
+            print "        if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
+            print '            GLint location = __glGetAttribLocationARB(programObj, name);'
+            print '            if (location >= 0) {'
+            bind_function = glapi.glapi.get_function_by_name('glBindAttribLocationARB')
+            self.fake_call(bind_function, ['programObj', 'location', 'name'])
+            print '            }'
+            print '        }'
+            print '    }'
 
-            # Initialize the enable flags
-            for camelcase_name, uppercase_name in self.arrays:
-                flag_name = '__' + uppercase_name.lower()
-                print '        GLboolean %s = GL_FALSE;' % flag_name
-            print
+        Tracer.trace_function_impl_body(self, function)
 
-            # Swicth for the interleaved formats
-            print '        switch (format) {'
-            for format in self.interleaved_formats:
-                print '            case %s:' % format
-                for camelcase_name, uppercase_name in self.arrays:
-                    flag_name = '__' + uppercase_name.lower()
-                    if format.find('_' + uppercase_name[0]) >= 0:
-                        print '                %s = GL_TRUE;' % flag_name
-                print '                break;'
-            print '            default:'
-            print '               return;'
-            print '        }'
-            print
+    def dispatch_function(self, function):
+        if function.name in ('glLinkProgram', 'glLinkProgramARB'):
+            # These functions have been dispatched already
+            return
 
-            # Emit fake glEnableClientState/glDisableClientState flags
-            for camelcase_name, uppercase_name in self.arrays:
-                flag_name = '__' + uppercase_name.lower()
-                enable_name = 'GL_%s_ARRAY' % uppercase_name
-
-                # Emit a fake function
-                print '        {'
-                print '            static const Trace::FunctionSig &__sig = %s ? __glEnableClientState_sig : __glDisableClientState_sig;' % flag_name
-                print '            unsigned __call = Trace::BeginEnter(__sig);'
-                print '            Trace::BeginArg(0);'
-                dump_instance(glapi.GLenum, enable_name)
-                print '            Trace::EndArg();'
-                print '            Trace::EndEnter();'
-                print '            Trace::BeginLeave(__call);'
-                print '            Trace::EndLeave();'
-                print '        }'
+        Tracer.dispatch_function(self, function)
+
+    def emit_memcpy(self, dest, src, length):
+        print '        unsigned __call = __writer.beginEnter(&__memcpy_sig);'
+        print '        __writer.beginArg(0);'
+        print '        __writer.writeOpaque(%s);' % dest
+        print '        __writer.endArg();'
+        print '        __writer.beginArg(1);'
+        print '        __writer.writeBlob(%s, %s);' % (src, length)
+        print '        __writer.endArg();'
+        print '        __writer.beginArg(2);'
+        print '        __writer.writeUInt(%s);' % length
+        print '        __writer.endArg();'
+        print '        __writer.endEnter();'
+        print '        __writer.beginLeave(__call);'
+        print '        __writer.endLeave();'
+       
+    buffer_targets = [
+        'ARRAY_BUFFER',
+        'ELEMENT_ARRAY_BUFFER',
+        'PIXEL_PACK_BUFFER',
+        'PIXEL_UNPACK_BUFFER',
+    ]
 
-            print '        return;'
+    def wrap_ret(self, function, instance):
+        Tracer.wrap_ret(self, function, instance)
+            
+        if function.name in ('glMapBuffer', 'glMapBufferARB'):
+            print '    struct buffer_mapping *mapping = get_buffer_mapping(target);'
+            print '    if (mapping) {'
+            print '        mapping->map = %s;' % (instance)
+            print '        mapping->length = 0;'
+            print '        __glGetBufferParameteriv(target, GL_BUFFER_SIZE, &mapping->length);'
+            print '        mapping->write = (access != GL_READ_ONLY);'
+            print '        mapping->explicit_flush = false;'
             print '    }'
 
-        Tracer.trace_function_impl_body(self, function)
+        if function.name == 'glMapBufferRange':
+            print '    struct buffer_mapping *mapping = get_buffer_mapping(target);'
+            print '    if (mapping) {'
+            print '        mapping->map = %s;' % (instance)
+            print '        mapping->length = length;'
+            print '        mapping->write = access & GL_MAP_WRITE_BIT;'
+            print '        mapping->explicit_flush = access & GL_MAP_FLUSH_EXPLICIT_BIT;'
+            print '    }'
 
     boolean_names = [
         'GL_FALSE',
@@ -277,17 +480,43 @@ class GlTracer(Tracer):
             print '    GLint __element_array_buffer = 0;'
             print '    __glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
             print '    if (!__element_array_buffer) {'
-            print '        Trace::LiteralBlob((const void *)%s, count*__gl_type_size(type));' % (arg.name)
+            if isinstance(arg.type, stdapi.Array):
+                Tracer.dump_arg_instance(self, function, arg)
+                print '        __writer.beginArray(%s);' % arg.type.length
+                print '        for(GLsizei i = 0; i < %s; ++i) {' % arg.type.length
+                print '            __writer.beginElement();'
+                print '            __writer.writeBlob((const void *)%s, count[i]*__gl_type_size(type));' % (arg.name)
+                print '            __writer.endElement();'
+                print '        }'
+                print '        __writer.endArray();'
+            else:
+                print '        __writer.writeBlob((const void *)%s, count*__gl_type_size(type));' % (arg.name)
+            print '    } else {'
+            Tracer.dump_arg_instance(self, function, arg)
+            print '    }'
+            return
+
+        # Several GL state functions take GLenum symbolic names as
+        # integer/floats; so dump the symbolic name whenever possible
+        if function.name.startswith('gl') \
+           and arg.type in (glapi.GLint, glapi.GLfloat) \
+           and arg.name == 'param':
+            assert arg.index > 0
+            assert function.args[arg.index - 1].name == 'pname'
+            assert function.args[arg.index - 1].type == glapi.GLenum
+            print '    if (is_symbolic_pname(pname) && is_symbolic_param(%s)) {' % arg.name
+            dump_instance(glapi.GLenum, arg.name)
             print '    } else {'
-            print '        Trace::LiteralOpaque((const void *)%s);' % (arg.name)
+            Tracer.dump_arg_instance(self, function, arg)
             print '    }'
             return
 
         Tracer.dump_arg_instance(self, function, arg)
 
-    def state_tracker_impl(self, api):
-        # A simple state tracker to track the pointer values
+    def footer(self, api):
+        Tracer.footer(self, api)
 
+        # A simple state tracker to track the pointer values
         # update the state
         print 'static void __trace_user_arrays(GLuint maxindex)'
         print '{'
@@ -318,19 +547,19 @@ class GlTracer(Tracer):
 
             # Emit a fake function
             self.array_trace_intermezzo(api, uppercase_name)
-            print '            unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name,)
+            print '            unsigned __call = __writer.beginEnter(&__%s_sig);' % (function.name,)
             for arg in function.args:
                 assert not arg.output
-                print '            Trace::BeginArg(%u);' % (arg.index,)
+                print '            __writer.beginArg(%u);' % (arg.index,)
                 if arg.name != 'pointer':
                     dump_instance(arg.type, arg.name)
                 else:
-                    print '            Trace::LiteralBlob((const void *)%s, __size);' % (arg.name)
-                print '            Trace::EndArg();'
+                    print '            __writer.writeBlob((const void *)%s, __size);' % (arg.name)
+                print '            __writer.endArg();'
             
-            print '            Trace::EndEnter();'
-            print '            Trace::BeginLeave(__call);'
-            print '            Trace::EndLeave();'
+            print '            __writer.endEnter();'
+            print '            __writer.beginLeave(__call);'
+            print '            __writer.endLeave();'
             print '        }'
             print '    }'
             self.array_epilog(api, uppercase_name)
@@ -362,19 +591,19 @@ class GlTracer(Tracer):
         print '                size_t __size = __%s_size(%s, maxindex);' % (function.name, arg_names)
 
         # Emit a fake function
-        print '                unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name,)
+        print '                unsigned __call = __writer.beginEnter(&__%s_sig);' % (function.name,)
         for arg in function.args:
             assert not arg.output
-            print '                Trace::BeginArg(%u);' % (arg.index,)
+            print '                __writer.beginArg(%u);' % (arg.index,)
             if arg.name != 'pointer':
                 dump_instance(arg.type, arg.name)
             else:
-                print '                Trace::LiteralBlob((const void *)%s, __size);' % (arg.name)
-            print '                Trace::EndArg();'
+                print '                __writer.writeBlob((const void *)%s, __size);' % (arg.name)
+            print '                __writer.endArg();'
         
-        print '                Trace::EndEnter();'
-        print '                Trace::BeginLeave(__call);'
-        print '                Trace::EndLeave();'
+        print '                __writer.endEnter();'
+        print '                __writer.beginLeave(__call);'
+        print '                __writer.endLeave();'
         print '            }'
         print '        }'
         print '    }'
@@ -429,15 +658,15 @@ class GlTracer(Tracer):
         self.fake_call(function, [texture])
 
     def fake_call(self, function, args):
-        print '            unsigned __fake_call = Trace::BeginEnter(__%s_sig);' % (function.name,)
+        print '            unsigned __fake_call = __writer.beginEnter(&__%s_sig);' % (function.name,)
         for arg, instance in zip(function.args, args):
             assert not arg.output
-            print '            Trace::BeginArg(%u);' % (arg.index,)
+            print '            __writer.beginArg(%u);' % (arg.index,)
             dump_instance(arg.type, instance)
-            print '            Trace::EndArg();'
-        print '            Trace::EndEnter();'
-        print '            Trace::BeginLeave(__fake_call);'
-        print '            Trace::EndLeave();'
+            print '            __writer.endArg();'
+        print '            __writer.endEnter();'
+        print '            __writer.beginLeave(__fake_call);'
+        print '            __writer.endLeave();'