2 # (C) Copyright IBM Corporation 2005
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # on the rights to use, copy, modify, merge, publish, distribute, sub
9 # license, and/or sell copies of the Software, and to permit persons to whom
10 # the Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 # Ian Romanick <idr@us.ibm.com>
30 import gl_XML
, glX_XML
, glX_proto_common
, license
33 class PrintGlxDispatch_h(gl_XML
.gl_print_base
):
35 gl_XML
.gl_print_base
.__init
__(self
)
37 self
.name
= "glX_proto_recv.py (from Mesa)"
38 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2005", "IBM")
40 self
.header_tag
= "_INDIRECT_DISPATCH_H_"
44 def printRealHeader(self
):
45 print '# include <X11/Xfuncproto.h>'
47 print 'struct __GLXclientStateRec;'
52 def printBody(self
, api
):
53 for func
in api
.functionIterateAll():
54 if not func
.ignore
and not func
.vectorequiv
:
56 print 'extern _X_HIDDEN void __glXDisp_%s(GLbyte * pc);' % (func
.name
)
57 print 'extern _X_HIDDEN _X_COLD void __glXDispSwap_%s(GLbyte * pc);' % (func
.name
)
58 elif func
.glx_sop
or func
.glx_vendorpriv
:
59 print 'extern _X_HIDDEN int __glXDisp_%s(struct __GLXclientStateRec *, GLbyte *);' % (func
.name
)
60 print 'extern _X_HIDDEN _X_COLD int __glXDispSwap_%s(struct __GLXclientStateRec *, GLbyte *);' % (func
.name
)
62 if func
.glx_sop
and func
.glx_vendorpriv
:
63 n
= func
.glx_vendorpriv_names
[0]
64 print 'extern _X_HIDDEN int __glXDisp_%s(struct __GLXclientStateRec *, GLbyte *);' % (n
)
65 print 'extern _X_HIDDEN _X_COLD int __glXDispSwap_%s(struct __GLXclientStateRec *, GLbyte *);' % (n
)
70 class PrintGlxDispatchFunctions(glX_proto_common
.glx_print_proto
):
71 def __init__(self
, do_swap
):
72 gl_XML
.gl_print_base
.__init
__(self
)
73 self
.name
= "glX_proto_recv.py (from Mesa)"
74 self
.license
= license
.bsd_license_template
% ( "(C) Copyright IBM Corporation 2005", "IBM")
76 self
.real_types
= [ '', '', 'uint16_t', '', 'uint32_t', '', '', '', 'uint64_t' ]
77 self
.do_swap
= do_swap
81 def printRealHeader(self
):
82 print '#include <inttypes.h>'
83 print '#include "glxserver.h"'
84 print '#include "indirect_size.h"'
85 print '#include "indirect_size_get.h"'
86 print '#include "indirect_dispatch.h"'
87 print '#include "glxbyteorder.h"'
88 print '#include "indirect_util.h"'
89 print '#include "singlesize.h"'
91 print 'typedef struct {'
92 print ' __GLX_PIXEL_3D_HDR;'
93 print '} __GLXpixel3DHeader;'
95 print 'extern GLboolean __glXErrorOccured( void );'
96 print 'extern void __glXClearErrorOccured( void );'
98 print 'static const unsigned dummy_answer[2] = {0, 0};'
103 def printBody(self
, api
):
105 self
.emit_swap_wrappers(api
)
108 for func
in api
.functionIterateByOffset():
109 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
):
110 self
.printFunction(func
, func
.name
)
111 if func
.glx_sop
and func
.glx_vendorpriv
:
112 self
.printFunction(func
, func
.glx_vendorpriv_names
[0])
117 def fptrType(self
, name
):
118 fptr
= "pfngl" + name
+ "proc"
121 def printFunction(self
, f
, name
):
122 if (f
.glx_sop
or f
.glx_vendorpriv
) and (len(f
.get_images()) != 0):
128 base
= '__glXDispSwap'
131 print 'void %s_%s(GLbyte * pc)' % (base
, name
)
133 print 'int %s_%s(__GLXclientState *cl, GLbyte *pc)' % (base
, name
)
138 print ' %s %s = __glGetProcAddress("gl%s");' % (self
.fptrType(name
), name
, name
)
140 if f
.glx_rop
or f
.vectorequiv
:
141 self
.printRenderFunction(f
)
142 elif f
.glx_sop
or f
.glx_vendorpriv
:
143 if len(f
.get_images()) == 0:
144 self
.printSingleFunction(f
, name
)
146 print "/* Missing GLX protocol for %s. */" % (name
)
153 def swap_name(self
, bytes
):
154 return 'bswap_%u_array' % (8 * bytes
)
157 def emit_swap_wrappers(self
, api
):
161 for t
in api
.typeIterate():
162 te
= t
.get_type_expression()
163 t_size
= te
.get_element_size()
165 if t_size
> 1 and t
.glx_name
:
167 t_name
= "GL" + t
.name
168 self
.type_map
[ t_name
] = t
.glx_name
170 if t
.glx_name
not in already_done
:
171 real_name
= self
.real_types
[t_size
]
173 print 'static _X_UNUSED %s' % (t_name
)
174 print 'bswap_%s(const void * src)' % (t
.glx_name
)
176 print ' union { %s dst; %s ret; } x;' % (real_name
, t_name
)
177 print ' x.dst = bswap_%u(*(%s *) src);' % (t_size
* 8, real_name
)
178 print ' return x.ret;'
181 already_done
.append( t
.glx_name
)
183 for bits
in [16, 32, 64]:
184 print 'static void *'
185 print 'bswap_%u_array(uint%u_t * src, unsigned count)' % (bits
, bits
)
189 print ' for (i = 0 ; i < count ; i++) {'
190 print ' uint%u_t temp = bswap_%u(src[i]);' % (bits
, bits
)
191 print ' src[i] = temp;'
199 def fetch_param(self
, param
):
200 t
= param
.type_string()
202 element_size
= param
.size() / param
.get_element_count()
204 if self
.do_swap
and (element_size
!= 1):
206 real_name
= self
.real_types
[ element_size
]
208 swap_func
= self
.swap_name( element_size
)
209 return ' (%-8s)%s( (%s *) (pc + %2s), %s )' % (t
, swap_func
, real_name
, o
, param
.count
)
211 t_name
= param
.get_base_type_string()
212 return ' (%-8s)bswap_%-7s( pc + %2s )' % (t
, self
.type_map
[ t_name
], o
)
215 return ' (%-8s)(pc + %2u)' % (t
, o
)
217 return '*(%-8s *)(pc + %2u)' % (t
, o
)
222 def emit_function_call(self
, f
, retval_assign
, indent
):
224 prefix
= "gl" if f
.is_abi() else ""
226 for param
in f
.parameterIterator():
230 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
):
231 location
= param
.name
233 location
= self
.fetch_param(param
)
235 list.append( '%s %s' % (indent
, location
) )
237 print '%s %s%s%s(%s);' % (indent
, retval_assign
, prefix
, f
.name
, string
.join(list, ',\n'))
240 def common_func_print_just_start(self
, f
, indent
):
245 f
.calculate_offsets()
246 for param
in f
.parameterIterateGlxSend():
247 # If any parameter has a 64-bit base type, then we
248 # have to do alignment magic for the while thing.
250 if param
.is_64_bit():
254 # FIXME img_null_flag is over-loaded. In addition to
255 # FIXME being used for images, it is used to signify
256 # FIXME NULL data pointers for vertex buffer object
257 # FIXME related functions. Re-name it to null_data
258 # FIXME or something similar.
260 if param
.img_null_flag
:
261 print '%s const CARD32 ptr_is_null = *(CARD32 *)(pc + %s);' % (indent
, param
.offset
- 4)
262 cond
= '(ptr_is_null != 0) ? NULL : '
267 type_string
= param
.type_string()
270 offset
= f
.offset_of( param
.name
)
272 print '%s %s const %s = (%s) (%s(pc + %s));' % (indent
, type_string
, param
.name
, type_string
, cond
, offset
)
275 print '%s __GLXpixel3DHeader * const hdr = (__GLXpixel3DHeader *)(pc);' % (indent
)
277 print '%s __GLXpixelHeader * const hdr = (__GLXpixelHeader *)(pc);' % (indent
)
280 elif param
.is_counter
or param
.name
in f
.count_parameter_list
:
281 location
= self
.fetch_param(param
)
282 print '%s const %s %s = %s;' % (indent
, type_string
, param
.name
, location
)
284 elif len(param
.count_parameter_list
):
285 if param
.size() == 1 and not self
.do_swap
:
286 location
= self
.fetch_param(param
)
287 print '%s %s %s = %s%s;' % (indent
, type_string
, param
.name
, cond
, location
)
289 print '%s %s %s;' % (indent
, type_string
, param
.name
)
298 print '#ifdef __GLX_ALIGN64'
300 if f
.has_variable_size_request():
301 self
.emit_packet_size_calculation(f
, 4)
304 s
= str((f
.command_fixed_length() + 3) & ~
3)
306 print ' if ((unsigned long)(pc) & 7) {'
307 print ' (void) memmove(pc-4, pc, %s);' % (s
)
316 for param
in f
.parameterIterateGlxSend():
317 if param
.count_parameter_list
:
319 count
= param
.get_element_count()
320 type_size
= param
.size() / count
323 count_name
= param
.counter
325 count_name
= str(count
)
327 # This is basically an ugly special-
328 # case for glCallLists.
332 x
.append( [1, ['BYTE', 'UNSIGNED_BYTE', '2_BYTES', '3_BYTES', '4_BYTES']] )
333 x
.append( [2, ['SHORT', 'UNSIGNED_SHORT']] )
334 x
.append( [4, ['INT', 'UNSIGNED_INT', 'FLOAT']] )
336 print ' switch(%s) {' % (param
.count_parameter_list
[0])
338 for t_name
in sub
[1]:
339 print ' case GL_%s:' % (t_name
)
342 print ' %s = (%s) (pc + %s); break;' % (param
.name
, param
.type_string(), o
)
344 swap_func
= self
.swap_name(sub
[0])
345 print ' %s = (%s) %s( (%s *) (pc + %s), %s ); break;' % (param
.name
, param
.type_string(), swap_func
, self
.real_types
[sub
[0]], o
, count_name
)
350 swap_func
= self
.swap_name(type_size
)
351 compsize
= self
.size_call(f
, 1)
352 print ' %s = (%s) %s( (%s *) (pc + %s), %s );' % (param
.name
, param
.type_string(), swap_func
, self
.real_types
[type_size
], o
, compsize
)
357 for param
in f
.parameterIterateGlxSend():
358 if param
.count_parameter_list
:
359 print '%s %s = (%s) (pc + %s);' % (indent
, param
.name
, param
.type_string(), param
.offset
)
370 def printSingleFunction(self
, f
, name
):
371 if name
not in f
.glx_vendorpriv_names
:
372 print ' xGLXSingleReq * const req = (xGLXSingleReq *) pc;'
374 print ' xGLXVendorPrivateReq * const req = (xGLXVendorPrivateReq *) pc;'
379 print ' __GLXcontext * const cx = __glXForceCurrent(cl, bswap_CARD32( &req->contextTag ), &error);'
381 print ' __GLXcontext * const cx = __glXForceCurrent(cl, req->contextTag, &error);'
384 if name
not in f
.glx_vendorpriv_names
:
385 print ' pc += __GLX_SINGLE_HDR_SIZE;'
387 print ' pc += __GLX_VENDPRIV_HDR_SIZE;'
389 print ' if ( cx != NULL ) {'
390 self
.common_func_print_just_start(f
, " ")
393 if f
.return_type
!= 'void':
394 print ' %s retval;' % (f
.return_type
)
395 retval_string
= "retval"
396 retval_assign
= "retval = "
403 answer_string
= "dummy_answer"
405 is_array_string
= "GL_FALSE"
407 for param
in f
.parameterIterateOutputs():
408 answer_type
= param
.get_base_type_string()
409 if answer_type
== "GLvoid":
410 answer_type
= "GLubyte"
413 c
= param
.get_element_count()
414 type_size
= (param
.size() / c
)
418 size_scale
= " * %u" % (type_size
)
421 if param
.count_parameter_list
:
422 print ' const GLuint compsize = %s;' % (self
.size_call(f
, 1))
423 print ' %s answerBuffer[200];' % (answer_type
)
424 print ' %s %s = __glXGetAnswerBuffer(cl, compsize%s, answerBuffer, sizeof(answerBuffer), %u);' % (param
.type_string(), param
.name
, size_scale
, type_size
)
425 answer_string
= param
.name
426 answer_count
= "compsize"
429 print ' if (%s == NULL) return BadAlloc;' % (param
.name
)
430 print ' __glXClearErrorOccured();'
433 print ' %s answerBuffer[200];' % (answer_type
)
434 print ' %s %s = __glXGetAnswerBuffer(cl, %s%s, answerBuffer, sizeof(answerBuffer), %u);' % (param
.type_string(), param
.name
, param
.counter
, size_scale
, type_size
)
435 answer_string
= param
.name
436 answer_count
= param
.counter
438 print ' if (%s == NULL) return BadAlloc;' % (param
.name
)
439 print ' __glXClearErrorOccured();'
442 print ' %s %s[%u];' % (answer_type
, param
.name
, c
)
443 answer_string
= param
.name
444 answer_count
= "%u" % (c
)
446 if f
.reply_always_array
:
447 is_array_string
= "GL_TRUE"
450 self
.emit_function_call(f
, retval_assign
, " ")
455 for param
in f
.parameterIterateOutputs():
456 c
= param
.get_element_count()
457 type_size
= (param
.size() / c
)
460 swap_name
= self
.swap_name( type_size
)
461 print ' (void) %s( (uint%u_t *) %s, %s );' % (swap_name
, 8 * type_size
, param
.name
, answer_count
)
464 reply_func
= '__glXSendReplySwap'
466 reply_func
= '__glXSendReply'
468 print ' %s(cl->client, %s, %s, %u, %s, %s);' % (reply_func
, answer_string
, answer_count
, type_size
, is_array_string
, retval_string
)
469 #elif f.note_unflushed:
470 # print ' cx->hasUnflushedCommands = GL_TRUE;'
472 print ' error = Success;'
475 print ' return error;'
479 def printRenderFunction(self
, f
):
480 # There are 4 distinct phases in a rendering dispatch function.
481 # In the first phase we compute the sizes and offsets of each
482 # element in the command. In the second phase we (optionally)
483 # re-align 64-bit data elements. In the third phase we
484 # (optionally) byte-swap array data. Finally, in the fourth
485 # phase we actually dispatch the function.
487 self
.common_func_print_just_start(f
, "")
489 images
= f
.get_images()
492 pre
= "bswap_CARD32( & "
500 # swapBytes and lsbFirst are single byte fields, so
501 # the must NEVER be byte-swapped.
503 if not (img
.img_type
== "GL_BITMAP" and img
.img_format
== "GL_COLOR_INDEX"):
504 print ' glPixelStorei(GL_UNPACK_SWAP_BYTES, hdr->swapBytes);'
506 print ' glPixelStorei(GL_UNPACK_LSB_FIRST, hdr->lsbFirst);'
508 print ' glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint) %shdr->rowLength%s);' % (pre
, post
)
510 print ' glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, (GLint) %shdr->imageHeight%s);' % (pre
, post
)
511 print ' glPixelStorei(GL_UNPACK_SKIP_ROWS, (GLint) %shdr->skipRows%s);' % (pre
, post
)
513 print ' glPixelStorei(GL_UNPACK_SKIP_IMAGES, (GLint) %shdr->skipImages%s);' % (pre
, post
)
514 print ' glPixelStorei(GL_UNPACK_SKIP_PIXELS, (GLint) %shdr->skipPixels%s);' % (pre
, post
)
515 print ' glPixelStorei(GL_UNPACK_ALIGNMENT, (GLint) %shdr->alignment%s);' % (pre
, post
)
519 self
.emit_function_call(f
, "", "")
524 """Parse any arguments passed and return a namespace."""
525 parser
= argparse
.ArgumentParser()
526 parser
.add_argument('-f',
528 default
='gl_API.xml',
529 help='an xml file describing an OpenGL API')
530 parser
.add_argument('-m',
532 default
='dispatch_c',
533 choices
=['dispatch_c', 'dispatch_h'],
534 help='what file to generate')
535 parser
.add_argument('-s',
538 help='emit swap in GlXDispatchFunctions')
539 return parser
.parse_args()
546 if args
.mode
== "dispatch_c":
547 printer
= PrintGlxDispatchFunctions(args
.swap
)
548 elif args
.mode
== "dispatch_h":
549 printer
= PrintGlxDispatch_h()
551 api
= gl_XML
.parse_GL_API(
552 args
.filename
, glX_proto_common
.glx_proto_item_factory())
557 if __name__
== '__main__':