VirtualBox

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

最後變更 在這個檔案從20104是 18088,由 vboxsync 提交於 16 年 前

crOpenGL: some code cleanup and small fixes

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

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