glapi: fix argument parsing in glX_proto_recv.py
[mesa.git] / src / mapi / glapi / gen / glX_proto_recv.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 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 argparse
29 import string
30
31 import gl_XML, glX_XML, glX_proto_common, license
32
33
34 class PrintGlxDispatch_h(gl_XML.gl_print_base):
35 def __init__(self):
36 gl_XML.gl_print_base.__init__(self)
37
38 self.name = "glX_proto_recv.py (from Mesa)"
39 self.license = license.bsd_license_template % ( "(C) Copyright IBM Corporation 2005", "IBM")
40
41 self.header_tag = "_INDIRECT_DISPATCH_H_"
42 return
43
44
45 def printRealHeader(self):
46 print '# include <X11/Xfuncproto.h>'
47 print ''
48 print 'struct __GLXclientStateRec;'
49 print ''
50 return
51
52
53 def printBody(self, api):
54 for func in api.functionIterateAll():
55 if not func.ignore and not func.vectorequiv:
56 if func.glx_rop:
57 print 'extern _X_HIDDEN void __glXDisp_%s(GLbyte * pc);' % (func.name)
58 print 'extern _X_HIDDEN void __glXDispSwap_%s(GLbyte * pc);' % (func.name)
59 elif func.glx_sop or func.glx_vendorpriv:
60 print 'extern _X_HIDDEN int __glXDisp_%s(struct __GLXclientStateRec *, GLbyte *);' % (func.name)
61 print 'extern _X_HIDDEN int __glXDispSwap_%s(struct __GLXclientStateRec *, GLbyte *);' % (func.name)
62
63 if func.glx_sop and func.glx_vendorpriv:
64 n = func.glx_vendorpriv_names[0]
65 print 'extern _X_HIDDEN int __glXDisp_%s(struct __GLXclientStateRec *, GLbyte *);' % (n)
66 print 'extern _X_HIDDEN int __glXDispSwap_%s(struct __GLXclientStateRec *, GLbyte *);' % (n)
67
68 return
69
70
71 class PrintGlxDispatchFunctions(glX_proto_common.glx_print_proto):
72 def __init__(self, do_swap):
73 gl_XML.gl_print_base.__init__(self)
74 self.name = "glX_proto_recv.py (from Mesa)"
75 self.license = license.bsd_license_template % ( "(C) Copyright IBM Corporation 2005", "IBM")
76
77 self.real_types = [ '', '', 'uint16_t', '', 'uint32_t', '', '', '', 'uint64_t' ]
78 self.do_swap = do_swap
79 return
80
81
82 def printRealHeader(self):
83 print '#include <X11/Xmd.h>'
84 print '#include <GL/gl.h>'
85 print '#include <GL/glxproto.h>'
86
87 print '#include <inttypes.h>'
88 print '#include "indirect_size.h"'
89 print '#include "indirect_size_get.h"'
90 print '#include "indirect_dispatch.h"'
91 print '#include "glxserver.h"'
92 print '#include "glxbyteorder.h"'
93 print '#include "indirect_util.h"'
94 print '#include "singlesize.h"'
95 print '#include "glapi.h"'
96 print '#include "glapitable.h"'
97 print '#include "dispatch.h"'
98 print ''
99 print '#define __GLX_PAD(x) (((x) + 3) & ~3)'
100 print ''
101 print 'typedef struct {'
102 print ' __GLX_PIXEL_3D_HDR;'
103 print '} __GLXpixel3DHeader;'
104 print ''
105 print 'extern GLboolean __glXErrorOccured( void );'
106 print 'extern void __glXClearErrorOccured( void );'
107 print ''
108 print 'static const unsigned dummy_answer[2] = {0, 0};'
109 print ''
110 return
111
112
113 def printBody(self, api):
114 if self.do_swap:
115 self.emit_swap_wrappers(api)
116
117
118 for func in api.functionIterateByOffset():
119 if not func.ignore and not func.server_handcode and not func.vectorequiv and (func.glx_rop or func.glx_sop or func.glx_vendorpriv):
120 self.printFunction(func, func.name)
121 if func.glx_sop and func.glx_vendorpriv:
122 self.printFunction(func, func.glx_vendorpriv_names[0])
123
124
125 return
126
127
128 def printFunction(self, f, name):
129 if (f.glx_sop or f.glx_vendorpriv) and (len(f.get_images()) != 0):
130 return
131
132 if not self.do_swap:
133 base = '__glXDisp'
134 else:
135 base = '__glXDispSwap'
136
137 if f.glx_rop:
138 print 'void %s_%s(GLbyte * pc)' % (base, name)
139 else:
140 print 'int %s_%s(__GLXclientState *cl, GLbyte *pc)' % (base, name)
141
142 print '{'
143
144 if f.glx_rop or f.vectorequiv:
145 self.printRenderFunction(f)
146 elif f.glx_sop or f.glx_vendorpriv:
147 if len(f.get_images()) == 0:
148 self.printSingleFunction(f, name)
149 else:
150 print "/* Missing GLX protocol for %s. */" % (name)
151
152 print '}'
153 print ''
154 return
155
156
157 def swap_name(self, bytes):
158 return 'bswap_%u_array' % (8 * bytes)
159
160
161 def emit_swap_wrappers(self, api):
162 self.type_map = {}
163 already_done = [ ]
164
165 for t in api.typeIterate():
166 te = t.get_type_expression()
167 t_size = te.get_element_size()
168
169 if t_size > 1 and t.glx_name:
170
171 t_name = "GL" + t.name
172 self.type_map[ t_name ] = t.glx_name
173
174 if t.glx_name not in already_done:
175 real_name = self.real_types[t_size]
176
177 print 'static %s' % (t_name)
178 print 'bswap_%s( const void * src )' % (t.glx_name)
179 print '{'
180 print ' union { %s dst; %s ret; } x;' % (real_name, t_name)
181 print ' x.dst = bswap_%u( *(%s *) src );' % (t_size * 8, real_name)
182 print ' return x.ret;'
183 print '}'
184 print ''
185 already_done.append( t.glx_name )
186
187 for bits in [16, 32, 64]:
188 print 'static void *'
189 print 'bswap_%u_array( uint%u_t * src, unsigned count )' % (bits, bits)
190 print '{'
191 print ' unsigned i;'
192 print ''
193 print ' for ( i = 0 ; i < count ; i++ ) {'
194 print ' uint%u_t temp = bswap_%u( src[i] );' % (bits, bits)
195 print ' src[i] = temp;'
196 print ' }'
197 print ''
198 print ' return src;'
199 print '}'
200 print ''
201
202
203 def fetch_param(self, param):
204 t = param.type_string()
205 o = param.offset
206 element_size = param.size() / param.get_element_count()
207
208 if self.do_swap and (element_size != 1):
209 if param.is_array():
210 real_name = self.real_types[ element_size ]
211
212 swap_func = self.swap_name( element_size )
213 return ' (%-8s)%s( (%s *) (pc + %2s), %s )' % (t, swap_func, real_name, o, param.count)
214 else:
215 t_name = param.get_base_type_string()
216 return ' (%-8s)bswap_%-7s( pc + %2s )' % (t, self.type_map[ t_name ], o)
217 else:
218 if param.is_array():
219 return ' (%-8s)(pc + %2u)' % (t, o)
220 else:
221 return '*(%-8s *)(pc + %2u)' % (t, o)
222
223 return None
224
225
226 def emit_function_call(self, f, retval_assign, indent):
227 list = []
228
229 for param in f.parameterIterator():
230 if param.is_padding:
231 continue
232
233 if param.is_counter or param.is_image() or param.is_output or param.name in f.count_parameter_list or len(param.count_parameter_list):
234 location = param.name
235 else:
236 location = self.fetch_param(param)
237
238 list.append( '%s %s' % (indent, location) )
239
240
241 if len( list ):
242 print '%s %sCALL_%s( GET_DISPATCH(), (' % (indent, retval_assign, f.name)
243 print string.join( list, ",\n" )
244 print '%s ) );' % (indent)
245 else:
246 print '%s %sCALL_%s( GET_DISPATCH(), () );' % (indent, retval_assign, f.name)
247 return
248
249
250 def common_func_print_just_start(self, f, indent):
251 align64 = 0
252 need_blank = 0
253
254
255 f.calculate_offsets()
256 for param in f.parameterIterateGlxSend():
257 # If any parameter has a 64-bit base type, then we
258 # have to do alignment magic for the while thing.
259
260 if param.is_64_bit():
261 align64 = 1
262
263
264 # FIXME img_null_flag is over-loaded. In addition to
265 # FIXME being used for images, it is used to signify
266 # FIXME NULL data pointers for vertex buffer object
267 # FIXME related functions. Re-name it to null_data
268 # FIXME or something similar.
269
270 if param.img_null_flag:
271 print '%s const CARD32 ptr_is_null = *(CARD32 *)(pc + %s);' % (indent, param.offset - 4)
272 cond = '(ptr_is_null != 0) ? NULL : '
273 else:
274 cond = ""
275
276
277 type_string = param.type_string()
278
279 if param.is_image():
280 offset = f.offset_of( param.name )
281
282 print '%s %s const %s = (%s) (%s(pc + %s));' % (indent, type_string, param.name, type_string, cond, offset)
283
284 if param.depth:
285 print '%s __GLXpixel3DHeader * const hdr = (__GLXpixel3DHeader *)(pc);' % (indent)
286 else:
287 print '%s __GLXpixelHeader * const hdr = (__GLXpixelHeader *)(pc);' % (indent)
288
289 need_blank = 1
290 elif param.is_counter or param.name in f.count_parameter_list:
291 location = self.fetch_param(param)
292 print '%s const %s %s = %s;' % (indent, type_string, param.name, location)
293 need_blank = 1
294 elif len(param.count_parameter_list):
295 if param.size() == 1 and not self.do_swap:
296 location = self.fetch_param(param)
297 print '%s %s %s = %s%s;' % (indent, type_string, param.name, cond, location)
298 else:
299 print '%s %s %s;' % (indent, type_string, param.name)
300 need_blank = 1
301
302
303
304 if need_blank:
305 print ''
306
307 if align64:
308 print '#ifdef __GLX_ALIGN64'
309
310 if f.has_variable_size_request():
311 self.emit_packet_size_calculation(f, 4)
312 s = "cmdlen"
313 else:
314 s = str((f.command_fixed_length() + 3) & ~3)
315
316 print ' if ((unsigned long)(pc) & 7) {'
317 print ' (void) memmove(pc-4, pc, %s);' % (s)
318 print ' pc -= 4;'
319 print ' }'
320 print '#endif'
321 print ''
322
323
324 need_blank = 0
325 if self.do_swap:
326 for param in f.parameterIterateGlxSend():
327 if param.count_parameter_list:
328 o = param.offset
329 count = param.get_element_count()
330 type_size = param.size() / count
331
332 if param.counter:
333 count_name = param.counter
334 else:
335 count_name = str(count)
336
337 # This is basically an ugly special-
338 # case for glCallLists.
339
340 if type_size == 1:
341 x = []
342 x.append( [1, ['BYTE', 'UNSIGNED_BYTE', '2_BYTES', '3_BYTES', '4_BYTES']] )
343 x.append( [2, ['SHORT', 'UNSIGNED_SHORT']] )
344 x.append( [4, ['INT', 'UNSIGNED_INT', 'FLOAT']] )
345
346 print ' switch(%s) {' % (param.count_parameter_list[0])
347 for sub in x:
348 for t_name in sub[1]:
349 print ' case GL_%s:' % (t_name)
350
351 if sub[0] == 1:
352 print ' %s = (%s) (pc + %s); break;' % (param.name, param.type_string(), o)
353 else:
354 swap_func = self.swap_name(sub[0])
355 print ' %s = (%s) %s( (%s *) (pc + %s), %s ); break;' % (param.name, param.type_string(), swap_func, self.real_types[sub[0]], o, count_name)
356 print ' default:'
357 print ' return;'
358 print ' }'
359 else:
360 swap_func = self.swap_name(type_size)
361 compsize = self.size_call(f, 1)
362 print ' %s = (%s) %s( (%s *) (pc + %s), %s );' % (param.name, param.type_string(), swap_func, self.real_types[type_size], o, compsize)
363
364 need_blank = 1
365
366 else:
367 for param in f.parameterIterateGlxSend():
368 if param.count_parameter_list:
369 print '%s %s = (%s) (pc + %s);' % (indent, param.name, param.type_string(), param.offset)
370 need_blank = 1
371
372
373 if need_blank:
374 print ''
375
376
377 return
378
379
380 def printSingleFunction(self, f, name):
381 if name not in f.glx_vendorpriv_names:
382 print ' xGLXSingleReq * const req = (xGLXSingleReq *) pc;'
383 else:
384 print ' xGLXVendorPrivateReq * const req = (xGLXVendorPrivateReq *) pc;'
385
386 print ' int error;'
387
388 if self.do_swap:
389 print ' __GLXcontext * const cx = __glXForceCurrent(cl, bswap_CARD32( &req->contextTag ), &error);'
390 else:
391 print ' __GLXcontext * const cx = __glXForceCurrent(cl, req->contextTag, &error);'
392
393 print ''
394 if name not in f.glx_vendorpriv_names:
395 print ' pc += __GLX_SINGLE_HDR_SIZE;'
396 else:
397 print ' pc += __GLX_VENDPRIV_HDR_SIZE;'
398
399 print ' if ( cx != NULL ) {'
400 self.common_func_print_just_start(f, " ")
401
402
403 if f.return_type != 'void':
404 print ' %s retval;' % (f.return_type)
405 retval_string = "retval"
406 retval_assign = "retval = "
407 else:
408 retval_string = "0"
409 retval_assign = ""
410
411
412 type_size = 0
413 answer_string = "dummy_answer"
414 answer_count = "0"
415 is_array_string = "GL_FALSE"
416
417 for param in f.parameterIterateOutputs():
418 answer_type = param.get_base_type_string()
419 if answer_type == "GLvoid":
420 answer_type = "GLubyte"
421
422
423 c = param.get_element_count()
424 type_size = (param.size() / c)
425 if type_size == 1:
426 size_scale = ""
427 else:
428 size_scale = " * %u" % (type_size)
429
430
431 if param.count_parameter_list:
432 print ' const GLuint compsize = %s;' % (self.size_call(f, 1))
433 print ' %s answerBuffer[200];' % (answer_type)
434 print ' %s %s = __glXGetAnswerBuffer(cl, compsize%s, answerBuffer, sizeof(answerBuffer), %u);' % (param.type_string(), param.name, size_scale, type_size )
435 answer_string = param.name
436 answer_count = "compsize"
437
438 print ''
439 print ' if (%s == NULL) return BadAlloc;' % (param.name)
440 print ' __glXClearErrorOccured();'
441 print ''
442 elif param.counter:
443 print ' %s answerBuffer[200];' % (answer_type)
444 print ' %s %s = __glXGetAnswerBuffer(cl, %s%s, answerBuffer, sizeof(answerBuffer), %u);' % (param.type_string(), param.name, param.counter, size_scale, type_size)
445 answer_string = param.name
446 answer_count = param.counter
447 elif c >= 1:
448 print ' %s %s[%u];' % (answer_type, param.name, c)
449 answer_string = param.name
450 answer_count = "%u" % (c)
451
452 if f.reply_always_array:
453 is_array_string = "GL_TRUE"
454
455
456 self.emit_function_call(f, retval_assign, " ")
457
458
459 if f.needs_reply():
460 if self.do_swap:
461 for param in f.parameterIterateOutputs():
462 c = param.get_element_count()
463 type_size = (param.size() / c)
464
465 if type_size > 1:
466 swap_name = self.swap_name( type_size )
467 print ' (void) %s( (uint%u_t *) %s, %s );' % (swap_name, 8 * type_size, param.name, answer_count)
468
469
470 reply_func = '__glXSendReplySwap'
471 else:
472 reply_func = '__glXSendReply'
473
474 print ' %s(cl->client, %s, %s, %u, %s, %s);' % (reply_func, answer_string, answer_count, type_size, is_array_string, retval_string)
475 #elif f.note_unflushed:
476 # print ' cx->hasUnflushedCommands = GL_TRUE;'
477
478 print ' error = Success;'
479 print ' }'
480 print ''
481 print ' return error;'
482 return
483
484
485 def printRenderFunction(self, f):
486 # There are 4 distinct phases in a rendering dispatch function.
487 # In the first phase we compute the sizes and offsets of each
488 # element in the command. In the second phase we (optionally)
489 # re-align 64-bit data elements. In the third phase we
490 # (optionally) byte-swap array data. Finally, in the fourth
491 # phase we actually dispatch the function.
492
493 self.common_func_print_just_start(f, "")
494
495 images = f.get_images()
496 if len(images):
497 if self.do_swap:
498 pre = "bswap_CARD32( & "
499 post = " )"
500 else:
501 pre = ""
502 post = ""
503
504 img = images[0]
505
506 # swapBytes and lsbFirst are single byte fields, so
507 # the must NEVER be byte-swapped.
508
509 if not (img.img_type == "GL_BITMAP" and img.img_format == "GL_COLOR_INDEX"):
510 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_SWAP_BYTES, hdr->swapBytes) );'
511
512 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_LSB_FIRST, hdr->lsbFirst) );'
513
514 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_ROW_LENGTH, (GLint) %shdr->rowLength%s) );' % (pre, post)
515 if img.depth:
516 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_IMAGE_HEIGHT, (GLint) %shdr->imageHeight%s) );' % (pre, post)
517 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_SKIP_ROWS, (GLint) %shdr->skipRows%s) );' % (pre, post)
518 if img.depth:
519 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_SKIP_IMAGES, (GLint) %shdr->skipImages%s) );' % (pre, post)
520 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_SKIP_PIXELS, (GLint) %shdr->skipPixels%s) );' % (pre, post)
521 print ' CALL_PixelStorei( GET_DISPATCH(), (GL_UNPACK_ALIGNMENT, (GLint) %shdr->alignment%s) );' % (pre, post)
522 print ''
523
524
525 self.emit_function_call(f, "", "")
526 return
527
528
529 def _parser():
530 """Parse any arguments passed and return a namespace."""
531 parser = argparse.ArgumentParser()
532 parser.add_argument('-f',
533 dest='filename',
534 default='gl_API.xml',
535 help='an xml file describing an OpenGL API')
536 parser.add_argument('-m',
537 dest='mode',
538 default='dispatch_c',
539 choices=['dispatch_c', 'dispatch_h'],
540 help='what file to generate')
541 parser.add_argument('-s',
542 dest='swap',
543 action='store_true',
544 help='emit swap in GlXDispatchFunctions')
545 return parser.parse_args()
546
547
548 def main():
549 """Main function."""
550 args = _parser()
551
552 if args.mode == "dispatch_c":
553 printer = PrintGlxDispatchFunctions(args.swap)
554 elif args.mode == "dispatch_h":
555 printer = PrintGlxDispatch_h()
556
557 api = gl_XML.parse_GL_API(
558 args.filename, glX_proto_common.glx_proto_item_factory())
559
560 printer.Print(api)
561
562
563 if __name__ == '__main__':
564 main()