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