DEADSOFTWARE

Windows and core profile support
[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>
27 //#include <cutils/log.h>
29 #include "nanogl.h"
30 #include "glesinterface.h"
31 #include "gl.h"
34 #define DEBUG_NANO 0
36 #ifdef __ANDROID__
37 #include <android/log.h>
38 #define LOG __android_log_print
40 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
41 #define LOGD(...) if (DEBUG_NANO) __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(...) printf("I: "__VA_ARGS__);printf("\n")
47 #define LOGD(...) if(DEBUG_NANO) {printf("D: "__VA_ARGS__);printf("\n");}
48 #define LOGE(...) printf("E: "__VA_ARGS__);printf("\n")
49 #define LOGW(...) printf("W: "__VA_ARGS__);printf("\n")
50 #else
51 #define LOGI printf
52 #define LOGD printf
53 #define LOGE printf
54 #define LOGW printf
56 #endif
57 #endif
59 #ifdef _WIN32
60 #include <windows.h>
61 #define dlopen(x,y) LoadLibraryA(x)
62 #define dlsym(x,y) (void*)GetProcAddress((HINSTANCE)x,y)
63 #define dlclose(x) FreeLibrary((HINSTANCE)x)
64 #else
65 #include <dlfcn.h>
66 #endif
69 //#define GL_ENTRY(_r, _api, ...) #_api,
71 static char const * const gl_names[] = {
72 #include "funcnames.h"
73 NULL
74 };
76 //const char * driver;
78 static void* glesLib = NULL;
80 GlESInterface* glEsImpl = NULL;
82 extern void InitGLStructs();
84 void APIENTRY gl_unimplemented(GLenum none) {
85 #ifndef USE_CORE_PROFILE
86 LOGE ("Called unimplemented OpenGL ES API\n");
87 #endif
88 }
90 void *nanoGL_GetProcAddress(const char *name)
91 {
92 #if defined(__MULTITEXTURE_SUPPORT__)
93 if (!strcmp(procname, "glMultiTexCoord2fARB"))
94 {
95 return (void*)&glMultiTexCoord2fARB;
96 }
97 else if (!strcmp(procname, "glActiveTextureARB"))
98 {
99 return (void*)&glActiveTexture;
101 else if (!strcmp(procname, "glClientActiveTextureARB"))
103 return (void*)&glClientActiveTexture;
105 #endif
106 return dlsym(glesLib, name);
109 static int CreateGlEsInterface( const char * name, void * lib, void * lib1, void * default_func )
111 // alloc space
112 if ( !glEsImpl )
113 glEsImpl = (GlESInterface *) malloc(sizeof(GlESInterface));
115 if (!glEsImpl) {
116 return 0;
119 // load GL API calls
120 char const * const * api;
121 api = gl_names;
123 // nanoGL interface pointer
124 void ** ptr = (void **)(glEsImpl);
126 while (*api)
128 void * f;
130 f = dlsym(lib, *api); // try libGLESxx_CM.so
132 #ifdef USE_CORE_PROFILE
133 // Hack: try ARB and EXT suffix
134 if (f == NULL) {
135 char namearb[256];
136 snprintf( namearb, 256, "%sARB", *api );
137 f = dlsym( lib, namearb );
139 if (f == NULL) {
140 char namearb[256];
141 snprintf( namearb, 256, "%sEXT", *api );
142 f = dlsym( lib, namearb );
144 #endif
145 if (f == NULL) {
146 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name); //driver);
148 // try lib1
149 if ( lib1 ) {
150 f = dlsym(lib1, *api); // libEGL.so
152 if ( f == NULL ) {
153 LOGE ( "<%s> not found in libEGL.so", *api);
154 f = default_func; //(void*)gl_unimplemented;
156 else {
157 LOGD ("<%s> @ 0x%p\n", *api, f);
160 else
162 LOGE ( "libEGL.so not loaded!");
163 f = default_func;
166 else {
167 LOGD ("<%s> @ 0x%p\n", *api, f);
170 *ptr++ = f;
171 api++;
172 }
174 return 1;
177 // Load using the dynamic loader
178 static int loadDriver(const char * name) {
179 glesLib = dlopen(name, RTLD_NOW | RTLD_LOCAL);
180 int rc = (glesLib) ? 1 : 0;
181 return rc;
184 /**
185 * Init
186 */
187 #ifdef _WIN32
188 int nanoGL_Init()
190 const char * lib1 = "opengl32.dll"; // Has both gl* & egl* funcs SDK < 1.5
191 const char * lib2 = "opengl32.dll"; // Only gl* funcs SDK >= 1.5
192 const char * lib3 = "opengl32.dll"; // Only egl* funcs SDK >= 1.5
193 const char * driver;
195 // load lib
196 LOGI("nanoGL: Init loading driver %s\n", lib1);
197 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
199 if ( ! loadDriver(lib1) )
201 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
203 if ( ! loadDriver(lib2) ) {
204 LOGE ("Failed to load %s.\n", lib2);
205 return 0;
207 else
208 driver = lib2;
210 else
211 driver = lib1;
213 void * eglLib;
215 //if ( strcmp(driver, lib2) == 0 ) {
216 LOGD ("**** Will Load EGL subs from %s ****", lib3);
218 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
220 if ( ! eglLib ) {
221 LOGE ( "Failed to load %s", lib3);
223 //}
225 // Load API gl* for 1.5+ else egl* gl*
226 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
227 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
229 // release lib
230 LOGE ( "CreateGlEsInterface failed.");
232 dlclose(glesLib);
233 return 0;
236 // Init nanoGL
237 InitGLStructs();
238 return 1;
240 #else
241 int nanoGL_Init()
243 const char * lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
244 const char * lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
245 const char * lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
246 const char * driver;
248 // load lib
249 LOGI("nanoGL: Init loading driver %s\n", lib1);
250 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
252 if ( ! loadDriver(lib1) )
254 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
256 if ( ! loadDriver(lib2) ) {
257 LOGE ("Failed to load %s.\n", lib2);
258 return 0;
260 else
261 driver = lib2;
263 else
264 driver = lib1;
266 void * eglLib;
268 //if ( strcmp(driver, lib2) == 0 ) {
269 LOGD ("**** Will Load EGL subs from %s ****", lib3);
271 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
273 if ( ! eglLib ) {
274 LOGE ( "Failed to load %s", lib3);
276 //}
278 // Load API gl* for 1.5+ else egl* gl*
279 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
280 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
282 // release lib
283 LOGE ( "CreateGlEsInterface failed.");
285 dlclose(glesLib);
286 return 0;
289 // Init nanoGL
290 InitGLStructs();
291 return 1;
293 #endif
294 void nanoGL_Destroy()
296 LOGD ("nanoGL_Destroy");
298 if (glEsImpl) {
299 free( glEsImpl);
300 glEsImpl = NULL;
303 // release lib
304 dlclose(glesLib);