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 glXFunction(gl_XML
.glFunction
):
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.
278 glx_doubles_in_order
= 0
285 def __init__(self
, context
, name
, attrs
):
286 self
.vectorequiv
= attrs
.get('vectorequiv', None)
287 self
.count_parameters
= None
290 self
.can_be_large
= 0
291 self
.reply_always_array
= 0
293 gl_XML
.glFunction
.__init
__(self
, context
, name
, attrs
)
296 def startElement(self
, name
, attrs
):
297 """Process elements within a function that are specific to 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"))
304 if attrs
.get('handcode', "false") == "true":
309 if attrs
.get('ignore', "false") == "true":
314 if attrs
.get('large', "false") == "true":
315 self
.can_be_large
= 1
317 self
.can_be_large
= 0
319 if attrs
.get('doubles_in_order', "false") == "true":
320 self
.glx_doubles_in_order
= 1
322 self
.glx_doubles_in_order
= 0
324 if attrs
.get('always_array', "false") == "true":
325 self
.reply_always_array
= 1
327 self
.reply_always_array
= 0
330 gl_XML
.glFunction
.startElement(self
, name
, attrs
)
333 def append(self
, tag_name
, p
):
334 gl_XML
.glFunction
.append(self
, tag_name
, p
)
336 if p
.is_variable_length_array():
338 elif not self
.glx_doubles_in_order
and p
.p_type
.size
== 8:
341 if p
.p_count_parameters
!= None:
342 self
.count_parameters
= p
.p_count_parameters
345 self
.counter
= p
.name
352 def variable_length_parameter(self
):
353 for param
in self
.fn_parameters
:
354 if param
.is_variable_length_array():
360 def command_payload_length(self
):
364 if p
.is_output
: continue
365 temp
= p
.size_string()
370 size_string
= size_string
+ " + __GLX_PAD(%s)" % (temp
)
372 return [size
, size_string
]
374 def command_length(self
):
375 [size
, size_string
] = self
.command_payload_length()
377 if self
.glx_rop
!= 0:
380 size
= ((size
+ 3) & ~
3)
381 return "%u%s" % (size
, size_string
)
384 def opcode_real_value(self
):
385 if self
.glx_vendorpriv
!= 0:
386 if self
.needs_reply():
391 return self
.opcode_value()
393 def opcode_value(self
):
394 if self
.glx_rop
!= 0:
396 elif self
.glx_sop
!= 0:
398 elif self
.glx_vendorpriv
!= 0:
399 return self
.glx_vendorpriv
403 def opcode_rop_basename(self
):
404 if self
.vectorequiv
== None:
407 return self
.vectorequiv
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
)
419 def opcode_real_name(self
):
420 if self
.glx_vendorpriv
!= 0:
421 if self
.needs_reply():
422 return "X_GLXVendorPrivateWithReply"
424 return "X_GLXVendorPrivate"
426 return self
.opcode_name()
429 def return_string(self
):
430 if self
.fn_return_type
!= 'void':
431 return "return retval;"
436 def needs_reply(self
):
437 return self
.fn_return_type
!= 'void' or self
.output
!= None
440 class GlxProto(gl_XML
.FilterGLAPISpecBase
):
441 name
= "glX_proto_send.py (from Mesa)"
444 gl_XML
.FilterGLAPISpecBase
.__init
__(self
)
445 self
.factory
= glXItemFactory()
446 self
.glx_enum_functions
= {}
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
455 for k
in self
.functions
:
456 f
= self
.functions
[k
]
457 if f
.vectorequiv
!= None:
458 equiv
= self
.find_function(f
.vectorequiv
)
460 f
.glx_doubles_in_order
= equiv
.glx_doubles_in_order
461 f
.glx_rop
= equiv
.glx_rop
463 raise RuntimeError("Could not find the vector equiv. function %s for %s!" % (f
.name
, f
.vectorequiv
))
465 gl_XML
.FilterGLAPISpecBase
.endElement(self
, name
)