+ # Which glVertexAttrib* variant to use
+ print 'enum vertex_attrib {'
+ print ' VERTEX_ATTRIB,'
+ print ' VERTEX_ATTRIB_ARB,'
+ print ' VERTEX_ATTRIB_NV,'
+ print '};'
+ print
+ print 'static vertex_attrib __get_vertex_attrib(void) {'
+ print ' if (__user_arrays_arb || __user_arrays_nv) {'
+ print ' GLboolean __vertex_program = GL_FALSE;'
+ print ' __glGetBooleanv(GL_VERTEX_PROGRAM_ARB, &__vertex_program);'
+ print ' if (__vertex_program) {'
+ print ' if (__user_arrays_nv) {'
+ print ' GLint __vertex_program_binding_nv = 0;'
+ print ' __glGetIntegerv(GL_VERTEX_PROGRAM_BINDING_NV, &__vertex_program_binding_nv);'
+ print ' if (__vertex_program_binding_nv) {'
+ print ' return VERTEX_ATTRIB_NV;'
+ print ' }'
+ print ' }'
+ print ' return VERTEX_ATTRIB_ARB;'
+ print ' }'
+ print ' }'
+ print ' return VERTEX_ATTRIB;'
+ print '}'
+ 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
+ enable_name = 'GL_%s_ARRAY' % uppercase_name
+ binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
+ print ' // %s' % function_name
+ self.array_prolog(api, uppercase_name)
+ print ' if (__glIsEnabled(%s)) {' % enable_name
+ print ' GLint __binding = 0;'
+ print ' __glGetIntegerv(%s, &__binding);' % binding_name
+ print ' if (!__binding) {'
+ self.array_cleanup(api, uppercase_name)
+ print ' return true;'
+ print ' }'
+ print ' }'
+ self.array_epilog(api, uppercase_name)
+ print
+
+ print ' vertex_attrib __vertex_attrib = __get_vertex_attrib();'
+ print
+ print ' // glVertexAttribPointer'
+ print ' if (__vertex_attrib == VERTEX_ATTRIB) {'
+ print ' GLint __max_vertex_attribs = 0;'
+ print ' __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &__max_vertex_attribs);'
+ print ' for (GLint index = 0; index < __max_vertex_attribs; ++index) {'
+ print ' GLint __enabled = 0;'
+ print ' __glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &__enabled);'
+ print ' if (__enabled) {'
+ print ' GLint __binding = 0;'
+ print ' __glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &__binding);'
+ print ' if (!__binding) {'
+ print ' return true;'
+ print ' }'
+ print ' }'
+ print ' }'
+ print ' }'
+ print
+ print ' // glVertexAttribPointerARB'
+ print ' if (__vertex_attrib == VERTEX_ATTRIB_ARB) {'
+ print ' GLint __max_vertex_attribs = 0;'
+ print ' __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS_ARB, &__max_vertex_attribs);'
+ print ' for (GLint index = 0; index < __max_vertex_attribs; ++index) {'
+ print ' GLint __enabled = 0;'
+ print ' __glGetVertexAttribivARB(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB, &__enabled);'
+ print ' if (__enabled) {'
+ print ' GLint __binding = 0;'
+ print ' __glGetVertexAttribivARB(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB, &__binding);'
+ print ' if (!__binding) {'
+ print ' return true;'
+ print ' }'
+ print ' }'
+ print ' }'
+ print ' }'
+ print
+ print ' // glVertexAttribPointerNV'
+ print ' if (__vertex_attrib == VERTEX_ATTRIB_NV) {'
+ print ' for (GLint index = 0; index < 16; ++index) {'
+ print ' GLint __enabled = 0;'
+ print ' __glGetIntegerv(GL_VERTEX_ATTRIB_ARRAY0_NV + index, &__enabled);'
+ print ' if (__enabled) {'
+ print ' return true;'
+ print ' }'
+ print ' }'
+ print ' }'
+ print
+
+ print ' return false;'
+ print '}'
+ print
+
+ 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("apitrace: 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("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);'
+ print ' return 1;'
+ print ' }'
+ print '}'
+ print
+
+ 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_function_names = set((
+ '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 = [
+ 'GL_V2F',
+ 'GL_V3F',
+ 'GL_C4UB_V2F',
+ 'GL_C4UB_V3F',
+ 'GL_C3F_V3F',
+ 'GL_N3F_V3F',
+ 'GL_C4F_N3F_V3F',
+ 'GL_T2F_V3F',
+ 'GL_T4F_V4F',
+ 'GL_T2F_C4UB_V3F',
+ 'GL_T2F_C3F_V3F',
+ 'GL_T2F_N3F_V3F',
+ 'GL_T2F_C4F_N3F_V3F',
+ 'GL_T4F_C4F_N3F_V4F',
+ ]
+
+ def trace_function_impl_body(self, function):
+ # Defer tracing of user array pointers...
+ if function.name in self.array_pointer_function_names:
+ print ' GLint __array_buffer = 0;'
+ print ' __glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
+ print ' if (!__array_buffer) {'
+ print ' __user_arrays = true;'
+ if function.name == "glVertexAttribPointerARB":
+ print ' __user_arrays_arb = true;'
+ if function.name == "glVertexAttribPointerNV":
+ print ' __user_arrays_nv = 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 = Trace::localWriter.beginEnter(&__sig);'
+ print ' Trace::localWriter.beginArg(0);'
+ dump_instance(glapi.GLenum, enable_name)
+ print ' Trace::localWriter.endArg();'
+ print ' Trace::localWriter.endEnter();'
+ print ' Trace::localWriter.beginLeave(__call);'
+ print ' Trace::localWriter.endLeave();'
+ print ' }'
+
+ print ' return;'
+ print ' }'
+
+ # ... to the draw calls
+ if function.name in self.draw_function_names:
+ print ' if (__need_user_arrays()) {'
+ arg_names = ', '.join([arg.name for arg in function.args[1:]])
+ print ' GLuint maxindex = __%s_maxindex(%s);' % (function.name, arg_names)
+ print ' __trace_user_arrays(maxindex);'
+ 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 ' }'
+
+ Tracer.trace_function_impl_body(self, function)
+
+ gremedy_functions = [
+ 'glStringMarkerGREMEDY',
+ 'glFrameTerminatorGREMEDY',
+ ]
+
+ def dispatch_function(self, function):
+ if function.name in ('glLinkProgram', 'glLinkProgramARB'):
+ # These functions have been dispatched already
+ return
+
+ # We implement the GREMEDY extensions, not the driver
+ if function.name in self.gremedy_functions:
+ return
+
+ if function.name in ('glXGetProcAddress', 'glXGetProcAddressARB', 'wglGetProcAddress'):
+ if_ = 'if'
+ for gremedy_function in self.gremedy_functions:
+ print ' %s (strcmp("%s", (const char *)%s) == 0) {' % (if_, gremedy_function, function.args[0].name)
+ print ' __result = (%s)&%s;' % (function.type, gremedy_function)
+ print ' }'
+ if_ = 'else if'
+ print ' else {'
+ Tracer.dispatch_function(self, function)
+ print ' }'
+ return