VirtualBox

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

最後變更 在這個檔案從46265是 40691,由 vboxsync 提交於 13 年 前

crOpenGL: basics for using multiple contexts on host

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 14.4 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# 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#include <string.h>
39#if defined(AIX)
40#define SYSTEM_GL "libGL.o"
41#else
42#define SYSTEM_GL "libGL.so.1"
43#endif
44typedef void (*glxfuncptr)();
45extern glxfuncptr glxGetProcAddressARB( const GLubyte *name );
46#else
47#error I don't know where your system's GL lives. Too bad.
48#endif
49
50static CRDLL *glDll = NULL;
51
52#ifdef DARWIN
53#define SYSTEM_GL_LIB_DIR "/System/Library/Frameworks/OpenGL.framework/Libraries"
54#define SYSTEM_CGL_DIR "/System/Library/Frameworks/OpenGL.framework"
55static CRDLL *cglDll = NULL;
56# ifndef VBOX_WITH_COCOA_QT
57# define SYSTEM_AGL_DIR "/System/Library/Frameworks/AGL.framework"
58static CRDLL *aglDll = NULL;
59# endif
60#endif
61
62#if defined(WINDOWS)
63#define GLLOADER_APIENTRY __stdcall
64#else
65#define GLLOADER_APIENTRY
66#endif
67
68#if defined(WINDOWS) && (defined(DEBUG_leo) || defined(DEBUG_ll158262) || defined(DEBUG_misha))
69# define CR_NO_GL_SYSTEM_PATH 1
70#endif
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 "wglShareLists",
349 "wglGetCurrentContext",
350 "wglChoosePixelFormat",
351 "wglDescribePixelFormat",
352 "wglSetPixelFormat",
353 "wglChoosePixelFormatEXT",
354 "wglGetPixelFormatAttribivEXT",
355 "wglGetPixelFormatAttribfvEXT",
356 "glGetString"
357]
358useful_agl_functions = [
359 "aglCreateContext",
360 "aglDestroyContext",
361 "aglSetCurrentContext",
362 "aglSwapBuffers",
363 "aglChoosePixelFormat",
364 "aglDestroyPixelFormat",
365 "aglDescribePixelFormat",
366 "aglGetCurrentContext",
367 "aglSetDrawable",
368 "aglGetDrawable",
369 "aglSetFullScreen",
370 "aglUpdateContext",
371 "aglUseFont",
372 "aglSetInteger",
373 "aglGetInteger",
374 "aglGetError",
375 "aglEnable",
376 "aglDisable"
377]
378in_gl_functions = [
379 "CGLGetCurrentContext",
380 "CGLSetCurrentContext"
381]
382useful_cgl_functions = [
383 "CGLChoosePixelFormat",
384 "CGLDestroyPixelFormat",
385 "CGLDescribePixelFormat",
386 "CGLQueryRendererInfo",
387 "CGLDestroyRendererInfo",
388 "CGLDescribeRenderer",
389 "CGLCreateContext",
390 "CGLDestroyContext",
391 "CGLCopyContext",
392 "CGLCreatePBuffer",
393 "CGLDestroyPBuffer",
394 "CGLDescribePBuffer",
395 "CGLTexImagePBuffer",
396 "CGLSetOffScreen",
397 "CGLGetOffScreen",
398 "CGLSetFullScreen",
399 "CGLSetPBuffer",
400 "CGLGetPBuffer",
401 "CGLClearDrawable",
402 "CGLFlushDrawable",
403 "CGLEnable",
404 "CGLDisable",
405 "CGLIsEnabled",
406 "CGLSetParameter",
407 "CGLGetParameter",
408 "CGLSetVirtualScreen",
409 "CGLGetVirtualScreen",
410 "CGLSetOption",
411 "CGLGetOption",
412 "CGLGetVersion",
413 "CGLErrorString",
414 "CGLSetSurface",
415 "CGLGetSurface",
416 "CGLUpdateContext",
417 "glGetString"
418]
419useful_glx_functions = [
420 "glXGetConfig",
421 "glXQueryExtension",
422 "glXQueryVersion",
423 "glXQueryExtensionsString",
424 "glXChooseVisual",
425 "glXCreateContext",
426 "glXDestroyContext",
427 "glXUseXFont",
428 "glXIsDirect",
429 "glXMakeCurrent",
430 "glGetString",
431 "glXSwapBuffers",
432 "glXGetCurrentDisplay",
433 "glXGetCurrentContext",
434 "glXGetClientString",
435 "glXWaitGL",
436 "glXWaitX",
437 "glXCopyContext"
438]
439possibly_useful_glx_functions = [
440 "glXGetProcAddressARB",
441 "glXJoinSwapGroupNV",
442 "glXBindSwapBarrierNV",
443 "glXQuerySwapGroupNV",
444 "glXQueryMaxSwapGroupsNV",
445 "glXQueryFrameCountNV",
446 "glXResetFrameCountNV",
447 "glXChooseFBConfig",
448 "glXGetFBConfigs",
449 "glXGetFBConfigAttrib",
450 "glXGetVisualFromFBConfig",
451 "glXCreateNewContext",
452 "glXCreatePbuffer",
453 "glXDestroyPbuffer",
454 "glXQueryContext",
455 "glXQueryDrawable",
456 "glXMakeContextCurrent",
457 "glXCreateWindow",
458 "glXGetVisualFromFBConfig",
459]
460
461print '#ifdef WINDOWS'
462
463for fun in useful_wgl_functions:
464 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun,fun,fun)
465
466print '#elif defined(DARWIN)'
467print '# ifndef VBOX_WITH_COCOA_QT'
468for fun in useful_agl_functions:
469 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( aglDll, "%s" );' % (fun,fun,fun)
470print '# endif'
471
472for fun in useful_cgl_functions:
473 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( cglDll, "%s" );' % (fun, fun,fun)
474
475for fun in in_gl_functions:
476 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun, fun,fun)
477
478print '#else'
479print '\t/* GLX */'
480
481# XXX merge these loops?
482for fun in useful_glx_functions:
483 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun, fun, fun)
484for fun in possibly_useful_glx_functions:
485 print '\tinterface->%s = (%sFunc_t) crDLLGetNoError( glDll, "%s" );' % (fun, fun, fun)
486print '#endif'
487
488print """
489 if (!entry)
490 return 1; /* token value */
491
492 for (i = 0; coreFunctions[i]; i++) {
493 const char *name = coreFunctions[i];
494 if (fillin(entry, name + 2, crDLLGetNoError(glDll, name)))
495 entry++;
496 else
497 crDebug("glLoader: NULL function %s", name);
498 }
499
500 /* end of table markers */
501 entry->name = NULL;
502 entry->fn = NULL;
503 return entry - table; /* number of entries filled */
504}
505
506
507/*
508 * Fill in table[] with all the OpenGL extension functions that we're
509 * interested in.
510 */
511int
512crLoadOpenGLExtensions( const crOpenGLInterface *interface, SPUNamedFunctionTable table[] )
513{
514 struct extfunc {
515 const char *funcName;
516 const char *aliasName;
517 SPUGenericFunction nopFunction;
518 };
519 static const struct extfunc functions[] = {
520"""
521
522for func_name in keys:
523 if IsExtensionFunc(func_name):
524 if apiutil.Category(func_name) == "Chromium":
525 prefix = "cr"
526 else:
527 prefix = "gl"
528 s = '\t\t{ "' + prefix + func_name + '", '
529 a = apiutil.ReverseAlias(func_name)
530 if a:
531 s += '"' + prefix + a + '", '
532 else:
533 s += 'NULL, '
534 s += '(SPUGenericFunction) Nop' + func_name + ' },'
535 print s
536
537print """
538 { NULL, NULL, NULL}
539 };
540 const struct extfunc *func;
541 SPUNamedFunctionTable *entry = table;
542
543#ifdef WINDOWS
544 if (interface->wglGetProcAddress == NULL)
545 crWarning("Unable to find wglGetProcAddress() in system GL library");
546#elif !defined(DARWIN)
547 if (interface->glXGetProcAddressARB == NULL)
548 crWarning("Unable to find glXGetProcAddressARB() in system GL library");
549#endif
550
551 for (func = functions; func->funcName; func++) {
552 SPUGenericFunction f = findExtFunction(interface, func->funcName);
553 if (!f && func->aliasName) {
554 f = findExtFunction(interface, func->aliasName);
555 }
556 if (!f) {
557 f = func->nopFunction;
558 }
559 (void) fillin(entry, func->funcName + 2 , f); /* +2 to skip "gl" */
560 entry++;
561 }
562
563 /* end of list */
564 entry->name = NULL;
565 entry->fn = NULL;
566 return entry - table; /* number of entries filled */
567}
568"""
569
570
571print """
572
573#ifdef USE_OSMESA
574int crLoadOSMesa( OSMesaContext (**createContext)( GLenum format, OSMesaContext sharelist ),
575 GLboolean (**makeCurrent)( OSMesaContext ctx, GLubyte *buffer,
576 GLenum type, GLsizei width, GLsizei height ),
577 void (**destroyContext)( OSMesaContext ctx ))
578{
579 static CRDLL *osMesaDll = NULL;
580
581 const char *env_syspath = crGetenv( "CR_SYSTEM_GL_PATH" );
582
583 crDebug( "Looking for the system's OSMesa library..." );
584 osMesaDll = __findSystemLib( env_syspath, "libOSMesa.so" );
585 if (!osMesaDll)
586 {
587 crError("Unable to find system OSMesa!");
588 return 0;
589 }
590
591 crDebug( "Found it in %s.", !env_syspath ? "default path" : env_syspath );
592
593 *createContext = (OSMesaContext (*) ( GLenum format, OSMesaContext sharelist ))
594 crDLLGetNoError( osMesaDll, "OSMesaCreateContext" );
595
596 *makeCurrent = (GLboolean (*) ( OSMesaContext ctx, GLubyte *buffer,
597 GLenum type, GLsizei width, GLsizei height ))
598 crDLLGetNoError( osMesaDll, "OSMesaMakeCurrent" );
599
600 *destroyContext = (void (*) ( OSMesaContext ctx))
601 crDLLGetNoError( osMesaDll, "OSMesaDestroyContext" );
602
603 return 1;
604}
605#endif
606
607"""
608
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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