José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 1 | ########################################################################## |
| 2 | # |
| 3 | # Copyright 2008-2010 VMware, Inc. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | # of this software and associated documentation files (the "Software"), to deal |
| 8 | # in the Software without restriction, including without limitation the rights |
| 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | # copies of the Software, and to permit persons to whom the Software is |
| 11 | # furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice shall be included in |
| 14 | # all copies or substantial portions of the Software. |
| 15 | # |
| 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | # THE SOFTWARE. |
| 23 | # |
| 24 | ##########################################################################/ |
| 25 | |
| 26 | |
| 27 | """GL tracing generator.""" |
| 28 | |
| 29 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 30 | import stdapi |
| 31 | import glapi |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 32 | from glxapi import glxapi |
| 33 | from trace import Tracer, dump_instance |
| 34 | |
| 35 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 36 | class TypeGetter(stdapi.Visitor): |
| 37 | '''Determine which glGet*v function that matches the specified type.''' |
| 38 | |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 39 | def __init__(self, prefix = 'glGet', long_suffix = True): |
| 40 | self.prefix = prefix |
| 41 | self.long_suffix = long_suffix |
| 42 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 43 | def visit_const(self, const): |
| 44 | return self.visit(const.type) |
| 45 | |
| 46 | def visit_alias(self, alias): |
| 47 | if alias.expr == 'GLboolean': |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 48 | if self.long_suffix: |
| 49 | return self.prefix + 'Booleanv', alias.expr |
| 50 | else: |
| 51 | return self.prefix + 'iv', 'GLint' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 52 | elif alias.expr == 'GLdouble': |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 53 | if self.long_suffix: |
| 54 | return self.prefix + 'Doublev', alias.expr |
| 55 | else: |
| 56 | return self.prefix + 'dv', alias.expr |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 57 | elif alias.expr == 'GLfloat': |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 58 | if self.long_suffix: |
| 59 | return self.prefix + 'Floatv', alias.expr |
| 60 | else: |
| 61 | return self.prefix + 'fv', alias.expr |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 62 | elif alias.expr in ('GLint', 'GLuint', 'GLsizei'): |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 63 | if self.long_suffix: |
| 64 | return self.prefix + 'Integerv', 'GLint' |
| 65 | else: |
| 66 | return self.prefix + 'iv', 'GLint' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 67 | else: |
| 68 | print alias.expr |
| 69 | assert False |
| 70 | |
| 71 | def visit_enum(self, enum): |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 72 | return self.visit(glapi.GLint) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 73 | |
| 74 | def visit_bitmask(self, bitmask): |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 75 | return self.visit(glapi.GLint) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 76 | |
| 77 | def visit_opaque(self, pointer): |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 78 | return self.prefix + 'Pointerv', 'GLvoid *' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 79 | |
| 80 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 81 | class GlTracer(Tracer): |
| 82 | |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 83 | def header(self, api): |
| 84 | Tracer.header(self, api) |
| 85 | self.state_tracker_decl(api) |
| 86 | |
| 87 | def footer(self, api): |
| 88 | Tracer.footer(self, api) |
| 89 | self.state_tracker_impl(api) |
| 90 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 91 | arrays = [ |
| 92 | ("Vertex", "VERTEX"), |
| 93 | ("Normal", "NORMAL"), |
| 94 | ("Color", "COLOR"), |
| 95 | ("Index", "INDEX"), |
| 96 | ("TexCoord", "TEXTURE_COORD"), |
| 97 | ("EdgeFlag", "EDGE_FLAG"), |
| 98 | ("FogCoord", "FOG_COORD"), |
| 99 | ("SecondaryColor", "SECONDARY_COLOR"), |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 100 | ] |
José Fonseca | c9f1223 | 2011-03-25 20:07:42 +0000 | [diff] [blame] | 101 | arrays.reverse() |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 102 | |
| 103 | def state_tracker_decl(self, api): |
José Fonseca | 25ebe54 | 2011-04-24 10:08:22 +0100 | [diff] [blame] | 104 | print '// Whether user arrays were used' |
| 105 | print 'static bool __user_arrays = false;' |
| 106 | print |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 107 | # Whether we need user arrays |
| 108 | print 'static inline bool __need_user_arrays(void)' |
| 109 | print '{' |
José Fonseca | 25ebe54 | 2011-04-24 10:08:22 +0100 | [diff] [blame] | 110 | print ' if (!__user_arrays) {' |
| 111 | print ' return false;' |
| 112 | print ' }' |
| 113 | print |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 114 | |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 115 | for camelcase_name, uppercase_name in self.arrays: |
| 116 | function_name = 'gl%sPointer' % camelcase_name |
| 117 | enable_name = 'GL_%s_ARRAY' % uppercase_name |
| 118 | binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name |
| 119 | print ' // %s' % function_name |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 120 | self.array_prolog(api, uppercase_name) |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 121 | print ' if (__glIsEnabled(%s)) {' % enable_name |
| 122 | print ' GLint __binding = 0;' |
| 123 | print ' __glGetIntegerv(%s, &__binding);' % binding_name |
| 124 | print ' if (!__binding) {' |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 125 | self.array_cleanup(api, uppercase_name) |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 126 | print ' return true;' |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 127 | print ' }' |
| 128 | print ' }' |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 129 | self.array_epilog(api, uppercase_name) |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 130 | print |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 131 | |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 132 | print ' // glVertexAttribPointer' |
José Fonseca | 0a96525 | 2011-04-14 09:21:15 +0100 | [diff] [blame] | 133 | print ' GLint __max_vertex_attribs = 0;' |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 134 | print ' __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &__max_vertex_attribs);' |
| 135 | print ' for (GLint index = 0; index < __max_vertex_attribs; ++index) {' |
| 136 | print ' GLint __enabled = 0;' |
| 137 | print ' __glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &__enabled);' |
| 138 | print ' if (__enabled) {' |
| 139 | print ' GLint __binding = 0;' |
| 140 | print ' __glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &__binding);' |
| 141 | print ' if (!__binding) {' |
| 142 | print ' return true;' |
| 143 | print ' }' |
| 144 | print ' }' |
| 145 | print ' }' |
| 146 | print |
| 147 | |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 148 | print ' return false;' |
| 149 | print '}' |
| 150 | print |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 151 | |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 152 | print 'static void __trace_user_arrays(GLuint maxindex);' |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 153 | print |
José Fonseca | 867b1b7 | 2011-04-24 11:58:04 +0100 | [diff] [blame] | 154 | |
| 155 | print 'struct buffer_mapping {' |
| 156 | print ' void *map;' |
| 157 | print ' GLint length;' |
| 158 | print ' bool write;' |
| 159 | print '};' |
| 160 | print |
| 161 | for target in self.buffer_targets: |
| 162 | print 'struct buffer_mapping __%s_mapping;' % target.lower(); |
| 163 | print |
| 164 | print 'static inline struct buffer_mapping *' |
| 165 | print 'get_buffer_mapping(GLenum target) {' |
| 166 | print ' switch(target) {' |
| 167 | for target in self.buffer_targets: |
| 168 | print ' case GL_%s:' % target |
| 169 | print ' return & __%s_mapping;' % target.lower() |
| 170 | print ' default:' |
| 171 | print ' OS::DebugMessage("warning: unknown buffer target 0x%04X\\n", target);' |
| 172 | print ' return NULL;' |
| 173 | print ' }' |
| 174 | print '}' |
| 175 | print |
| 176 | |
| 177 | # Generate memcpy's signature |
| 178 | self.trace_function_decl(glapi.memcpy) |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 179 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 180 | array_pointer_function_names = set(( |
| 181 | "glVertexPointer", |
| 182 | "glNormalPointer", |
| 183 | "glColorPointer", |
| 184 | "glIndexPointer", |
| 185 | "glTexCoordPointer", |
| 186 | "glEdgeFlagPointer", |
| 187 | "glFogCoordPointer", |
| 188 | "glSecondaryColorPointer", |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 189 | |
José Fonseca | 4ec1daf | 2011-04-01 09:22:22 +0100 | [diff] [blame] | 190 | #"glVertexPointerEXT", |
| 191 | #"glNormalPointerEXT", |
| 192 | #"glColorPointerEXT", |
| 193 | #"glIndexPointerEXT", |
| 194 | #"glTexCoordPointerEXT", |
| 195 | #"glEdgeFlagPointerEXT", |
| 196 | #"glFogCoordPointerEXT", |
| 197 | #"glSecondaryColorPointerEXT", |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 198 | |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 199 | "glVertexAttribPointer", |
| 200 | "glVertexAttribPointerARB", |
| 201 | "glVertexAttribPointerNV", |
| 202 | "glVertexAttribLPointer", |
| 203 | "glVertexAttribLPointerEXT", |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 204 | |
| 205 | #"glMatrixIndexPointerARB", |
| 206 | )) |
| 207 | |
| 208 | draw_function_names = set(( |
| 209 | 'glDrawArrays', |
| 210 | 'glDrawElements', |
| 211 | 'glDrawRangeElements', |
| 212 | )) |
| 213 | |
José Fonseca | c9f1223 | 2011-03-25 20:07:42 +0000 | [diff] [blame] | 214 | interleaved_formats = [ |
| 215 | 'GL_V2F', |
| 216 | 'GL_V3F', |
| 217 | 'GL_C4UB_V2F', |
| 218 | 'GL_C4UB_V3F', |
| 219 | 'GL_C3F_V3F', |
| 220 | 'GL_N3F_V3F', |
| 221 | 'GL_C4F_N3F_V3F', |
| 222 | 'GL_T2F_V3F', |
| 223 | 'GL_T4F_V4F', |
| 224 | 'GL_T2F_C4UB_V3F', |
| 225 | 'GL_T2F_C3F_V3F', |
| 226 | 'GL_T2F_N3F_V3F', |
| 227 | 'GL_T2F_C4F_N3F_V3F', |
| 228 | 'GL_T4F_C4F_N3F_V4F', |
| 229 | ] |
| 230 | |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 231 | def trace_function_impl_body(self, function): |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 232 | # Defer tracing of user array pointers... |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 233 | if function.name in self.array_pointer_function_names: |
| 234 | print ' GLint __array_buffer = 0;' |
| 235 | print ' __glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);' |
| 236 | print ' if (!__array_buffer) {' |
José Fonseca | 25ebe54 | 2011-04-24 10:08:22 +0100 | [diff] [blame] | 237 | print ' __user_arrays = true;' |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 238 | self.dispatch_function(function) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 239 | print ' return;' |
| 240 | print ' }' |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 241 | |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 242 | # ... to the draw calls |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 243 | if function.name in self.draw_function_names: |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 244 | print ' if (__need_user_arrays()) {' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 245 | arg_names = ', '.join([arg.name for arg in function.args[1:]]) |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 246 | print ' GLuint maxindex = __%s_maxindex(%s);' % (function.name, arg_names) |
| 247 | print ' __trace_user_arrays(maxindex);' |
| 248 | print ' }' |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 249 | |
José Fonseca | c9f1223 | 2011-03-25 20:07:42 +0000 | [diff] [blame] | 250 | # And also break down glInterleavedArrays into the individual calls |
| 251 | if function.name == 'glInterleavedArrays': |
| 252 | print ' GLint __array_buffer = 0;' |
| 253 | print ' __glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);' |
| 254 | print ' if (!__array_buffer) {' |
José Fonseca | 25ebe54 | 2011-04-24 10:08:22 +0100 | [diff] [blame] | 255 | print ' __user_arrays = true;' |
José Fonseca | c9f1223 | 2011-03-25 20:07:42 +0000 | [diff] [blame] | 256 | self.dispatch_function(function) |
| 257 | print |
| 258 | |
| 259 | # Initialize the enable flags |
| 260 | for camelcase_name, uppercase_name in self.arrays: |
| 261 | flag_name = '__' + uppercase_name.lower() |
| 262 | print ' GLboolean %s = GL_FALSE;' % flag_name |
| 263 | print |
| 264 | |
| 265 | # Swicth for the interleaved formats |
| 266 | print ' switch (format) {' |
| 267 | for format in self.interleaved_formats: |
| 268 | print ' case %s:' % format |
| 269 | for camelcase_name, uppercase_name in self.arrays: |
| 270 | flag_name = '__' + uppercase_name.lower() |
| 271 | if format.find('_' + uppercase_name[0]) >= 0: |
| 272 | print ' %s = GL_TRUE;' % flag_name |
| 273 | print ' break;' |
| 274 | print ' default:' |
| 275 | print ' return;' |
| 276 | print ' }' |
| 277 | print |
| 278 | |
| 279 | # Emit fake glEnableClientState/glDisableClientState flags |
| 280 | for camelcase_name, uppercase_name in self.arrays: |
| 281 | flag_name = '__' + uppercase_name.lower() |
| 282 | enable_name = 'GL_%s_ARRAY' % uppercase_name |
| 283 | |
| 284 | # Emit a fake function |
| 285 | print ' {' |
| 286 | print ' static const Trace::FunctionSig &__sig = %s ? __glEnableClientState_sig : __glDisableClientState_sig;' % flag_name |
| 287 | print ' unsigned __call = Trace::BeginEnter(__sig);' |
| 288 | print ' Trace::BeginArg(0);' |
| 289 | dump_instance(glapi.GLenum, enable_name) |
| 290 | print ' Trace::EndArg();' |
| 291 | print ' Trace::EndEnter();' |
| 292 | print ' Trace::BeginLeave(__call);' |
| 293 | print ' Trace::EndLeave();' |
| 294 | print ' }' |
| 295 | |
| 296 | print ' return;' |
| 297 | print ' }' |
| 298 | |
José Fonseca | 867b1b7 | 2011-04-24 11:58:04 +0100 | [diff] [blame] | 299 | # Emit a fake memcpy on |
| 300 | if function.name in ('glUnmapBuffer', 'glUnmapBufferARB'): |
| 301 | print ' struct buffer_mapping *mapping = get_buffer_mapping(target);' |
| 302 | print ' if (mapping && mapping->write) {' |
| 303 | print ' unsigned __call = Trace::BeginEnter(__memcpy_sig);' |
| 304 | print ' Trace::BeginArg(0);' |
| 305 | print ' Trace::LiteralOpaque(mapping->map);' |
| 306 | print ' Trace::EndArg();' |
| 307 | print ' Trace::BeginArg(1);' |
| 308 | print ' Trace::LiteralBlob(mapping->map, mapping->length);' |
| 309 | print ' Trace::EndArg();' |
| 310 | print ' Trace::BeginArg(2);' |
| 311 | print ' Trace::LiteralUInt(mapping->length);' |
| 312 | print ' Trace::EndArg();' |
| 313 | print ' Trace::EndEnter();' |
| 314 | print ' Trace::BeginLeave(__call);' |
| 315 | print ' Trace::EndLeave();' |
| 316 | print ' }' |
| 317 | |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 318 | Tracer.trace_function_impl_body(self, function) |
José Fonseca | 867b1b7 | 2011-04-24 11:58:04 +0100 | [diff] [blame] | 319 | |
| 320 | buffer_targets = [ |
| 321 | 'ARRAY_BUFFER', |
| 322 | 'ELEMENT_ARRAY_BUFFER', |
| 323 | 'PIXEL_PACK_BUFFER', |
| 324 | 'PIXEL_UNPACK_BUFFER', |
| 325 | ] |
| 326 | |
| 327 | def wrap_ret(self, function, instance): |
| 328 | Tracer.wrap_ret(self, function, instance) |
| 329 | |
| 330 | if function.name in ('glMapBuffer', 'glMapBufferARB'): |
| 331 | print ' struct buffer_mapping *mapping = get_buffer_mapping(target);' |
| 332 | print ' if (mapping) {' |
| 333 | print ' mapping->map = %s;' % (instance) |
| 334 | print ' mapping->length = 0;' |
| 335 | print ' __glGetBufferParameteriv(target, GL_BUFFER_SIZE, &mapping->length);' |
| 336 | print ' mapping->write = (access != GL_READ_ONLY);' |
| 337 | print ' }' |
| 338 | |
| 339 | if function.name == 'glMapBufferRange': |
| 340 | print ' struct buffer_mapping *mapping = get_buffer_mapping(target);' |
| 341 | print ' if (mapping) {' |
| 342 | print ' mapping->map = %s;' % (instance) |
| 343 | print ' mapping->length = length;' |
| 344 | print ' mapping->write = access & GL_MAP_WRITE_BIT;' |
| 345 | print ' }' |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 346 | |
José Fonseca | c9f1223 | 2011-03-25 20:07:42 +0000 | [diff] [blame] | 347 | boolean_names = [ |
| 348 | 'GL_FALSE', |
| 349 | 'GL_TRUE', |
| 350 | ] |
| 351 | |
| 352 | def gl_boolean(self, value): |
| 353 | return self.boolean_names[int(bool(value))] |
| 354 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 355 | def dump_arg_instance(self, function, arg): |
| 356 | if function.name in self.draw_function_names and arg.name == 'indices': |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 357 | print ' GLint __element_array_buffer = 0;' |
| 358 | print ' __glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);' |
| 359 | print ' if (!__element_array_buffer) {' |
| 360 | print ' Trace::LiteralBlob((const void *)%s, count*__gl_type_size(type));' % (arg.name) |
| 361 | print ' } else {' |
| 362 | print ' Trace::LiteralOpaque((const void *)%s);' % (arg.name) |
| 363 | print ' }' |
| 364 | return |
| 365 | |
| 366 | Tracer.dump_arg_instance(self, function, arg) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 367 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 368 | def state_tracker_impl(self, api): |
| 369 | # A simple state tracker to track the pointer values |
| 370 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 371 | # update the state |
José Fonseca | 8a6c6cb | 2011-03-23 16:44:30 +0000 | [diff] [blame] | 372 | print 'static void __trace_user_arrays(GLuint maxindex)' |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 373 | print '{' |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 374 | |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 375 | for camelcase_name, uppercase_name in self.arrays: |
| 376 | function_name = 'gl%sPointer' % camelcase_name |
| 377 | enable_name = 'GL_%s_ARRAY' % uppercase_name |
| 378 | binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 379 | function = api.get_function_by_name(function_name) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 380 | |
| 381 | print ' // %s' % function.name |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 382 | self.array_trace_prolog(api, uppercase_name) |
| 383 | self.array_prolog(api, uppercase_name) |
| 384 | print ' if (__glIsEnabled(%s)) {' % enable_name |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 385 | print ' GLint __binding = 0;' |
| 386 | print ' __glGetIntegerv(%s, &__binding);' % binding_name |
| 387 | print ' if (!__binding) {' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 388 | |
| 389 | # Get the arguments via glGet* |
| 390 | for arg in function.args: |
| 391 | arg_get_enum = 'GL_%s_ARRAY_%s' % (uppercase_name, arg.name.upper()) |
| 392 | arg_get_function, arg_type = TypeGetter().visit(arg.type) |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 393 | print ' %s %s = 0;' % (arg_type, arg.name) |
| 394 | print ' __%s(%s, &%s);' % (arg_get_function, arg_get_enum, arg.name) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 395 | |
| 396 | arg_names = ', '.join([arg.name for arg in function.args[:-1]]) |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 397 | print ' size_t __size = __%s_size(%s, maxindex);' % (function.name, arg_names) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 398 | |
| 399 | # Emit a fake function |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 400 | self.array_trace_intermezzo(api, uppercase_name) |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 401 | print ' unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name,) |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 402 | for arg in function.args: |
| 403 | assert not arg.output |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 404 | print ' Trace::BeginArg(%u);' % (arg.index,) |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 405 | if arg.name != 'pointer': |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 406 | dump_instance(arg.type, arg.name) |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 407 | else: |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 408 | print ' Trace::LiteralBlob((const void *)%s, __size);' % (arg.name) |
| 409 | print ' Trace::EndArg();' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 410 | |
José Fonseca | 7f5163e | 2011-03-31 23:37:26 +0100 | [diff] [blame] | 411 | print ' Trace::EndEnter();' |
| 412 | print ' Trace::BeginLeave(__call);' |
| 413 | print ' Trace::EndLeave();' |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 414 | print ' }' |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 415 | print ' }' |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 416 | self.array_epilog(api, uppercase_name) |
| 417 | self.array_trace_epilog(api, uppercase_name) |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 418 | print |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 419 | |
| 420 | # Samething, but for glVertexAttribPointer |
| 421 | print ' // glVertexAttribPointer' |
José Fonseca | 0a96525 | 2011-04-14 09:21:15 +0100 | [diff] [blame] | 422 | print ' GLint __max_vertex_attribs = 0;' |
José Fonseca | 1a2fdd2 | 2011-04-01 00:55:09 +0100 | [diff] [blame] | 423 | print ' __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &__max_vertex_attribs);' |
| 424 | print ' for (GLint index = 0; index < __max_vertex_attribs; ++index) {' |
| 425 | print ' GLint __enabled = 0;' |
| 426 | print ' __glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &__enabled);' |
| 427 | print ' if (__enabled) {' |
| 428 | print ' GLint __binding = 0;' |
| 429 | print ' __glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &__binding);' |
| 430 | print ' if (!__binding) {' |
| 431 | |
| 432 | function = api.get_function_by_name('glVertexAttribPointer') |
| 433 | |
| 434 | # Get the arguments via glGet* |
| 435 | for arg in function.args[1:]: |
| 436 | arg_get_enum = 'GL_VERTEX_ATTRIB_ARRAY_%s' % (arg.name.upper(),) |
| 437 | arg_get_function, arg_type = TypeGetter('glGetVertexAttrib', False).visit(arg.type) |
| 438 | print ' %s %s = 0;' % (arg_type, arg.name) |
| 439 | print ' __%s(index, %s, &%s);' % (arg_get_function, arg_get_enum, arg.name) |
| 440 | |
| 441 | arg_names = ', '.join([arg.name for arg in function.args[1:-1]]) |
| 442 | print ' size_t __size = __%s_size(%s, maxindex);' % (function.name, arg_names) |
| 443 | |
| 444 | # Emit a fake function |
| 445 | print ' unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name,) |
| 446 | for arg in function.args: |
| 447 | assert not arg.output |
| 448 | print ' Trace::BeginArg(%u);' % (arg.index,) |
| 449 | if arg.name != 'pointer': |
| 450 | dump_instance(arg.type, arg.name) |
| 451 | else: |
| 452 | print ' Trace::LiteralBlob((const void *)%s, __size);' % (arg.name) |
| 453 | print ' Trace::EndArg();' |
| 454 | |
| 455 | print ' Trace::EndEnter();' |
| 456 | print ' Trace::BeginLeave(__call);' |
| 457 | print ' Trace::EndLeave();' |
| 458 | print ' }' |
| 459 | print ' }' |
| 460 | print ' }' |
| 461 | print |
| 462 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 463 | print '}' |
| 464 | print |
| 465 | |
José Fonseca | fb6744f | 2011-04-15 11:18:37 +0100 | [diff] [blame] | 466 | # |
| 467 | # Hooks for glTexCoordPointer, which is identical to the other array |
| 468 | # pointers except the fact that it is indexed by glClientActiveTexture. |
| 469 | # |
| 470 | |
| 471 | def array_prolog(self, api, uppercase_name): |
| 472 | if uppercase_name == 'TEXTURE_COORD': |
| 473 | print ' GLint client_active_texture = 0;' |
| 474 | print ' __glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &client_active_texture);' |
| 475 | print ' GLint max_texture_coords = 0;' |
| 476 | print ' __glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);' |
| 477 | print ' for (GLint unit = 0; unit < max_texture_coords; ++unit) {' |
| 478 | print ' GLenum texture = GL_TEXTURE0 + unit;' |
| 479 | print ' __glClientActiveTexture(texture);' |
| 480 | |
| 481 | def array_trace_prolog(self, api, uppercase_name): |
| 482 | if uppercase_name == 'TEXTURE_COORD': |
| 483 | print ' bool client_active_texture_dirty = false;' |
| 484 | |
| 485 | def array_epilog(self, api, uppercase_name): |
| 486 | if uppercase_name == 'TEXTURE_COORD': |
| 487 | print ' }' |
| 488 | self.array_cleanup(api, uppercase_name) |
| 489 | |
| 490 | def array_cleanup(self, api, uppercase_name): |
| 491 | if uppercase_name == 'TEXTURE_COORD': |
| 492 | print ' __glClientActiveTexture(client_active_texture);' |
| 493 | |
| 494 | def array_trace_intermezzo(self, api, uppercase_name): |
| 495 | if uppercase_name == 'TEXTURE_COORD': |
| 496 | print ' if (texture != client_active_texture || client_active_texture_dirty) {' |
| 497 | print ' client_active_texture_dirty = true;' |
| 498 | self.fake_glClientActiveTexture_call(api, "texture"); |
| 499 | print ' }' |
| 500 | |
| 501 | def array_trace_epilog(self, api, uppercase_name): |
| 502 | if uppercase_name == 'TEXTURE_COORD': |
| 503 | print ' if (client_active_texture_dirty) {' |
| 504 | self.fake_glClientActiveTexture_call(api, "client_active_texture"); |
| 505 | print ' }' |
| 506 | |
| 507 | def fake_glClientActiveTexture_call(self, api, texture): |
| 508 | function = api.get_function_by_name('glClientActiveTexture') |
| 509 | self.fake_call(function, [texture]) |
| 510 | |
| 511 | def fake_call(self, function, args): |
| 512 | print ' unsigned __fake_call = Trace::BeginEnter(__%s_sig);' % (function.name,) |
| 513 | for arg, instance in zip(function.args, args): |
| 514 | assert not arg.output |
| 515 | print ' Trace::BeginArg(%u);' % (arg.index,) |
| 516 | dump_instance(arg.type, instance) |
| 517 | print ' Trace::EndArg();' |
| 518 | print ' Trace::EndEnter();' |
| 519 | print ' Trace::BeginLeave(__fake_call);' |
| 520 | print ' Trace::EndLeave();' |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |