| 1 |
// Comments: |
| 2 |
// |
| 3 |
// The trick with GLH_EXT_SINGLE_FILE is that you need to have it defined in |
| 4 |
// exactly one cpp file because it piggy-backs the function implementations in |
| 5 |
// the header. You don't want multiple implementations defined or the linker |
| 6 |
// gets mad, but one file must have the implementations in it or the linker |
| 7 |
// gets mad for different reasons. |
| 8 |
// |
| 9 |
// The goal was to avoid having this helper require a separate cpp file. One |
| 10 |
// thing you could do is just have a glh_extensions.cpp that did |
| 11 |
// |
| 12 |
// #define GLH_EXT_SINGLE_FILE |
| 13 |
// #include <glh_extensions.h> |
| 14 |
// |
| 15 |
// and make it the only file that ever defines GLH_EXT_SINGLE_FILE. |
| 16 |
|
| 17 |
#ifndef GLH_EXTENSIONS |
| 18 |
#define GLH_EXTENSIONS |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
|
| 24 |
|
| 25 |
#if defined(WIN32) |
| 26 |
# include <windows.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#if _MSC_VER > 1500 |
| 30 |
# include <system_error> |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef MACOS |
| 34 |
#include <OpenGL/gl.h> |
| 35 |
#else |
| 36 |
#include <GL/gl.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifdef WIN32 |
| 40 |
# include <GL/wglext.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#define CHECK_MEMORY(ptr) \ |
| 44 |
if (NULL == ptr) { \ |
| 45 |
printf("Error allocating memory in file %s, line %d\n", __FILE__, __LINE__); \ |
| 46 |
exit(-1); \ |
| 47 |
} |
| 48 |
|
| 49 |
#ifdef GLH_EXT_SINGLE_FILE |
| 50 |
# define GLH_EXTENSIONS_SINGLE_FILE // have to do this because glh_genext.h unsets GLH_EXT_SINGLE_FILE |
| 51 |
#endif |
| 52 |
|
| 53 |
#if (defined(WIN32) || defined(UNIX)) |
| 54 |
#include "glh_genext.h" |
| 55 |
#elif defined(MACOS) |
| 56 |
#include <OpenGL/glext.h> |
| 57 |
#else |
| 58 |
#include <GL/glext.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef __cplusplus |
| 62 |
extern "C" { |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef GLH_EXTENSIONS_SINGLE_FILE |
| 66 |
static char *unsupportedExts = NULL; |
| 67 |
static char *sysExts = NULL; |
| 68 |
#ifndef GL_SHADER_CONSISTENT_NV |
| 69 |
#define GL_SHADER_CONSISTENT_NV 0x86DD |
| 70 |
#endif |
| 71 |
#ifndef GL_TEXTURE_SHADER_NV |
| 72 |
#define GL_TEXTURE_SHADER_NV 0x86DE |
| 73 |
#endif |
| 74 |
#ifndef GL_SHADER_OPERATION_NV |
| 75 |
#define GL_SHADER_OPERATION_NV 0x86DF |
| 76 |
#endif |
| 77 |
|
| 78 |
static int ExtensionExists(const char* extName, const char* sysExts) |
| 79 |
{ |
| 80 |
char *padExtName = (char*)malloc(strlen(extName) + 2); |
| 81 |
strcat(strcpy(padExtName, extName), " "); |
| 82 |
|
| 83 |
if (0 == strcmp(extName, "GL_VERSION_1_2")) { |
| 84 |
const char *version = (const char*)glGetString(GL_VERSION); |
| 85 |
if (strstr(version, "1.0") == version || strstr(version, "1.1") == version) { |
| 86 |
return GL_FALSE; |
| 87 |
} else { |
| 88 |
return GL_TRUE; |
| 89 |
} |
| 90 |
} |
| 91 |
if (0 == strcmp(extName, "GL_VERSION_1_3")) { |
| 92 |
const char *version = (const char*)glGetString(GL_VERSION); |
| 93 |
if (strstr(version, "1.0") == version || |
| 94 |
strstr(version, "1.1") == version || |
| 95 |
strstr(version, "1.2") == version) { |
| 96 |
return GL_FALSE; |
| 97 |
} else { |
| 98 |
return GL_TRUE; |
| 99 |
} |
| 100 |
} |
| 101 |
if (0 == strcmp(extName, "GL_VERSION_1_4")) { |
| 102 |
const char *version = (const char*)glGetString(GL_VERSION); |
| 103 |
if (strstr(version, "1.0") == version || |
| 104 |
strstr(version, "1.1") == version || |
| 105 |
strstr(version, "1.2") == version || |
| 106 |
strstr(version, "1.3") == version) { |
| 107 |
return GL_FALSE; |
| 108 |
} else { |
| 109 |
return GL_TRUE; |
| 110 |
} |
| 111 |
} |
| 112 |
if (0 == strcmp(extName, "GL_VERSION_1_5")) { |
| 113 |
const char *version = (const char*)glGetString(GL_VERSION); |
| 114 |
if (strstr(version, "1.0") == version || |
| 115 |
strstr(version, "1.1") == version || |
| 116 |
strstr(version, "1.2") == version || |
| 117 |
strstr(version, "1.3") == version || |
| 118 |
strstr(version, "1.4") == version) { |
| 119 |
return GL_FALSE; |
| 120 |
} else { |
| 121 |
return GL_TRUE; |
| 122 |
} |
| 123 |
} |
| 124 |
if (strstr(sysExts, padExtName)) { |
| 125 |
free(padExtName); |
| 126 |
return GL_TRUE; |
| 127 |
} else { |
| 128 |
free(padExtName); |
| 129 |
return GL_FALSE; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
static const char* EatWhiteSpace(const char *str) |
| 134 |
{ |
| 135 |
for (; *str && (' ' == *str || '\t' == *str || '\n' == *str); str++); |
| 136 |
return str; |
| 137 |
} |
| 138 |
|
| 139 |
static const char* EatNonWhiteSpace(const char *str) |
| 140 |
{ |
| 141 |
for (; *str && (' ' != *str && '\t' != *str && '\n' != *str); str++); |
| 142 |
return str; |
| 143 |
} |
| 144 |
|
| 145 |
#if _MSC_VER > 1500 |
| 146 |
// Suppress VS warning on using strdup |
| 147 |
#pragma warning(disable: 4996) |
| 148 |
#endif |
| 149 |
int glh_init_extensions(const char *origReqExts) |
| 150 |
{ |
| 151 |
// Length of requested extensions string |
| 152 |
size_t reqExtsLen; |
| 153 |
char *reqExts; |
| 154 |
// Ptr for individual extensions within reqExts |
| 155 |
char *reqExt; |
| 156 |
int success = GL_TRUE; |
| 157 |
// build space-padded extension string |
| 158 |
if (NULL == sysExts) { |
| 159 |
const char *extensions = (const char*)glGetString(GL_EXTENSIONS); |
| 160 |
size_t sysExtsLen = strlen(extensions); |
| 161 |
const char *winsys_extensions = ""; |
| 162 |
size_t winsysExtsLen = 0; |
| 163 |
#if defined(WIN32) |
| 164 |
{ |
| 165 |
PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0; |
| 166 |
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB"); |
| 167 |
if(wglGetExtensionsStringARB) |
| 168 |
{ |
| 169 |
winsys_extensions = wglGetExtensionsStringARB(wglGetCurrentDC()); |
| 170 |
winsysExtsLen = strlen(winsys_extensions); |
| 171 |
} |
| 172 |
} |
| 173 |
#elif defined(UNIX) |
| 174 |
{ |
| 175 |
|
| 176 |
winsys_extensions = glXQueryExtensionsString (glXGetCurrentDisplay(),DefaultScreen(glXGetCurrentDisplay())) ; |
| 177 |
winsysExtsLen = strlen (winsys_extensions); |
| 178 |
} |
| 179 |
#endif |
| 180 |
// Add 2 bytes, one for padding space, one for terminating NULL |
| 181 |
sysExts = (char*)malloc(sysExtsLen + winsysExtsLen + 3); |
| 182 |
CHECK_MEMORY(sysExts); |
| 183 |
strcpy(sysExts, extensions); |
| 184 |
sysExts[sysExtsLen] = ' '; |
| 185 |
sysExts[sysExtsLen + 1] = 0; |
| 186 |
strcat(sysExts, winsys_extensions); |
| 187 |
sysExts[sysExtsLen + 1 + winsysExtsLen] = ' '; |
| 188 |
sysExts[sysExtsLen + 1 + winsysExtsLen + 1] = 0; |
| 189 |
} |
| 190 |
|
| 191 |
if (NULL == origReqExts) |
| 192 |
return GL_TRUE; |
| 193 |
reqExts = strdup(origReqExts); |
| 194 |
reqExtsLen = strlen(reqExts); |
| 195 |
if (NULL == unsupportedExts) { |
| 196 |
unsupportedExts = (char*)malloc(reqExtsLen + 2); |
| 197 |
} else if (reqExtsLen > strlen(unsupportedExts)) { |
| 198 |
unsupportedExts = (char*)realloc(unsupportedExts, reqExtsLen + 2); |
| 199 |
} |
| 200 |
CHECK_MEMORY(unsupportedExts); |
| 201 |
*unsupportedExts = 0; |
| 202 |
|
| 203 |
// Parse requested extension list |
| 204 |
for (reqExt = reqExts; |
| 205 |
(reqExt = (char*)EatWhiteSpace(reqExt)) != NULL && *reqExt; |
| 206 |
reqExt = (char*)EatNonWhiteSpace(reqExt)) |
| 207 |
{ |
| 208 |
char *extEnd = (char*)EatNonWhiteSpace(reqExt); |
| 209 |
char saveChar = *extEnd; |
| 210 |
*extEnd = (char)0; |
| 211 |
|
| 212 |
#if (defined(WIN32) || defined(UNIX)) |
| 213 |
if (!ExtensionExists(reqExt, sysExts) || !glh_init_extension(reqExt)) { |
| 214 |
#elif defined(MACOS) |
| 215 |
if (!ExtensionExists(reqExt, sysExts)) { // don't try to get function pointers if on MacOS |
| 216 |
#endif |
| 217 |
// add reqExt to end of unsupportedExts |
| 218 |
strcat(unsupportedExts, reqExt); |
| 219 |
strcat(unsupportedExts, " "); |
| 220 |
success = GL_FALSE; |
| 221 |
} |
| 222 |
*extEnd = saveChar; |
| 223 |
} |
| 224 |
free(reqExts); |
| 225 |
return success; |
| 226 |
} |
| 227 |
#if _MSC_VER > 1500 |
| 228 |
#pragma warning(default: 4996) |
| 229 |
#endif |
| 230 |
|
| 231 |
const char* glh_get_unsupported_extensions() |
| 232 |
{ |
| 233 |
return (const char*)unsupportedExts; |
| 234 |
} |
| 235 |
|
| 236 |
void glh_shutdown_extensions() |
| 237 |
{ |
| 238 |
if (unsupportedExts) |
| 239 |
{ |
| 240 |
free(unsupportedExts); |
| 241 |
unsupportedExts = NULL; |
| 242 |
} |
| 243 |
if (sysExts) |
| 244 |
{ |
| 245 |
free(sysExts); |
| 246 |
sysExts = NULL; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
int glh_extension_supported(const char *extension) |
| 251 |
{ |
| 252 |
static const GLubyte *extensions = NULL; |
| 253 |
const GLubyte *start; |
| 254 |
GLubyte *where, *terminator; |
| 255 |
|
| 256 |
// Extension names should not have spaces. |
| 257 |
where = (GLubyte *) strchr(extension, ' '); |
| 258 |
if (where || *extension == '\0') |
| 259 |
return 0; |
| 260 |
|
| 261 |
if (!extensions) |
| 262 |
extensions = glGetString(GL_EXTENSIONS); |
| 263 |
|
| 264 |
// It takes a bit of care to be fool-proof about parsing the |
| 265 |
// OpenGL extensions string. Don't be fooled by sub-strings, |
| 266 |
// etc. |
| 267 |
start = extensions; |
| 268 |
for (;;) |
| 269 |
{ |
| 270 |
where = (GLubyte *) strstr((const char *) start, extension); |
| 271 |
if (!where) |
| 272 |
break; |
| 273 |
terminator = where + strlen(extension); |
| 274 |
if (where == start || *(where - 1) == ' ') |
| 275 |
{ |
| 276 |
if (*terminator == ' ' || *terminator == '\0') |
| 277 |
{ |
| 278 |
return 1; |
| 279 |
} |
| 280 |
} |
| 281 |
start = terminator; |
| 282 |
} |
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
#else |
| 287 |
int glh_init_extensions(const char *origReqExts); |
| 288 |
const char* glh_get_unsupported_extensions(); |
| 289 |
void glh_shutdown_extensions(); |
| 290 |
int glh_extension_supported(const char *extension); |
| 291 |
#endif /* GLH_EXT_SINGLE_FILE */ |
| 292 |
|
| 293 |
#ifdef __cplusplus |
| 294 |
} |
| 295 |
#endif |
| 296 |
|
| 297 |
#endif /* GLH_EXTENSIONS */ |