7a93ef10b64f819ab59ced06cb6d89c04ea3ef0d
[mesa.git] / src / mesa / glapi / glX_proto_send.py
1 #!/usr/bin/python2
2
3 # (C) Copyright IBM Corporation 2004
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 from xml.sax import saxutils
29 from xml.sax import make_parser
30 from xml.sax.handler import feature_namespaces
31
32 import gl_XML
33 import glX_XML
34 import license
35 import sys, getopt
36
37
38 class PrintGlxProtoStubs(glX_XML.GlxProto):
39 def __init__(self):
40 glX_XML.GlxProto.__init__(self)
41 self.last_category = ""
42 self.license = license.bsd_license_template % ( "(C) Copyright IBM Corporation 2004", "IBM")
43 self.generic_sizes = [3, 4, 6, 8, 12, 16, 24, 32]
44 return
45
46 def printRealHeader(self):
47 print ''
48 print '#include <GL/gl.h>'
49 print '#include "indirect.h"'
50 print '#include "glxclient.h"'
51 print '#include "size.h"'
52 print '#include <GL/glxproto.h>'
53 print ''
54 print '#define __GLX_PAD(n) (((n) + 3) & ~3)'
55 print ''
56 glX_XML.printFastcall()
57 glX_XML.printNoinline()
58 print ''
59 print '#if !defined __GNUC__ || __GNUC__ < 3'
60 print '# define __builtin_expect(x, y) x'
61 print '#endif'
62 print ''
63 print '/* If the size and opcode values are known at compile-time, this will, on'
64 print ' * x86 at least, emit them with a single instruction.'
65 print ' */'
66 print '#define emit_header(dest, op, size) \\'
67 print ' do { union { short s[2]; int i; } temp; \\'
68 print ' temp.s[0] = (size); temp.s[1] = (op); \\'
69 print ' *((int *)(dest)) = temp.i; } while(0)'
70 print ''
71 print """static NOINLINE CARD32
72 read_reply( Display *dpy, size_t size, void * dest, GLboolean reply_is_always_array )
73 {
74 xGLXSingleReply reply;
75
76 (void) _XReply(dpy, (xReply *) & reply, 0, False);
77 if (size != 0) {
78 if ((reply.length > 0) || reply_is_always_array) {
79 const GLint bytes = (reply_is_always_array)
80 ? (4 * reply.length) : (reply.size * size);
81 const GLint extra = 4 - (bytes & 3);
82
83 _XRead(dpy, dest, bytes);
84 if ( extra < 4 ) {
85 _XEatData(dpy, extra);
86 }
87 }
88 else {
89 (void) memcpy( dest, &(reply.pad3), size);
90 }
91 }
92
93 return reply.retval;
94 }
95
96 #define X_GLXSingle 0
97
98 static NOINLINE FASTCALL GLubyte *
99 setup_single_request( __GLXcontext * gc, GLint sop, GLint cmdlen )
100 {
101 xGLXSingleReq * req;
102 Display * const dpy = gc->currentDpy;
103
104 (void) __glXFlushRenderBuffer(gc, gc->pc);
105 LockDisplay(dpy);
106 GetReqExtra(GLXSingle, cmdlen, req);
107 req->reqType = gc->majorOpcode;
108 req->contextTag = gc->currentContextTag;
109 req->glxCode = sop;
110 return (GLubyte *)(req) + sz_xGLXSingleReq;
111 }
112
113 static NOINLINE FASTCALL GLubyte *
114 setup_vendor_request( __GLXcontext * gc, GLint code, GLint vop, GLint cmdlen )
115 {
116 xGLXVendorPrivateReq * req;
117 Display * const dpy = gc->currentDpy;
118
119 (void) __glXFlushRenderBuffer(gc, gc->pc);
120 LockDisplay(dpy);
121 GetReqExtra(GLXVendorPrivate, cmdlen, req);
122 req->reqType = gc->majorOpcode;
123 req->glxCode = code;
124 req->vendorCode = vop;
125 req->contextTag = gc->currentContextTag;
126 return (GLubyte *)(req) + sz_xGLXVendorPrivateReq;
127 }
128 """
129
130 for size in self.generic_sizes:
131 self.print_generic_function(size)
132 return
133
134 def printFunction(self, f):
135 if f.fn_offset < 0 or f.handcode or f.ignore: return
136
137 if f.glx_rop != 0 or f.vectorequiv != None:
138 self.printRenderFunction(f)
139 elif f.glx_sop != 0 or f.glx_vendorpriv != 0:
140 self.printSingleFunction(f)
141 else:
142 print "/* Missing GLX protocol for %s. */" % (f.name)
143
144 def print_generic_function(self, n):
145 size = (n + 3) & ~3
146 print """static FASTCALL NOINLINE void
147 generic_%u_byte( GLint rop, const void * ptr )
148 {
149 __GLXcontext * const gc = __glXGetCurrentContext();
150 const GLuint cmdlen = %u;
151
152 emit_header(gc->pc, rop, cmdlen);
153 (void) memcpy((void *)(gc->pc + 4), ptr, %u);
154 gc->pc += cmdlen;
155 if (__builtin_expect(gc->pc > gc->limit, 0)) { (void) __glXFlushRenderBuffer(gc, gc->pc); }
156 }
157 """ % (n, size + 4, size)
158
159
160 def common_emit_one_arg(self, p, offset, pc, indent, adjust):
161 t = p.p_type
162 if p.is_array():
163 src_ptr = p.name
164 else:
165 src_ptr = "&" + p.name
166
167 print '%s (void) memcpy((void *)(%s + %u), (void *)(%s), %s);' \
168 % (indent, pc, offset + adjust, src_ptr, p.size_string() )
169
170 def common_emit_args(self, f, pc, indent, adjust, skip_vla):
171 offset = 0
172
173 if skip_vla:
174 r = 1
175 else:
176 r = 2
177
178 for p in f.parameterIterator(1, r):
179 self.common_emit_one_arg(p, offset, pc, indent, adjust)
180 offset += p.size()
181
182 return offset
183
184
185 def large_emit_begin(self, indent, f):
186 print '%s const GLint op = %s;' % (indent, f.opcode_real_name())
187 print '%s const GLuint cmdlenLarge = cmdlen + 4;' % (indent)
188 print '%s GLubyte * const pc = __glXFlushRenderBuffer(gc, gc->pc);' % (indent)
189 print '%s (void) memcpy((void *)(pc + 0), (void *)(&cmdlenLarge), 4);' % (indent)
190 print '%s (void) memcpy((void *)(pc + 4), (void *)(&op), 4);' % (indent)
191 return
192
193
194 def common_func_print_just_header(self, f):
195 print '#define %s %d' % (f.opcode_name(), f.opcode_value())
196
197 print '%s' % (f.fn_return_type)
198 print '__indirect_gl%s(%s)' % (f.name, f.get_parameter_string())
199 print '{'
200
201
202 def common_func_print_header(self, f):
203 self.common_func_print_just_header(f)
204
205 print ' __GLXcontext * const gc = __glXGetCurrentContext();'
206
207 # The only reason that single and vendor private commands need
208 # a variable called 'dpy' is becuase they use the SyncHandle
209 # macro. For whatever brain-dead reason, that macro is hard-
210 # coded to use a variable called 'dpy' instead of taking a
211 # parameter.
212
213 if not f.glx_rop:
214 print ' Display * const dpy = gc->currentDpy;'
215 skip_condition = "dpy != NULL"
216 elif f.can_be_large:
217 skip_condition = "gc->currentDpy != NULL"
218 else:
219 skip_condition = None
220
221
222 if f.fn_return_type != 'void':
223 print ' %s retval = (%s) 0;' % (f.fn_return_type, f.fn_return_type)
224
225 if f.count_parameters != None:
226 print ' const GLuint compsize = __gl%s_size(%s);' % (f.name, f.count_parameters)
227
228 print ' const GLuint cmdlen = %s;' % (f.command_length())
229
230 if f.counter:
231 if skip_condition:
232 skip_condition = "(%s >= 0) && (%s)" % (f.counter, skip_condition)
233 else:
234 skip_condition = "%s >= 0" % (f.counter)
235
236
237 if skip_condition:
238 print ' if (__builtin_expect(%s, 1)) {' % (skip_condition)
239 return 1
240 else:
241 return 0
242
243
244 def printSingleFunction(self, f):
245 self.common_func_print_header(f)
246
247 if f.fn_parameters != []:
248 pc_decl = "GLubyte const * pc ="
249 else:
250 pc_decl = "(void)"
251
252 if f.glx_vendorpriv != 0:
253 print ' %s setup_vendor_request(gc, %s, %s, cmdlen);' % (pc_decl, f.opcode_real_name(), f.opcode_name())
254 else:
255 print ' %s setup_single_request(gc, %s, cmdlen);' % (pc_decl, f.opcode_name())
256
257 self.common_emit_args(f, "pc", " ", 0, 0)
258
259 if f.needs_reply():
260 if f.output != None:
261 output_size = f.output.p_type.size
262 output_str = f.output.name
263 else:
264 output_size = 0
265 output_str = "NULL"
266
267 if f.fn_return_type != 'void':
268 return_str = " retval = (%s)" % (f.fn_return_type)
269 else:
270 return_str = " (void)"
271
272 if f.reply_always_array:
273 aa = "GL_TRUE"
274 else:
275 aa = "GL_FALSE"
276
277 print " %s read_reply(dpy, %s, %s, %s);" % (return_str, output_size, output_str, aa)
278
279 print ' UnlockDisplay(dpy); SyncHandle();'
280 print ' }'
281 print ' %s' % f.return_string()
282 print '}'
283 print ''
284 return
285
286
287 def printRenderFunction(self, f):
288 # There is a class of GL functions that take a single pointer
289 # as a parameter. This pointer points to a fixed-size chunk
290 # of data, and the protocol for this functions is very
291 # regular. Since they are so regular and there are so many
292 # of them, special case them with generic functions. On
293 # x86, this saves about 26KB in the libGL.so binary.
294
295 if f.variable_length_parameter() == None and len(f.fn_parameters) == 1:
296 p = f.fn_parameters[0]
297 if p.is_pointer:
298 [cmdlen, size_string] = f.command_payload_length()
299 if cmdlen in self.generic_sizes:
300 self.common_func_print_just_header(f)
301 print ' generic_%u_byte( %s, %s );' % (cmdlen, f.opcode_real_name(), p.name)
302 print '}'
303 print ''
304 return
305
306 if self.common_func_print_header(f):
307 indent = " "
308 trailer = " }"
309 else:
310 indent = ""
311 trailer = None
312
313 if f.can_be_large:
314 print '%s if (cmdlen <= gc->maxSmallRenderCommandSize) {' % (indent)
315 print '%s if ( (gc->pc + cmdlen) > gc->bufEnd ) {' % (indent)
316 print '%s (void) __glXFlushRenderBuffer(gc, gc->pc);' % (indent)
317 print '%s }' % (indent)
318 indent += " "
319
320 print '%s emit_header(gc->pc, %s, cmdlen);' % (indent, f.opcode_real_name())
321
322 self.common_emit_args(f, "gc->pc", indent, 4, 0)
323 print '%s gc->pc += cmdlen;' % (indent)
324 print '%s if (__builtin_expect(gc->pc > gc->limit, 0)) { (void) __glXFlushRenderBuffer(gc, gc->pc); }' % (indent)
325
326 if f.can_be_large:
327 print '%s}' % (indent)
328 print '%selse {' % (indent)
329
330 self.large_emit_begin(indent, f)
331 offset = self.common_emit_args(f, "pc", indent, 8, 1)
332
333 p = f.variable_length_parameter()
334 print '%s __glXSendLargeCommand(gc, pc, %u, %s, %s);' % (indent, offset + 8, p.name, p.size_string())
335 print '%s}' % (indent)
336
337 if trailer: print trailer
338 print '}'
339 print ''
340 return
341
342
343 class PrintGlxProtoInit_c(glX_XML.GlxProto):
344 def __init__(self):
345 glX_XML.GlxProto.__init__(self)
346 self.last_category = ""
347 self.license = license.bsd_license_template % ( \
348 """Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
349 (C) Copyright IBM Corporation 2004""", "PRECISION INSIGHT, IBM")
350
351
352 def printRealHeader(self):
353 print """/**
354 * \\file indirect_init.c
355 * Initialize indirect rendering dispatch table.
356 *
357 * \\author Kevin E. Martin <kevin@precisioninsight.com>
358 * \\author Brian Paul <brian@precisioninsight.com>
359 * \\author Ian Romanick <idr@us.ibm.com>
360 */
361
362 #include "indirect_init.h"
363 #include "indirect.h"
364 #include "glapi.h"
365
366
367 /**
368 * No-op function used to initialize functions that have no GLX protocol
369 * support.
370 */
371 static int NoOp(void)
372 {
373 return 0;
374 }
375
376 /**
377 * Create and initialize a new GL dispatch table. The table is initialized
378 * with GLX indirect rendering protocol functions.
379 */
380 __GLapi * __glXNewIndirectAPI( void )
381 {
382 __GLapi *glAPI;
383 GLuint entries;
384
385 entries = _glapi_get_dispatch_table_size();
386 glAPI = (__GLapi *) Xmalloc(entries * sizeof(void *));
387
388 /* first, set all entries to point to no-op functions */
389 {
390 int i;
391 void **dispatch = (void **) glAPI;
392 for (i = 0; i < entries; i++) {
393 dispatch[i] = (void *) NoOp;
394 }
395 }
396
397 /* now, initialize the entries we understand */"""
398
399 def printRealFooter(self):
400 print """
401 return glAPI;
402 }
403 """
404
405 def printFunction(self, f):
406 if f.fn_offset < 0 or f.ignore: return
407
408 if f.category != self.last_category:
409 self.last_category = f.category
410 print ''
411 print ' /* %s */' % (self.last_category)
412 print ''
413
414 print ' glAPI->%s = __indirect_gl%s;' % (f.name, f.name)
415
416
417 class PrintGlxProtoInit_h(glX_XML.GlxProto):
418 def __init__(self):
419 glX_XML.GlxProto.__init__(self)
420 self.last_category = ""
421 self.license = license.bsd_license_template % ( \
422 """Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
423 (C) Copyright IBM Corporation 2004""", "PRECISION INSIGHT, IBM")
424
425
426 def printRealHeader(self):
427 print """
428 /**
429 * \\file
430 * Prototypes for indirect rendering functions.
431 *
432 * \\author Kevin E. Martin <kevin@precisioninsight.com>
433 * \\author Ian Romanick <idr@us.ibm.com>
434 */
435
436 #if !defined( _INDIRECT_H_ )
437 # define _INDIRECT_H_
438
439 """
440 glX_XML.printVisibility( "HIDDEN", "hidden" )
441
442
443 def printRealFooter(self):
444 print "# undef HIDDEN"
445 print "#endif /* !defined( _INDIRECT_H_ ) */"
446
447 def printFunction(self, f):
448 if f.fn_offset < 0 or f.ignore: return
449 print 'extern HIDDEN %s __indirect_gl%s(%s);' % (f.fn_return_type, f.name, f.get_parameter_string())
450
451
452 class PrintGlxSizeStubs(glX_XML.GlxProto):
453 def __init__(self):
454 glX_XML.GlxProto.__init__(self)
455 self.license = license.bsd_license_template % ( "(C) Copyright IBM Corporation 2004", "IBM")
456 self.aliases = []
457 self.glx_enum_sigs = {}
458
459 def printRealHeader(self):
460 print ''
461 print '#include <GL/gl.h>'
462 print '#include "indirect_size.h"'
463
464 print ''
465 glX_XML.printPure()
466 print ''
467 glX_XML.printFastcall()
468 print ''
469 glX_XML.printVisibility( "INTERNAL", "internal" )
470 print ''
471 print ''
472 print '#ifdef HAVE_ALIAS'
473 print '# define ALIAS2(from,to) \\'
474 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
475 print ' __attribute__ ((alias( # to )));'
476 print '# define ALIAS(from,to) ALIAS2( from, __gl ## to ## _size )'
477 print '#else'
478 print '# define ALIAS(from,to) \\'
479 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
480 print ' { return __gl ## to ## _size( e ); }'
481 print '#endif'
482 print ''
483 print ''
484
485 def printRealFooter(self):
486 for a in self.aliases:
487 print a
488
489 def printFunction(self, f):
490 if self.glx_enum_functions.has_key(f.name):
491 ef = self.glx_enum_functions[f.name]
492
493 sig = ef.signature();
494 if self.glx_enum_sigs.has_key(sig):
495 n = self.glx_enum_sigs[sig];
496 a = 'ALIAS( %s, %s )' % (f.name, n)
497 self.aliases.append(a)
498 else:
499 ef.Print( f.name )
500 self.glx_enum_sigs[sig] = f.name;
501
502
503
504 class PrintGlxSizeStubs_h(glX_XML.GlxProto):
505 def __init__(self):
506 glX_XML.GlxProto.__init__(self)
507 self.license = license.bsd_license_template % ( "(C) Copyright IBM Corporation 2004", "IBM")
508 self.aliases = []
509 self.glx_enum_sigs = {}
510
511 def printRealHeader(self):
512 print """
513 /**
514 * \\file
515 * Prototypes for functions used to determine the number of data elements in
516 * various GLX protocol messages.
517 *
518 * \\author Ian Romanick <idr@us.ibm.com>
519 */
520
521 #if !defined( _GLXSIZE_H_ )
522 # define _GLXSIZE_H_
523
524 """
525 glX_XML.printPure();
526 print ''
527 glX_XML.printFastcall();
528 print ''
529 glX_XML.printVisibility( "INTERNAL", "internal" );
530 print ''
531
532 def printRealFooter(self):
533 print ''
534 print "# undef INTERNAL"
535 print "# undef PURE"
536 print "# undef FASTCALL"
537 print "#endif /* !defined( _GLXSIZE_H_ ) */"
538
539
540 def printFunction(self, f):
541 if self.glx_enum_functions.has_key(f.name):
542 ef = self.glx_enum_functions[f.name]
543 print 'extern INTERNAL PURE FASTCALL GLint __gl%s_size(GLenum);' % (f.name)
544
545
546 def show_usage():
547 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys.argv[0]
548 sys.exit(1)
549
550
551 if __name__ == '__main__':
552 file_name = "gl_API.xml"
553
554 try:
555 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
556 except Exception,e:
557 show_usage()
558
559 mode = "proto"
560 for (arg,val) in args:
561 if arg == "-f":
562 file_name = val
563 elif arg == "-m":
564 mode = val
565
566 if mode == "proto":
567 dh = PrintGlxProtoStubs()
568 elif mode == "init_c":
569 dh = PrintGlxProtoInit_c()
570 elif mode == "init_h":
571 dh = PrintGlxProtoInit_h()
572 elif mode == "size_c":
573 dh = PrintGlxSizeStubs()
574 elif mode == "size_h":
575 dh = PrintGlxSizeStubs_h()
576 else:
577 show_usage()
578
579 parser = make_parser()
580 parser.setFeature(feature_namespaces, 0)
581 parser.setContentHandler(dh)
582
583 f = open(file_name)
584
585 dh.printHeader()
586 parser.parse(f)
587 dh.printFooter()