VirtualBox

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

Last change on this file since 21216 was 20614, checked in by vboxsync, 16 years ago

crOpenGL: define to allow running under opengl debuggers

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