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 # If this is set to true, it means that GLdouble parameters should be
85 # written to the GLX protocol packet in the order they appear in the
86 # prototype. This is different from the "classic" ordering. In the
87 # classic ordering GLdoubles are written to the protocol packet first,
88 # followed by non-doubles. NV_vertex_program was the first extension
89 # to break with this tradition.
91 self
.glx_doubles_in_order
= 0
93 self
.vectorequiv
= None
96 self
.reply_always_array
= 0
97 self
.dimensions_in_reply
= 0
100 self
.server_handcode
= 0
101 self
.client_handcode
= 0
104 self
.count_parameter_list
= []
105 self
.counter_list
= []
106 self
.parameters_by_name
= {}
107 self
.offsets_calculated
= 0
109 gl_XML
.gl_function
.__init
__(self
, element
, context
)
113 def process_element(self
, element
):
114 gl_XML
.gl_function
.process_element(self
, element
)
116 self
.vectorequiv
= element
.nsProp( "vectorequiv", None )
119 if element
.nsProp( "name", None ) == self
.name
:
120 for param
in self
.parameters
:
121 self
.parameters_by_name
[ param
.name
] = param
123 if len(param
.count_parameter_list
):
124 self
.count_parameter_list
.extend( param
.count_parameter_list
)
126 if param
.counter
and param
.counter
not in self
.counter_list
:
127 self
.counter_list
.append(param
.counter
)
130 child
= element
.children
132 if child
.type == "element":
133 if child
.name
== "glx":
134 rop
= child
.nsProp( 'rop', None )
135 sop
= child
.nsProp( 'sop', None )
136 vop
= child
.nsProp( 'vendorpriv', None )
139 self
.glx_rop
= int(rop
)
144 self
.glx_sop
= int(sop
)
149 self
.glx_vendorpriv
= int(vop
)
151 self
.glx_vendorpriv
= 0
153 self
.img_reset
= child
.nsProp( 'img_reset', None )
155 # The 'handcode' attribute can be one of 'true',
156 # 'false', 'client', or 'server'.
158 handcode
= child
.nsProp( 'handcode', None )
159 if handcode
== "false":
160 self
.server_handcode
= 0
161 self
.client_handcode
= 0
162 elif handcode
== "true":
163 self
.server_handcode
= 1
164 self
.client_handcode
= 1
165 elif handcode
== "client":
166 self
.server_handcode
= 0
167 self
.client_handcode
= 1
168 elif handcode
== "server":
169 self
.server_handcode
= 1
170 self
.client_handcode
= 0
172 raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode
, self
.name
))
174 self
.ignore
= gl_XML
.is_attr_true( child
, 'ignore' )
175 self
.can_be_large
= gl_XML
.is_attr_true( child
, 'large' )
176 self
.glx_doubles_in_order
= gl_XML
.is_attr_true( child
, 'doubles_in_order' )
177 self
.reply_always_array
= gl_XML
.is_attr_true( child
, 'always_array' )
178 self
.dimensions_in_reply
= gl_XML
.is_attr_true( child
, 'dimensions_in_reply' )
183 # Do some validation of the GLX protocol information. As
184 # new tests are discovered, they should be added here.
186 for param
in self
.parameters
:
187 if param
.is_output
and self
.glx_rop
!= 0:
188 raise RuntimeError("Render / RenderLarge commands cannot have outputs (%s)." % (self
.name
))
193 def has_variable_size_request(self
):
194 """Determine if the GLX request packet is variable sized.
196 The GLX request packet is variable sized in several common
199 1. The function has a non-output parameter that is counted
200 by another parameter (e.g., the 'textures' parameter of
203 2. The function has a non-output parameter whose count is
204 determined by another parameter that is an enum (e.g., the
205 'params' parameter of glLightfv).
207 3. The function has a non-output parameter that is an
210 4. The function must be hand-coded on the server.
213 if self
.glx_rop
== 0:
216 if self
.server_handcode
or self
.images
:
219 for param
in self
.parameters
:
220 if not param
.is_output
:
221 if param
.counter
or len(param
.count_parameter_list
):
227 def variable_length_parameter(self
):
228 for param
in self
.parameters
:
229 if not param
.is_output
:
230 if param
.counter
or len(param
.count_parameter_list
):
236 def calculate_offsets(self
):
237 if not self
.offsets_calculated
:
238 # Calculate the offset of the first function parameter
239 # in the GLX command packet. This byte offset is
240 # measured from the end of the Render / RenderLarge
241 # header. The offset for all non-pixel commends is
242 # zero. The offset for pixel commands depends on the
243 # number of dimensions of the pixel data.
245 if len(self
.images
) and not self
.images
[0].is_output
:
246 [dim
, junk
, junk
, junk
, junk
] = self
.images
[0].get_dimensions()
248 # The base size is the size of the pixel pack info
249 # header used by images with the specified number
257 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim
, self
.image
.name
, self
.name
))
261 for param
in self
.parameterIterateGlxSend():
262 if param
.img_null_flag
:
265 if param
.name
!= self
.img_reset
:
266 param
.offset
= offset
267 if not param
.is_variable_length():
268 offset
+= param
.size()
270 if self
.pad_after( param
):
274 self
.offsets_calculated
= 1
278 def offset_of(self
, param_name
):
279 self
.calculate_offsets()
280 return self
.parameters_by_name
[ param_name
].offset
283 def parameterIterateGlxSend(self
, include_variable_parameters
= 1):
284 """Create an iterator for parameters in GLX request order."""
286 # The parameter lists are usually quite short, so it's easier
287 # (i.e., less code) to just generate a new list with the
288 # required elements than it is to create a new iterator class.
290 temp
= [ [], [], [] ]
291 for param
in self
.parameters
:
292 if param
.is_output
: continue
294 if param
.is_variable_length():
295 temp
[2].append( param
)
296 elif not self
.glx_doubles_in_order
and param
.is_64_bit():
297 temp
[0].append( param
)
299 temp
[1].append( param
)
302 parameters
.extend( temp
[1] )
303 if include_variable_parameters
:
304 parameters
.extend( temp
[2] )
305 return parameters
.__iter
__()
308 def parameterIterateCounters(self
):
310 for name
in self
.counter_list
:
311 temp
.append( self
.parameters_by_name
[ name
] )
313 return temp
.__iter
__()
316 def parameterIterateOutputs(self
):
318 for p
in self
.parameters
:
325 def command_fixed_length(self
):
326 """Return the length, in bytes as an integer, of the
327 fixed-size portion of the command."""
329 if len(self
.parameters
) == 0:
332 self
.calculate_offsets()
335 for param
in self
.parameterIterateGlxSend(0):
336 if param
.name
!= self
.img_reset
:
338 size
= param
.offset
+ param
.size()
342 if self
.pad_after( param
):
345 for param
in self
.images
:
346 if param
.img_null_flag
or param
.is_output
:
352 def command_variable_length(self
):
353 """Return the length, as a string, of the variable-sized
354 portion of the command."""
357 for p
in self
.parameterIterateGlxSend():
358 if (not p
.is_output
) and (p
.is_variable_length() or p
.is_image()):
359 # FIXME Replace the 1 in the size_string call
360 # FIXME w/0 to eliminate some un-needed parnes
361 # FIXME This would already be done, but it
362 # FIXME adds some extra diffs to the generated
365 size_string
= size_string
+ " + __GLX_PAD(%s)" % (p
.size_string(1))
370 def command_length(self
):
371 size
= self
.command_fixed_length()
373 if self
.glx_rop
!= 0:
376 size
= ((size
+ 3) & ~
3)
377 return "%u%s" % (size
, self
.command_variable_length())
380 def opcode_real_value(self
):
381 """Get the true numeric value of the GLX opcode
383 Behaves similarly to opcode_value, except for
384 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
385 In these cases the value for the GLX opcode field (i.e.,
386 16 for X_GLXVendorPrivate or 17 for
387 X_GLXVendorPrivateWithReply) is returned. For other 'single'
388 commands, the opcode for the command (e.g., 101 for
389 X_GLsop_NewList) is returned."""
391 if self
.glx_vendorpriv
!= 0:
392 if self
.needs_reply():
397 return self
.opcode_value()
400 def opcode_value(self
):
401 """Get the unique protocol opcode for the glXFunction"""
403 if (self
.glx_rop
== 0) and self
.vectorequiv
:
404 equiv
= self
.context
.functions_by_name
[ self
.vectorequiv
]
405 self
.glx_rop
= equiv
.glx_rop
408 if self
.glx_rop
!= 0:
410 elif self
.glx_sop
!= 0:
412 elif self
.glx_vendorpriv
!= 0:
413 return self
.glx_vendorpriv
418 def opcode_rop_basename(self
):
419 """Return either the name to be used for GLX protocol enum.
421 Returns either the name of the function or the name of the
422 name of the equivalent vector (e.g., glVertex3fv for
423 glVertex3f) function."""
425 if self
.vectorequiv
== None:
428 return self
.vectorequiv
431 def opcode_name(self
):
432 """Get the unique protocol enum name for the glXFunction"""
434 if (self
.glx_rop
== 0) and self
.vectorequiv
:
435 equiv
= self
.context
.functions_by_name
[ self
.vectorequiv
]
436 self
.glx_rop
= equiv
.glx_rop
437 self
.glx_doubles_in_order
= equiv
.glx_doubles_in_order
440 if self
.glx_rop
!= 0:
441 return "X_GLrop_%s" % (self
.opcode_rop_basename())
442 elif self
.glx_sop
!= 0:
443 return "X_GLsop_%s" % (self
.name
)
444 elif self
.glx_vendorpriv
!= 0:
445 return "X_GLvop_%s" % (self
.name
)
447 raise RuntimeError('Function "%s" has no opcode.' % (self
.name
))
450 def opcode_real_name(self
):
451 """Get the true protocol enum name for the GLX opcode
453 Behaves similarly to opcode_name, except for
454 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
455 In these cases the string 'X_GLXVendorPrivate' or
456 'X_GLXVendorPrivateWithReply' is returned. For other
457 single or render commands 'X_GLsop' or 'X_GLrop' plus the
458 name of the function returned."""
460 if self
.glx_vendorpriv
!= 0:
461 if self
.needs_reply():
462 return "X_GLXVendorPrivateWithReply"
464 return "X_GLXVendorPrivate"
466 return self
.opcode_name()
469 def needs_reply(self
):
471 x
= self
._needs
_reply
474 if self
.return_type
!= 'void':
477 for param
in self
.parameters
:
482 self
._needs
_reply
= x
487 def pad_after(self
, p
):
488 """Returns the name of the field inserted after the
489 specified field to pad out the command header."""
491 for image
in self
.images
:
492 if image
.img_pad_dimensions
:
494 if p
.name
== image
.width
:
496 elif p
.name
== image
.img_xoff
:
498 elif not image
.extent
:
499 if p
.name
== image
.depth
:
500 # Should this be "size4d"?
502 elif p
.name
== image
.img_zoff
:
508 def client_supported_for_indirect(self
):
509 """Returns true if the function is supported on the client
510 side for indirect rendering."""
512 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
)
515 class glx_function_iterator
:
516 """Class to iterate over a list of glXFunctions"""
518 def __init__(self
, context
):
519 self
.iterator
= context
.functionIterateByOffset()
528 f
= self
.iterator
.next()
529 if f
.client_supported_for_indirect():
535 class glx_api(gl_XML
.gl_api
):
536 def functionIterateGlx(self
):
537 return glx_function_iterator(self
)