2 # (C) Copyright IBM Corporation 2004, 2005
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # on the rights to use, copy, modify, merge, publish, distribute, sub
9 # license, and/or sell copies of the Software, and to permit persons to whom
10 # the Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 # Ian Romanick <idr@us.ibm.com>
29 import sys
, getopt
, string
32 class glx_item_factory(gl_XML
.gl_item_factory
):
33 """Factory to create GLX protocol oriented objects derived from gl_item."""
35 def create_function(self
, element
, context
):
36 return glx_function(element
, context
)
38 def create_enum(self
, element
, context
, category
):
39 return glx_enum(element
, context
, category
)
45 class glx_enum(gl_XML
.gl_enum
):
46 def __init__(self
, element
, context
, category
):
47 gl_XML
.gl_enum
.__init
__(self
, element
, context
, category
)
51 for child
in element
.getchildren():
52 if child
.tag
== "size":
53 n
= child
.get( "name" )
54 c
= child
.get( "count" )
55 m
= child
.get( "mode", "set" )
58 c
= self
.default_count
67 if n
not in self
.functions
:
68 self
.functions
[ n
] = [c
, mode
]
73 class glx_function(gl_XML
.gl_function
):
74 def __init__(self
, element
, context
):
77 self
.glx_vendorpriv
= 0
79 self
.glx_vendorpriv_names
= []
81 # If this is set to true, it means that GLdouble parameters should be
82 # written to the GLX protocol packet in the order they appear in the
83 # prototype. This is different from the "classic" ordering. In the
84 # classic ordering GLdoubles are written to the protocol packet first,
85 # followed by non-doubles. NV_vertex_program was the first extension
86 # to break with this tradition.
88 self
.glx_doubles_in_order
= 0
90 self
.vectorequiv
= None
93 self
.reply_always_array
= 0
94 self
.dimensions_in_reply
= 0
97 self
.server_handcode
= 0
98 self
.client_handcode
= 0
101 self
.count_parameter_list
= []
102 self
.counter_list
= []
103 self
.parameters_by_name
= {}
104 self
.offsets_calculated
= 0
106 gl_XML
.gl_function
.__init
__(self
, element
, context
)
110 def process_element(self
, element
):
111 gl_XML
.gl_function
.process_element(self
, element
)
113 # If the function already has a vector equivalent set, don't
114 # set it again. This can happen if an alias to a function
115 # appears after the function that it aliases.
117 if not self
.vectorequiv
:
118 self
.vectorequiv
= element
.get("vectorequiv")
121 name
= element
.get("name")
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 for child
in element
.getchildren():
134 if child
.tag
== "glx":
135 rop
= child
.get( 'rop' )
136 sop
= child
.get( 'sop' )
137 vop
= child
.get( 'vendorpriv' )
140 self
.glx_rop
= int(rop
)
143 self
.glx_sop
= int(sop
)
146 self
.glx_vendorpriv
= int(vop
)
147 self
.glx_vendorpriv_names
.append(name
)
149 self
.img_reset
= child
.get( 'img_reset' )
151 # The 'handcode' attribute can be one of 'true',
152 # 'false', 'client', or 'server'.
154 handcode
= child
.get( 'handcode', 'false' )
155 if handcode
== "false":
156 self
.server_handcode
= 0
157 self
.client_handcode
= 0
158 elif handcode
== "true":
159 self
.server_handcode
= 1
160 self
.client_handcode
= 1
161 elif handcode
== "client":
162 self
.server_handcode
= 0
163 self
.client_handcode
= 1
164 elif handcode
== "server":
165 self
.server_handcode
= 1
166 self
.client_handcode
= 0
168 raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode
, self
.name
))
170 self
.ignore
= gl_XML
.is_attr_true( child
, 'ignore' )
171 self
.can_be_large
= gl_XML
.is_attr_true( child
, 'large' )
172 self
.glx_doubles_in_order
= gl_XML
.is_attr_true( child
, 'doubles_in_order' )
173 self
.reply_always_array
= gl_XML
.is_attr_true( child
, 'always_array' )
174 self
.dimensions_in_reply
= gl_XML
.is_attr_true( child
, 'dimensions_in_reply' )
177 # Do some validation of the GLX protocol information. As
178 # new tests are discovered, they should be added here.
180 for param
in self
.parameters
:
181 if param
.is_output
and self
.glx_rop
!= 0:
182 raise RuntimeError("Render / RenderLarge commands cannot have outputs (%s)." % (self
.name
))
187 def has_variable_size_request(self
):
188 """Determine if the GLX request packet is variable sized.
190 The GLX request packet is variable sized in several common
193 1. The function has a non-output parameter that is counted
194 by another parameter (e.g., the 'textures' parameter of
197 2. The function has a non-output parameter whose count is
198 determined by another parameter that is an enum (e.g., the
199 'params' parameter of glLightfv).
201 3. The function has a non-output parameter that is an
204 4. The function must be hand-coded on the server.
207 if self
.glx_rop
== 0:
210 if self
.server_handcode
or self
.images
:
213 for param
in self
.parameters
:
214 if not param
.is_output
:
215 if param
.counter
or len(param
.count_parameter_list
):
221 def variable_length_parameter(self
):
222 for param
in self
.parameters
:
223 if not param
.is_output
:
224 if param
.counter
or len(param
.count_parameter_list
):
230 def calculate_offsets(self
):
231 if not self
.offsets_calculated
:
232 # Calculate the offset of the first function parameter
233 # in the GLX command packet. This byte offset is
234 # measured from the end of the Render / RenderLarge
235 # header. The offset for all non-pixel commends is
236 # zero. The offset for pixel commands depends on the
237 # number of dimensions of the pixel data.
239 if len(self
.images
) and not self
.images
[0].is_output
:
240 [dim
, junk
, junk
, junk
, junk
] = self
.images
[0].get_dimensions()
242 # The base size is the size of the pixel pack info
243 # header used by images with the specified number
251 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim
, self
.image
.name
, self
.name
))
255 for param
in self
.parameterIterateGlxSend():
256 if param
.img_null_flag
:
259 if param
.name
!= self
.img_reset
:
260 param
.offset
= offset
261 if not param
.is_variable_length() and not param
.is_client_only
:
262 offset
+= param
.size()
264 if self
.pad_after( param
):
268 self
.offsets_calculated
= 1
272 def offset_of(self
, param_name
):
273 self
.calculate_offsets()
274 return self
.parameters_by_name
[ param_name
].offset
277 def parameterIterateGlxSend(self
, include_variable_parameters
= 1):
278 """Create an iterator for parameters in GLX request order."""
280 # The parameter lists are usually quite short, so it's easier
281 # (i.e., less code) to just generate a new list with the
282 # required elements than it is to create a new iterator class.
284 temp
= [ [], [], [] ]
285 for param
in self
.parameters
:
286 if param
.is_output
: continue
288 if param
.is_variable_length():
289 temp
[2].append( param
)
290 elif not self
.glx_doubles_in_order
and param
.is_64_bit():
291 temp
[0].append( param
)
293 temp
[1].append( param
)
296 parameters
.extend( temp
[1] )
297 if include_variable_parameters
:
298 parameters
.extend( temp
[2] )
299 return iter(parameters
)
302 def parameterIterateCounters(self
):
304 for name
in self
.counter_list
:
305 temp
.append( self
.parameters_by_name
[ name
] )
310 def parameterIterateOutputs(self
):
312 for p
in self
.parameters
:
319 def command_fixed_length(self
):
320 """Return the length, in bytes as an integer, of the
321 fixed-size portion of the command."""
323 if len(self
.parameters
) == 0:
326 self
.calculate_offsets()
329 for param
in self
.parameterIterateGlxSend(0):
330 if param
.name
!= self
.img_reset
and not param
.is_client_only
:
332 size
= param
.offset
+ param
.size()
336 if self
.pad_after( param
):
339 for param
in self
.images
:
340 if param
.img_null_flag
or param
.is_output
:
346 def command_variable_length(self
):
347 """Return the length, as a string, of the variable-sized
348 portion of the command."""
351 for p
in self
.parameterIterateGlxSend():
352 if (not p
.is_output
) and (p
.is_variable_length() or p
.is_image()):
353 # FIXME Replace the 1 in the size_string call
354 # FIXME w/0 to eliminate some un-needed parnes
355 # FIXME This would already be done, but it
356 # FIXME adds some extra diffs to the generated
359 size_string
= size_string
+ " + safe_pad(%s)" % (p
.size_string(1))
364 def command_length(self
):
365 size
= self
.command_fixed_length()
367 if self
.glx_rop
!= 0:
370 size
= ((size
+ 3) & ~
3)
371 return "%u%s" % (size
, self
.command_variable_length())
374 def opcode_real_value(self
):
375 """Get the true numeric value of the GLX opcode
377 Behaves similarly to opcode_value, except for
378 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
379 In these cases the value for the GLX opcode field (i.e.,
380 16 for X_GLXVendorPrivate or 17 for
381 X_GLXVendorPrivateWithReply) is returned. For other 'single'
382 commands, the opcode for the command (e.g., 101 for
383 X_GLsop_NewList) is returned."""
385 if self
.glx_vendorpriv
!= 0:
386 if self
.needs_reply():
391 return self
.opcode_value()
394 def opcode_value(self
):
395 """Get the unique protocol opcode for the glXFunction"""
397 if (self
.glx_rop
== 0) and self
.vectorequiv
:
398 equiv
= self
.context
.functions_by_name
[ self
.vectorequiv
]
399 self
.glx_rop
= equiv
.glx_rop
402 if self
.glx_rop
!= 0:
404 elif self
.glx_sop
!= 0:
406 elif self
.glx_vendorpriv
!= 0:
407 return self
.glx_vendorpriv
412 def opcode_rop_basename(self
):
413 """Return either the name to be used for GLX protocol enum.
415 Returns either the name of the function or the name of the
416 name of the equivalent vector (e.g., glVertex3fv for
417 glVertex3f) function."""
419 if self
.vectorequiv
== None:
422 return self
.vectorequiv
425 def opcode_name(self
):
426 """Get the unique protocol enum name for the glXFunction"""
428 if (self
.glx_rop
== 0) and self
.vectorequiv
:
429 equiv
= self
.context
.functions_by_name
[ self
.vectorequiv
]
430 self
.glx_rop
= equiv
.glx_rop
431 self
.glx_doubles_in_order
= equiv
.glx_doubles_in_order
434 if self
.glx_rop
!= 0:
435 return "X_GLrop_%s" % (self
.opcode_rop_basename())
436 elif self
.glx_sop
!= 0:
437 return "X_GLsop_%s" % (self
.name
)
438 elif self
.glx_vendorpriv
!= 0:
439 return "X_GLvop_%s" % (self
.name
)
441 raise RuntimeError('Function "%s" has no opcode.' % (self
.name
))
444 def opcode_vendor_name(self
, name
):
445 if name
in self
.glx_vendorpriv_names
:
446 return "X_GLvop_%s" % (name
)
448 raise RuntimeError('Function "%s" has no VendorPrivate opcode.' % (name
))
451 def opcode_real_name(self
):
452 """Get the true protocol enum name for the GLX opcode
454 Behaves similarly to opcode_name, except for
455 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
456 In these cases the string 'X_GLXVendorPrivate' or
457 'X_GLXVendorPrivateWithReply' is returned. For other
458 single or render commands 'X_GLsop' or 'X_GLrop' plus the
459 name of the function returned."""
461 if self
.glx_vendorpriv
!= 0:
462 if self
.needs_reply():
463 return "X_GLXVendorPrivateWithReply"
465 return "X_GLXVendorPrivate"
467 return self
.opcode_name()
470 def needs_reply(self
):
472 x
= self
._needs
_reply
475 if self
.return_type
!= 'void':
478 for param
in self
.parameters
:
483 self
._needs
_reply
= x
488 def pad_after(self
, p
):
489 """Returns the name of the field inserted after the
490 specified field to pad out the command header."""
492 for image
in self
.images
:
493 if image
.img_pad_dimensions
:
495 if p
.name
== image
.width
:
497 elif p
.name
== image
.img_xoff
:
499 elif not image
.extent
:
500 if p
.name
== image
.depth
:
501 # Should this be "size4d"?
503 elif p
.name
== image
.img_zoff
:
509 def has_different_protocol(self
, name
):
510 """Returns true if the named version of the function uses different protocol from the other versions.
512 Some functions, such as glDeleteTextures and
513 glDeleteTexturesEXT are functionally identical, but have
514 different protocol. This function returns true if the
515 named function is an alias name and that named version uses
516 different protocol from the function that is aliased.
519 return (name
in self
.glx_vendorpriv_names
) and self
.glx_sop
522 def static_glx_name(self
, name
):
523 if self
.has_different_protocol(name
):
524 for n
in self
.glx_vendorpriv_names
:
525 if n
in self
.static_entry_points
:
528 return self
.static_name(name
)
531 def client_supported_for_indirect(self
):
532 """Returns true if the function is supported on the client
533 side for indirect rendering."""
535 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
)
538 class glx_function_iterator(object):
539 """Class to iterate over a list of glXFunctions"""
541 def __init__(self
, context
):
542 self
.iterator
= context
.functionIterateByOffset()
552 f
= next(self
.iterator
)
554 if f
.client_supported_for_indirect():
560 class glx_api(gl_XML
.gl_api
):
561 def functionIterateGlx(self
):
562 return glx_function_iterator(self
)