1 ##########################################################################
3 # Copyright 2010 VMware, Inc.
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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 NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 ##########################################################################/
27 """Generated an header, glproc.hpp, which does pretty much what GLEW does, but
28 covers all the functions we support.
33 from glapi import glapi
34 from glxapi import glxapi
35 from wglapi import wglapi
38 # See http://www.opengl.org/registry/ABI/
39 public_symbols = set([
42 "glAreTexturesResident",
95 "glCopyTexSubImage1D",
96 "glCopyTexSubImage2D",
105 "glDisableClientState",
114 "glEnableClientState",
157 "glGetPolygonStipple",
165 "glGetTexLevelParameterfv",
166 "glGetTexLevelParameteriv",
167 "glGetTexParameterfv",
168 "glGetTexParameteriv",
183 "glInterleavedArrays",
248 "glPrioritizeTextures",
250 "glPushClientAttrib",
377 "wglChoosePixelFormat",
380 "wglCreateLayerContext",
382 "wglDescribeLayerPlane",
383 "wglDescribePixelFormat",
384 "wglGetCurrentContext",
386 "wglGetDefaultProcAddress",
387 "wglGetLayerPaletteEntries",
391 "wglRealizeLayerPalette",
392 "wglSetLayerPaletteEntries",
396 "wglSwapLayerBuffers",
397 "wglSwapMultipleBuffers",
398 "wglUseFontBitmapsA",
399 "wglUseFontBitmapsW",
400 "wglUseFontOutlinesA",
401 "wglUseFontOutlinesW",
403 "glXGetProcAddressARB",
412 #print 'typedef void (*__PROC)(void);'
414 #print 'static __PROC __getPublicProcAddress(const char *name);'
415 #print 'static __PROC __getPrivateProcAddress(const char *name);'
418 def dispatch_api(self, api):
419 for function in api.functions:
420 dispatcher.dispatch_function(function)
422 print '#ifdef RETRACE'
423 for function in api.functions:
424 if not self.is_public_function(function):
425 print '#define %s __%s' % (function.name, function.name)
426 print '#endif /* RETRACE */'
429 def function_pointer_type(self, function):
430 return '__PFN' + function.name.upper()
432 def function_pointer_value(self, function):
433 return '__' + function.name + '_ptr'
435 def dispatch_function(self, function):
436 if self.is_public_function(function):
437 print '#ifndef RETRACE'
439 ptype = self.function_pointer_type(function)
440 pvalue = self.function_pointer_value(function)
441 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
442 print 'static %s %s = NULL;' % (ptype, pvalue)
444 print 'static inline ' + function.prototype('__' + function.name) + ' {'
445 if function.type is stdapi.Void:
449 self.get_true_pointer(function)
450 pvalue = self.function_pointer_value(function)
451 print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))
454 if self.is_public_function(function):
455 print '#endif /* !RETRACE */'
458 def is_public_function(self, function):
461 def get_true_pointer(self, function):
462 ptype = self.function_pointer_type(function)
463 pvalue = self.function_pointer_value(function)
464 if self.is_public_function(function):
465 get_proc_address = '__getPublicProcAddress'
467 get_proc_address = '__getPrivateProcAddress'
468 print ' if (!%s) {' % (pvalue,)
469 print ' %s = (%s)%s("%s");' % (pvalue, ptype, get_proc_address, function.name)
470 print ' if (!%s) {' % (pvalue,)
471 self.fail_function(function)
475 def fail_function(self, function):
476 print ' OS::DebugMessage("error: unavailable function \\"%s\\"\\n");' % function.name
477 if function.fail is not None:
478 if function.type is stdapi.Void:
479 assert function.fail == ''
482 assert function.fail != ''
483 print ' return %s;' % function.fail
488 class GlDispatcher(Dispatcher):
491 Dispatcher.header(self)
492 print '#ifdef RETRACE'
493 print '# ifdef WIN32'
494 print '# define __getPrivateProcAddress(name) wglGetProcAddress(name)'
496 print '# define __getPrivateProcAddress(name) glXGetProcAddressARB((const GLubyte *)(name))'
498 print '# define __abort() OS::Abort()'
499 print '#else /* !RETRACE */'
500 print '# ifdef WIN32'
501 print '# define __getPrivateProcAddress(name) __wglGetProcAddress(name)'
502 print ' static inline PROC __stdcall __wglGetProcAddress(const char * lpszProc);'
504 print '# define __getPublicProcAddress(name) dlsym(RTLD_NEXT, name)'
505 print '# define __getPrivateProcAddress(name) __glXGetProcAddressARB((const GLubyte *)(name))'
506 print ' static inline __GLXextFuncPtr __glXGetProcAddressARB(const GLubyte * procName);'
508 print '# define __abort() Trace::Abort()'
509 print '#endif /* !RETRACE */'
512 def is_public_function(self, function):
513 return function.name in public_symbols
516 if __name__ == '__main__':
518 print '#include "glimports.hpp"'
519 print '#include "os.hpp"'
522 dispatcher = GlDispatcher()
526 dispatcher.dispatch_api(wglapi)
527 print '#else /* !WIN32 */'
529 dispatcher.dispatch_api(glxapi)
530 print '#endif /* !WIN32 */'
532 dispatcher.dispatch_api(glapi)
539 static HINSTANCE g_hDll = NULL;
542 __getProcAddress(const char *name)
545 char szDll[MAX_PATH] = {0};
547 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
551 strcat(szDll, "\\\\opengl32.dll");
553 g_hDll = LoadLibraryA(szDll);
559 return GetProcAddress(g_hDll, name);
564 static void g_module = NULL;
567 __getProcAddress(const char *name)
570 g_module = dlopen("libGL.so", RTLD_LAZY);
576 return (__PROC)dlsym(g_module, name);
580 __glGetProcAddress(const char *name) {
581 return __glXGetProcAddressARB(name);