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