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 import gl_XML
, glX_XML
30 import sys
, getopt
, copy
, string
33 class glx_enum_function
:
34 def __init__(self
, func_name
, enum_dict
):
39 # "enums" is a set of lists. The element in the set is the
40 # value of the enum. The list is the list of names for that
41 # value. For example, [0x8126] = {"POINT_SIZE_MIN",
42 # "POINT_SIZE_MIN_ARB", "POINT_SIZE_MIN_EXT",
43 # "POINT_SIZE_MIN_SGIS"}.
47 # "count" is indexed by count values. Each element of count
48 # is a list of index to "enums" that have that number of
49 # associated data elements. For example, [4] =
50 # {GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_EMISSION,
51 # GL_AMBIENT_AND_DIFFUSE} (the enum names are used here,
52 # but the actual hexadecimal values would be in the array).
57 # Fill self.count and self.enums using the dictionary of enums
58 # that was passed in. The generic Get functions (e.g.,
59 # GetBooleanv and friends) are handled specially here. In
60 # the data the generic Get functions are refered to as "Get".
62 if func_name
in ["GetIntegerv", "GetBooleanv", "GetFloatv", "GetDoublev"]:
65 match_name
= func_name
68 for enum_name
in enum_dict
:
69 e
= enum_dict
[ enum_name
]
71 if e
.functions
.has_key( match_name
):
72 [count
, mode
] = e
.functions
[ match_name
]
74 if mode_set
and mode
!= self
.mode
:
75 raise RuntimeError("Not all enums for %s have the same mode." % (func_name
))
79 if self
.enums
.has_key( e
.value
):
80 if e
.name
not in self
.enums
[ e
.value
]:
81 self
.enums
[ e
.value
].append( e
)
83 if not self
.count
.has_key( count
):
84 self
.count
[ count
] = []
86 self
.enums
[ e
.value
] = [ e
]
87 self
.count
[ count
].append( e
.value
)
93 def signature( self
):
98 raise RuntimeError("i is None. WTF?")
101 for e
in self
.count
[i
]:
102 self
.sig
+= "%04x,%d," % (e
, i
)
111 def PrintUsingTable(self
):
112 """Emit the body of the __gl*_size function using a pair
113 of look-up tables and a mask. The mask is calculated such
114 that (e & mask) is unique for all the valid values of e for
115 this function. The result of (e & mask) is used as an index
116 into the first look-up table. If it matches e, then the
117 same entry of the second table is returned. Otherwise zero
120 It seems like this should cause better code to be generated.
121 However, on x86 at least, the resulting .o file is about 20%
122 larger then the switch-statment version. I am leaving this
123 code in because the results may be different on other
124 platforms (e.g., PowerPC or x86-64)."""
131 if self
.count
.has_key(-1):
134 # Determine if there is some mask M, such that M = (2^N) - 1,
135 # that will generate unique values for all of the enums.
138 for i
in [1, 2, 3, 4, 5, 6, 7, 8]:
145 if (a
& mask
) == (b
& mask
):
153 if (mask
!= 0) and (mask
< (2 * count
)):
157 for i
in range(0, mask
+ 1):
158 masked_enums
[i
] = "0";
162 for e
in self
.count
[c
]:
164 enum_obj
= self
.enums
[e
][0]
165 masked_enums
[i
] = '0x%04x /* %s */' % (e
, enum_obj
.name
)
169 print ' static const GLushort a[%u] = {' % (mask
+ 1)
170 for e
in masked_enums
:
171 print ' %s, ' % (masked_enums
[e
])
174 print ' static const GLubyte b[%u] = {' % (mask
+ 1)
175 for c
in masked_count
:
176 print ' %u, ' % (masked_count
[c
])
179 print ' const unsigned idx = (e & 0x%02xU);' % (mask
)
181 print ' return (e == a[idx]) ? (GLint) b[idx] : 0;'
187 def PrintUsingSwitch(self
, name
):
188 """Emit the body of the __gl*_size function using a
191 print ' switch( e ) {'
194 for e
in self
.count
[c
]:
197 # There may be multiple enums with the same
198 # value. This happens has extensions are
199 # promoted from vendor-specific or EXT to
200 # ARB and to the core. Emit the first one as
201 # a case label, and emit the others as
202 # commented-out case labels.
205 for enum_obj
in self
.enums
[e
]:
206 list[ enum_obj
.priority() ] = enum_obj
.name
213 print ' case GL_%s:' % (j
)
216 print '/* case GL_%s:*/' % (j
)
219 print ' return __gl%s_variable_size( e );' % (name
)
221 print ' return %u;' % (c
)
223 print ' default: return 0;'
227 def Print(self
, name
):
228 print 'INTERNAL PURE FASTCALL GLint'
229 print '__gl%s_size( GLenum e )' % (name
)
232 if not self
.PrintUsingTable():
233 self
.PrintUsingSwitch(name
)
239 class glx_server_enum_function(glx_enum_function
):
240 def __init__(self
, func
, enum_dict
):
241 glx_enum_function
.__init
__(self
, func
.name
, enum_dict
)
247 def signature( self
):
249 sig
= glx_enum_function
.signature(self
)
251 p
= self
.function
.variable_length_parameter()
253 sig
+= "%u" % (p
.size())
260 def Print(self
, name
, printer
):
262 printer
.common_func_print_just_header( f
)
267 for param_name
in f
.count_parameter_list
:
268 o
= f
.offset_of( param_name
)
271 for param_name
in f
.counter_list
:
272 o
= f
.offset_of( param_name
)
278 p
= f
.parameters_by_name
[ foo
[o
] ]
280 printer
.common_emit_one_arg(p
, "pc", " ", 0)
281 fixup
.append( p
.name
)
284 print ' GLsizei compsize;'
287 printer
.common_emit_fixups(fixup
)
290 print ' compsize = __gl%s_size(%s);' % (f
.name
, string
.join(f
.count_parameter_list
, ","))
291 p
= f
.variable_length_parameter()
292 print ' return __GLX_PAD(%s);' % (p
.size_string())
298 class PrintGlxSizeStubs_common(gl_XML
.gl_print_base
):
302 def __init__(self
, which_functions
):
303 gl_XML
.gl_print_base
.__init
__(self
)
305 self
.name
= "glX_proto_size.py (from Mesa)"
306 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2004", "IBM")
308 self
.emit_set
= ((which_functions
& PrintGlxSizeStubs_common
.do_set
) != 0)
309 self
.emit_get
= ((which_functions
& PrintGlxSizeStubs_common
.do_get
) != 0)
313 class PrintGlxSizeStubs_c(PrintGlxSizeStubs_common
):
314 def printRealHeader(self
):
316 print '#include <GL/gl.h>'
318 print '#include "indirect_size_get.h"'
320 print '#include "indirect_size.h"'
327 self
.printVisibility( "INTERNAL", "internal" )
330 print '#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__APPLE__)'
331 print '# undef HAVE_ALIAS'
333 print '#ifdef HAVE_ALIAS'
334 print '# define ALIAS2(from,to) \\'
335 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
336 print ' __attribute__ ((alias( # to )));'
337 print '# define ALIAS(from,to) ALIAS2( from, __gl ## to ## _size )'
339 print '# define ALIAS(from,to) \\'
340 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
341 print ' { return __gl ## to ## _size( e ); }'
347 def printBody(self
, api
):
351 for func
in api
.functionIterateGlx():
352 ef
= glx_enum_function( func
.name
, api
.enums_by_name
)
353 if len(ef
.enums
) == 0:
356 if (ef
.is_set() and self
.emit_set
) or (not ef
.is_set() and self
.emit_get
):
358 if enum_sigs
.has_key( sig
):
359 aliases
.append( [func
.name
, enum_sigs
[ sig
]] )
361 enum_sigs
[ sig
] = func
.name
362 ef
.Print( func
.name
)
365 for [alias_name
, real_name
] in aliases
:
366 print 'ALIAS( %s, %s )' % (alias_name
, real_name
)
370 class PrintGlxSizeStubs_h(PrintGlxSizeStubs_common
):
371 def printRealHeader(self
):
374 * Prototypes for functions used to determine the number of data elements in
375 * various GLX protocol messages.
377 * \\author Ian Romanick <idr@us.ibm.com>
382 self
.printFastcall();
384 self
.printVisibility( "INTERNAL", "internal" );
388 def printBody(self
, api
):
389 for func
in api
.functionIterateGlx():
390 ef
= glx_enum_function( func
.name
, api
.enums_by_name
)
391 if len(ef
.enums
) == 0:
394 if (ef
.is_set() and self
.emit_set
) or (not ef
.is_set() and self
.emit_get
):
395 print 'extern INTERNAL PURE FASTCALL GLint __gl%s_size(GLenum);' % (func
.name
)
398 class PrintGlxReqSize_common(gl_XML
.gl_print_base
):
399 """Common base class for PrintGlxSizeReq_h and PrintGlxSizeReq_h.
401 The main purpose of this common base class is to provide the infrastructure
402 for the derrived classes to iterate over the same set of functions.
406 gl_XML
.gl_print_base
.__init
__(self
)
408 self
.name
= "glX_proto_size.py (from Mesa)"
409 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2005", "IBM")
412 class PrintGlxReqSize_h(PrintGlxReqSize_common
):
414 PrintGlxReqSize_common
.__init
__(self
)
415 self
.header_tag
= "_INDIRECT_REQSIZE_H_"
418 def printRealHeader(self
):
419 self
.printVisibility("HIDDEN", "hidden")
425 def printBody(self
, api
):
426 for func
in api
.functionIterateGlx():
427 if not func
.ignore
and func
.has_variable_size_request():
428 print 'extern PURE HIDDEN int __glX%sReqSize(const GLbyte *pc, Bool swap);' % (func
.name
)
431 class PrintGlxReqSize_c(PrintGlxReqSize_common
):
432 """Create the server-side 'request size' functions.
434 Create the server-side functions that are used to determine what the
435 size of a varible length command should be. The server then uses
436 this value to determine if the incoming command packed it malformed.
440 PrintGlxReqSize_common
.__init
__(self
)
441 self
.counter_sigs
= {}
444 def printRealHeader(self
):
446 print '#include <GL/gl.h>'
447 print '#include "glxserver.h"'
448 print '#include "indirect_size.h"'
449 print '#include "indirect_reqsize.h"'
451 print '#if defined(linux)'
452 print '# include <byteswap.h>'
453 print '# define SWAP_32(v) do { (v) = bswap_32(v)); } while(0)'
455 print '# include <X11/misc.h>'
456 print '# define SWAP_32(v) do { char tmp; swapl(&v, tmp); } while(0)'
460 print '#define __GLX_PAD(x) (((x) + 3) & ~3)'
462 print '#if defined(__CYGWIN__) || defined(__MINGW32__)'
463 print '# undef HAVE_ALIAS'
465 print '#ifdef HAVE_ALIAS'
466 print '# define ALIAS2(from,to) \\'
467 print ' GLint __glX ## from ## ReqSize( const GLbyte * pc, Bool swap ) \\'
468 print ' __attribute__ ((alias( # to )));'
469 print '# define ALIAS(from,to) ALIAS2( from, __glX ## to ## ReqSize )'
471 print '# define ALIAS(from,to) \\'
472 print ' GLint __glX ## from ## ReqSize( const GLbyte * pc, Bool swap ) \\'
473 print ' { return __glX ## to ## ReqSize( pc, swap ); }'
479 def printBody(self
, api
):
484 for func
in api
.functionIterateGlx():
485 if not func
.has_variable_size_request(): continue
487 ef
= glx_server_enum_function( func
, api
.enums_by_name
)
488 if len(ef
.enums
) == 0: continue
492 if not enum_functions
.has_key(func
.name
):
493 enum_functions
[ func
.name
] = sig
495 if not enum_sigs
.has_key( sig
):
496 enum_sigs
[ sig
] = ef
500 for func
in api
.functionIterateGlx():
501 # Even though server-handcode fuctions are on "the
502 # list", and prototypes are generated for them, there
503 # isn't enough information to generate a size
504 # function. If there was enough information, they
505 # probably wouldn't need to be handcoded in the first
508 if func
.server_handcode
: continue
509 if not func
.has_variable_size_request(): continue
511 if enum_functions
.has_key(func
.name
):
512 sig
= enum_functions
[func
.name
]
513 ef
= enum_sigs
[ sig
]
515 if ef
.name
!= func
.name
:
516 aliases
.append( [func
.name
, ef
.name
] )
518 ef
.Print( func
.name
, self
)
521 self
.printPixelFunction(func
)
522 elif func
.has_variable_size_request():
523 a
= self
.printCountedFunction(func
)
524 if a
: aliases
.append(a
)
527 for [alias_name
, real_name
] in aliases
:
528 print 'ALIAS( %s, %s )' % (alias_name
, real_name
)
533 def common_emit_fixups(self
, fixup
):
534 """Utility function to emit conditional byte-swaps."""
539 print ' SWAP_32( %s );' % (name
)
545 def common_emit_one_arg(self
, p
, pc
, indent
, adjust
):
548 src
= '(%s *)' % (p
.type_string())
549 print '%s%-18s = *%11s(%s + %u);' % (indent
, dst
, src
, pc
, offset
+ adjust
);
553 def common_func_print_just_header(self
, f
):
555 print '__glX%sReqSize( const GLbyte * pc, Bool swap )' % (f
.name
)
559 def printPixelFunction(self
, f
):
560 self
.common_func_print_just_header(f
)
562 f
.offset_of( f
.parameters
[0].name
)
563 [dim
, w
, h
, d
, junk
] = f
.get_images()[0].get_dimensions()
565 print ' GLint row_length = * (GLint *)(pc + 4);'
568 fixup
= ['row_length', 'skip_rows', 'alignment']
569 print ' GLint image_height = 0;'
570 print ' GLint skip_images = 0;'
571 print ' GLint skip_rows = * (GLint *)(pc + 8);'
572 print ' GLint alignment = * (GLint *)(pc + 16);'
574 fixup
= ['row_length', 'image_height', 'skip_rows', 'skip_images', 'alignment']
575 print ' GLint image_height = * (GLint *)(pc + 8);'
576 print ' GLint skip_rows = * (GLint *)(pc + 16);'
577 print ' GLint skip_images = * (GLint *)(pc + 20);'
578 print ' GLint alignment = * (GLint *)(pc + 32);'
581 for p
in f
.parameterIterateGlxSend():
582 if p
.name
in [w
, h
, d
, img
.img_format
, img
.img_type
, img
.img_target
]:
583 self
.common_emit_one_arg( p
, "pc", " ", 0 )
584 fixup
.append( p
.name
)
588 self
.common_emit_fixups(fixup
)
591 print ' return __glXImageSize(%s, %s, %s, %s, %s, %s,' % (img
.img_format
, img
.img_type
, img
.img_target
, w
, h
, d
)
592 print ' image_height, row_length, skip_images,'
593 print ' skip_rows, alignment);'
599 def printCountedFunction(self
, f
):
609 # Calculate the offset of each counter parameter and the
610 # size string for the variable length parameter(s). While
611 # that is being done, calculate a unique signature for this
614 for p
in f
.parameterIterateGlxSend():
616 fixup
.append( p
.name
)
622 sig
+= "(%u,%u)" % (f
.offset_of(p
.counter
), s
)
623 size
+= '%s%s' % (plus
, p
.size_string())
627 # If the calculated signature matches a function that has
628 # already be emitted, don't emit this function. Instead, add
629 # it to the list of function aliases.
631 if self
.counter_sigs
.has_key(sig
):
632 n
= self
.counter_sigs
[sig
];
636 self
.counter_sigs
[sig
] = f
.name
638 self
.common_func_print_just_header(f
)
641 self
.common_emit_one_arg(p
, "pc", " ", 0 )
645 self
.common_emit_fixups(fixup
)
648 print ' return __GLX_PAD(%s);' % (size
)
656 print "Usage: %s [-f input_file_name] -m output_mode [--only-get | --only-set] [--get-alias-set]" % sys
.argv
[0]
657 print " -m output_mode Output mode can be one of 'size_c' or 'size_h'."
658 print " --only-get Only emit 'get'-type functions."
659 print " --only-set Only emit 'set'-type functions."
661 print "By default, both 'get' and 'set'-type functions are emitted."
665 if __name__
== '__main__':
666 file_name
= "gl_API.xml"
669 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:m:h:", ["only-get", "only-set", "header-tag"])
675 which_functions
= PrintGlxSizeStubs_common
.do_get | PrintGlxSizeStubs_common
.do_set
677 for (arg
,val
) in args
:
682 elif arg
== "--only-get":
683 which_functions
= PrintGlxSizeStubs_common
.do_get
684 elif arg
== "--only-set":
685 which_functions
= PrintGlxSizeStubs_common
.do_set
686 elif (arg
== '-h') or (arg
== "--header-tag"):
690 printer
= PrintGlxSizeStubs_c( which_functions
)
691 elif mode
== "size_h":
692 printer
= PrintGlxSizeStubs_h( which_functions
)
694 printer
.header_tag
= header_tag
695 elif mode
== "reqsize_c":
696 printer
= PrintGlxReqSize_c()
697 elif mode
== "reqsize_h":
698 printer
= PrintGlxReqSize_h()
702 api
= gl_XML
.parse_GL_API( file_name
, glX_XML
.glx_item_factory() )