blob: 6da454c58c12ea29dcd1f72547061c5eb9898755 [file] [log] [blame]
Ian Romanick74764062004-12-03 20:31:59 +00001#!/usr/bin/python2
2
Ian Romanick5f1f2292005-01-07 02:39:09 +00003# (C) Copyright IBM Corporation 2004, 2005
Ian Romanick74764062004-12-03 20:31:59 +00004# 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
28from xml.sax import saxutils
29from xml.sax import make_parser
30from xml.sax.handler import feature_namespaces
31
32import gl_XML
33import license
34import sys, getopt
35
36
37def printPure():
38 print """# if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
39# define PURE __attribute__((pure))
40# else
41# define PURE
42# endif"""
43
44def printFastcall():
45 print """# if defined(__i386__) && defined(__GNUC__)
46# define FASTCALL __attribute__((fastcall))
47# else
48# define FASTCALL
49# endif"""
50
51def 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
58def printNoinline():
59 print """# if defined(__GNUC__)
60# define NOINLINE __attribute__((noinline))
61# else
62# define NOINLINE
63# endif"""
64
65
66class 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
79class 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
236class 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
260class 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
Ian Romanick1d270842004-12-21 21:26:36 +0000266class glXParameterIterator:
267 """Class to iterate over a list of glXParameters.
268
269 Objects of this class are returned by the parameterIterator method of
270 the glXFunction class. They are used to iterate over the list of
271 parameters to the function."""
272
273 def __init__(self, data, skip_output, max_order):
274 self.data = data
275 self.index = 0
276 self.order = 0
277 self.skip_output = skip_output
278 self.max_order = max_order
279
280 def __iter__(self):
281 return self
282
283 def next(self):
284 if len( self.data ) == 0:
285 raise StopIteration
286
287 while 1:
288 if self.index == len( self.data ):
289 if self.order == self.max_order:
290 raise StopIteration
291 else:
292 self.order += 1
293 self.index = 0
294
295 i = self.index
296 self.index += 1
297
298 if self.data[i].order == self.order and not (self.data[i].is_output and self.skip_output):
299 return self.data[i]
300
301
Ian Romanick74764062004-12-03 20:31:59 +0000302class glXFunction(gl_XML.glFunction):
303 glx_rop = 0
304 glx_sop = 0
305 glx_vendorpriv = 0
306
307 # If this is set to true, it means that GLdouble parameters should be
308 # written to the GLX protocol packet in the order they appear in the
309 # prototype. This is different from the "classic" ordering. In the
310 # classic ordering GLdoubles are written to the protocol packet first,
311 # followed by non-doubles. NV_vertex_program was the first extension
312 # to break with this tradition.
313
314 glx_doubles_in_order = 0
315
316 vectorequiv = None
317 handcode = 0
318 ignore = 0
319 can_be_large = 0
320
321 def __init__(self, context, name, attrs):
322 self.vectorequiv = attrs.get('vectorequiv', None)
323 self.count_parameters = None
324 self.counter = None
325 self.output = None
326 self.can_be_large = 0
327 self.reply_always_array = 0
328
329 gl_XML.glFunction.__init__(self, context, name, attrs)
330 return
331
Ian Romanick1d270842004-12-21 21:26:36 +0000332
333 def parameterIterator(self, skip_output, max_order):
334 return glXParameterIterator(self.fn_parameters, skip_output, max_order)
335
336
Ian Romanick74764062004-12-03 20:31:59 +0000337 def startElement(self, name, attrs):
338 """Process elements within a function that are specific to GLX."""
339
340 if name == "glx":
341 self.glx_rop = int(attrs.get('rop', "0"))
342 self.glx_sop = int(attrs.get('sop', "0"))
343 self.glx_vendorpriv = int(attrs.get('vendorpriv', "0"))
344
345 if attrs.get('handcode', "false") == "true":
346 self.handcode = 1
347 else:
348 self.handcode = 0
349
350 if attrs.get('ignore', "false") == "true":
351 self.ignore = 1
352 else:
353 self.ignore = 0
354
355 if attrs.get('large', "false") == "true":
356 self.can_be_large = 1
357 else:
358 self.can_be_large = 0
359
360 if attrs.get('doubles_in_order', "false") == "true":
361 self.glx_doubles_in_order = 1
362 else:
363 self.glx_doubles_in_order = 0
364
365 if attrs.get('always_array', "false") == "true":
366 self.reply_always_array = 1
367 else:
368 self.reply_always_array = 0
369
370 else:
371 gl_XML.glFunction.startElement(self, name, attrs)
372
373
374 def append(self, tag_name, p):
375 gl_XML.glFunction.append(self, tag_name, p)
376
377 if p.is_variable_length_array():
378 p.order = 2;
379 elif not self.glx_doubles_in_order and p.p_type.size == 8:
380 p.order = 0;
381
382 if p.p_count_parameters != None:
383 self.count_parameters = p.p_count_parameters
384
385 if p.is_counter:
386 self.counter = p.name
387
388 if p.is_output:
389 self.output = p
390
391 return
392
393 def variable_length_parameter(self):
394 for param in self.fn_parameters:
395 if param.is_variable_length_array():
396 return param
397
398 return None
399
400
401 def command_payload_length(self):
402 size = 0
Ian Romanick5f1f2292005-01-07 02:39:09 +0000403
404 if self.image:
405 [dim, junk, junk, junk, junk] = self.dimensions()
406
407 # The base size is the size of the pixel pack info
408 # header used by images with the specified number
409 # of dimensions.
410
411 if dim <= 2:
412 size = 20
413 elif dim <= 4:
414 size = 36
415 else:
416 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim, self.image.name, self.name))
417
418 if self.image.img_null_flag:
419 size += 4
420
421 if self.image.img_pad_dimensions:
422 size += 4 * (dim & 1)
423
424 # If the image has offset parameters, like
425 # TexSubImage1D or TexSubImage3D, they need to
426 # be padded out as well.
427
428 if self.image.img_xoff:
429 size += 4 * (dim & 1)
430
431
432
Ian Romanick74764062004-12-03 20:31:59 +0000433 size_string = ""
Ian Romanick1d270842004-12-21 21:26:36 +0000434 for p in gl_XML.glFunction.parameterIterator(self):
Ian Romanick74764062004-12-03 20:31:59 +0000435 if p.is_output: continue
436 temp = p.size_string()
437 try:
438 s = int(temp)
439 size += s
440 except Exception,e:
441 size_string = size_string + " + __GLX_PAD(%s)" % (temp)
442
443 return [size, size_string]
444
445 def command_length(self):
446 [size, size_string] = self.command_payload_length()
447
448 if self.glx_rop != 0:
449 size += 4
450
451 size = ((size + 3) & ~3)
452 return "%u%s" % (size, size_string)
453
454
455 def opcode_real_value(self):
Ian Romanick1d270842004-12-21 21:26:36 +0000456 """Get the true numeric value of the GLX opcode
457
458 Behaves similarly to opcode_value, except for
459 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
460 In these cases the value for the GLX opcode field (i.e.,
461 16 for X_GLXVendorPrivate or 17 for
462 X_GLXVendorPrivateWithReply) is returned. For other 'single'
463 commands, the opcode for the command (e.g., 101 for
464 X_GLsop_NewList) is returned."""
465
Ian Romanick74764062004-12-03 20:31:59 +0000466 if self.glx_vendorpriv != 0:
467 if self.needs_reply():
468 return 17
469 else:
470 return 16
471 else:
472 return self.opcode_value()
473
474 def opcode_value(self):
Ian Romanick1d270842004-12-21 21:26:36 +0000475 """Get the unique protocol opcode for the glXFunction"""
476
Ian Romanick74764062004-12-03 20:31:59 +0000477 if self.glx_rop != 0:
478 return self.glx_rop
479 elif self.glx_sop != 0:
480 return self.glx_sop
481 elif self.glx_vendorpriv != 0:
482 return self.glx_vendorpriv
483 else:
484 return -1
485
486 def opcode_rop_basename(self):
Ian Romanick1d270842004-12-21 21:26:36 +0000487 """Return either the name to be used for GLX protocol enum.
488
489 Returns either the name of the function or the name of the
490 name of the equivalent vector (e.g., glVertex3fv for
491 glVertex3f) function."""
492
Ian Romanick74764062004-12-03 20:31:59 +0000493 if self.vectorequiv == None:
494 return self.name
495 else:
496 return self.vectorequiv
497
498 def opcode_name(self):
Ian Romanick1d270842004-12-21 21:26:36 +0000499 """Get the unique protocol enum name for the glXFunction"""
500
Ian Romanick74764062004-12-03 20:31:59 +0000501 if self.glx_rop != 0:
502 return "X_GLrop_%s" % (self.opcode_rop_basename())
503 elif self.glx_sop != 0:
504 return "X_GLsop_%s" % (self.name)
505 elif self.glx_vendorpriv != 0:
506 return "X_GLvop_%s" % (self.name)
507 else:
508 return "ERROR"
509
510 def opcode_real_name(self):
Ian Romanick1d270842004-12-21 21:26:36 +0000511 """Get the true protocol enum name for the GLX opcode
512
513 Behaves similarly to opcode_name, except for
514 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
515 In these cases the string 'X_GLXVendorPrivate' or
516 'X_GLXVendorPrivateWithReply' is returned. For other
517 single or render commands 'X_GLsop' or 'X_GLrop' plus the
518 name of the function returned."""
519
Ian Romanick74764062004-12-03 20:31:59 +0000520 if self.glx_vendorpriv != 0:
521 if self.needs_reply():
522 return "X_GLXVendorPrivateWithReply"
523 else:
524 return "X_GLXVendorPrivate"
525 else:
526 return self.opcode_name()
527
528
529 def return_string(self):
530 if self.fn_return_type != 'void':
531 return "return retval;"
532 else:
533 return "return;"
534
535
536 def needs_reply(self):
537 return self.fn_return_type != 'void' or self.output != None
538
539
Ian Romanick5f1f2292005-01-07 02:39:09 +0000540 def dimensions(self):
541 """Determine the dimensions of an image.
542
543 Returns a tuple representing the number of dimensions and the
544 string name of each of the dimensions of an image, If the
545 function is not a pixel function, the number of dimensions
546 will be zero."""
547
548 if not self.image:
549 return [0, "0", "0", "0", "0"]
550 else:
551 dim = 1
552 w = self.image.width
553
554 if self.image.height:
555 dim = 2
556 h = self.image.height
557 else:
558 h = "1"
559
560 if self.image.depth:
561 dim = 3
562 d = self.image.depth
563 else:
564 d = "1"
565
566 if self.image.extent:
567 dim = 4
568 e = self.image.extent
569 else:
570 e = "1"
571
572 return [dim, w, h, d, e]
573
574
575 def pad_after(self, p):
576 """Returns the name of the field inserted after the
577 specified field to pad out the command header."""
578
579 if self.image and self.image.img_pad_dimensions:
580 if not self.image.height:
581 if p.name == self.image.width:
582 return "height"
583 elif p.name == self.image.img_xoff:
584 return "yoffset"
585 elif not self.image.extent:
586 if p.name == self.image.depth:
587 # Should this be "size4d"?
588 return "extent"
589 elif p.name == self.image.img_zoff:
590 return "woffset"
591 return None
592
593
Ian Romanick74764062004-12-03 20:31:59 +0000594class GlxProto(gl_XML.FilterGLAPISpecBase):
595 name = "glX_proto_send.py (from Mesa)"
596
597 def __init__(self):
598 gl_XML.FilterGLAPISpecBase.__init__(self)
599 self.factory = glXItemFactory()
600 self.glx_enum_functions = {}
601
602
603 def endElement(self, name):
604 if name == 'OpenGLAPI':
605 # Once all the parsing is done, we have to go back and
606 # fix-up some cross references between different
607 # functions.
608
609 for k in self.functions:
610 f = self.functions[k]
611 if f.vectorequiv != None:
612 equiv = self.find_function(f.vectorequiv)
613 if equiv != None:
614 f.glx_doubles_in_order = equiv.glx_doubles_in_order
615 f.glx_rop = equiv.glx_rop
616 else:
617 raise RuntimeError("Could not find the vector equiv. function %s for %s!" % (f.name, f.vectorequiv))
618 else:
619 gl_XML.FilterGLAPISpecBase.endElement(self, name)
620 return