Ian Romanick | 7476406 | 2004-12-03 20:31:59 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python2 |
| 2 | |
| 3 | # (C) Copyright IBM Corporation 2004 |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | # copy of this software and associated documentation files (the "Software"), |
| 8 | # to deal in the Software without restriction, including without limitation |
| 9 | # on the rights to use, copy, modify, merge, publish, distribute, sub |
| 10 | # license, and/or sell copies of the Software, and to permit persons to whom |
| 11 | # the Software is furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice (including the next |
| 14 | # paragraph) shall be included in all copies or substantial portions of the |
| 15 | # Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 20 | # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | # IN THE SOFTWARE. |
| 24 | # |
| 25 | # Authors: |
| 26 | # Ian Romanick <idr@us.ibm.com> |
| 27 | |
| 28 | from xml.sax import saxutils |
| 29 | from xml.sax import make_parser |
| 30 | from xml.sax.handler import feature_namespaces |
| 31 | |
| 32 | import gl_XML |
| 33 | import license |
| 34 | import sys, getopt |
| 35 | |
| 36 | |
| 37 | def printPure(): |
| 38 | print """# if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) |
| 39 | # define PURE __attribute__((pure)) |
| 40 | # else |
| 41 | # define PURE |
| 42 | # endif""" |
| 43 | |
| 44 | def printFastcall(): |
| 45 | print """# if defined(__i386__) && defined(__GNUC__) |
| 46 | # define FASTCALL __attribute__((fastcall)) |
| 47 | # else |
| 48 | # define FASTCALL |
| 49 | # endif""" |
| 50 | |
| 51 | def printVisibility(S, s): |
| 52 | print """# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) |
| 53 | # define %s __attribute__((visibility("%s"))) |
| 54 | # else |
| 55 | # define %s |
| 56 | # endif""" % (S, s, S) |
| 57 | |
| 58 | def printNoinline(): |
| 59 | print """# if defined(__GNUC__) |
| 60 | # define NOINLINE __attribute__((noinline)) |
| 61 | # else |
| 62 | # define NOINLINE |
| 63 | # endif""" |
| 64 | |
| 65 | |
| 66 | class glXItemFactory(gl_XML.glItemFactory): |
| 67 | """Factory to create GLX protocol oriented objects derived from glItem.""" |
| 68 | |
| 69 | def create(self, context, name, attrs): |
| 70 | if name == "function": |
| 71 | return glXFunction(context, name, attrs) |
| 72 | elif name == "enum": |
| 73 | return glXEnum(context, name, attrs) |
| 74 | elif name == "param": |
| 75 | return glXParameter(context, name, attrs) |
| 76 | else: |
| 77 | return gl_XML.glItemFactory.create(self, context, name, attrs) |
| 78 | |
| 79 | class glXEnumFunction: |
| 80 | def __init__(self, name): |
| 81 | self.name = name |
| 82 | |
| 83 | # "enums" is a set of lists. The element in the set is the |
| 84 | # value of the enum. The list is the list of names for that |
| 85 | # value. For example, [0x8126] = {"POINT_SIZE_MIN", |
| 86 | # "POINT_SIZE_MIN_ARB", "POINT_SIZE_MIN_EXT", |
| 87 | # "POINT_SIZE_MIN_SGIS"}. |
| 88 | |
| 89 | self.enums = {} |
| 90 | |
| 91 | # "count" is indexed by count values. Each element of count |
| 92 | # is a list of index to "enums" that have that number of |
| 93 | # associated data elements. For example, [4] = |
| 94 | # {GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_EMISSION, |
| 95 | # GL_AMBIENT_AND_DIFFUSE} (the enum names are used here, |
| 96 | # but the actual hexadecimal values would be in the array). |
| 97 | |
| 98 | self.count = {} |
| 99 | |
| 100 | |
| 101 | def append(self, count, value, name): |
| 102 | if self.enums.has_key( value ): |
| 103 | self.enums[value].append(name) |
| 104 | else: |
| 105 | if not self.count.has_key(count): |
| 106 | self.count[count] = [] |
| 107 | |
| 108 | self.enums[value] = [] |
| 109 | self.enums[value].append(name) |
| 110 | self.count[count].append(value) |
| 111 | |
| 112 | |
| 113 | def signature( self ): |
| 114 | sig = "" |
| 115 | for i in self.count: |
| 116 | for e in self.count[i]: |
| 117 | sig += "%04x,%u," % (e, i) |
| 118 | |
| 119 | return sig; |
| 120 | |
| 121 | |
| 122 | def PrintUsingTable(self): |
| 123 | """Emit the body of the __gl*_size function using a pair |
| 124 | of look-up tables and a mask. The mask is calculated such |
| 125 | that (e & mask) is unique for all the valid values of e for |
| 126 | this function. The result of (e & mask) is used as an index |
| 127 | into the first look-up table. If it matches e, then the |
| 128 | same entry of the second table is returned. Otherwise zero |
| 129 | is returned. |
| 130 | |
| 131 | It seems like this should cause better code to be generated. |
| 132 | However, on x86 at least, the resulting .o file is about 20% |
| 133 | larger then the switch-statment version. I am leaving this |
| 134 | code in because the results may be different on other |
| 135 | platforms (e.g., PowerPC or x86-64).""" |
| 136 | |
| 137 | return 0 |
| 138 | count = 0 |
| 139 | for a in self.enums: |
| 140 | count += 1 |
| 141 | |
| 142 | # Determine if there is some mask M, such that M = (2^N) - 1, |
| 143 | # that will generate unique values for all of the enums. |
| 144 | |
| 145 | mask = 0 |
| 146 | for i in [1, 2, 3, 4, 5, 6, 7, 8]: |
| 147 | mask = (1 << i) - 1 |
| 148 | |
| 149 | fail = 0; |
| 150 | for a in self.enums: |
| 151 | for b in self.enums: |
| 152 | if a != b: |
| 153 | if (a & mask) == (b & mask): |
| 154 | fail = 1; |
| 155 | |
| 156 | if not fail: |
| 157 | break; |
| 158 | else: |
| 159 | mask = 0 |
| 160 | |
| 161 | if (mask != 0) and (mask < (2 * count)): |
| 162 | masked_enums = {} |
| 163 | masked_count = {} |
| 164 | |
| 165 | for i in range(0, mask + 1): |
| 166 | masked_enums[i] = "0"; |
| 167 | masked_count[i] = 0; |
| 168 | |
| 169 | for c in self.count: |
| 170 | for e in self.count[c]: |
| 171 | i = e & mask |
| 172 | masked_enums[i] = '0x%04x /* %s */' % (e, self.enums[e][0]) |
| 173 | masked_count[i] = c |
| 174 | |
| 175 | |
| 176 | print ' static const GLushort a[%u] = {' % (mask + 1) |
| 177 | for e in masked_enums: |
| 178 | print ' %s, ' % (masked_enums[e]) |
| 179 | print ' };' |
| 180 | |
| 181 | print ' static const GLubyte b[%u] = {' % (mask + 1) |
| 182 | for c in masked_count: |
| 183 | print ' %u, ' % (masked_count[c]) |
| 184 | print ' };' |
| 185 | |
| 186 | print ' const unsigned idx = (e & 0x%02xU);' % (mask) |
| 187 | print '' |
| 188 | print ' return (e == a[idx]) ? (GLint) b[idx] : 0;' |
| 189 | return 1; |
| 190 | else: |
| 191 | return 0; |
| 192 | |
| 193 | def PrintUsingSwitch(self): |
| 194 | """Emit the body of the __gl*_size function using a |
| 195 | switch-statement.""" |
| 196 | |
| 197 | print ' switch( e ) {' |
| 198 | |
| 199 | for c in self.count: |
| 200 | for e in self.count[c]: |
| 201 | first = 1 |
| 202 | |
| 203 | # There may be multiple enums with the same |
| 204 | # value. This happens has extensions are |
| 205 | # promoted from vendor-specific or EXT to |
| 206 | # ARB and to the core. Emit the first one as |
| 207 | # a case label, and emit the others as |
| 208 | # commented-out case labels. |
| 209 | |
| 210 | for j in self.enums[e]: |
| 211 | if first: |
| 212 | print ' case %s:' % (j) |
| 213 | first = 0 |
| 214 | else: |
| 215 | print '/* case %s:*/' % (j) |
| 216 | |
| 217 | print ' return %u;' % (c) |
| 218 | |
| 219 | print ' default: return 0;' |
| 220 | print ' }' |
| 221 | |
| 222 | |
| 223 | def Print(self, name): |
| 224 | print 'INTERNAL PURE FASTCALL GLint' |
| 225 | print '__gl%s_size( GLenum e )' % (name) |
| 226 | print '{' |
| 227 | |
| 228 | if not self.PrintUsingTable(): |
| 229 | self.PrintUsingSwitch() |
| 230 | |
| 231 | print '}' |
| 232 | print '' |
| 233 | |
| 234 | |
| 235 | |
| 236 | class glXEnum(gl_XML.glEnum): |
| 237 | def __init__(self, context, name, attrs): |
| 238 | gl_XML.glEnum.__init__(self, context, name, attrs) |
| 239 | self.glx_functions = [] |
| 240 | |
| 241 | def startElement(self, name, attrs): |
| 242 | if name == "size": |
| 243 | n = attrs.get('name', None) |
| 244 | if not self.context.glx_enum_functions.has_key( n ): |
| 245 | f = glXEnumFunction( n ) |
| 246 | self.context.glx_enum_functions[ f.name ] = f |
| 247 | |
| 248 | temp = attrs.get('count', None) |
| 249 | try: |
| 250 | c = int(temp) |
| 251 | except Exception,e: |
| 252 | raise RuntimeError('Invalid count value "%s" for enum "%s" in function "%s" when an integer was expected.' % (temp, self.name, n)) |
| 253 | |
| 254 | self.context.glx_enum_functions[ n ].append( c, self.value, self.name ) |
| 255 | else: |
| 256 | gl_XML.glEnum.startElement(self, context, name, attrs) |
| 257 | return |
| 258 | |
| 259 | |
| 260 | class glXParameter(gl_XML.glParameter): |
| 261 | def __init__(self, context, name, attrs): |
| 262 | self.order = 1; |
| 263 | gl_XML.glParameter.__init__(self, context, name, attrs); |
| 264 | |
| 265 | |
| 266 | class glXFunction(gl_XML.glFunction): |
| 267 | glx_rop = 0 |
| 268 | glx_sop = 0 |
| 269 | glx_vendorpriv = 0 |
| 270 | |
| 271 | # If this is set to true, it means that GLdouble parameters should be |
| 272 | # written to the GLX protocol packet in the order they appear in the |
| 273 | # prototype. This is different from the "classic" ordering. In the |
| 274 | # classic ordering GLdoubles are written to the protocol packet first, |
| 275 | # followed by non-doubles. NV_vertex_program was the first extension |
| 276 | # to break with this tradition. |
| 277 | |
| 278 | glx_doubles_in_order = 0 |
| 279 | |
| 280 | vectorequiv = None |
| 281 | handcode = 0 |
| 282 | ignore = 0 |
| 283 | can_be_large = 0 |
| 284 | |
| 285 | def __init__(self, context, name, attrs): |
| 286 | self.vectorequiv = attrs.get('vectorequiv', None) |
| 287 | self.count_parameters = None |
| 288 | self.counter = None |
| 289 | self.output = None |
| 290 | self.can_be_large = 0 |
| 291 | self.reply_always_array = 0 |
| 292 | |
| 293 | gl_XML.glFunction.__init__(self, context, name, attrs) |
| 294 | return |
| 295 | |
| 296 | def startElement(self, name, attrs): |
| 297 | """Process elements within a function that are specific to GLX.""" |
| 298 | |
| 299 | if name == "glx": |
| 300 | self.glx_rop = int(attrs.get('rop', "0")) |
| 301 | self.glx_sop = int(attrs.get('sop', "0")) |
| 302 | self.glx_vendorpriv = int(attrs.get('vendorpriv', "0")) |
| 303 | |
| 304 | if attrs.get('handcode', "false") == "true": |
| 305 | self.handcode = 1 |
| 306 | else: |
| 307 | self.handcode = 0 |
| 308 | |
| 309 | if attrs.get('ignore', "false") == "true": |
| 310 | self.ignore = 1 |
| 311 | else: |
| 312 | self.ignore = 0 |
| 313 | |
| 314 | if attrs.get('large', "false") == "true": |
| 315 | self.can_be_large = 1 |
| 316 | else: |
| 317 | self.can_be_large = 0 |
| 318 | |
| 319 | if attrs.get('doubles_in_order', "false") == "true": |
| 320 | self.glx_doubles_in_order = 1 |
| 321 | else: |
| 322 | self.glx_doubles_in_order = 0 |
| 323 | |
| 324 | if attrs.get('always_array', "false") == "true": |
| 325 | self.reply_always_array = 1 |
| 326 | else: |
| 327 | self.reply_always_array = 0 |
| 328 | |
| 329 | else: |
| 330 | gl_XML.glFunction.startElement(self, name, attrs) |
| 331 | |
| 332 | |
| 333 | def append(self, tag_name, p): |
| 334 | gl_XML.glFunction.append(self, tag_name, p) |
| 335 | |
| 336 | if p.is_variable_length_array(): |
| 337 | p.order = 2; |
| 338 | elif not self.glx_doubles_in_order and p.p_type.size == 8: |
| 339 | p.order = 0; |
| 340 | |
| 341 | if p.p_count_parameters != None: |
| 342 | self.count_parameters = p.p_count_parameters |
| 343 | |
| 344 | if p.is_counter: |
| 345 | self.counter = p.name |
| 346 | |
| 347 | if p.is_output: |
| 348 | self.output = p |
| 349 | |
| 350 | return |
| 351 | |
| 352 | def variable_length_parameter(self): |
| 353 | for param in self.fn_parameters: |
| 354 | if param.is_variable_length_array(): |
| 355 | return param |
| 356 | |
| 357 | return None |
| 358 | |
| 359 | |
| 360 | def command_payload_length(self): |
| 361 | size = 0 |
| 362 | size_string = "" |
| 363 | for p in self: |
| 364 | if p.is_output: continue |
| 365 | temp = p.size_string() |
| 366 | try: |
| 367 | s = int(temp) |
| 368 | size += s |
| 369 | except Exception,e: |
| 370 | size_string = size_string + " + __GLX_PAD(%s)" % (temp) |
| 371 | |
| 372 | return [size, size_string] |
| 373 | |
| 374 | def command_length(self): |
| 375 | [size, size_string] = self.command_payload_length() |
| 376 | |
| 377 | if self.glx_rop != 0: |
| 378 | size += 4 |
| 379 | |
| 380 | size = ((size + 3) & ~3) |
| 381 | return "%u%s" % (size, size_string) |
| 382 | |
| 383 | |
| 384 | def opcode_real_value(self): |
| 385 | if self.glx_vendorpriv != 0: |
| 386 | if self.needs_reply(): |
| 387 | return 17 |
| 388 | else: |
| 389 | return 16 |
| 390 | else: |
| 391 | return self.opcode_value() |
| 392 | |
| 393 | def opcode_value(self): |
| 394 | if self.glx_rop != 0: |
| 395 | return self.glx_rop |
| 396 | elif self.glx_sop != 0: |
| 397 | return self.glx_sop |
| 398 | elif self.glx_vendorpriv != 0: |
| 399 | return self.glx_vendorpriv |
| 400 | else: |
| 401 | return -1 |
| 402 | |
| 403 | def opcode_rop_basename(self): |
| 404 | if self.vectorequiv == None: |
| 405 | return self.name |
| 406 | else: |
| 407 | return self.vectorequiv |
| 408 | |
| 409 | def opcode_name(self): |
| 410 | if self.glx_rop != 0: |
| 411 | return "X_GLrop_%s" % (self.opcode_rop_basename()) |
| 412 | elif self.glx_sop != 0: |
| 413 | return "X_GLsop_%s" % (self.name) |
| 414 | elif self.glx_vendorpriv != 0: |
| 415 | return "X_GLvop_%s" % (self.name) |
| 416 | else: |
| 417 | return "ERROR" |
| 418 | |
| 419 | def opcode_real_name(self): |
| 420 | if self.glx_vendorpriv != 0: |
| 421 | if self.needs_reply(): |
| 422 | return "X_GLXVendorPrivateWithReply" |
| 423 | else: |
| 424 | return "X_GLXVendorPrivate" |
| 425 | else: |
| 426 | return self.opcode_name() |
| 427 | |
| 428 | |
| 429 | def return_string(self): |
| 430 | if self.fn_return_type != 'void': |
| 431 | return "return retval;" |
| 432 | else: |
| 433 | return "return;" |
| 434 | |
| 435 | |
| 436 | def needs_reply(self): |
| 437 | return self.fn_return_type != 'void' or self.output != None |
| 438 | |
| 439 | |
| 440 | class GlxProto(gl_XML.FilterGLAPISpecBase): |
| 441 | name = "glX_proto_send.py (from Mesa)" |
| 442 | |
| 443 | def __init__(self): |
| 444 | gl_XML.FilterGLAPISpecBase.__init__(self) |
| 445 | self.factory = glXItemFactory() |
| 446 | self.glx_enum_functions = {} |
| 447 | |
| 448 | |
| 449 | def endElement(self, name): |
| 450 | if name == 'OpenGLAPI': |
| 451 | # Once all the parsing is done, we have to go back and |
| 452 | # fix-up some cross references between different |
| 453 | # functions. |
| 454 | |
| 455 | for k in self.functions: |
| 456 | f = self.functions[k] |
| 457 | if f.vectorequiv != None: |
| 458 | equiv = self.find_function(f.vectorequiv) |
| 459 | if equiv != None: |
| 460 | f.glx_doubles_in_order = equiv.glx_doubles_in_order |
| 461 | f.glx_rop = equiv.glx_rop |
| 462 | else: |
| 463 | raise RuntimeError("Could not find the vector equiv. function %s for %s!" % (f.name, f.vectorequiv)) |
| 464 | else: |
| 465 | gl_XML.FilterGLAPISpecBase.endElement(self, name) |
| 466 | return |