glx_obs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stddef.h>
  4. #include "glx_obs.h"
  5. #if defined(__APPLE__)
  6. #include <dlfcn.h>
  7. static void* AppleGLGetProcAddress (const char *name)
  8. {
  9. static void* image = NULL;
  10. if (image == NULL)
  11. image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
  12. return (image ? dlsym(image, name) : NULL);
  13. }
  14. #endif /* __APPLE__ */
  15. #if defined(__sgi) || defined (__sun)
  16. #include <dlfcn.h>
  17. #include <stdio.h>
  18. static void* SunGetProcAddress (const GLubyte* name)
  19. {
  20. static void* h = NULL;
  21. static void* gpa;
  22. if (h == NULL)
  23. {
  24. if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL;
  25. gpa = dlsym(h, "glXGetProcAddress");
  26. }
  27. if (gpa != NULL)
  28. return ((void*(*)(const GLubyte*))gpa)(name);
  29. else
  30. return dlsym(h, (const char*)name);
  31. }
  32. #endif /* __sgi || __sun */
  33. #if defined(_WIN32)
  34. #ifdef _MSC_VER
  35. #pragma warning(disable: 4055)
  36. #pragma warning(disable: 4054)
  37. #pragma warning(disable: 4996)
  38. #endif
  39. static int TestPointer(const PROC pTest)
  40. {
  41. ptrdiff_t iTest;
  42. if(!pTest) return 0;
  43. iTest = (ptrdiff_t)pTest;
  44. if(iTest == 1 || iTest == 2 || iTest == 3 || iTest == -1) return 0;
  45. return 1;
  46. }
  47. static PROC WinGetProcAddress(const char *name)
  48. {
  49. HMODULE glMod = NULL;
  50. PROC pFunc = wglGetProcAddress((LPCSTR)name);
  51. if(TestPointer(pFunc))
  52. {
  53. return pFunc;
  54. }
  55. glMod = GetModuleHandleA("OpenGL32.dll");
  56. return (PROC)GetProcAddress(glMod, (LPCSTR)name);
  57. }
  58. #define IntGetProcAddress(name) WinGetProcAddress(name)
  59. #else
  60. #if defined(__APPLE__)
  61. #define IntGetProcAddress(name) AppleGLGetProcAddress(name)
  62. #else
  63. #if defined(__sgi) || defined(__sun)
  64. #define IntGetProcAddress(name) SunGetProcAddress(name)
  65. #else /* GLX */
  66. #include <GL/glx.h>
  67. #define IntGetProcAddress(name) (*glXGetProcAddressARB)((const GLubyte*)name)
  68. #endif
  69. #endif
  70. #endif
  71. int glx_ext_ARB_create_context = glx_LOAD_FAILED;
  72. int glx_ext_ARB_create_context_profile = glx_LOAD_FAILED;
  73. int glx_ext_ARB_create_context_robustness = glx_LOAD_FAILED;
  74. int glx_ext_ARB_fbconfig_float = glx_LOAD_FAILED;
  75. int glx_ext_ARB_framebuffer_sRGB = glx_LOAD_FAILED;
  76. int glx_ext_ARB_multisample = glx_LOAD_FAILED;
  77. int glx_ext_EXT_create_context_es2_profile = glx_LOAD_FAILED;
  78. int glx_ext_EXT_fbconfig_packed_float = glx_LOAD_FAILED;
  79. int glx_ext_EXT_framebuffer_sRGB = glx_LOAD_FAILED;
  80. int glx_ext_EXT_import_context = glx_LOAD_FAILED;
  81. int glx_ext_EXT_swap_control = glx_LOAD_FAILED;
  82. int glx_ext_EXT_swap_control_tear = glx_LOAD_FAILED;
  83. GLXContext (CODEGEN_FUNCPTR *_ptrc_glXCreateContextAttribsARB)(Display *, GLXFBConfig, GLXContext, Bool, const int *) = NULL;
  84. static int Load_ARB_create_context()
  85. {
  86. int numFailed = 0;
  87. _ptrc_glXCreateContextAttribsARB = (GLXContext (CODEGEN_FUNCPTR *)(Display *, GLXFBConfig, GLXContext, Bool, const int *))IntGetProcAddress("glXCreateContextAttribsARB");
  88. if(!_ptrc_glXCreateContextAttribsARB) numFailed++;
  89. return numFailed;
  90. }
  91. void (CODEGEN_FUNCPTR *_ptrc_glXFreeContextEXT)(Display *, GLXContext) = NULL;
  92. GLXContextID (CODEGEN_FUNCPTR *_ptrc_glXGetContextIDEXT)(const GLXContext) = NULL;
  93. Display * (CODEGEN_FUNCPTR *_ptrc_glXGetCurrentDisplayEXT)() = NULL;
  94. GLXContext (CODEGEN_FUNCPTR *_ptrc_glXImportContextEXT)(Display *, GLXContextID) = NULL;
  95. int (CODEGEN_FUNCPTR *_ptrc_glXQueryContextInfoEXT)(Display *, GLXContext, int, int *) = NULL;
  96. static int Load_EXT_import_context()
  97. {
  98. int numFailed = 0;
  99. _ptrc_glXFreeContextEXT = (void (CODEGEN_FUNCPTR *)(Display *, GLXContext))IntGetProcAddress("glXFreeContextEXT");
  100. if(!_ptrc_glXFreeContextEXT) numFailed++;
  101. _ptrc_glXGetContextIDEXT = (GLXContextID (CODEGEN_FUNCPTR *)(const GLXContext))IntGetProcAddress("glXGetContextIDEXT");
  102. if(!_ptrc_glXGetContextIDEXT) numFailed++;
  103. _ptrc_glXGetCurrentDisplayEXT = (Display * (CODEGEN_FUNCPTR *)())IntGetProcAddress("glXGetCurrentDisplayEXT");
  104. if(!_ptrc_glXGetCurrentDisplayEXT) numFailed++;
  105. _ptrc_glXImportContextEXT = (GLXContext (CODEGEN_FUNCPTR *)(Display *, GLXContextID))IntGetProcAddress("glXImportContextEXT");
  106. if(!_ptrc_glXImportContextEXT) numFailed++;
  107. _ptrc_glXQueryContextInfoEXT = (int (CODEGEN_FUNCPTR *)(Display *, GLXContext, int, int *))IntGetProcAddress("glXQueryContextInfoEXT");
  108. if(!_ptrc_glXQueryContextInfoEXT) numFailed++;
  109. return numFailed;
  110. }
  111. void (CODEGEN_FUNCPTR *_ptrc_glXSwapIntervalEXT)(Display *, GLXDrawable, int) = NULL;
  112. static int Load_EXT_swap_control()
  113. {
  114. int numFailed = 0;
  115. _ptrc_glXSwapIntervalEXT = (void (CODEGEN_FUNCPTR *)(Display *, GLXDrawable, int))IntGetProcAddress("glXSwapIntervalEXT");
  116. if(!_ptrc_glXSwapIntervalEXT) numFailed++;
  117. return numFailed;
  118. }
  119. typedef int (*PFN_LOADFUNCPOINTERS)();
  120. typedef struct glx_StrToExtMap_s
  121. {
  122. char *extensionName;
  123. int *extensionVariable;
  124. PFN_LOADFUNCPOINTERS LoadExtension;
  125. } glx_StrToExtMap;
  126. static glx_StrToExtMap ExtensionMap[12] = {
  127. {"GLX_ARB_create_context", &glx_ext_ARB_create_context, Load_ARB_create_context},
  128. {"GLX_ARB_create_context_profile", &glx_ext_ARB_create_context_profile, NULL},
  129. {"GLX_ARB_create_context_robustness", &glx_ext_ARB_create_context_robustness, NULL},
  130. {"GLX_ARB_fbconfig_float", &glx_ext_ARB_fbconfig_float, NULL},
  131. {"GLX_ARB_framebuffer_sRGB", &glx_ext_ARB_framebuffer_sRGB, NULL},
  132. {"GLX_ARB_multisample", &glx_ext_ARB_multisample, NULL},
  133. {"GLX_EXT_create_context_es2_profile", &glx_ext_EXT_create_context_es2_profile, NULL},
  134. {"GLX_EXT_fbconfig_packed_float", &glx_ext_EXT_fbconfig_packed_float, NULL},
  135. {"GLX_EXT_framebuffer_sRGB", &glx_ext_EXT_framebuffer_sRGB, NULL},
  136. {"GLX_EXT_import_context", &glx_ext_EXT_import_context, Load_EXT_import_context},
  137. {"GLX_EXT_swap_control", &glx_ext_EXT_swap_control, Load_EXT_swap_control},
  138. {"GLX_EXT_swap_control_tear", &glx_ext_EXT_swap_control_tear, NULL},
  139. };
  140. static int g_extensionMapSize = 12;
  141. static glx_StrToExtMap *FindExtEntry(const char *extensionName)
  142. {
  143. int loop;
  144. glx_StrToExtMap *currLoc = ExtensionMap;
  145. for(loop = 0; loop < g_extensionMapSize; ++loop, ++currLoc)
  146. {
  147. if(strcmp(extensionName, currLoc->extensionName) == 0)
  148. return currLoc;
  149. }
  150. return NULL;
  151. }
  152. static void ClearExtensionVars()
  153. {
  154. glx_ext_ARB_create_context = glx_LOAD_FAILED;
  155. glx_ext_ARB_create_context_profile = glx_LOAD_FAILED;
  156. glx_ext_ARB_create_context_robustness = glx_LOAD_FAILED;
  157. glx_ext_ARB_fbconfig_float = glx_LOAD_FAILED;
  158. glx_ext_ARB_framebuffer_sRGB = glx_LOAD_FAILED;
  159. glx_ext_ARB_multisample = glx_LOAD_FAILED;
  160. glx_ext_EXT_create_context_es2_profile = glx_LOAD_FAILED;
  161. glx_ext_EXT_fbconfig_packed_float = glx_LOAD_FAILED;
  162. glx_ext_EXT_framebuffer_sRGB = glx_LOAD_FAILED;
  163. glx_ext_EXT_import_context = glx_LOAD_FAILED;
  164. glx_ext_EXT_swap_control = glx_LOAD_FAILED;
  165. glx_ext_EXT_swap_control_tear = glx_LOAD_FAILED;
  166. }
  167. static void LoadExtByName(const char *extensionName)
  168. {
  169. glx_StrToExtMap *entry = NULL;
  170. entry = FindExtEntry(extensionName);
  171. if(entry)
  172. {
  173. if(entry->LoadExtension)
  174. {
  175. int numFailed = entry->LoadExtension();
  176. if(numFailed == 0)
  177. {
  178. *(entry->extensionVariable) = glx_LOAD_SUCCEEDED;
  179. }
  180. else
  181. {
  182. *(entry->extensionVariable) = glx_LOAD_SUCCEEDED + numFailed;
  183. }
  184. }
  185. else
  186. {
  187. *(entry->extensionVariable) = glx_LOAD_SUCCEEDED;
  188. }
  189. }
  190. }
  191. static void ProcExtsFromExtString(const char *strExtList)
  192. {
  193. size_t iExtListLen = strlen(strExtList);
  194. const char *strExtListEnd = strExtList + iExtListLen;
  195. const char *strCurrPos = strExtList;
  196. char strWorkBuff[256];
  197. while(*strCurrPos)
  198. {
  199. /*Get the extension at our position.*/
  200. int iStrLen = 0;
  201. const char *strEndStr = strchr(strCurrPos, ' ');
  202. int iStop = 0;
  203. if(strEndStr == NULL)
  204. {
  205. strEndStr = strExtListEnd;
  206. iStop = 1;
  207. }
  208. iStrLen = (int)((ptrdiff_t)strEndStr - (ptrdiff_t)strCurrPos);
  209. if(iStrLen > 255)
  210. return;
  211. strncpy(strWorkBuff, strCurrPos, iStrLen);
  212. strWorkBuff[iStrLen] = '\0';
  213. LoadExtByName(strWorkBuff);
  214. strCurrPos = strEndStr + 1;
  215. if(iStop) break;
  216. }
  217. }
  218. int glx_LoadFunctions(Display *display, int screen)
  219. {
  220. ClearExtensionVars();
  221. ProcExtsFromExtString((const char *)glXQueryExtensionsString(display, screen));
  222. return glx_LOAD_SUCCEEDED;
  223. }