aglBind.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <err.h>
  23. #include "agl.h"
  24. #include "platform/platform.h"
  25. //-----------------------------------------------------------------------------
  26. // Instantiation of function pointers.
  27. #define GL_GROUP_BEGIN(name)
  28. #define GL_FUNCTION(name, type, args) type (XGL_DLL *name) args;
  29. #define GL_GROUP_END()
  30. #include "../generated/glcfn.h"
  31. #undef GL_GROUP_BEGIN
  32. #undef GL_FUNCTION
  33. #undef GL_GROUP_END
  34. #define EXECUTE_ONLY_ONCE() \
  35. static bool _doneOnce = false; \
  36. if(_doneOnce) return; \
  37. _doneOnce = true;
  38. namespace GL
  39. {
  40. //-----------------------------------------------------------------------------
  41. // The OpenGL Bundle is shared by all the devices.
  42. static CFBundleRef _openglFrameworkRef;
  43. // Unlike WGL, AGL does not require a set of function pointers per
  44. // context pixel format. All functions in the DLL are bound in a single
  45. // table and shared by all contexts.
  46. static AGLExtensionPtrs _LibraryFunctions;
  47. static AGLExtensionFlags _ExtensionFlags;
  48. bool hasExtension(const char *name,const char* extensions);
  49. bool hasVersion(const char *name,const char* prefix,int major,int minor);
  50. //-----------------------------------------------------------------------------
  51. void* MacGetProcAddress(CFBundleRef bundle,char* name)
  52. {
  53. CFStringRef cfName = CFStringCreateWithCString(kCFAllocatorDefault, name, CFStringGetSystemEncoding());
  54. void* ret = CFBundleGetFunctionPointerForName(bundle, cfName);
  55. CFRelease(cfName);
  56. return ret;
  57. }
  58. //-----------------------------------------------------------------------------
  59. bool bindFunction(CFBundleRef bundle,void *&fnAddress, const char *name)
  60. {
  61. CFStringRef cfName = CFStringCreateWithCString(0,name,CFStringGetSystemEncoding());
  62. fnAddress = CFBundleGetFunctionPointerForName(bundle, cfName);
  63. CFRelease(cfName);
  64. return fnAddress != 0;
  65. }
  66. //-----------------------------------------------------------------------------
  67. bool gglBindCoreFunctions(CFBundleRef bundle,AGLExtensionPtrs* glp)
  68. {
  69. bool bound = true;
  70. // Bind static functions which are quarenteed to be part of the
  71. // OpenGL library. In this case, OpenGL 1.0 and GLX 1.0 functions
  72. #define GL_GROUP_BEGIN(name)
  73. #define GL_GROUP_END()
  74. #define GL_FUNCTION(fn_name, fn_return, fn_args) \
  75. bound &= bindFunction(bundle,*(void**)&fn_name, #fn_name);
  76. #include "../generated/glcfn.h"
  77. #undef GL_FUNCTION
  78. #undef GL_GROUP_BEGIN
  79. #undef GL_GROUP_END
  80. // Try and bind all known extension functions. We'll check later to
  81. // see which ones are actually valid for a context.
  82. #define GL_GROUP_BEGIN(name)
  83. #define GL_FUNCTION(fn_name, fn_return, fn_args) \
  84. bindFunction(bundle,*(void**)&glp->_##fn_name, #fn_name);
  85. #define GL_GROUP_END()
  86. #include "../generated/glefn.h"
  87. #undef GL_FUNCTION
  88. #undef GL_GROUP_BEGIN
  89. #undef GL_GROUP_END
  90. return bound;
  91. }
  92. //-----------------------------------------------------------------------------
  93. bool gglBindExtensions(GLExtensionFlags* gl)
  94. {
  95. dMemset(gl,0,sizeof(GLExtensionFlags));
  96. // Get GL version and extensions
  97. const char* glExtensions = (const char*)glGetString(GL_EXTENSIONS);
  98. const char* glVersion = (const char*) glGetString(GL_VERSION);
  99. if (!glExtensions || !glVersion)
  100. return false;
  101. // Parse the GL version string "major.minor"
  102. const char *itr = glVersion;
  103. int glMajor = atoi(itr);
  104. while (isdigit(*itr))
  105. *itr++;
  106. int glMinor = atoi(++itr);
  107. // Check which extensions are available on the active context.
  108. // GL and GLX versions ubove 1.0 are also tested here.
  109. #define GL_GROUP_BEGIN(name) \
  110. gl->has_##name = hasVersion(#name,"GL_VERSION",glMajor,glMinor) || \
  111. hasExtension(#name,glExtensions);
  112. #define GL_FUNCTION(fn_name, fn_return, fn_args)
  113. #define GL_GROUP_END()
  114. #include "../generated/glefn.h"
  115. #undef GL_FUNCTION
  116. #undef GL_GROUP_BEGIN
  117. #undef GL_GROUP_END
  118. gl->bound = true;
  119. return true;
  120. }
  121. #define _hasGLXExtension(display,name) (display->glx.has_##name)
  122. //-----------------------------------------------------------------------------
  123. void gglPerformBinds()
  124. {
  125. // Some of the following code is copied from the Apple Opengl Documentation.
  126. // Load and bind OpenGL bundle functions
  127. if (!_openglFrameworkRef) {
  128. // Load OpenGL.framework
  129. _openglFrameworkRef = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
  130. if (!_openglFrameworkRef) {
  131. warn("Could not create OpenGL Framework bundle");
  132. return;
  133. }
  134. if (!CFBundleLoadExecutable(_openglFrameworkRef)) {
  135. warn("Could not load MachO executable");
  136. return;
  137. }
  138. // Bind our functions.
  139. if (!gglBindCoreFunctions(_openglFrameworkRef, &_LibraryFunctions)) {
  140. warn("GLDevice: Failed to bind all core functions");
  141. return;
  142. }
  143. // Save the pointer to the set of opengl functions
  144. _GGLptr = &_LibraryFunctions;
  145. }
  146. }
  147. //-----------------------------------------------------------------------------
  148. void gglPerformExtensionBinds(void *context)
  149. {
  150. // we don't care about the passed context when binding the opengl functions,
  151. // we only care about the current opengl context.
  152. if( !_openglFrameworkRef || !_GGLptr )
  153. {
  154. gglPerformBinds();
  155. }
  156. gglBindExtensions( &_ExtensionFlags );
  157. _GGLflag = &_ExtensionFlags;
  158. }
  159. } // Namespace