VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/spu_loader/glloader.py@ 24107

Last change on this file since 24107 was 22509, checked in by vboxsync, 15 years ago

crOpenGL-OSX: Initial version for the Cocoa port.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1# Copyright (c) 2001, Stanford University
2# All rights reserved.
3#
4# See the file LICENSE.txt for information on redistributing this software.
5
6
7import sys
8import apiutil
9
10
11keys = apiutil.GetDispatchedFunctions(sys.argv[1]+"/APIspec.txt")
12
13apiutil.CopyrightC()
14
15print """
16/* DO NOT EDIT - THIS FILE GENERATED BY THE glloader.py SCRIPT */
17#include "cr_error.h"
18#include "cr_dll.h"
19#include "cr_spu.h"
20#include "cr_string.h"
21#include "cr_error.h"
22#include "cr_environment.h"
23
24#include <stdio.h>
25#if defined(WINDOWS)
26#include <windows.h>
27#include <process.h>
28#include <direct.h>
29#define SYSTEM_GL "opengl32.dll"
30#elif defined (DARWIN)
31#define SYSTEM_GL "libGL.dylib"
32#define SYSTEM_CGL "OpenGL"
33# ifndef VBOX_WITH_COCOA_QT
34# define SYSTEM_AGL "AGL"
35# endif
36#elif defined(IRIX) || defined(IRIX64) || defined(Linux) || defined(FreeBSD) || defined(AIX) || defined(SunOS) || defined(OSF1)
37#if defined(Linux)
38#include <string.h>
39#endif
40#if defined(AIX)
41#define SYSTEM_GL "libGL.o"
42#else
43#define SYSTEM_GL "libGL.so.1"
44#endif
45typedef void (*glxfuncptr)();
46extern glxfuncptr glxGetProcAddressARB( const GLubyte *name );
47#else
48#error I don't know where your system's GL lives. Too bad.
49#endif
50
51static CRDLL *glDll = NULL;
52
53#ifdef DARWIN
54#define SYSTEM_GL_LIB_DIR "/System/Library/Frameworks/OpenGL.framework/Libraries"
55#define SYSTEM_CGL_DIR "/System/Library/Frameworks/OpenGL.framework"
56static CRDLL *cglDll = NULL;
57# ifndef VBOX_WITH_COCOA_QT
58# define SYSTEM_AGL_DIR "/System/Library/Frameworks/AGL.framework"
59static CRDLL *aglDll = NULL;
60# endif
61#endif
62
63#if defined(WINDOWS)
64#define GLLOADER_APIENTRY __stdcall
65#else
66#define GLLOADER_APIENTRY
67#endif
68
69/*#define CR_NO_GL_SYSTEM_PATH 1*/
70
71/*
72 * Add an entry to the SPUNamedFunctionTable
73 */
74static int
75fillin( SPUNamedFunctionTable *entry, const char *funcName, SPUGenericFunction funcPtr )
76{
77 if (funcPtr) {
78 entry->name = crStrdup( funcName );
79 entry->fn = funcPtr;
80 return 1;
81 }
82 return 0;
83}
84
85#ifndef WINDOWS
86static int FileExists(char *directory, char *filename)
87{
88 FILE *f;
89 char fullFilename[8096];
90
91 crStrcpy(fullFilename, directory);
92 crStrcat(fullFilename, "/");
93 crStrcat(fullFilename, filename);
94
95 f = fopen(fullFilename, "r");
96 if (f) {
97 fclose(f);
98 return 1;
99 }
100 else {
101 return 0;
102 }
103}
104#endif
105
106
107/*
108 * Locate the native OpenGL library, open it and return shared library
109 * handle.
110 */
111static CRDLL *
112__findSystemLib( const char *provided_system_path, char *lib )
113{
114 CRDLL *dll;
115 char system_path[8096];
116
117 memset(system_path, 0, sizeof(system_path));
118
119 if (provided_system_path && (crStrlen(provided_system_path) > 0) )
120 {
121 crStrcpy( system_path, provided_system_path );
122 }
123 else
124 {
125#if defined(WINDOWS)
126 GetSystemDirectory(system_path, MAX_PATH);
127#elif defined(IRIX) || defined(IRIX64)
128#ifdef IRIX_64BIT
129 crStrcpy( system_path, "/usr/lib64" );
130#else
131 crStrcpy( system_path, "/usr/lib32" );
132#endif
133#elif defined(PLAYSTATION2)
134 crStrcpy( system_path, "/usr/X11R6/lib" );
135#else
136 /* On RedHat 9, the correct default system directory
137 * is /usr/lib/tls/ (and if /usr/lib/ is used,
138 * the dynamic loader will generate a floating point
139 * exception SIGFPE). On other systems, including
140 * earlier versions of RedHat, the OpenGL library
141 * lives in /usr/lib. We'll use the /usr/lib/tls/
142 * version if it exists; otherwise, we'll use /usr/lib.
143 */
144 /*crStrcpy(system_path, "/usr/lib");*/
145#if defined(__linux__) && defined(__amd64__)
146 /*if (sizeof(void *) == 8 && FileExists("/usr/lib64", lib)) {
147 crStrcat(system_path, "64");
148 }*/
149#endif
150 /*if (FileExists("/usr/lib/tls", lib) ||
151 FileExists("/usr/lib64/tls", lib)) {
152 crStrcat(system_path, "/tls");
153 }*/
154#endif
155 }
156#if !defined(__linux__) && !defined(SunOS) && !defined(__FreeBSD__)
157 crStrcat( system_path, "/" );
158#endif
159#if !defined(CR_NO_GL_SYSTEM_PATH)
160 crStrcat( system_path, lib );
161 dll = crDLLOpen(system_path, 1 /*resolveGlobal*/);
162#else
163 dll = crDLLOpen(lib, 1 /*resolveGlobal*/);
164#endif
165 return dll;
166}
167
168
169static CRDLL *
170#ifdef DARWIN
171__findSystemGL( const char *provided_system_path, const char *default_system_path, char *provided_lib_name )
172#else
173__findSystemGL( const char *provided_system_path )
174#endif
175{
176#ifdef DARWIN
177 const char *the_path = (provided_system_path && crStrlen(provided_system_path) > 0) ? provided_system_path : default_system_path;
178
179 /* Fallback for loading frameworks */
180 if( !provided_lib_name )
181 return crDLLOpen( the_path, 1 );
182 else
183 return __findSystemLib( the_path, provided_lib_name );
184#else
185 return __findSystemLib( provided_system_path, SYSTEM_GL );
186#endif
187}
188
189static SPUGenericFunction
190findExtFunction( const crOpenGLInterface *interface, const char *funcName )
191{
192#ifdef WINDOWS
193 if (interface->wglGetProcAddress)
194 return (SPUGenericFunction) interface->wglGetProcAddress( funcName );
195 else
196 return (SPUGenericFunction) NULL;
197#else
198 /* XXX for some reason, the NVIDIA glXGetProcAddressARB() function
199 * returns pointers that cause Chromium to crash. If we use the
200 * pointer returned by crDLLGetNoError() instead, we're OK.
201 */
202 SPUGenericFunction f = crDLLGetNoError(glDll, funcName);
203 if (f)
204 return f;
205#if !defined(DARWIN)
206 else if (interface->glXGetProcAddressARB)
207 return interface->glXGetProcAddressARB( (const GLubyte *) funcName );
208#endif
209 else
210 return NULL;
211#endif
212}
213"""
214
215
216def IsExtensionFunc(func_name):
217 """Determine if the named function is a core function, or extension."""
218 cat = apiutil.Category(func_name)
219 if cat == "1.0" or cat == "1.1" or cat == "1.2" or cat == "1.3":
220 return 0
221 else:
222 return 1
223
224#
225# Generate a no-op function.
226#
227def GenerateNop(func_name):
228 return_type = apiutil.ReturnType(func_name);
229 params = apiutil.Parameters(func_name)
230 print 'static %s GLLOADER_APIENTRY Nop%s(%s)' % (return_type, func_name, apiutil.MakeDeclarationString(params))
231 print '{'
232 for (name, type, vecSize) in params:
233 if name != "":
234 print '\t(void) %s;' % name
235 if apiutil.ReturnType(func_name) != 'void':
236 print '\treturn 0;'
237 print '}'
238 print ''
239
240
241
242#
243# Make no-op funcs for all OpenGL extension functions
244#
245for func_name in keys:
246 if IsExtensionFunc(func_name):
247 GenerateNop(func_name)
248
249
250#
251# Generate the crLoadOpenGL() function
252#
253print """
254void
255crUnloadOpenGL( void )
256{
257 crDLLClose( glDll );
258 glDll = NULL;
259
260#ifdef DARWIN
261 crDLLClose( cglDll );
262 cglDll = NULL;
263
264# ifndef VBOX_WITH_COCOA_QT
265 crDLLClose( aglDll );
266 aglDll = NULL;
267# endif
268#endif
269}
270
271/*
272 * Initialize the 'interface' structure with the WGL or GLX window system
273 * interface functions.
274 * Then, fill in the table with (name, pointer) pairs for all the core
275 * OpenGL entrypoint functions. But only if table is not NULL
276 * Return: number of entries placed in table[], or 0 if error.
277 */
278int
279crLoadOpenGL( crOpenGLInterface *interface, SPUNamedFunctionTable table[] )
280{
281 static const char *coreFunctions[] = {
282"""
283
284for func_name in keys:
285 if not IsExtensionFunc(func_name):
286 print '\t\t"gl%s",' % func_name
287
288print """
289 NULL
290 };
291 SPUNamedFunctionTable *entry = table;
292 int i;
293
294 const char *env_syspath = crGetenv( "CR_SYSTEM_GL_PATH" );
295#ifdef DARWIN
296 const char *env_cgl_syspath = crGetenv( "CR_SYSTEM_CGL_PATH" );
297# ifndef VBOX_WITH_COCOA_QT
298 const char *env_agl_syspath = crGetenv( "CR_SYSTEM_AGL_PATH" );
299# endif
300#endif
301
302 crDebug( "Looking for the system's OpenGL library..." );
303#ifdef DARWIN
304 glDll = __findSystemGL( env_syspath, SYSTEM_GL_LIB_DIR, SYSTEM_GL );
305#else
306 glDll = __findSystemGL( env_syspath );
307#endif
308 if (!glDll)
309 {
310 crError("Unable to find system OpenGL!");
311 return 0;
312 }
313
314 crDebug( "Found it in %s.", !env_syspath ? "default path" : env_syspath );
315
316#ifdef DARWIN
317 crDebug( "Looking for the system's CGL library..." );
318 cglDll = __findSystemGL( env_cgl_syspath, SYSTEM_CGL_DIR, SYSTEM_CGL );
319 if (!cglDll)
320 {
321 crError("Unable to find system CGL!");
322 return 0;
323 }
324
325 crDebug( "Found it in %s.", !env_cgl_syspath ? "default path" : env_cgl_syspath );
326
327# ifndef VBOX_WITH_COCOA_QT
328 crDebug( "Looking for the system's AGL library..." );
329 aglDll = __findSystemGL( env_agl_syspath, SYSTEM_AGL_DIR, SYSTEM_AGL );
330 if (!aglDll)
331 {
332 crError("Unable to find system AGL!");
333 return 0;
334 }
335
336 crDebug( "Found it in %s.", !env_agl_syspath ? "default path" : env_agl_syspath );
337# endif
338#endif
339"""
340
341useful_wgl_functions = [
342 "wglGetProcAddress",
343 "wglMakeCurrent",
344 "wglSwapBuffers",
345 "wglCreateContext",
346 "wglDeleteContext",
347 "wglGetCurrentContext",
348 "wglChoosePixelFormat",
349 "wglDescribePixelFormat",
350 "wglSetPixelFormat",
351 "wglChoosePixelFormatEXT",
352 "wglGetPixelFormatAttribivEXT",
353 "wglGetPixelFormatAttribfvEXT",
354 "glGetString"
355]
356useful_agl_functions = [
357 "aglCreateContext",
358 "aglDestroyContext",
359 "aglSetCurrentContext",
360 "aglSwapBuffers",
361 "aglChoosePixelFormat",
362 "aglDestroyPixelFormat",
363 "aglDescribePixelFormat",
364 "aglGetCurrentContext",
365 "aglSetDrawable",
366 "aglGetDrawable",
367 "aglSetFullScreen",
368 "aglUpdateContext",
369 "aglUseFont",
370 "aglSetInteger",
371 "aglGetInteger",
372 "aglGetError",
373 "aglEnable",
374 "aglDisable"
375]
376in_gl_functions = [
377 "CGLGetCurrentContext",
378 "CGLSetCurrentContext"
379]
380useful_cgl_functions = [
381 "CGLChoosePixelFormat",
382 "CGLDestroyPixelFormat",
383 "CGLDescribePixelFormat",
384 "CGLQueryRendererInfo",
385 "CGLDestroyRendererInfo",
386 "CGLDescribeRenderer",
387 "CGLCreateContext",
388 "CGLDestroyContext",
389 "CGLCopyContext",
390 "CGLCreatePBuffer",
391 "CGLDestroyPBuffer",
392 "CGLDescribePBuffer",
393 "CGLTexImagePBuffer",
394 "CGLSetOffScreen",
395 "CGLGetOffScreen",
396 "CGLSetFullScreen",
397 "CGLSetPBuffer",
398 "CGLGetPBuffer",
399 "CGLClearDrawable",
400 "CGLFlushDrawable",
401 "CGLEnable",
402 "CGLDisable",
403 "CGLIsEnabled",
404 "CGLSetParameter",
405 "CGLGetParameter",
406 "CGLSetVirtualScreen",
407 "CGLGetVirtualScreen",
408 "CGLSetOption",
409 "CGLGetOption",
410 "CGLGetVersion",
411 "CGLErrorString",
412 "CGLSetSurface",
413 "CGLGetSurface",
414 "CGLUpdateContext",
415 "glGetString"
416]
417useful_glx_functions = [
418 "glXGetConfig",
419 "glXQueryExtension",
420 "glXQueryVersion",
421 "glXQueryExtensionsString",
422 "glXChooseVisual",
423 "glXCreateContext",
424 "glXDestroyContext",
425 "glXUseXFont",
426 "glXIsDirect",
427 "glXMakeCurrent",
428 "glGetString",
429 "glXSwapBuffers",
430 "glXGetCurrentDisplay",
431 "glXGetCurrentContext",
432 "glXGetClientString",
433 "glXWaitGL",
434 "glXWaitX",
435 "glXCopyContext"
436]
437possibly_useful_glx_functions = [
438 "glXGetProcAddressARB",
439 "glXJoinSwapGroupNV",
440 "glXBindSwapBarrierNV",
441 "glXQuerySwapGroupNV",
442 "glXQueryMaxSwapGroupsNV",
443 "glXQueryFrameCountNV",
444 "glXResetFrameCountNV",
445 "glXChooseFBConfig",
446 "glXGetFBConfigs",
447 "glXGetFBConfigAttrib",
448 "glXGetVisualFromFBConfig",
449 "glXCreateNewContext",
450 "glXCreatePbuffer",
451 "glXDestroyPbuffer",
452 "glXQueryContext",
453 "glXQueryDrawable",
454 "glXMakeContextCurrent",
455 "glXCreateWindow",
456 "glXGetVisualFromFBConfig",
457]
458
459print '#ifdef WINDOWS'
460
461for fun in useful_wgl_functions:
462 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun,fun,fun)
463
464print '#elif defined(DARWIN)'
465print '# ifndef VBOX_WITH_COCOA_QT'
466for fun in useful_agl_functions:
467 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( aglDll, "%s" );' % (fun,fun,fun)
468print '# endif'
469
470for fun in useful_cgl_functions:
471 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( cglDll, "%s" );' % (fun, fun,fun)
472
473for fun in in_gl_functions:
474 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun, fun,fun)
475
476print '#else'
477print '\t/* GLX */'
478
479# XXX merge these loops?
480for fun in useful_glx_functions:
481 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun, fun, fun)
482for fun in possibly_useful_glx_functions:
483 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun, fun, fun)
484print '#endif'
485
486print """
487 if (!entry)
488 return 1; /* token value */
489
490 for (i = 0; coreFunctions[i]; i++) {
491 const char *name = coreFunctions[i];
492 if (fillin(entry, name + 2, crDLLGetNoError(glDll, name)))
493 entry++;
494 else
495 crDebug("glLoader: NULL function %s", name);
496 }
497
498 /* end of table markers */
499 entry->name = NULL;
500 entry->fn = NULL;
501 return entry - table; /* number of entries filled */
502}
503
504
505/*
506 * Fill in table[] with all the OpenGL extension functions that we're
507 * interested in.
508 */
509int
510crLoadOpenGLExtensions( const crOpenGLInterface *interface, SPUNamedFunctionTable table[] )
511{
512 struct extfunc {
513 const char *funcName;
514 const char *aliasName;
515 SPUGenericFunction nopFunction;
516 };
517 static const struct extfunc functions[] = {
518"""
519
520for func_name in keys:
521 if IsExtensionFunc(func_name):
522 if apiutil.Category(func_name) == "Chromium":
523 prefix = "cr"
524 else:
525 prefix = "gl"
526 s = '\t\t{ "' + prefix + func_name + '", '
527 a = apiutil.ReverseAlias(func_name)
528 if a:
529 s += '"' + prefix + a + '", '
530 else:
531 s += 'NULL, '
532 s += '(SPUGenericFunction) Nop' + func_name + ' },'
533 print s
534
535print """
536 { NULL, NULL, NULL}
537 };
538 const struct extfunc *func;
539 SPUNamedFunctionTable *entry = table;
540
541#ifdef WINDOWS
542 if (interface->wglGetProcAddress == NULL)
543 crWarning("Unable to find wglGetProcAddress() in system GL library");
544#elif !defined(DARWIN)
545 if (interface->glXGetProcAddressARB == NULL)
546 crWarning("Unable to find glXGetProcAddressARB() in system GL library");
547#endif
548
549 for (func = functions; func->funcName; func++) {
550 SPUGenericFunction f = findExtFunction(interface, func->funcName);
551 if (!f && func->aliasName) {
552 f = findExtFunction(interface, func->aliasName);
553 }
554 if (!f) {
555 f = func->nopFunction;
556 }
557 (void) fillin(entry, func->funcName + 2 , f); /* +2 to skip "gl" */
558 entry++;
559 }
560
561 /* end of list */
562 entry->name = NULL;
563 entry->fn = NULL;
564 return entry - table; /* number of entries filled */
565}
566"""
567
568
569print """
570
571#ifdef USE_OSMESA
572int crLoadOSMesa( OSMesaContext (**createContext)( GLenum format, OSMesaContext sharelist ),
573 GLboolean (**makeCurrent)( OSMesaContext ctx, GLubyte *buffer,
574 GLenum type, GLsizei width, GLsizei height ),
575 void (**destroyContext)( OSMesaContext ctx ))
576{
577 static CRDLL *osMesaDll = NULL;
578
579 const char *env_syspath = crGetenv( "CR_SYSTEM_GL_PATH" );
580
581 crDebug( "Looking for the system's OSMesa library..." );
582 osMesaDll = __findSystemLib( env_syspath, "libOSMesa.so" );
583 if (!osMesaDll)
584 {
585 crError("Unable to find system OSMesa!");
586 return 0;
587 }
588
589 crDebug( "Found it in %s.", !env_syspath ? "default path" : env_syspath );
590
591 *createContext = (OSMesaContext (*) ( GLenum format, OSMesaContext sharelist ))
592 crDLLGetNoError( osMesaDll, "OSMesaCreateContext" );
593
594 *makeCurrent = (GLboolean (*) ( OSMesaContext ctx, GLubyte *buffer,
595 GLenum type, GLsizei width, GLsizei height ))
596 crDLLGetNoError( osMesaDll, "OSMesaMakeCurrent" );
597
598 *destroyContext = (void (*) ( OSMesaContext ctx))
599 crDLLGetNoError( osMesaDll, "OSMesaDestroyContext" );
600
601 return 1;
602}
603#endif
604
605"""
606
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette