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>
30 import sys
, getopt
, string
33 class glx_item_factory(gl_XML
.gl_item_factory
):
34 """Factory to create GLX protocol oriented objects derived from gl_item."""
36 def create_item(self
, name
, element
, context
):
37 if name
== "function":
38 return glx_function(element
, context
)
40 return glx_enum(element
, context
)
44 return gl_XML
.gl_item_factory
.create_item(self
, name
, element
, context
)
47 class glx_enum(gl_XML
.gl_enum
):
48 def __init__(self
, element
, context
):
49 gl_XML
.gl_enum
.__init
__(self
, element
, context
)
53 child
= element
.children
55 if child
.type == "element" and child
.name
== "size":
56 n
= child
.nsProp( "name", None )
57 c
= child
.nsProp( "count", None )
58 m
= child
.nsProp( "mode", None )
61 c
= self
.default_count
70 if not self
.functions
.has_key(n
):
71 self
.functions
[ n
] = [c
, mode
]
78 class glx_function(gl_XML
.gl_function
):
79 def __init__(self
, element
, context
):
82 self
.glx_vendorpriv
= 0
84 self
.glx_vendorpriv_names
= []
86 # If this is set to true, it means that GLdouble parameters should be
87 # written to the GLX protocol packet in the order they appear in the
88 # prototype. This is different from the "classic" ordering. In the
89 # classic ordering GLdoubles are written to the protocol packet first,
90 # followed by non-doubles. NV_vertex_program was the first extension
91 # to break with this tradition.
93 self
.glx_doubles_in_order
= 0
95 self
.vectorequiv
= None
98 self
.reply_always_array
= 0
99 self
.dimensions_in_reply
= 0
100 self
.img_reset
= None
102 self
.server_handcode
= 0
103 self
.client_handcode
= 0
106 self
.count_parameter_list
= []
107 self
.counter_list
= []
108 self
.parameters_by_name
= {}
109 self
.offsets_calculated
= 0
111 gl_XML
.gl_function
.__init
__(self
, element
, context
)
115 def process_element(self
, element
):
116 gl_XML
.gl_function
.process_element(self
, element
)
118 self
.vectorequiv
= element
.nsProp( "vectorequiv", None )
121 name
= element
.nsProp("name", None)
122 if name
== self
.name
:
123 for param
in self
.parameters
:
124 self
.parameters_by_name
[ param
.name
] = param
126 if len(param
.count_parameter_list
):
127 self
.count_parameter_list
.extend( param
.count_parameter_list
)
129 if param
.counter
and param
.counter
not in self
.counter_list
:
130 self
.counter_list
.append(param
.counter
)
133 child
= element
.children
135 if child
.type == "element":
136 if child
.name
== "glx":
137 rop
= child
.nsProp( 'rop', None )
138 sop
= child
.nsProp( 'sop', None )
139 vop
= child
.nsProp( 'vendorpriv', None )
142 self
.glx_rop
= int(rop
)
145 self
.glx_sop
= int(sop
)
148 self
.glx_vendorpriv
= int(vop
)
149 self
.glx_vendorpriv_names
.append(name
)
151 self
.img_reset
= child
.nsProp( 'img_reset', None )
153 # The 'handcode' attribute can be one of 'true',
154 # 'false', 'client', or 'server'.
156 handcode
= child
.nsProp( 'handcode', None )
157 if handcode
== "false":
158 self
.server_handcode
= 0
159 self
.client_handcode
= 0
160 elif handcode
== "true":
161 self
.server_handcode
= 1
162 self
.client_handcode
= 1
163 elif handcode
== "client":
164 self
.server_handcode
= 0
165 self
.client_handcode
= 1
166 elif handcode
== "server":
167 self
.server_handcode
= 1
168 self
.client_handcode
= 0
170 raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode
, self
.name
))
172 self
.ignore
= gl_XML
.is_attr_true( child
, 'ignore' )
173 self
.can_be_large
= gl_XML
.is_attr_true( child
, 'large' )
174 self
.glx_doubles_in_order
= gl_XML
.is_attr_true( child
, 'doubles_in_order' )
175 self
.reply_always_array
= gl_XML
.is_attr_true( child
, 'always_array' )
176 self
.dimensions_in_reply
= gl_XML
.is_attr_true( child
, 'dimensions_in_reply' )
181 # Do some validation of the GLX protocol information. As
182 # new tests are discovered, they should be added here.
184 for param
in self
.parameters
:
185 if param
.is_output
and self
.glx_rop
!= 0:
186 raise RuntimeError("Render / RenderLarge commands cannot have outputs (%s)." % (self
.name
))
191 def has_variable_size_request(self
):
192 """Determine if the GLX request packet is variable sized.
194 The GLX request packet is variable sized in several common
197 1. The function has a non-output parameter that is counted
198 by another parameter (e.g., the 'textures' parameter of
201 2. The function has a non-output parameter whose count is
202 determined by another parameter that is an enum (e.g., the
203 'params' parameter of glLightfv).
205 3. The function has a non-output parameter that is an
208 4. The function must be hand-coded on the server.
211 if self
.glx_rop
== 0:
214 if self
.server_handcode
or self
.images
:
217 for param
in self
.parameters
:
218 if not param
.is_output
:
219 if param
.counter
or len(param
.count_parameter_list
):
225 def variable_length_parameter(self
):
226 for param
in self
.parameters
:
227 if not param
.is_output
:
228 if param
.counter
or len(param
.count_parameter_list
):
234 def calculate_offsets(self
):
235 if not self
.offsets_calculated
:
236 # Calculate the offset of the first function parameter
237 # in the GLX command packet. This byte offset is
238 # measured from the end of the Render / RenderLarge
239 # header. The offset for all non-pixel commends is
240 # zero. The offset for pixel commands depends on the
241 # number of dimensions of the pixel data.
243 if len(self
.images
) and not self
.images
[0].is_output
:
244 [dim
, junk
, junk
, junk
, junk
] = self
.images
[0].get_dimensions()
246 # The base size is the size of the pixel pack info
247 # header used by images with the specified number
255 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim
, self
.image
.name
, self
.name
))
259 for param
in self
.parameterIterateGlxSend():
260 if param
.img_null_flag
:
263 if param
.name
!= self
.img_reset
:
264 param
.offset
= offset
265 if not param
.is_variable_length() and not param
.is_client_only
:
266 offset
+= param
.size()
268 if self
.pad_after( param
):
272 self
.offsets_calculated
= 1
276 def offset_of(self
, param_name
):
277 self
.calculate_offsets()
278 return self
.parameters_by_name
[ param_name
].offset
281 def parameterIterateGlxSend(self
, include_variable_parameters
= 1):
282 """Create an iterator for parameters in GLX request order."""
284 # The parameter lists are usually quite short, so it's easier
285 # (i.e., less code) to just generate a new list with the
286 # required elements than it is to create a new iterator class.
288 temp
= [ [], [], [] ]
289 for param
in self
.parameters
:
290 if param
.is_output
: continue
292 if param
.is_variable_length():
293 temp
[2].append( param
)
294 elif not self
.glx_doubles_in_order
and param
.is_64_bit():
295 temp
[0].append( param
)
297 temp
[1].append( param
)
300 parameters
.extend( temp
[1] )
301 if include_variable_parameters
:
302 parameters
.extend( temp
[2] )
303 return parameters
.__iter
__()
306 def parameterIterateCounters(self
):
308 for name
in self
.counter_list
:
309 temp
.append( self
.parameters_by_name
[ name
] )
311 return temp
.__iter
__()
314 def parameterIterateOutputs(self
):
316 for p
in self
.parameters
:
323 def command_fixed_length(self
):
324 """Return the length, in bytes as an integer, of the
325 fixed-size portion of the command."""
327 if len(self
.parameters
) == 0:
330 self
.calculate_offsets()
333 for param
in self
.parameterIterateGlxSend(0):
334 if param
.name
!= self
.img_reset
and not param
.is_client_only
:
336 size
= param
.offset
+ param
.size()
340 if self
.pad_after( param
):
343 for param
in self
.images
:
344 if param
.img_null_flag
or param
.is_output
:
350 def command_variable_length(self
):
351 """Return the length, as a string, of the variable-sized
352 portion of the command."""
355 for p
in self
.parameterIterateGlxSend():
356 if (not p
.is_output
) and (p
.is_variable_length() or p
.is_image()):
357 # FIXME Replace the 1 in the size_string call
358 # FIXME w/0 to eliminate some un-needed parnes
359 # FIXME This would already be done, but it
360 # FIXME adds some extra diffs to the generated
363 size_string
= size_string
+ " + __GLX_PAD(%s)" % (p
.size_string(1))
368 def command_length(self
):
369 size
= self
.command_fixed_length()
371 if self
.glx_rop
!= 0:
374 size
= ((size
+ 3) & ~
3)
375 return "%u%s" % (size
, self
.command_variable_length())
378 def opcode_real_value(self
):
379 """Get the true numeric value of the GLX opcode
381 Behaves similarly to opcode_value, except for
382 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
383 In these cases the value for the GLX opcode field (i.e.,
384 16 for X_GLXVendorPrivate or 17 for
385 X_GLXVendorPrivateWithReply) is returned. For other 'single'
386 commands, the opcode for the command (e.g., 101 for
387 X_GLsop_NewList) is returned."""
389 if self
.glx_vendorpriv
!= 0:
390 if self
.needs_reply():
395 return self
.opcode_value()
398 def opcode_value(self
):
399 """Get the unique protocol opcode for the glXFunction"""
401 if (self
.glx_rop
== 0) and self
.vectorequiv
:
402 equiv
= self
.context
.functions_by_name
[ self
.vectorequiv
]
403 self
.glx_rop
= equiv
.glx_rop
406 if self
.glx_rop
!= 0:
408 elif self
.glx_sop
!= 0:
410 elif self
.glx_vendorpriv
!= 0:
411 return self
.glx_vendorpriv
416 def opcode_rop_basename(self
):
417 """Return either the name to be used for GLX protocol enum.
419 Returns either the name of the function or the name of the
420 name of the equivalent vector (e.g., glVertex3fv for
421 glVertex3f) function."""
423 if self
.vectorequiv
== None:
426 return self
.vectorequiv
429 def opcode_name(self
):
430 """Get the unique protocol enum name for the glXFunction"""
432 if (self
.glx_rop
== 0) and self
.vectorequiv
:
433 equiv
= self
.context
.functions_by_name
[ self
.vectorequiv
]
434 self
.glx_rop
= equiv
.glx_rop
435 self
.glx_doubles_in_order
= equiv
.glx_doubles_in_order
438 if self
.glx_rop
!= 0:
439 return "X_GLrop_%s" % (self
.opcode_rop_basename())
440 elif self
.glx_sop
!= 0:
441 return "X_GLsop_%s" % (self
.name
)
442 elif self
.glx_vendorpriv
!= 0:
443 return "X_GLvop_%s" % (self
.name
)
445 raise RuntimeError('Function "%s" has no opcode.' % (self
.name
))
448 def opcode_vendor_name(self
, name
):
449 if name
in self
.glx_vendorpriv_names
:
450 return "X_GLvop_%s" % (name
)
452 raise RuntimeError('Function "%s" has no VendorPrivate opcode.' % (name
))
455 def opcode_real_name(self
):
456 """Get the true protocol enum name for the GLX opcode
458 Behaves similarly to opcode_name, except for
459 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
460 In these cases the string 'X_GLXVendorPrivate' or
461 'X_GLXVendorPrivateWithReply' is returned. For other
462 single or render commands 'X_GLsop' or 'X_GLrop' plus the
463 name of the function returned."""
465 if self
.glx_vendorpriv
!= 0:
466 if self
.needs_reply():
467 return "X_GLXVendorPrivateWithReply"
469 return "X_GLXVendorPrivate"
471 return self
.opcode_name()
474 def needs_reply(self
):
476 x
= self
._needs
_reply
479 if self
.return_type
!= 'void':
482 for param
in self
.parameters
:
487 self
._needs
_reply
= x
492 def pad_after(self
, p
):
493 """Returns the name of the field inserted after the
494 specified field to pad out the command header."""
496 for image
in self
.images
:
497 if image
.img_pad_dimensions
:
499 if p
.name
== image
.width
:
501 elif p
.name
== image
.img_xoff
:
503 elif not image
.extent
:
504 if p
.name
== image
.depth
:
505 # Should this be "size4d"?
507 elif p
.name
== image
.img_zoff
:
513 def has_different_protocol(self
, name
):
514 """Returns true if the named version of the function uses different protocol from the other versions.
516 Some functions, such as glDeleteTextures and
517 glDeleteTexturesEXT are functionally identical, but have
518 different protocol. This function returns true if the
519 named function is an alias name and that named version uses
520 different protocol from the function that is aliased.
523 return (name
in self
.glx_vendorpriv_names
) and self
.glx_sop
526 def static_glx_name(self
, name
):
527 if self
.has_different_protocol(name
):
528 for n
in self
.glx_vendorpriv_names
:
529 if n
in self
.static_entry_points
:
532 return self
.static_name(name
)
535 def client_supported_for_indirect(self
):
536 """Returns true if the function is supported on the client
537 side for indirect rendering."""
539 return not self
.ignore
and (self
.offset
!= -1) and (self
.glx_rop
or self
.glx_sop
or self
.glx_vendorpriv
or self
.vectorequiv
or self
.client_handcode
)
542 class glx_function_iterator
:
543 """Class to iterate over a list of glXFunctions"""
545 def __init__(self
, context
):
546 self
.iterator
= context
.functionIterateByOffset()
555 f
= self
.iterator
.next()
556 if f
.client_supported_for_indirect():
562 class glx_api(gl_XML
.gl_api
):
563 def functionIterateGlx(self
):
564 return glx_function_iterator(self
)