3 # (C) Copyright IBM Corporation 2004, 2005
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
34 import sys
, getopt
, string
37 class glXItemFactory(gl_XML
.glItemFactory
):
38 """Factory to create GLX protocol oriented objects derived from glItem."""
40 def create(self
, context
, name
, attrs
):
41 if name
== "function":
42 return glXFunction(context
, name
, attrs
)
44 return glXEnum(context
, name
, attrs
)
46 return glXParameter(context
, name
, attrs
)
48 return gl_XML
.glItemFactory
.create(self
, context
, name
, attrs
)
50 class glXEnumFunction
:
51 def __init__(self
, name
, context
):
53 self
.context
= context
57 # "enums" is a set of lists. The element in the set is the
58 # value of the enum. The list is the list of names for that
59 # value. For example, [0x8126] = {"POINT_SIZE_MIN",
60 # "POINT_SIZE_MIN_ARB", "POINT_SIZE_MIN_EXT",
61 # "POINT_SIZE_MIN_SGIS"}.
65 # "count" is indexed by count values. Each element of count
66 # is a list of index to "enums" that have that number of
67 # associated data elements. For example, [4] =
68 # {GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_EMISSION,
69 # GL_AMBIENT_AND_DIFFUSE} (the enum names are used here,
70 # but the actual hexadecimal values would be in the array).
75 def append(self
, count
, value
, name
):
76 if self
.enums
.has_key( value
):
77 self
.enums
[value
].append(name
)
79 if not self
.count
.has_key(count
):
80 self
.count
[count
] = []
82 self
.enums
[value
] = []
83 self
.enums
[value
].append(name
)
84 self
.count
[count
].append(value
)
87 def signature( self
):
92 for e
in self
.count
[i
]:
93 self
.sig
+= "%04x,%u," % (e
, i
)
98 def set_mode( self
, mode
):
99 """Mark an enum-function as a 'set' function."""
108 def PrintUsingTable(self
):
109 """Emit the body of the __gl*_size function using a pair
110 of look-up tables and a mask. The mask is calculated such
111 that (e & mask) is unique for all the valid values of e for
112 this function. The result of (e & mask) is used as an index
113 into the first look-up table. If it matches e, then the
114 same entry of the second table is returned. Otherwise zero
117 It seems like this should cause better code to be generated.
118 However, on x86 at least, the resulting .o file is about 20%
119 larger then the switch-statment version. I am leaving this
120 code in because the results may be different on other
121 platforms (e.g., PowerPC or x86-64)."""
128 # Determine if there is some mask M, such that M = (2^N) - 1,
129 # that will generate unique values for all of the enums.
132 for i
in [1, 2, 3, 4, 5, 6, 7, 8]:
139 if (a
& mask
) == (b
& mask
):
147 if (mask
!= 0) and (mask
< (2 * count
)):
151 for i
in range(0, mask
+ 1):
152 masked_enums
[i
] = "0";
156 for e
in self
.count
[c
]:
158 masked_enums
[i
] = '0x%04x /* %s */' % (e
, self
.enums
[e
][0])
162 print ' static const GLushort a[%u] = {' % (mask
+ 1)
163 for e
in masked_enums
:
164 print ' %s, ' % (masked_enums
[e
])
167 print ' static const GLubyte b[%u] = {' % (mask
+ 1)
168 for c
in masked_count
:
169 print ' %u, ' % (masked_count
[c
])
172 print ' const unsigned idx = (e & 0x%02xU);' % (mask
)
174 print ' return (e == a[idx]) ? (GLint) b[idx] : 0;'
179 def PrintUsingSwitch(self
):
180 """Emit the body of the __gl*_size function using a
183 print ' switch( e ) {'
186 for e
in self
.count
[c
]:
189 # There may be multiple enums with the same
190 # value. This happens has extensions are
191 # promoted from vendor-specific or EXT to
192 # ARB and to the core. Emit the first one as
193 # a case label, and emit the others as
194 # commented-out case labels.
196 for j
in self
.enums
[e
]:
198 print ' case %s:' % (j
)
201 print '/* case %s:*/' % (j
)
203 print ' return %u;' % (c
)
205 print ' default: return 0;'
209 def Print(self
, name
):
210 print 'INTERNAL PURE FASTCALL GLint'
211 print '__gl%s_size( GLenum e )' % (name
)
214 if not self
.PrintUsingTable():
215 self
.PrintUsingSwitch()
222 class glXEnum(gl_XML
.glEnum
):
223 def __init__(self
, context
, name
, attrs
):
224 gl_XML
.glEnum
.__init
__(self
, context
, name
, attrs
)
227 def startElement(self
, name
, attrs
):
229 [n
, c
, mode
] = self
.process_attributes(attrs
)
231 if not self
.context
.glx_enum_functions
.has_key( n
):
232 f
= self
.context
.createEnumFunction( n
)
234 self
.context
.glx_enum_functions
[ f
.name
] = f
236 self
.context
.glx_enum_functions
[ n
].append( c
, self
.value
, self
.name
)
238 gl_XML
.glEnum
.startElement(self
, context
, name
, attrs
)
242 class glXParameter(gl_XML
.glParameter
):
243 def __init__(self
, context
, name
, attrs
):
245 gl_XML
.glParameter
.__init
__(self
, context
, name
, attrs
);
248 class glXParameterIterator
:
249 """Class to iterate over a list of glXParameters.
251 Objects of this class are returned by the parameterIterator method of
252 the glXFunction class. They are used to iterate over the list of
253 parameters to the function."""
255 def __init__(self
, data
, skip_output
, max_order
):
259 self
.skip_output
= skip_output
260 self
.max_order
= max_order
266 if len( self
.data
) == 0:
270 if self
.index
== len( self
.data
):
271 if self
.order
== self
.max_order
:
280 if self
.data
[i
].order
== self
.order
and not (self
.data
[i
].is_output
and self
.skip_output
):
284 class glXFunction(gl_XML
.glFunction
):
289 # If this is set to true, it means that GLdouble parameters should be
290 # written to the GLX protocol packet in the order they appear in the
291 # prototype. This is different from the "classic" ordering. In the
292 # classic ordering GLdoubles are written to the protocol packet first,
293 # followed by non-doubles. NV_vertex_program was the first extension
294 # to break with this tradition.
296 glx_doubles_in_order
= 0
301 def __init__(self
, context
, name
, attrs
):
302 self
.vectorequiv
= attrs
.get('vectorequiv', None)
305 self
.can_be_large
= 0
306 self
.reply_always_array
= 0
307 self
.dimensions_in_reply
= 0
308 self
.img_reset
= None
310 self
.server_handcode
= 0
311 self
.client_handcode
= 0
314 gl_XML
.glFunction
.__init
__(self
, context
, name
, attrs
)
318 def parameterIterator(self
, skip_output
, max_order
):
319 return glXParameterIterator(self
.fn_parameters
, skip_output
, max_order
)
322 def startElement(self
, name
, attrs
):
323 """Process elements within a function that are specific to GLX."""
326 self
.glx_rop
= int(attrs
.get('rop', "0"))
327 self
.glx_sop
= int(attrs
.get('sop', "0"))
328 self
.glx_vendorpriv
= int(attrs
.get('vendorpriv', "0"))
329 self
.img_reset
= attrs
.get('img_reset', None)
331 # The 'handcode' attribute can be one of 'true',
332 # 'false', 'client', or 'server'.
334 handcode
= attrs
.get('handcode', "false")
335 if handcode
== "false":
336 self
.server_handcode
= 0
337 self
.client_handcode
= 0
338 elif handcode
== "true":
339 self
.server_handcode
= 1
340 self
.client_handcode
= 1
341 elif handcode
== "client":
342 self
.server_handcode
= 0
343 self
.client_handcode
= 1
344 elif handcode
== "server":
345 self
.server_handcode
= 1
346 self
.client_handcode
= 0
348 raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode
, self
.name
))
351 if attrs
.get('ignore', "false") == "true":
356 if attrs
.get('large', "false") == "true":
357 self
.can_be_large
= 1
359 self
.can_be_large
= 0
361 if attrs
.get('doubles_in_order', "false") == "true":
362 self
.glx_doubles_in_order
= 1
364 self
.glx_doubles_in_order
= 0
366 if attrs
.get('always_array', "false") == "true":
367 self
.reply_always_array
= 1
369 self
.reply_always_array
= 0
371 if attrs
.get('dimensions_in_reply', "false") == "true":
372 self
.dimensions_in_reply
= 1
374 self
.dimensions_in_reply
= 0
376 gl_XML
.glFunction
.startElement(self
, name
, attrs
)
379 def endElement(self
, name
):
380 if name
== "function":
381 # Mark any function that does not have GLX protocol
382 # defined as "ignore". This prevents bad things from
383 # happening when people add new functions to the GL
384 # API XML without adding any GLX section.
386 # This will also mark functions that don't have a
387 # dispatch offset at ignored.
389 if (self
.fn_offset
== -1 and not self
.fn_alias
) or not (self
.client_handcode
or self
.server_handcode
or self
.glx_rop
or self
.glx_sop
or self
.glx_vendorpriv
or self
.vectorequiv
or self
.fn_alias
):
391 # if self.fn_offset == -1:
392 # print '/* %s ignored becuase no offset assigned. */' % (self.name)
394 # print '/* %s ignored becuase no GLX opcode assigned. */' % (self.name)
398 return gl_XML
.glFunction
.endElement(self
, name
)
401 def append(self
, tag_name
, p
):
402 gl_XML
.glFunction
.append(self
, tag_name
, p
)
404 if p
.is_variable_length_array():
406 elif not self
.glx_doubles_in_order
and p
.p_type
.size
== 8:
410 self
.counter
= p
.name
418 def variable_length_parameter(self
):
419 for param
in self
.fn_parameters
:
420 if param
.is_variable_length_array():
426 def output_parameter(self
):
427 for param
in self
.fn_parameters
:
434 def offset_of_first_parameter(self
):
435 """Get the offset of the first parameter in the command.
437 Gets the offset of the first function parameter in the GLX
438 command packet. This byte offset is measured from the end
439 of the Render / RenderLarge header. The offset for all non-
440 pixel commends is zero. The offset for pixel commands depends
441 on the number of dimensions of the pixel data."""
443 if self
.image
and not self
.image
.is_output
:
444 [dim
, junk
, junk
, junk
, junk
] = self
.dimensions()
446 # The base size is the size of the pixel pack info
447 # header used by images with the specified number
455 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim
, self
.image
.name
, self
.name
))
460 def command_fixed_length(self
):
461 """Return the length, in bytes as an integer, of the
462 fixed-size portion of the command."""
464 size
= self
.offset_of_first_parameter()
466 for p
in gl_XML
.glFunction
.parameterIterator(self
):
467 if not p
.is_output
and p
.name
!= self
.img_reset
:
469 if self
.pad_after(p
):
472 if self
.image
and (self
.image
.img_null_flag
or self
.image
.is_output
):
478 def command_variable_length(self
):
479 """Return the length, as a string, of the variable-sized
480 portion of the command."""
483 for p
in gl_XML
.glFunction
.parameterIterator(self
):
484 if (not p
.is_output
) and (p
.size() == 0):
485 size_string
= size_string
+ " + __GLX_PAD(%s)" % (p
.size_string())
490 def command_length(self
):
491 size
= self
.command_fixed_length()
493 if self
.glx_rop
!= 0:
496 size
= ((size
+ 3) & ~
3)
497 return "%u%s" % (size
, self
.command_variable_length())
500 def opcode_real_value(self
):
501 """Get the true numeric value of the GLX opcode
503 Behaves similarly to opcode_value, except for
504 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
505 In these cases the value for the GLX opcode field (i.e.,
506 16 for X_GLXVendorPrivate or 17 for
507 X_GLXVendorPrivateWithReply) is returned. For other 'single'
508 commands, the opcode for the command (e.g., 101 for
509 X_GLsop_NewList) is returned."""
511 if self
.glx_vendorpriv
!= 0:
512 if self
.needs_reply():
517 return self
.opcode_value()
519 def opcode_value(self
):
520 """Get the unique protocol opcode for the glXFunction"""
522 if self
.glx_rop
!= 0:
524 elif self
.glx_sop
!= 0:
526 elif self
.glx_vendorpriv
!= 0:
527 return self
.glx_vendorpriv
531 def opcode_rop_basename(self
):
532 """Return either the name to be used for GLX protocol enum.
534 Returns either the name of the function or the name of the
535 name of the equivalent vector (e.g., glVertex3fv for
536 glVertex3f) function."""
538 if self
.vectorequiv
== None:
541 return self
.vectorequiv
543 def opcode_name(self
):
544 """Get the unique protocol enum name for the glXFunction"""
546 if self
.glx_rop
!= 0:
547 return "X_GLrop_%s" % (self
.opcode_rop_basename())
548 elif self
.glx_sop
!= 0:
549 return "X_GLsop_%s" % (self
.name
)
550 elif self
.glx_vendorpriv
!= 0:
551 return "X_GLvop_%s" % (self
.name
)
553 raise RuntimeError('Function "%s" has no opcode.' % (self
.name
))
556 def opcode_real_name(self
):
557 """Get the true protocol enum name for the GLX opcode
559 Behaves similarly to opcode_name, except for
560 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
561 In these cases the string 'X_GLXVendorPrivate' or
562 'X_GLXVendorPrivateWithReply' is returned. For other
563 single or render commands 'X_GLsop' or 'X_GLrop' plus the
564 name of the function returned."""
566 if self
.glx_vendorpriv
!= 0:
567 if self
.needs_reply():
568 return "X_GLXVendorPrivateWithReply"
570 return "X_GLXVendorPrivate"
572 return self
.opcode_name()
575 def return_string(self
):
576 if self
.fn_return_type
!= 'void':
577 return "return retval;"
582 def needs_reply(self
):
583 return self
.fn_return_type
!= 'void' or self
.output
!= None
586 def dimensions(self
):
587 """Determine the dimensions of an image.
589 Returns a tuple representing the number of dimensions and the
590 string name of each of the dimensions of an image, If the
591 function is not a pixel function, the number of dimensions
595 return [0, "0", "0", "0", "0"]
600 if self
.image
.height
:
602 h
= self
.image
.height
612 if self
.image
.extent
:
614 e
= self
.image
.extent
618 return [dim
, w
, h
, d
, e
]
621 def pad_after(self
, p
):
622 """Returns the name of the field inserted after the
623 specified field to pad out the command header."""
625 if self
.image
and self
.image
.img_pad_dimensions
:
626 if not self
.image
.height
:
627 if p
.name
== self
.image
.width
:
629 elif p
.name
== self
.image
.img_xoff
:
631 elif not self
.image
.extent
:
632 if p
.name
== self
.image
.depth
:
633 # Should this be "size4d"?
635 elif p
.name
== self
.image
.img_zoff
:
640 class glXFunctionIterator(gl_XML
.glFunctionIterator
):
641 """Class to iterate over a list of glXFunctions"""
643 def __init__(self
, context
):
644 self
.context
= context
645 self
.keys
= context
.functions
.keys()
648 for self
.index
in range(0, len(self
.keys
)):
649 if self
.keys
[ self
.index
] >= 0: break
655 if self
.index
== len(self
.keys
):
658 f
= self
.context
.functions
[ self
.keys
[ self
.index
] ]
667 class GlxProto(gl_XML
.FilterGLAPISpecBase
):
668 name
= "glX_proto_send.py (from Mesa)"
671 gl_XML
.FilterGLAPISpecBase
.__init
__(self
)
672 self
.factory
= glXItemFactory()
673 self
.glx_enum_functions
= {}
676 def endElement(self
, name
):
677 if name
== 'OpenGLAPI':
678 # Once all the parsing is done, we have to go back and
679 # fix-up some cross references between different
682 for k
in self
.functions
:
683 f
= self
.functions
[k
]
684 if f
.vectorequiv
!= None:
685 equiv
= self
.find_function(f
.vectorequiv
)
687 f
.glx_doubles_in_order
= equiv
.glx_doubles_in_order
688 f
.glx_rop
= equiv
.glx_rop
690 raise RuntimeError("Could not find the vector equiv. function %s for %s!" % (f
.name
, f
.vectorequiv
))
692 gl_XML
.FilterGLAPISpecBase
.endElement(self
, name
)
696 def createEnumFunction(self
, n
):
697 return glXEnumFunction(n
, self
)
700 def functionIterator(self
):
701 return glXFunctionIterator(self
)
704 def size_call(self
, func
):
705 """Create C code to calculate 'compsize'.
707 Creates code to calculate 'compsize'. If the function does
708 not need 'compsize' to be calculated, None will be
711 if not func
.image
and not func
.count_parameter_list
:
715 parameters
= string
.join( func
.count_parameter_list
, "," )
716 compsize
= "__gl%s_size(%s)" % (func
.name
, parameters
)
718 [dim
, w
, h
, d
, junk
] = func
.dimensions()
720 compsize
= '__glImageSize(%s, %s, %s, %s, %s, %s)' % (w
, h
, d
, func
.image
.img_format
, func
.image
.img_type
, func
.image
.img_target
)
721 if not func
.image
.img_send_null
:
722 compsize
= '(%s != NULL) ? %s : 0' % (func
.image
.name
, compsize
)