DEADSOFTWARE

glIsEnabled uses interanl structures, added glPushAttrib/glPopAttrib (not fully imple...
[nanogl.git] / nanogl.cpp
1 /*
2 Copyright (C) 2007-2009 Olli Hinkka
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #define LOG_TAG "nanoGL"
23 #include <stdio.h>
24 #include <stdlib.h>
26 //#include <cutils/log.h>
28 #include "nanogl.h"
29 #include "glesinterface.h"
30 #include "gl.h"
32 #define DEBUG_NANO 0
34 #ifdef __ANDROID__
35 #include <android/log.h>
36 #define LOG __android_log_print
38 #define LOGI( ... ) __android_log_print( ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__ )
39 #define LOGD( ... ) \
40 if ( DEBUG_NANO ) \
41 __android_log_print( ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__ )
42 #define LOGE( ... ) __android_log_print( ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__ )
43 #define LOGW( ... ) __android_log_print( ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__ )
44 #else
45 #ifndef _MSC_VER
46 #define LOGI( ... ) \
47 printf( "I: " __VA_ARGS__ ); \
48 printf( "\n" )
49 #define LOGD( ... ) \
50 if ( DEBUG_NANO ) \
51 { \
52 printf( "D: " __VA_ARGS__ ); \
53 printf( "\n" ); \
54 }
55 #define LOGE( ... ) \
56 printf( "E: " __VA_ARGS__ ); \
57 printf( "\n" )
58 #define LOGW( ... ) \
59 printf( "W: " __VA_ARGS__ ); \
60 printf( "\n" )
61 #else
62 #define LOGI printf
63 #define LOGD printf
64 #define LOGE printf
65 #define LOGW printf
67 #endif
68 #endif
70 #ifdef _WIN32
71 #include <windows.h>
72 #define loadDriver( x ) LoadLibraryA( x )
73 #define procAddress( x, y ) (( void * ) GetProcAddress( (HINSTANCE)x, y ))
74 #define freeDriver( x ) FreeLibrary( (HINSTANCE)x )
75 #define GL_LIB "opengl32.dll"
76 #define GLES_LIB "GLESv1_CM.dll"
77 #define EGL_LIB "EGL.dll"
78 #else
79 #include <dlfcn.h>
80 #define loadDriver( x ) dlopen( x, RTLD_NOW | RTLD_LOCAL )
81 #define procAddress( x, y ) dlsym( x, y )
82 #define freeDriver( x ) dlclose() x )
83 #define GL_LIB "libGL.so.1"
84 #define GLES_LIB "libGLESv1_CM.so"
85 #define EGL_LIB "libEGL.so"
86 #endif
88 //#define GL_ENTRY(_r, _api, ...) #_api,
90 static char const *const gl_names[] = {
91 #include "funcnames.h"
92 NULL};
94 //const char * driver;
96 static void *glesLib = NULL;
98 GlESInterface *glEsImpl = NULL;
100 extern void InitGLStructs( );
101 #ifdef WIN32
102 void APIENTRY gl_unimplemented( GLenum none )
104 #ifndef USE_CORE_PROFILE
105 LOGE( "Called unimplemented OpenGL ES API\n" );
106 #endif
108 #else // make glGetString not crash
109 const char * APIENTRY gl_unimplemented( GLenum none )
111 #ifndef USE_CORE_PROFILE
112 LOGE( "Called unimplemented OpenGL ES API\n" );
113 #endif
114 return "";
116 #endif
117 #ifdef XASH_SDL
118 #define NANOGL_SDL
119 #endif
120 #ifdef __ANDROID__
121 #define NANOGL_EGL
122 #endif
123 #ifdef NANOGL_SDL
124 #include "SDL.h"
125 #endif
127 #ifdef NANOGL_EGL
128 void *eglLib;
129 #endif
131 void *nanoGL_GetProcAddress( const char *name )
133 void *addr = NULL;
134 #ifdef NANOGL_SDL
135 addr = SDL_GL_GetProcAddress( name );
136 if ( !addr )
137 #endif
138 addr = procAddress( glesLib, name );
139 #ifdef NANOGL_EGL
140 if( !addr )
141 addr = procAddress( eglLib, name );
142 if( !addr && glEsImpl->eglGetProcAddress )
143 addr = (void *)glEsImpl->eglGetProcAddress( name );
144 #endif
145 return addr;
148 #if 1
149 int nanoGL_Init( void)
151 // load GL API calls
152 char const *const *api;
153 api = gl_names;
154 int count = 0;
156 // alloc space
157 if ( !glEsImpl )
158 glEsImpl = (GlESInterface *)malloc( sizeof( GlESInterface ) );
159 memset( glEsImpl, 0, sizeof( GlESInterface ) );
161 #ifdef NANOGL_EGL
162 eglLib = loadDriver( EGL_LIB );
163 #endif
164 #ifdef USE_CORE_PROFILE
165 glesLib = loadDriver( GL_LIB );
166 #else
167 glesLib = loadDriver( GLES_LIB );
168 #endif
170 // nanoGL interface pointer
171 void **ptr = (void **)( glEsImpl );
173 while ( *api )
175 void *f;
177 f = nanoGL_GetProcAddress( *api);
179 #ifdef USE_CORE_PROFILE
180 // Hack: try ARB and EXT suffix
181 if ( f == NULL )
183 char namearb[256];
184 snprintf( namearb, 256, "%sARB", *api );
185 f = nanoGL_GetProcAddress( namearb );
187 if ( f == NULL )
189 char namearb[256];
190 snprintf( namearb, 256, "%sEXT", *api );
191 f = nanoGL_GetProcAddress( namearb );
193 #endif
194 if ( f == NULL )
196 LOGW( "<%s> not found.", *api);
197 f = (void*)gl_unimplemented;
199 else
201 LOGD( "<%s> @ 0x%p\n", *api, f );
202 count++;
205 *ptr++ = f;
206 api++;
209 InitGLStructs();
211 // it has loaded something, maybe it will work
212 if( count > 10 )
213 return 1;
214 else
215 return 0;
217 #else
219 static int CreateGlEsInterface( const char *name, void *lib, void *lib1, void *default_func )
221 // alloc space
222 if ( !glEsImpl )
223 glEsImpl = (GlESInterface *)malloc( sizeof( GlESInterface ) );
225 if ( !glEsImpl )
227 return 0;
230 // load GL API calls
231 char const *const *api;
232 api = gl_names;
234 // nanoGL interface pointer
235 void **ptr = (void **)( glEsImpl );
237 while ( *api )
239 void *f;
241 f = dlsym( lib, *api ); // try libGLESxx_CM.so
243 #ifdef USE_CORE_PROFILE
244 // Hack: try ARB and EXT suffix
245 if ( f == NULL )
247 char namearb[256];
248 snprintf( namearb, 256, "%sARB", *api );
249 f = dlsym( lib, namearb );
251 if ( f == NULL )
253 char namearb[256];
254 snprintf( namearb, 256, "%sEXT", *api );
255 f = dlsym( lib, namearb );
257 #endif
258 if ( f == NULL )
260 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name ); //driver);
262 // try lib1
263 if ( lib1 )
265 f = dlsym( lib1, *api ); // libEGL.so
267 if ( f == NULL )
269 LOGE( "<%s> not found in libEGL.so", *api );
270 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
271 f = (void *)glEsImpl->eglGetProcAddress( *api );
272 if ( f == NULL )
273 f = (void *)default_func; //(void*)gl_unimplemented;
275 else
277 LOGD( "<%s> @ 0x%p\n", *api, f );
280 else
282 LOGE( "libEGL.so not loaded!" );
283 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
284 f = (void *)glEsImpl->eglGetProcAddress( *api );
285 if ( !f )
286 f = (void *)default_func;
289 else
291 LOGD( "<%s> @ 0x%p\n", *api, f );
294 *ptr++ = f;
295 api++;
298 return 1;
301 // Load using the dynamic loader
302 static int loadDriver( const char *name )
304 glesLib = dlopen( name, RTLD_NOW | RTLD_LOCAL );
305 int rc = ( glesLib ) ? 1 : 0;
306 return rc;
309 /**
310 * Init
311 */
312 #ifdef _WIN32
313 int nanoGL_Init( )
315 const char *lib1 = "opengl32.dll"; // Has both gl* & egl* funcs SDK < 1.5
316 const char *lib2 = "opengl32.dll"; // Only gl* funcs SDK >= 1.5
317 const char *lib3 = "opengl32.dll"; // Only egl* funcs SDK >= 1.5
318 const char *driver;
320 // load lib
321 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
322 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
324 if ( !loadDriver( lib1 ) )
326 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
328 if ( !loadDriver( lib2 ) )
330 LOGE( "Failed to load %s.\n", lib2 );
331 return 0;
333 else
334 driver = lib2;
336 else
337 driver = lib1;
339 void *eglLib;
341 //if ( strcmp(driver, lib2) == 0 ) {
342 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
344 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
346 if ( !eglLib )
348 LOGE( "Failed to load %s", lib3 );
350 //}
352 // Load API gl* for 1.5+ else egl* gl*
353 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
354 if ( !CreateGlEsInterface( driver, glesLib, eglLib, (void *)gl_unimplemented ) == -1 )
356 // release lib
357 LOGE( "CreateGlEsInterface failed." );
359 dlclose( glesLib );
360 return 0;
363 // Init nanoGL
364 InitGLStructs( );
365 return 1;
367 #else
368 int nanoGL_Init( )
370 const char *lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
371 const char *lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
372 const char *lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
373 const char *driver;
375 // load lib
376 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
377 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
379 if ( !loadDriver( lib1 ) )
381 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
383 if ( !loadDriver( lib2 ) )
385 LOGE( "Failed to load %s.\n", lib2 );
386 return 0;
388 else
389 driver = lib2;
391 else
392 driver = lib1;
394 void *eglLib;
396 //if ( strcmp(driver, lib2) == 0 ) {
397 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
399 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
401 if ( !eglLib )
403 LOGE( "Failed to load %s", lib3 );
405 //}
407 // Load API gl* for 1.5+ else egl* gl*
408 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
409 if ( !CreateGlEsInterface( driver, glesLib, eglLib, (void *)gl_unimplemented ) == -1 )
411 // release lib
412 LOGE( "CreateGlEsInterface failed." );
414 dlclose( glesLib );
415 return 0;
418 #ifdef __ANDROID__
419 // somewhy it does not initialize correctly
420 *( (void **)&glEsImpl->glGenFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenFramebuffersOES" );
421 *( (void **)&glEsImpl->glGenRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenRenderbuffersOES" );
422 *( (void **)&glEsImpl->glRenderbufferStorage ) = (void *)glEsImpl->eglGetProcAddress( "glRenderbufferStorageOES" );
423 *( (void **)&glEsImpl->glBindFramebuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindFramebufferOES" );
424 *( (void **)&glEsImpl->glBindRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindRenderbufferOES" );
425 *( (void **)&glEsImpl->glFramebufferTexture2D ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferTexture2DOES" );
426 *( (void **)&glEsImpl->glDeleteRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteRenderbuffersOES" );
427 *( (void **)&glEsImpl->glDeleteFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteFramebuffersOES" );
428 *( (void **)&glEsImpl->glFramebufferRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferRenderbufferOES" );
429 *( (void **)&glEsImpl->glTexGeniOES ) = (void *)glEsImpl->eglGetProcAddress( "glTexGeniOES" );
430 *( (void **)&glEsImpl->glTexGenfvOES ) = (void *)glEsImpl->eglGetProcAddress( "glTexGenfv" );
433 #endif
435 // Init nanoGL
436 InitGLStructs( );
437 return 1;
439 #endif
441 #endif
442 void nanoGL_Destroy( )
444 LOGD( "nanoGL_Destroy" );
446 if ( glEsImpl )
448 free( glEsImpl );
449 glEsImpl = NULL;
452 // release lib
453 dlclose( glesLib );