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