VirtualBox

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

Last change on this file since 25856 was 24229, checked in by vboxsync, 15 years ago

glloader.py: fixed memset warnings on darwin.

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