3 # (C) Copyright IBM Corporation 2004
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:
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
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
26 # Ian Romanick <idr@us.ibm.com>
28 from xml
.sax
import saxutils
29 from xml
.sax
import make_parser
30 from xml
.sax
.handler
import feature_namespaces
38 print """# if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
39 # define PURE __attribute__((pure))
45 print """# if defined(__i386__) && defined(__GNUC__)
46 # define FASTCALL __attribute__((fastcall))
51 def printVisibility(S
, s
):
52 print """# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
53 # define %s __attribute__((visibility("%s")))
56 # endif""" % (S
, s
, S
)
59 print """# if defined(__GNUC__)
60 # define NOINLINE __attribute__((noinline))
66 class glXItemFactory(gl_XML
.glItemFactory
):
67 """Factory to create GLX protocol oriented objects derived from glItem."""
69 def create(self
, context
, name
, attrs
):
70 if name
== "function":
71 return glXFunction(context
, name
, attrs
)
73 return glXEnum(context
, name
, attrs
)
75 return glXParameter(context
, name
, attrs
)
77 return gl_XML
.glItemFactory
.create(self
, context
, name
, attrs
)
79 class glXEnumFunction
:
80 def __init__(self
, name
):
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"}.
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).
101 def append(self
, count
, value
, name
):
102 if self
.enums
.has_key( value
):
103 self
.enums
[value
].append(name
)
105 if not self
.count
.has_key(count
):
106 self
.count
[count
] = []
108 self
.enums
[value
] = []
109 self
.enums
[value
].append(name
)
110 self
.count
[count
].append(value
)
113 def signature( self
):
116 for e
in self
.count
[i
]:
117 sig
+= "%04x,%u," % (e
, i
)
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
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)."""
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.
146 for i
in [1, 2, 3, 4, 5, 6, 7, 8]:
153 if (a
& mask
) == (b
& mask
):
161 if (mask
!= 0) and (mask
< (2 * count
)):
165 for i
in range(0, mask
+ 1):
166 masked_enums
[i
] = "0";
170 for e
in self
.count
[c
]:
172 masked_enums
[i
] = '0x%04x /* %s */' % (e
, self
.enums
[e
][0])
176 print ' static const GLushort a[%u] = {' % (mask
+ 1)
177 for e
in masked_enums
:
178 print ' %s, ' % (masked_enums
[e
])
181 print ' static const GLubyte b[%u] = {' % (mask
+ 1)
182 for c
in masked_count
:
183 print ' %u, ' % (masked_count
[c
])
186 print ' const unsigned idx = (e & 0x%02xU);' % (mask
)
188 print ' return (e == a[idx]) ? (GLint) b[idx] : 0;'
193 def PrintUsingSwitch(self
):
194 """Emit the body of the __gl*_size function using a
197 print ' switch( e ) {'
200 for e
in self
.count
[c
]:
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.
210 for j
in self
.enums
[e
]:
212 print ' case %s:' % (j
)
215 print '/* case %s:*/' % (j
)
217 print ' return %u;' % (c
)
219 print ' default: return 0;'
223 def Print(self
, name
):
224 print 'INTERNAL PURE FASTCALL GLint'
225 print '__gl%s_size( GLenum e )' % (name
)
228 if not self
.PrintUsingTable():
229 self
.PrintUsingSwitch()
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
= []
241 def startElement(self
, name
, attrs
):
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
248 temp
= attrs
.get('count', None)
252 raise RuntimeError('Invalid count value "%s" for enum "%s" in function "%s" when an integer was expected.' % (temp
, self
.name
, n
))
254 self
.context
.glx_enum_functions
[ n
].append( c
, self
.value
, self
.name
)
256 gl_XML
.glEnum
.startElement(self
, context
, name
, attrs
)
260 class glXParameter(gl_XML
.glParameter
):
261 def __init__(self
, context
, name
, attrs
):
263 gl_XML
.glParameter
.__init
__(self
, context
, name
, attrs
);
266 class glXParameterIterator
:
267 """Class to iterate over a list of glXParameters.
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."""
273 def __init__(self
, data
, skip_output
, max_order
):
277 self
.skip_output
= skip_output
278 self
.max_order
= max_order
284 if len( self
.data
) == 0:
288 if self
.index
== len( self
.data
):
289 if self
.order
== self
.max_order
:
298 if self
.data
[i
].order
== self
.order
and not (self
.data
[i
].is_output
and self
.skip_output
):
302 class glXFunction(gl_XML
.glFunction
):
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.
314 glx_doubles_in_order
= 0
321 def __init__(self
, context
, name
, attrs
):
322 self
.vectorequiv
= attrs
.get('vectorequiv', None)
323 self
.count_parameters
= None
326 self
.can_be_large
= 0
327 self
.reply_always_array
= 0
329 gl_XML
.glFunction
.__init
__(self
, context
, name
, attrs
)
333 def parameterIterator(self
, skip_output
, max_order
):
334 return glXParameterIterator(self
.fn_parameters
, skip_output
, max_order
)
337 def startElement(self
, name
, attrs
):
338 """Process elements within a function that are specific to 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"))
345 if attrs
.get('handcode', "false") == "true":
350 if attrs
.get('ignore', "false") == "true":
355 if attrs
.get('large', "false") == "true":
356 self
.can_be_large
= 1
358 self
.can_be_large
= 0
360 if attrs
.get('doubles_in_order', "false") == "true":
361 self
.glx_doubles_in_order
= 1
363 self
.glx_doubles_in_order
= 0
365 if attrs
.get('always_array', "false") == "true":
366 self
.reply_always_array
= 1
368 self
.reply_always_array
= 0
371 gl_XML
.glFunction
.startElement(self
, name
, attrs
)
374 def append(self
, tag_name
, p
):
375 gl_XML
.glFunction
.append(self
, tag_name
, p
)
377 if p
.is_variable_length_array():
379 elif not self
.glx_doubles_in_order
and p
.p_type
.size
== 8:
382 if p
.p_count_parameters
!= None:
383 self
.count_parameters
= p
.p_count_parameters
386 self
.counter
= p
.name
393 def variable_length_parameter(self
):
394 for param
in self
.fn_parameters
:
395 if param
.is_variable_length_array():
401 def command_payload_length(self
):
404 for p
in gl_XML
.glFunction
.parameterIterator(self
):
405 if p
.is_output
: continue
406 temp
= p
.size_string()
411 size_string
= size_string
+ " + __GLX_PAD(%s)" % (temp
)
413 return [size
, size_string
]
415 def command_length(self
):
416 [size
, size_string
] = self
.command_payload_length()
418 if self
.glx_rop
!= 0:
421 size
= ((size
+ 3) & ~
3)
422 return "%u%s" % (size
, size_string
)
425 def opcode_real_value(self
):
426 """Get the true numeric value of the GLX opcode
428 Behaves similarly to opcode_value, except for
429 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
430 In these cases the value for the GLX opcode field (i.e.,
431 16 for X_GLXVendorPrivate or 17 for
432 X_GLXVendorPrivateWithReply) is returned. For other 'single'
433 commands, the opcode for the command (e.g., 101 for
434 X_GLsop_NewList) is returned."""
436 if self
.glx_vendorpriv
!= 0:
437 if self
.needs_reply():
442 return self
.opcode_value()
444 def opcode_value(self
):
445 """Get the unique protocol opcode for the glXFunction"""
447 if self
.glx_rop
!= 0:
449 elif self
.glx_sop
!= 0:
451 elif self
.glx_vendorpriv
!= 0:
452 return self
.glx_vendorpriv
456 def opcode_rop_basename(self
):
457 """Return either the name to be used for GLX protocol enum.
459 Returns either the name of the function or the name of the
460 name of the equivalent vector (e.g., glVertex3fv for
461 glVertex3f) function."""
463 if self
.vectorequiv
== None:
466 return self
.vectorequiv
468 def opcode_name(self
):
469 """Get the unique protocol enum name for the glXFunction"""
471 if self
.glx_rop
!= 0:
472 return "X_GLrop_%s" % (self
.opcode_rop_basename())
473 elif self
.glx_sop
!= 0:
474 return "X_GLsop_%s" % (self
.name
)
475 elif self
.glx_vendorpriv
!= 0:
476 return "X_GLvop_%s" % (self
.name
)
480 def opcode_real_name(self
):
481 """Get the true protocol enum name for the GLX opcode
483 Behaves similarly to opcode_name, except for
484 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
485 In these cases the string 'X_GLXVendorPrivate' or
486 'X_GLXVendorPrivateWithReply' is returned. For other
487 single or render commands 'X_GLsop' or 'X_GLrop' plus the
488 name of the function returned."""
490 if self
.glx_vendorpriv
!= 0:
491 if self
.needs_reply():
492 return "X_GLXVendorPrivateWithReply"
494 return "X_GLXVendorPrivate"
496 return self
.opcode_name()
499 def return_string(self
):
500 if self
.fn_return_type
!= 'void':
501 return "return retval;"
506 def needs_reply(self
):
507 return self
.fn_return_type
!= 'void' or self
.output
!= None
510 class GlxProto(gl_XML
.FilterGLAPISpecBase
):
511 name
= "glX_proto_send.py (from Mesa)"
514 gl_XML
.FilterGLAPISpecBase
.__init
__(self
)
515 self
.factory
= glXItemFactory()
516 self
.glx_enum_functions
= {}
519 def endElement(self
, name
):
520 if name
== 'OpenGLAPI':
521 # Once all the parsing is done, we have to go back and
522 # fix-up some cross references between different
525 for k
in self
.functions
:
526 f
= self
.functions
[k
]
527 if f
.vectorequiv
!= None:
528 equiv
= self
.find_function(f
.vectorequiv
)
530 f
.glx_doubles_in_order
= equiv
.glx_doubles_in_order
531 f
.glx_rop
= equiv
.glx_rop
533 raise RuntimeError("Could not find the vector equiv. function %s for %s!" % (f
.name
, f
.vectorequiv
))
535 gl_XML
.FilterGLAPISpecBase
.endElement(self
, name
)