DEADSOFTWARE

9af6d1d36a6fdc9f898f4c19809a6052b881024e
[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 dlopen( x, y ) LoadLibraryA( x )
73 #define dlsym( x, y ) ( void * ) GetProcAddress( (HINSTANCE)x, y )
74 #define dlclose( x ) FreeLibrary( (HINSTANCE)x )
75 #else
76 #include <dlfcn.h>
77 #endif
79 //#define GL_ENTRY(_r, _api, ...) #_api,
81 static char const *const gl_names[] = {
82 #include "funcnames.h"
83 NULL};
85 //const char * driver;
87 static void *glesLib = NULL;
89 GlESInterface *glEsImpl = NULL;
91 extern void InitGLStructs( );
93 void APIENTRY gl_unimplemented( GLenum none )
94 {
95 #ifndef USE_CORE_PROFILE
96 LOGE( "Called unimplemented OpenGL ES API\n" );
97 #endif
98 }
100 #ifdef XASH_SDL
101 #include "SDL.h"
102 #endif
104 void *nanoGL_GetProcAddress( const char *name )
106 void *addr = NULL;
107 #ifdef XASH_SDL
108 addr = SDL_GL_GetProcAddress( name );
109 if ( !addr )
110 #endif
111 addr = dlsym( glesLib, name );
112 return addr;
115 static int CreateGlEsInterface( const char *name, void *lib, void *lib1, void *default_func )
117 // alloc space
118 if ( !glEsImpl )
119 glEsImpl = (GlESInterface *)malloc( sizeof( GlESInterface ) );
121 if ( !glEsImpl )
123 return 0;
126 // load GL API calls
127 char const *const *api;
128 api = gl_names;
130 // nanoGL interface pointer
131 void **ptr = (void **)( glEsImpl );
133 while ( *api )
135 void *f;
137 f = dlsym( lib, *api ); // try libGLESxx_CM.so
139 #ifdef USE_CORE_PROFILE
140 // Hack: try ARB and EXT suffix
141 if ( f == NULL )
143 char namearb[256];
144 snprintf( namearb, 256, "%sARB", *api );
145 f = dlsym( lib, namearb );
147 if ( f == NULL )
149 char namearb[256];
150 snprintf( namearb, 256, "%sEXT", *api );
151 f = dlsym( lib, namearb );
153 #endif
154 if ( f == NULL )
156 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name ); //driver);
158 // try lib1
159 if ( lib1 )
161 f = dlsym( lib1, *api ); // libEGL.so
163 if ( f == NULL )
165 LOGE( "<%s> not found in libEGL.so", *api );
166 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
167 f = (void *)glEsImpl->eglGetProcAddress( *api );
168 if ( f == NULL )
169 f = (void *)default_func; //(void*)gl_unimplemented;
171 else
173 LOGD( "<%s> @ 0x%p\n", *api, f );
176 else
178 LOGE( "libEGL.so not loaded!" );
179 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
180 f = (void *)glEsImpl->eglGetProcAddress( *api );
181 if ( !f )
182 f = (void *)default_func;
185 else
187 LOGD( "<%s> @ 0x%p\n", *api, f );
190 *ptr++ = f;
191 api++;
194 return 1;
197 // Load using the dynamic loader
198 static int loadDriver( const char *name )
200 glesLib = dlopen( name, RTLD_NOW | RTLD_LOCAL );
201 int rc = ( glesLib ) ? 1 : 0;
202 return rc;
205 /**
206 * Init
207 */
208 #ifdef _WIN32
209 int nanoGL_Init( )
211 const char *lib1 = "opengl32.dll"; // Has both gl* & egl* funcs SDK < 1.5
212 const char *lib2 = "opengl32.dll"; // Only gl* funcs SDK >= 1.5
213 const char *lib3 = "opengl32.dll"; // Only egl* funcs SDK >= 1.5
214 const char *driver;
216 // load lib
217 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
218 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
220 if ( !loadDriver( lib1 ) )
222 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
224 if ( !loadDriver( lib2 ) )
226 LOGE( "Failed to load %s.\n", lib2 );
227 return 0;
229 else
230 driver = lib2;
232 else
233 driver = lib1;
235 void *eglLib;
237 //if ( strcmp(driver, lib2) == 0 ) {
238 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
240 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
242 if ( !eglLib )
244 LOGE( "Failed to load %s", lib3 );
246 //}
248 // Load API gl* for 1.5+ else egl* gl*
249 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
250 if ( !CreateGlEsInterface( driver, glesLib, eglLib, (void *)gl_unimplemented ) == -1 )
252 // release lib
253 LOGE( "CreateGlEsInterface failed." );
255 dlclose( glesLib );
256 return 0;
259 // Init nanoGL
260 InitGLStructs( );
261 return 1;
263 #else
264 int nanoGL_Init( )
266 const char *lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
267 const char *lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
268 const char *lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
269 const char *driver;
271 // load lib
272 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
273 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
275 if ( !loadDriver( lib1 ) )
277 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
279 if ( !loadDriver( lib2 ) )
281 LOGE( "Failed to load %s.\n", lib2 );
282 return 0;
284 else
285 driver = lib2;
287 else
288 driver = lib1;
290 void *eglLib;
292 //if ( strcmp(driver, lib2) == 0 ) {
293 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
295 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
297 if ( !eglLib )
299 LOGE( "Failed to load %s", lib3 );
301 //}
303 // Load API gl* for 1.5+ else egl* gl*
304 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
305 if ( !CreateGlEsInterface( driver, glesLib, eglLib, (void *)gl_unimplemented ) == -1 )
307 // release lib
308 LOGE( "CreateGlEsInterface failed." );
310 dlclose( glesLib );
311 return 0;
314 #ifdef __ANDROID__
315 // somewhy it does not initialize correctly
316 *( (void **)&glEsImpl->glGenFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenFramebuffersOES" );
317 *( (void **)&glEsImpl->glGenRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenRenderbuffersOES" );
318 *( (void **)&glEsImpl->glRenderbufferStorage ) = (void *)glEsImpl->eglGetProcAddress( "glRenderbufferStorageOES" );
319 *( (void **)&glEsImpl->glBindFramebuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindFramebufferOES" );
320 *( (void **)&glEsImpl->glBindRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindRenderbufferOES" );
321 *( (void **)&glEsImpl->glFramebufferTexture2D ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferTexture2DOES" );
322 *( (void **)&glEsImpl->glDeleteRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteRenderbuffersOES" );
323 *( (void **)&glEsImpl->glDeleteFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteFramebuffersOES" );
324 *( (void **)&glEsImpl->glFramebufferRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferRenderbufferOES" );
325 #endif
327 // Init nanoGL
328 InitGLStructs( );
329 return 1;
331 #endif
332 void nanoGL_Destroy( )
334 LOGD( "nanoGL_Destroy" );
336 if ( glEsImpl )
338 free( glEsImpl );
339 glEsImpl = NULL;
342 // release lib
343 dlclose( glesLib );