Fix the scripts for the cygwin & mingw changes
[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
63 if m == "get":
64 mode = 0
65 else:
66 mode = 1
67
68 if not self.functions.has_key(n):
69 self.functions[ n ] = [c, mode]
70
71 child = child.next
72
73 return
74
75
76 class glx_function(gl_XML.gl_function):
77 def __init__(self, element, context):
78 self.glx_rop = 0
79 self.glx_sop = 0
80 self.glx_vendorpriv = 0
81
82 # If this is set to true, it means that GLdouble parameters should be
83 # written to the GLX protocol packet in the order they appear in the
84 # prototype. This is different from the "classic" ordering. In the
85 # classic ordering GLdoubles are written to the protocol packet first,
86 # followed by non-doubles. NV_vertex_program was the first extension
87 # to break with this tradition.
88
89 self.glx_doubles_in_order = 0
90
91 self.vectorequiv = None
92 self.output = None
93 self.can_be_large = 0
94 self.reply_always_array = 0
95 self.dimensions_in_reply = 0
96 self.img_reset = None
97
98 self.server_handcode = 0
99 self.client_handcode = 0
100 self.ignore = 0
101
102 self.count_parameter_list = []
103 self.counter_list = []
104 self.parameters_by_name = {}
105 self.offsets_calculated = 0
106
107 gl_XML.gl_function.__init__(self, element, context)
108 return
109
110
111 def process_element(self, element):
112 gl_XML.gl_function.process_element(self, element)
113
114 self.vectorequiv = element.nsProp( "vectorequiv", None )
115
116
117 if element.nsProp( "name", None ) == self.name:
118 for param in self.parameters:
119 self.parameters_by_name[ param.name ] = param
120
121 if len(param.count_parameter_list):
122 self.count_parameter_list.extend( param.count_parameter_list )
123
124 if param.counter and param.counter not in self.counter_list:
125 self.counter_list.append(param.counter)
126
127
128 child = element.children
129 while child:
130 if child.type == "element":
131 if child.name == "glx":
132 rop = child.nsProp( 'rop', None )
133 sop = child.nsProp( 'sop', None )
134 vop = child.nsProp( 'vendorpriv', None )
135
136 if rop:
137 self.glx_rop = int(rop)
138 else:
139 self.glx_rop = 0
140
141 if sop:
142 self.glx_sop = int(sop)
143 else:
144 self.glx_sop = 0
145
146 if vop:
147 self.glx_vendorpriv = int(vop)
148 else:
149 self.glx_vendorpriv = 0
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_real_name(self):
449 """Get the true protocol enum name for the GLX opcode
450
451 Behaves similarly to opcode_name, except for
452 X_GLXVendorPrivate and X_GLXVendorPrivateWithReply commands.
453 In these cases the string 'X_GLXVendorPrivate' or
454 'X_GLXVendorPrivateWithReply' is returned. For other
455 single or render commands 'X_GLsop' or 'X_GLrop' plus the
456 name of the function returned."""
457
458 if self.glx_vendorpriv != 0:
459 if self.needs_reply():
460 return "X_GLXVendorPrivateWithReply"
461 else:
462 return "X_GLXVendorPrivate"
463 else:
464 return self.opcode_name()
465
466
467 def needs_reply(self):
468 try:
469 x = self._needs_reply
470 except Exception, e:
471 x = 0
472 if self.return_type != 'void':
473 x = 1
474
475 for param in self.parameters:
476 if param.is_output:
477 x = 1
478 break
479
480 self._needs_reply = x
481
482 return x
483
484
485 def pad_after(self, p):
486 """Returns the name of the field inserted after the
487 specified field to pad out the command header."""
488
489 for image in self.images:
490 if image.img_pad_dimensions:
491 if not image.height:
492 if p.name == image.width:
493 return "height"
494 elif p.name == image.img_xoff:
495 return "yoffset"
496 elif not image.extent:
497 if p.name == image.depth:
498 # Should this be "size4d"?
499 return "extent"
500 elif p.name == image.img_zoff:
501 return "woffset"
502
503 return None
504
505
506 class glx_function_iterator:
507 """Class to iterate over a list of glXFunctions"""
508
509 def __init__(self, context):
510 self.iterator = context.functionIterateByOffset()
511 return
512
513
514 def __iter__(self):
515 return self
516
517
518 def next(self):
519 f = self.iterator.next()
520 if f.ignore or not (f.glx_rop or f.glx_sop or f.glx_vendorpriv or f.vectorequiv or f.client_handcode):
521 return self.next()
522 else:
523 return f
524
525
526 class glx_api(gl_XML.gl_api):
527 def functionIterateGlx(self):
528 return glx_function_iterator(self)
529