e3d4c666cfc5166ee3ad436bbed0b7c1ec6d1062
[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 # 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.
90
91 self.glx_doubles_in_order = 0
92
93 self.vectorequiv = None
94 self.output = None
95 self.can_be_large = 0
96 self.reply_always_array = 0
97 self.dimensions_in_reply = 0
98 self.img_reset = None
99
100 self.server_handcode = 0
101 self.client_handcode = 0
102 self.ignore = 0
103
104 self.count_parameter_list = []
105 self.counter_list = []
106 self.parameters_by_name = {}
107 self.offsets_calculated = 0
108
109 gl_XML.gl_function.__init__(self, element, context)
110 return
111
112
113 def process_element(self, element):
114 gl_XML.gl_function.process_element(self, element)
115
116 self.vectorequiv = element.nsProp( "vectorequiv", None )
117
118
119 if element.nsProp( "name", None ) == self.name:
120 for param in self.parameters:
121 self.parameters_by_name[ param.name ] = param
122
123 if len(param.count_parameter_list):
124 self.count_parameter_list.extend( param.count_parameter_list )
125
126 if param.counter and param.counter not in self.counter_list:
127 self.counter_list.append(param.counter)
128
129
130 child = element.children
131 while child:
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 )
137
138 if rop:
139 self.glx_rop = int(rop)
140 else:
141 self.glx_rop = 0
142
143 if sop:
144 self.glx_sop = int(sop)
145 else:
146 self.glx_sop = 0
147
148 if vop:
149 self.glx_vendorpriv = int(vop)
150 else:
151 self.glx_vendorpriv = 0
152
153 self.img_reset = child.nsProp( 'img_reset', None )
154
155 # The 'handcode' attribute can be one of 'true',
156 # 'false', 'client', or 'server'.
157
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
171 else:
172 raise RuntimeError('Invalid handcode mode "%s" in function "%s".' % (handcode, self.name))
173
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' )
179
180 child = child.next
181
182
183 # Do some validation of the GLX protocol information. As
184 # new tests are discovered, they should be added here.
185
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))
189
190 return
191
192
193 def has_variable_size_request(self):
194 """Determine if the GLX request packet is variable sized.
195
196 The GLX request packet is variable sized in several common
197 situations.
198
199 1. The function has a non-output parameter that is counted
200 by another parameter (e.g., the 'textures' parameter of
201 glDeleteTextures).
202
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).
206
207 3. The function has a non-output parameter that is an
208 image.
209
210 4. The function must be hand-coded on the server.
211 """
212
213 if self.glx_rop == 0:
214 return 0
215
216 if self.server_handcode or self.images:
217 return 1
218
219 for param in self.parameters:
220 if not param.is_output:
221 if param.counter or len(param.count_parameter_list):
222 return 1
223
224 return 0
225
226
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):
231 return param
232
233 return None
234
235
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.
244
245 if len(self.images) and not self.images[0].is_output:
246 [dim, junk, junk, junk, junk] = self.images[0].get_dimensions()
247
248 # The base size is the size of the pixel pack info
249 # header used by images with the specified number
250 # of dimensions.
251
252 if dim <= 2:
253 offset = 20
254 elif dim <= 4:
255 offset = 36
256 else:
257 raise RuntimeError('Invalid number of dimensions %u for parameter "%s" in function "%s".' % (dim, self.image.name, self.name))
258 else:
259 offset = 0
260
261 for param in self.parameterIterateGlxSend():
262 if param.img_null_flag:
263 offset += 4
264
265 if param.name != self.img_reset:
266 param.offset = offset
267 if not param.is_variable_length():
268 offset += param.size()
269
270 if self.pad_after( param ):
271 offset += 4
272
273
274 self.offsets_calculated = 1
275 return
276
277
278 def offset_of(self, param_name):
279 self.calculate_offsets()
280 return self.parameters_by_name[ param_name ].offset
281
282
283 def parameterIterateGlxSend(self, include_variable_parameters = 1):
284 """Create an iterator for parameters in GLX request order."""
285
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.
289
290 temp = [ [], [], [] ]
291 for param in self.parameters:
292 if param.is_output: continue
293
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 )
298 else:
299 temp[1].append( param )
300
301 parameters = temp[0]
302 parameters.extend( temp[1] )
303 if include_variable_parameters:
304 parameters.extend( temp[2] )
305 return parameters.__iter__()
306
307
308 def parameterIterateCounters(self):
309 temp = []
310 for name in self.counter_list:
311 temp.append( self.parameters_by_name[ name ] )
312
313 return temp.__iter__()
314
315
316 def parameterIterateOutputs(self):
317 temp = []
318 for p in self.parameters:
319 if p.is_output:
320 temp.append( p )
321
322 return temp
323
324
325 def command_fixed_length(self):
326 """Return the length, in bytes as an integer, of the
327 fixed-size portion of the command."""
328
329 if len(self.parameters) == 0:
330 return 0
331
332 self.calculate_offsets()
333
334 size = 0
335 for param in self.parameterIterateGlxSend(0):
336 if param.name != self.img_reset:
337 if size == 0:
338 size = param.offset + param.size()
339 else:
340 size += param.size()
341
342 if self.pad_after( param ):
343 size += 4
344
345 for param in self.images:
346 if param.img_null_flag or param.is_output:
347 size += 4
348
349 return size
350
351
352 def command_variable_length(self):
353 """Return the length, as a string, of the variable-sized
354 portion of the command."""
355
356 size_string = ""
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
363 # FIXME code.
364
365 size_string = size_string + " + __GLX_PAD(%s)" % (p.size_string(1))
366
367 return size_string
368
369
370 def command_length(self):
371 size = self.command_fixed_length()
372
373 if self.glx_rop != 0:
374 size += 4
375
376 size = ((size + 3) & ~3)
377 return "%u%s" % (size, self.command_variable_length())
378
379
380 def opcode_real_value(self):
381 """Get the true numeric value of the GLX opcode
382
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."""
390
391 if self.glx_vendorpriv != 0:
392 if self.needs_reply():
393 return 17
394 else:
395 return 16
396 else:
397 return self.opcode_value()
398
399
400 def opcode_value(self):
401 """Get the unique protocol opcode for the glXFunction"""
402
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
406
407
408 if self.glx_rop != 0:
409 return self.glx_rop
410 elif self.glx_sop != 0:
411 return self.glx_sop
412 elif self.glx_vendorpriv != 0:
413 return self.glx_vendorpriv
414 else:
415 return -1
416
417
418 def opcode_rop_basename(self):
419 """Return either the name to be used for GLX protocol enum.
420
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."""
424
425 if self.vectorequiv == None:
426 return self.name
427 else:
428 return self.vectorequiv
429
430
431 def opcode_name(self):
432 """Get the unique protocol enum name for the glXFunction"""
433
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
438
439
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)
446 else:
447 raise RuntimeError('Function "%s" has no opcode.' % (self.name))
448
449
450 def opcode_real_name(self):
451 """Get the true protocol enum name for the GLX opcode
452
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."""
459
460 if self.glx_vendorpriv != 0:
461 if self.needs_reply():
462 return "X_GLXVendorPrivateWithReply"
463 else:
464 return "X_GLXVendorPrivate"
465 else:
466 return self.opcode_name()
467
468
469 def needs_reply(self):
470 try:
471 x = self._needs_reply
472 except Exception, e:
473 x = 0
474 if self.return_type != 'void':
475 x = 1
476
477 for param in self.parameters:
478 if param.is_output:
479 x = 1
480 break
481
482 self._needs_reply = x
483
484 return x
485
486
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."""
490
491 for image in self.images:
492 if image.img_pad_dimensions:
493 if not image.height:
494 if p.name == image.width:
495 return "height"
496 elif p.name == image.img_xoff:
497 return "yoffset"
498 elif not image.extent:
499 if p.name == image.depth:
500 # Should this be "size4d"?
501 return "extent"
502 elif p.name == image.img_zoff:
503 return "woffset"
504
505 return None
506
507
508 def client_supported_for_indirect(self):
509 """Returns true if the function is supported on the client
510 side for indirect rendering."""
511
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)
513
514
515 class glx_function_iterator:
516 """Class to iterate over a list of glXFunctions"""
517
518 def __init__(self, context):
519 self.iterator = context.functionIterateByOffset()
520 return
521
522
523 def __iter__(self):
524 return self
525
526
527 def next(self):
528 f = self.iterator.next()
529 if f.client_supported_for_indirect():
530 return f
531 else:
532 return self.next()
533
534
535 class glx_api(gl_XML.gl_api):
536 def functionIterateGlx(self):
537 return glx_function_iterator(self)
538