15d1fa69fd3ae52774ef38632aa072856b4b3d00
3 # (C) Copyright IBM Corporation 2004
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:
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
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
26 # Ian Romanick <idr@us.ibm.com>
28 from xml
.sax
import saxutils
29 from xml
.sax
import make_parser
30 from xml
.sax
.handler
import feature_namespaces
38 class PrintGlxProtoStubs(glX_XML
.GlxProto
):
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]
46 def printRealHeader(self
):
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>'
54 print '#define __GLX_PAD(n) (((n) + 3) & ~3)'
56 glX_XML
.printFastcall()
57 glX_XML
.printNoinline()
59 print '#if !defined __GNUC__ || __GNUC__ < 3'
60 print '# define __builtin_expect(x, y) x'
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.'
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)'
71 print """static NOINLINE CARD32
72 read_reply( Display *dpy, size_t size, void * dest, GLboolean reply_is_always_array )
74 xGLXSingleReply reply;
76 (void) _XReply(dpy, (xReply *) & reply, 0, False);
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);
83 _XRead(dpy, dest, bytes);
85 _XEatData(dpy, extra);
89 (void) memcpy( dest, &(reply.pad3), size);
98 static NOINLINE FASTCALL GLubyte *
99 setup_single_request( __GLXcontext * gc, GLint sop, GLint cmdlen )
102 Display * const dpy = gc->currentDpy;
104 (void) __glXFlushRenderBuffer(gc, gc->pc);
106 GetReqExtra(GLXSingle, cmdlen, req);
107 req->reqType = gc->majorOpcode;
108 req->contextTag = gc->currentContextTag;
110 return (GLubyte *)(req) + sz_xGLXSingleReq;
113 static NOINLINE FASTCALL GLubyte *
114 setup_vendor_request( __GLXcontext * gc, GLint code, GLint vop, GLint cmdlen )
116 xGLXVendorPrivateReq * req;
117 Display * const dpy = gc->currentDpy;
119 (void) __glXFlushRenderBuffer(gc, gc->pc);
121 GetReqExtra(GLXVendorPrivate, cmdlen, req);
122 req->reqType = gc->majorOpcode;
124 req->vendorCode = vop;
125 req->contextTag = gc->currentContextTag;
126 return (GLubyte *)(req) + sz_xGLXVendorPrivateReq;
130 for size
in self
.generic_sizes
:
131 self
.print_generic_function(size
)
134 def printFunction(self
, f
):
135 if f
.fn_offset
< 0 or f
.handcode
or f
.ignore
: return
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
)
142 print "/* Missing GLX protocol for %s. */" % (f
.name
)
144 def print_generic_function(self
, n
):
145 print """static FASTCALL NOINLINE void
146 generic_%u_byte( GLint rop, const void * ptr )
148 __GLXcontext * const gc = __glXGetCurrentContext();
149 const GLuint cmdlen = %u;
151 emit_header(gc->pc, rop, cmdlen);
152 (void) memcpy((void *)(gc->pc + 4), ptr, %u);
154 if (__builtin_expect(gc->pc > gc->limit, 0)) { (void) __glXFlushRenderBuffer(gc, gc->pc); }
159 def common_emit_one_arg(self
, p
, offset
, pc
, indent
, adjust
):
164 src_ptr
= "&" + p
.name
166 print '%s (void) memcpy((void *)(%s + %u), (void *)(%s), %s);' \
167 % (indent
, pc
, offset
+ adjust
, src_ptr
, p
.size_string() )
169 def common_emit_args(self
, f
, pc
, indent
, adjust
, skip_vla
):
177 for p
in f
.parameterIterator(1, r
):
178 self
.common_emit_one_arg(p
, offset
, pc
, indent
, adjust
)
184 def large_emit_begin(self
, indent
, f
):
185 print '%s const GLint op = %s;' % (indent
, f
.opcode_real_name())
186 print '%s const GLuint cmdlenLarge = cmdlen + 4;' % (indent
)
187 print '%s GLubyte * const pc = __glXFlushRenderBuffer(gc, gc->pc);' % (indent
)
188 print '%s (void) memcpy((void *)(pc + 0), (void *)(&cmdlenLarge), 4);' % (indent
)
189 print '%s (void) memcpy((void *)(pc + 4), (void *)(&op), 4);' % (indent
)
193 def common_func_print_just_header(self
, f
):
194 print '#define %s %d' % (f
.opcode_name(), f
.opcode_value())
196 print '%s' % (f
.fn_return_type
)
197 print '__indirect_gl%s(%s)' % (f
.name
, f
.get_parameter_string())
201 def common_func_print_header(self
, f
):
202 self
.common_func_print_just_header(f
)
204 print ' __GLXcontext * const gc = __glXGetCurrentContext();'
206 # The only reason that single and vendor private commands need
207 # a variable called 'dpy' is becuase they use the SyncHandle
208 # macro. For whatever brain-dead reason, that macro is hard-
209 # coded to use a variable called 'dpy' instead of taking a
213 print ' Display * const dpy = gc->currentDpy;'
214 skip_condition
= "dpy != NULL"
216 skip_condition
= "gc->currentDpy != NULL"
218 skip_condition
= None
221 if f
.fn_return_type
!= 'void':
222 print ' %s retval = (%s) 0;' % (f
.fn_return_type
, f
.fn_return_type
)
224 if f
.count_parameters
!= None:
225 print ' const GLuint compsize = __gl%s_size(%s);' % (f
.name
, f
.count_parameters
)
227 print ' const GLuint cmdlen = %s;' % (f
.command_length())
231 skip_condition
= "(%s >= 0) && (%s)" % (f
.counter
, skip_condition
)
233 skip_condition
= "%s >= 0" % (f
.counter
)
237 print ' if (__builtin_expect(%s, 1)) {' % (skip_condition
)
243 def printSingleFunction(self
, f
):
244 self
.common_func_print_header(f
)
246 if f
.fn_parameters
!= []:
247 pc_decl
= "GLubyte const * pc ="
251 if f
.glx_vendorpriv
!= 0:
252 print ' %s setup_vendor_request(gc, %s, %s, cmdlen);' % (pc_decl
, f
.opcode_real_name(), f
.opcode_name())
254 print ' %s setup_single_request(gc, %s, cmdlen);' % (pc_decl
, f
.opcode_name())
256 self
.common_emit_args(f
, "pc", " ", 0, 0)
260 output_size
= f
.output
.p_type
.size
261 output_str
= f
.output
.name
266 if f
.fn_return_type
!= 'void':
267 return_str
= " retval = (%s)" % (f
.fn_return_type
)
269 return_str
= " (void)"
271 if f
.reply_always_array
:
276 print " %s read_reply(dpy, %s, %s, %s);" % (return_str
, output_size
, output_str
, aa
)
278 print ' UnlockDisplay(dpy); SyncHandle();'
280 print ' %s' % f
.return_string()
286 def printRenderFunction(self
, f
):
287 # There is a class of GL functions that take a single pointer
288 # as a parameter. This pointer points to a fixed-size chunk
289 # of data, and the protocol for this functions is very
290 # regular. Since they are so regular and there are so many
291 # of them, special case them with generic functions. On
292 # x86, this saves about 26KB in the libGL.so binary.
294 if f
.variable_length_parameter() == None and len(f
.fn_parameters
) == 1:
295 p
= f
.fn_parameters
[0]
297 [cmdlen
, size_string
] = f
.command_payload_length()
298 if cmdlen
in self
.generic_sizes
:
299 self
.common_func_print_just_header(f
)
300 print ' generic_%u_byte( %s, %s );' % (cmdlen
, f
.opcode_real_name(), p
.name
)
305 if self
.common_func_print_header(f
):
313 print '%s if (cmdlen <= gc->maxSmallRenderCommandSize) {' % (indent
)
314 print '%s if ( (gc->pc + cmdlen) > gc->bufEnd ) {' % (indent
)
315 print '%s (void) __glXFlushRenderBuffer(gc, gc->pc);' % (indent
)
316 print '%s }' % (indent
)
319 print '%s emit_header(gc->pc, %s, cmdlen);' % (indent
, f
.opcode_real_name())
321 self
.common_emit_args(f
, "gc->pc", indent
, 4, 0)
322 print '%s gc->pc += cmdlen;' % (indent
)
323 print '%s if (__builtin_expect(gc->pc > gc->limit, 0)) { (void) __glXFlushRenderBuffer(gc, gc->pc); }' % (indent
)
326 print '%s}' % (indent
)
327 print '%selse {' % (indent
)
329 self
.large_emit_begin(indent
, f
)
330 offset
= self
.common_emit_args(f
, "pc", indent
, 8, 1)
332 p
= f
.variable_length_parameter()
333 print '%s __glXSendLargeCommand(gc, pc, %u, %s, %s);' % (indent
, offset
+ 8, p
.name
, p
.size_string())
334 print '%s}' % (indent
)
336 if trailer
: print trailer
342 class PrintGlxProtoInit_c(glX_XML
.GlxProto
):
344 glX_XML
.GlxProto
.__init
__(self
)
345 self
.last_category
= ""
346 self
.license
= license
.bsd_license_template
% ( \
347 """Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
348 (C) Copyright IBM Corporation 2004""", "PRECISION INSIGHT, IBM")
351 def printRealHeader(self
):
353 * \\file indirect_init.c
354 * Initialize indirect rendering dispatch table.
356 * \\author Kevin E. Martin <kevin@precisioninsight.com>
357 * \\author Brian Paul <brian@precisioninsight.com>
358 * \\author Ian Romanick <idr@us.ibm.com>
361 #include "indirect_init.h"
362 #include "indirect.h"
367 * No-op function used to initialize functions that have no GLX protocol
370 static int NoOp(void)
376 * Create and initialize a new GL dispatch table. The table is initialized
377 * with GLX indirect rendering protocol functions.
379 __GLapi * __glXNewIndirectAPI( void )
384 entries = _glapi_get_dispatch_table_size();
385 glAPI = (__GLapi *) Xmalloc(entries * sizeof(void *));
387 /* first, set all entries to point to no-op functions */
390 void **dispatch = (void **) glAPI;
391 for (i = 0; i < entries; i++) {
392 dispatch[i] = (void *) NoOp;
396 /* now, initialize the entries we understand */"""
398 def printRealFooter(self
):
404 def printFunction(self
, f
):
405 if f
.fn_offset
< 0 or f
.ignore
: return
407 if f
.category
!= self
.last_category
:
408 self
.last_category
= f
.category
410 print ' /* %s */' % (self
.last_category
)
413 print ' glAPI->%s = __indirect_gl%s;' % (f
.name
, f
.name
)
416 class PrintGlxProtoInit_h(glX_XML
.GlxProto
):
418 glX_XML
.GlxProto
.__init
__(self
)
419 self
.last_category
= ""
420 self
.license
= license
.bsd_license_template
% ( \
421 """Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
422 (C) Copyright IBM Corporation 2004""", "PRECISION INSIGHT, IBM")
425 def printRealHeader(self
):
429 * Prototypes for indirect rendering functions.
431 * \\author Kevin E. Martin <kevin@precisioninsight.com>
432 * \\author Ian Romanick <idr@us.ibm.com>
435 #if !defined( _INDIRECT_H_ )
436 # define _INDIRECT_H_
439 glX_XML
.printVisibility( "HIDDEN", "hidden" )
442 def printRealFooter(self
):
443 print "# undef HIDDEN"
444 print "#endif /* !defined( _INDIRECT_H_ ) */"
446 def printFunction(self
, f
):
447 if f
.fn_offset
< 0 or f
.ignore
: return
448 print 'extern HIDDEN %s __indirect_gl%s(%s);' % (f
.fn_return_type
, f
.name
, f
.get_parameter_string())
451 class PrintGlxSizeStubs(glX_XML
.GlxProto
):
453 glX_XML
.GlxProto
.__init
__(self
)
454 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2004", "IBM")
456 self
.glx_enum_sigs
= {}
458 def printRealHeader(self
):
460 print '#include <GL/gl.h>'
461 print '#include "indirect_size.h"'
466 glX_XML
.printFastcall()
468 glX_XML
.printVisibility( "INTERNAL", "internal" )
471 print '#ifdef HAVE_ALIAS'
472 print '# define ALIAS2(from,to) \\'
473 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
474 print ' __attribute__ ((alias( # to )));'
475 print '# define ALIAS(from,to) ALIAS2( from, __gl ## to ## _size )'
477 print '# define ALIAS(from,to) \\'
478 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
479 print ' { return __gl ## to ## _size( e ); }'
484 def printRealFooter(self
):
485 for a
in self
.aliases
:
488 def printFunction(self
, f
):
489 if self
.glx_enum_functions
.has_key(f
.name
):
490 ef
= self
.glx_enum_functions
[f
.name
]
492 sig
= ef
.signature();
493 if self
.glx_enum_sigs
.has_key(sig
):
494 n
= self
.glx_enum_sigs
[sig
];
495 a
= 'ALIAS( %s, %s )' % (f
.name
, n
)
496 self
.aliases
.append(a
)
499 self
.glx_enum_sigs
[sig
] = f
.name
;
503 class PrintGlxSizeStubs_h(glX_XML
.GlxProto
):
505 glX_XML
.GlxProto
.__init
__(self
)
506 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2004", "IBM")
508 self
.glx_enum_sigs
= {}
510 def printRealHeader(self
):
514 * Prototypes for functions used to determine the number of data elements in
515 * various GLX protocol messages.
517 * \\author Ian Romanick <idr@us.ibm.com>
520 #if !defined( _GLXSIZE_H_ )
526 glX_XML
.printFastcall();
528 glX_XML
.printVisibility( "INTERNAL", "internal" );
531 def printRealFooter(self
):
533 print "# undef INTERNAL"
535 print "# undef FASTCALL"
536 print "#endif /* !defined( _GLXSIZE_H_ ) */"
539 def printFunction(self
, f
):
540 if self
.glx_enum_functions
.has_key(f
.name
):
541 ef
= self
.glx_enum_functions
[f
.name
]
542 print 'extern INTERNAL PURE FASTCALL GLint __gl%s_size(GLenum);' % (f
.name
)
546 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys
.argv
[0]
550 if __name__
== '__main__':
551 file_name
= "gl_API.xml"
554 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:m:")
559 for (arg
,val
) in args
:
566 dh
= PrintGlxProtoStubs()
567 elif mode
== "init_c":
568 dh
= PrintGlxProtoInit_c()
569 elif mode
== "init_h":
570 dh
= PrintGlxProtoInit_h()
571 elif mode
== "size_c":
572 dh
= PrintGlxSizeStubs()
573 elif mode
== "size_h":
574 dh
= PrintGlxSizeStubs_h()
578 parser
= make_parser()
579 parser
.setFeature(feature_namespaces
, 0)
580 parser
.setContentHandler(dh
)