Fix bug #4681.
[mesa.git] / src / mesa / glapi / glX_XML.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
4 # All Rights Reserved.
5 #
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:
12 #
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
15 # Software.
16 #
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
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 import gl_XML
29 import license
30 import sys, getopt, string
31
32
33 class glx_item_factory(gl_XML.gl_item_factory):
34 """Factory to create GLX protocol oriented objects derived from gl_item."""
35
36 def create_item(self, name, element, context):
37 if name == "function":
38 return glx_function(element, context)
39 elif name == "enum":
40 return glx_enum(element, context)
41 elif name == "api":
42 return glx_api(self)
43 else:
44 return gl_XML.gl_item_factory.create_item(self, name, element, context)
45
46
47 class glx_enum(gl_XML.gl_enum):
48 def __init__(self, element, context):
49 gl_XML.gl_enum.__init__(self, element, context)
50
51 self.functions = {}
52
53 child = element.children
54 while child:
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 )
59
60 if not c:
61 c = self.default_count
62 else:
63 c = int(c)
64
65 if m == "get":
66 mode = 0
67 else:
68 mode = 1
69
70 if not self.functions.has_key(n):
71 self.functions[ n ] = [c, mode]
72
73 child = child.next
74
75 return
76
77
78 class glx_function(gl_XML.gl_function):
79 def __init__(self, element, context):
80 self.glx_rop = 0
81 self.glx_sop = 0
82 self.glx_vendorpriv = 0
83
84 self.glx_vendorpriv_names = []
85
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.
92
93 self.glx_doubles_in_order = 0
94
95 self.vectorequiv = None
96 self.output = None
97 self.can_be_large = 0
98 self.reply_always_array = 0
99 self.dimensions_in_reply = 0
100 self.img_reset = None
101
102 self.server_handcode = 0
103 self.client_handcode = 0
104 self.ignore = 0
105
106 self.count_parameter_list = []
107 self.counter_list = []
108 self.parameters_by_name = {}
109 self.offsets_calculated = 0
110
111 gl_XML.gl_function.__init__(self, element, context)
112 return
113
114
115 def process_element(self, element):
116 gl_XML.gl_function.process_element(self, element)
117
118 self.vectorequiv = element.nsProp( "vectorequiv", None )
119
120
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
125
126 if len(param.count_parameter_list):
127 self.count_parameter_list.extend( param.count_parameter_list )
128
129 if param.counter and param.counter not in self.counter_list:
130 self.counter_list.append(param.counter)
131
132
133 child = element.children
134 while child:
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 )
140
141 if rop:
142 self.glx_rop = int(rop)
143
144 if sop:
145 self.glx_sop = int(sop)
146
147 if vop:
148 self.glx_vendorpriv = int(vop)
149 self.glx_vendorpriv_names.append(name)
150
151 self.img_reset = child.nsProp( 'img_reset', None )
152
153 # The 'handcode' attribute can be one of 'true',
154 # 'false', 'client', or 'server'.
155
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
169 else:
170 raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode, self.name))
171
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' )
177
178 child = child.next
179
180
181 # Do some validation of the GLX protocol information. As
182 # new tests are discovered, they should be added here.
183
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))
187
188 return
189
190
191 def has_variable_size_request(self):
192 """Determine if the GLX request packet is variable sized.
193
194 The GLX request packet is variable sized in several common
195 situations.
196
197 1. The function has a non-output parameter that is counted
198 by another parameter (e.g., the 'textures' parameter of
199 glDeleteTextures).
200
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).
204
205 3. The function has a non-output parameter that is an
206 image.
207
208 4. The function must be hand-coded on the server.
209 """
210
211 if self.glx_rop == 0:
212 return 0
213
214 if self.server_handcode or self.images:
215 return 1
216
217 for param in self.parameters:
218 if not param.is_output:
219 if param.counter or len(param.count_parameter_list):
220 return 1
221
222 return 0
223
224
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):
229 return param
230
231 return None
232
233
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.
242
243 if len(self.images) and not self.images[0].is_output:
244 [dim, junk, junk, junk, junk] = self.images[0].get_dimensions()
245
246 # The base size is the size of the pixel pack info
247 # header used by images with the specified number
248 # of dimensions.
249
250 if dim <= 2:
251 offset = 20
252 elif dim <= 4:
253 offset = 36
254 else:
255 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim, self.image.name, self.name))
256 else:
257 offset = 0
258
259 for param in self.parameterIterateGlxSend():
260 if param.img_null_flag:
261 offset += 4
262
263 if param.name != self.img_reset:
264 param.offset = offset
265 if not param.is_variable_length():
266 offset += param.size()
267
268 if self.pad_after( param ):
269 offset += 4
270
271
272 self.offsets_calculated = 1
273 return
274
275
276 def offset_of(self, param_name):
277 self.calculate_offsets()
278 return self.parameters_by_name[ param_name ].offset
279
280
281 def parameterIterateGlxSend(self, include_variable_parameters = 1):
282 """Create an iterator for parameters in GLX request order."""
283
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.
287
288 temp = [ [], [], [] ]
289 for param in self.parameters:
290 if param.is_output: continue
291
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 )
296 else:
297 temp[1].append( param )
298
299 parameters = temp[0]
300 parameters.extend( temp[1] )
301 if include_variable_parameters:
302 parameters.extend( temp[2] )
303 return parameters.__iter__()
304
305
306 def parameterIterateCounters(self):
307 temp = []
308 for name in self.counter_list:
309 temp.append( self.parameters_by_name[ name ] )
310
311 return temp.__iter__()
312
313
314 def parameterIterateOutputs(self):
315 temp = []
316 for p in self.parameters:
317 if p.is_output:
318 temp.append( p )
319
320 return temp
321
322
323 def command_fixed_length(self):
324 """Return the length, in bytes as an integer, of the
325 fixed-size portion of the command."""
326
327 if len(self.parameters) == 0:
328 return 0
329
330 self.calculate_offsets()
331
332 size = 0
333 for param in self.parameterIterateGlxSend(0):
334 if param.name != self.img_reset:
335 if size == 0:
336 size = param.offset + param.size()
337 else:
338 size += param.size()
339
340 if self.pad_after( param ):
341 size += 4
342
343 for param in self.images:
344 if param.img_null_flag or param.is_output:
345 size += 4
346
347 return size
348
349
350 def command_variable_length(self):
351 """Return the length, as a string, of the variable-sized
352 portion of the command."""
353
354 size_string = ""
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
361 # FIXME code.
362
363 size_string = size_string + " + __GLX_PAD(%s)" % (p.size_string(1))
364
365 return size_string
366
367
368 def command_length(self):
369 size = self.command_fixed_length()
370
371 if self.glx_rop != 0:
372 size += 4
373
374 size = ((size + 3) & ~3)
375 return "%u%s" % (size, self.command_variable_length())
376
377
378 def opcode_real_value(self):
379 """Get the true numeric value of the GLX opcode
380
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."""
388
389 if self.glx_vendorpriv != 0:
390 if self.needs_reply():
391 return 17
392 else:
393 return 16
394 else:
395 return self.opcode_value()
396
397
398 def opcode_value(self):
399 """Get the unique protocol opcode for the glXFunction"""
400
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
404
405
406 if self.glx_rop != 0:
407 return self.glx_rop
408 elif self.glx_sop != 0:
409 return self.glx_sop
410 elif self.glx_vendorpriv != 0:
411 return self.glx_vendorpriv
412 else:
413 return -1
414
415
416 def opcode_rop_basename(self):
417 """Return either the name to be used for GLX protocol enum.
418
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."""
422
423 if self.vectorequiv == None:
424 return self.name
425 else:
426 return self.vectorequiv
427
428
429 def opcode_name(self):
430 """Get the unique protocol enum name for the glXFunction"""
431
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
436
437
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)
444 else:
445 raise RuntimeError('Function "%s" has no opcode.' % (self.name))
446
447
448 def opcode_vendor_name(self, name):
449 if name in self.glx_vendorpriv_names:
450 return "X_GLvop_%s" % (name)
451 else:
452 raise RuntimeError('Function "%s" has no VendorPrivate opcode.' % (name))
453
454
455 def opcode_real_name(self):
456 """Get the true protocol enum name for the GLX opcode
457
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."""
464
465 if self.glx_vendorpriv != 0:
466 if self.needs_reply():
467 return "X_GLXVendorPrivateWithReply"
468 else:
469 return "X_GLXVendorPrivate"
470 else:
471 return self.opcode_name()
472
473
474 def needs_reply(self):
475 try:
476 x = self._needs_reply
477 except Exception, e:
478 x = 0
479 if self.return_type != 'void':
480 x = 1
481
482 for param in self.parameters:
483 if param.is_output:
484 x = 1
485 break
486
487 self._needs_reply = x
488
489 return x
490
491
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."""
495
496 for image in self.images:
497 if image.img_pad_dimensions:
498 if not image.height:
499 if p.name == image.width:
500 return "height"
501 elif p.name == image.img_xoff:
502 return "yoffset"
503 elif not image.extent:
504 if p.name == image.depth:
505 # Should this be "size4d"?
506 return "extent"
507 elif p.name == image.img_zoff:
508 return "woffset"
509
510 return None
511
512
513 def has_different_protocol(self, name):
514 """Returns true if the named version of the function uses different protocol from the other versions.
515
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.
521 """
522
523 return (name in self.glx_vendorpriv_names) and self.glx_sop
524
525
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:
530 return n
531
532 return self.static_name(name)
533
534
535 def client_supported_for_indirect(self):
536 """Returns true if the function is supported on the client
537 side for indirect rendering."""
538
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)
540
541
542 class glx_function_iterator:
543 """Class to iterate over a list of glXFunctions"""
544
545 def __init__(self, context):
546 self.iterator = context.functionIterateByOffset()
547 return
548
549
550 def __iter__(self):
551 return self
552
553
554 def next(self):
555 f = self.iterator.next()
556 if f.client_supported_for_indirect():
557 return f
558 else:
559 return self.next()
560
561
562 class glx_api(gl_XML.gl_api):
563 def functionIterateGlx(self):
564 return glx_function_iterator(self)
565