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 the size and opcode values are known at compile-time, this will, on'
60 print ' * x86 at least, emit them with a single instruction.'
62 print '#define emit_header(dest, op, size) \\'
63 print ' do { union { short s[2]; int i; } temp; \\'
64 print ' temp.s[0] = (size); temp.s[1] = (op); \\'
65 print ' *((int *)(dest)) = temp.i; } while(0)'
67 print """static NOINLINE CARD32
68 read_reply( Display *dpy, size_t size, void * dest, GLboolean reply_is_always_array )
70 xGLXSingleReply reply;
72 (void) _XReply(dpy, (xReply *) & reply, 0, False);
74 if ((reply.size >= 1) || reply_is_always_array) {
75 const GLint bytes = (reply_is_always_array)
76 ? (4 * reply.length) : (reply.size * size);
77 const GLint extra = 4 - (bytes & 3);
79 _XRead(dpy, dest, bytes);
81 _XEatData(dpy, extra);
85 (void) memcpy( dest, &(reply.pad3), size);
94 static NOINLINE GLubyte *
95 setup_single_request( __GLXcontext * gc, GLint sop, GLint cmdlen )
98 Display * const dpy = gc->currentDpy;
100 (void) __glXFlushRenderBuffer(gc, gc->pc);
102 GetReqExtra(GLXSingle, cmdlen, req);
103 req->reqType = gc->majorOpcode;
104 req->contextTag = gc->currentContextTag;
106 return (GLubyte *)(req) + sz_xGLXSingleReq;
109 static NOINLINE GLubyte *
110 setup_vendor_request( __GLXcontext * gc, GLint code, GLint vop, GLint cmdlen )
112 xGLXVendorPrivateReq * req;
113 Display * const dpy = gc->currentDpy;
115 (void) __glXFlushRenderBuffer(gc, gc->pc);
117 GetReqExtra(GLXVendorPrivate, cmdlen, req);
118 req->reqType = gc->majorOpcode;
120 req->vendorCode = vop;
121 req->contextTag = gc->currentContextTag;
122 return (GLubyte *)(req) + sz_xGLXVendorPrivateReq;
126 for size
in self
.generic_sizes
:
127 self
.print_generic_function(size
)
130 def printFunction(self
, f
):
131 if f
.fn_offset
< 0 or f
.handcode
or f
.ignore
: return
133 if f
.glx_rop
!= 0 or f
.vectorequiv
!= None:
134 self
.printRenderFunction(f
)
135 elif f
.glx_sop
!= 0 or f
.glx_vendorpriv
!= 0:
136 self
.printSingleFunction(f
)
138 print "/* Missing GLX protocol for %s. */" % (f
.name
)
140 def print_generic_function(self
, n
):
141 print """static FASTCALL NOINLINE void
142 generic_%u_byte( GLint rop, const void * ptr )
144 __GLXcontext * const gc = __glXGetCurrentContext();
145 const GLuint cmdlen = %u;
147 emit_header(gc->pc, rop, cmdlen);
148 (void) memcpy((void *)(gc->pc + 4), ptr, %u);
150 if (gc->pc > gc->limit) { (void) __glXFlushRenderBuffer(gc, gc->pc); }
155 def common_emit_one_arg(self
, p
, offset
, pc
, indent
, adjust
):
156 if p
.is_output
: return
162 src_ptr
= "&" + p
.name
164 print '%s (void) memcpy((void *)(%s + %u), (void *)(%s), %s);' \
165 % (indent
, pc
, offset
+ adjust
, src_ptr
, p
.size_string() )
167 def common_emit_args(self
, f
, pc
, indent
, adjust
, skip_vla
):
168 # First emit all of the fixed-length 8-byte (i.e., GLdouble)
180 if p
.is_output
or p
.order
!= order
: continue
182 self
.common_emit_one_arg(p
, offset
, pc
, indent
, adjust
)
189 def common_func_print_just_header(self
, f
):
190 print '#define %s %d' % (f
.opcode_name(), f
.opcode_value())
192 print '%s' % (f
.fn_return_type
)
193 print '__indirect_gl%s(%s)' % (f
.name
, f
.get_parameter_string())
197 def common_func_print_header(self
, f
):
198 self
.common_func_print_just_header(f
)
200 print ' __GLXcontext * const gc = __glXGetCurrentContext();'
201 print ' Display * const dpy = gc->currentDpy;'
203 if f
.fn_return_type
!= 'void':
204 print ' %s retval = (%s) 0;' % (f
.fn_return_type
, f
.fn_return_type
)
206 if f
.count_parameters
!= None:
207 print ' const GLuint compsize = __gl%s_size(%s);' % (f
.name
, f
.count_parameters
)
209 print ' const GLuint cmdlen = %s;' % (f
.command_length())
211 if f
.counter
!= None:
212 print ' if (%s < 0) %s' % (f
.counter
, f
.return_string())
215 print ' if (dpy == NULL) return;'
216 print ' if ( ((gc->pc + cmdlen) > gc->bufEnd)'
217 print ' || (cmdlen > gc->maxSmallRenderCommandSize)) {'
218 print ' (void) __glXFlushRenderBuffer(gc, gc->pc);'
226 def printSingleFunction(self
, f
):
227 self
.common_func_print_header(f
)
229 print ' if (dpy != NULL) {'
231 if f
.fn_parameters
!= []:
232 pc_decl
= "GLubyte const * pc ="
236 if f
.glx_vendorpriv
!= 0:
237 print ' %s setup_vendor_request(gc, %s, %s, cmdlen);' % (pc_decl
, f
.opcode_real_name(), f
.opcode_name())
239 print ' %s setup_single_request(gc, %s, cmdlen);' % (pc_decl
, f
.opcode_name())
241 self
.common_emit_args(f
, "pc", " ", 0, 0)
245 output_size
= f
.output
.p_type
.size
246 output_str
= f
.output
.name
251 if f
.fn_return_type
!= 'void':
252 return_str
= " retval = (%s)" % (f
.fn_return_type
)
254 return_str
= " (void)"
256 if f
.reply_always_array
:
261 print " %s read_reply(gc->currentDpy, %s, %s, %s);" % (return_str
, output_size
, output_str
, aa
)
263 print ' UnlockDisplay(gc->currentDpy); SyncHandle();'
265 print ' %s' % f
.return_string()
271 def printRenderFunction(self
, f
):
272 # There is a class of GL functions that take a single pointer
273 # as a parameter. This pointer points to a fixed-size chunk
274 # of data, and the protocol for this functions is very
275 # regular. Since they are so regular and there are so many
276 # of them, special case them with generic functions. On
277 # x86, this save about 26KB in the libGL.so binary.
279 if f
.variable_length_parameter() == None and len(f
.fn_parameters
) == 1:
280 p
= f
.fn_parameters
[0]
282 [cmdlen
, size_string
] = f
.command_payload_length()
283 if cmdlen
in self
.generic_sizes
:
284 self
.common_func_print_just_header(f
)
285 print ' generic_%u_byte( %s, %s );' % (cmdlen
, f
.opcode_real_name(), p
.name
)
290 self
.common_func_print_header(f
)
293 print ' if (cmdlen <= gc->maxSmallRenderCommandSize) {'
298 print '%s emit_header(gc->pc, %s, cmdlen);' % (indent
, f
.opcode_real_name())
300 self
.common_emit_args(f
, "gc->pc", indent
, 4, 0)
301 print '%s gc->pc += cmdlen;' % (indent
)
302 print '%s if (gc->pc > gc->limit) { (void) __glXFlushRenderBuffer(gc, gc->pc); }' % (indent
)
307 print ' const GLint op = %s;' % (f
.opcode_real_name())
308 print ' const GLuint cmdlenLarge = cmdlen + 4;'
309 print ' (void) memcpy((void *)(gc->pc + 0), (void *)(&op), 4);'
310 print ' (void) memcpy((void *)(gc->pc + 4), (void *)(&cmdlenLarge), 4);'
311 offset
= self
.common_emit_args(f
, "gc->pc", indent
, 8, 1)
313 p
= f
.variable_length_parameter()
314 print ' __glXSendLargeCommand(gc, gc->pc, %u, %s, %s);' % (offset
+ 8, p
.name
, p
.size_string())
322 class PrintGlxProtoInit_c(glX_XML
.GlxProto
):
324 glX_XML
.GlxProto
.__init
__(self
)
325 self
.last_category
= ""
326 self
.license
= license
.bsd_license_template
% ( \
327 """Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
328 (C) Copyright IBM Corporation 2004""", "PRECISION INSIGHT, IBM")
331 def printRealHeader(self
):
333 * \\file indirect_init.c
334 * Initialize indirect rendering dispatch table.
336 * \\author Kevin E. Martin <kevin@precisioninsight.com>
337 * \\author Brian Paul <brian@precisioninsight.com>
338 * \\author Ian Romanick <idr@us.ibm.com>
341 #include "indirect_init.h"
342 #include "indirect.h"
347 * No-op function used to initialize functions that have no GLX protocol
350 static int NoOp(void)
356 * Create and initialize a new GL dispatch table. The table is initialized
357 * with GLX indirect rendering protocol functions.
359 __GLapi * __glXNewIndirectAPI( void )
364 entries = _glapi_get_dispatch_table_size();
365 glAPI = (__GLapi *) Xmalloc(entries * sizeof(void *));
367 /* first, set all entries to point to no-op functions */
370 void **dispatch = (void **) glAPI;
371 for (i = 0; i < entries; i++) {
372 dispatch[i] = (void *) NoOp;
376 /* now, initialize the entries we understand */"""
378 def printRealFooter(self
):
384 def printFunction(self
, f
):
385 if f
.fn_offset
< 0 or f
.ignore
: return
387 if f
.category
!= self
.last_category
:
388 self
.last_category
= f
.category
390 print ' /* %s */' % (self
.last_category
)
393 print ' glAPI->%s = __indirect_gl%s;' % (f
.name
, f
.name
)
396 class PrintGlxProtoInit_h(glX_XML
.GlxProto
):
398 glX_XML
.GlxProto
.__init
__(self
)
399 self
.last_category
= ""
400 self
.license
= license
.bsd_license_template
% ( \
401 """Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
402 (C) Copyright IBM Corporation 2004""", "PRECISION INSIGHT, IBM")
405 def printRealHeader(self
):
409 * Prototypes for indirect rendering functions.
411 * \\author Kevin E. Martin <kevin@precisioninsight.com>
412 * \\author Ian Romanick <idr@us.ibm.com>
415 #if !defined( _INDIRECT_H_ )
416 # define _INDIRECT_H_
419 glX_XML
.printVisibility( "HIDDEN", "hidden" )
422 def printRealFooter(self
):
423 print "# undef HIDDEN"
424 print "#endif /* !defined( _INDIRECT_H_ ) */"
426 def printFunction(self
, f
):
427 if f
.fn_offset
< 0 or f
.ignore
: return
428 print 'extern HIDDEN %s __indirect_gl%s(%s);' % (f
.fn_return_type
, f
.name
, f
.get_parameter_string())
431 class PrintGlxSizeStubs(glX_XML
.GlxProto
):
433 glX_XML
.GlxProto
.__init
__(self
)
434 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2004", "IBM")
436 self
.glx_enum_sigs
= {}
438 def printRealHeader(self
):
440 print '#include <GL/gl.h>'
441 print '#include "indirect_size.h"'
446 glX_XML
.printFastcall()
448 glX_XML
.printVisibility( "INTERNAL", "internal" )
451 print '#ifdef HAVE_ALIAS'
452 print '# define ALIAS2(from,to) \\'
453 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
454 print ' __attribute__ ((alias( # to )));'
455 print '# define ALIAS(from,to) ALIAS2( from, __gl ## to ## _size )'
457 print '# define ALIAS(from,to) \\'
458 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
459 print ' { return __gl ## to ## _size( e ); }'
464 def printRealFooter(self
):
465 for a
in self
.aliases
:
468 def printFunction(self
, f
):
469 if self
.glx_enum_functions
.has_key(f
.name
):
470 ef
= self
.glx_enum_functions
[f
.name
]
472 sig
= ef
.signature();
473 if self
.glx_enum_sigs
.has_key(sig
):
474 n
= self
.glx_enum_sigs
[sig
];
475 a
= 'ALIAS( %s, %s )' % (f
.name
, n
)
476 self
.aliases
.append(a
)
479 self
.glx_enum_sigs
[sig
] = f
.name
;
483 class PrintGlxSizeStubs_h(glX_XML
.GlxProto
):
485 glX_XML
.GlxProto
.__init
__(self
)
486 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2004", "IBM")
488 self
.glx_enum_sigs
= {}
490 def printRealHeader(self
):
494 * Prototypes for functions used to determine the number of data elements in
495 * various GLX protocol messages.
497 * \\author Ian Romanick <idr@us.ibm.com>
500 #if !defined( _GLXSIZE_H_ )
506 glX_XML
.printFastcall();
508 glX_XML
.printVisibility( "INTERNAL", "internal" );
511 def printRealFooter(self
):
513 print "# undef INTERNAL"
515 print "# undef FASTCALL"
516 print "#endif /* !defined( _GLXSIZE_H_ ) */"
519 def printFunction(self
, f
):
520 if self
.glx_enum_functions
.has_key(f
.name
):
521 ef
= self
.glx_enum_functions
[f
.name
]
522 print 'extern INTERNAL PURE FASTCALL GLint __gl%s_size(GLenum);' % (f
.name
)
526 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys
.argv
[0]
530 if __name__
== '__main__':
531 file_name
= "gl_API.xml"
534 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:m:")
539 for (arg
,val
) in args
:
546 dh
= PrintGlxProtoStubs()
547 elif mode
== "init_c":
548 dh
= PrintGlxProtoInit_c()
549 elif mode
== "init_h":
550 dh
= PrintGlxProtoInit_h()
551 elif mode
== "size_c":
552 dh
= PrintGlxSizeStubs()
553 elif mode
== "size_h":
554 dh
= PrintGlxSizeStubs_h()
558 parser
= make_parser()
559 parser
.setFeature(feature_namespaces
, 0)
560 parser
.setContentHandler(dh
)