wglBind.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "wgl.h"
  23. #include "core/strings/stringFunctions.h"
  24. #include "console/console.h"
  25. //-----------------------------------------------------------------------------
  26. // Instantiation of GL function pointers in global namespace.
  27. #define GL_GROUP_BEGIN(name)
  28. #define GL_FUNCTION(name, type, args) type (__stdcall *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. namespace GL
  35. {
  36. bool bindFunction(DLibrary* dll,void *&fnAddress, const char *name);
  37. bool hasExtension(const char *name,const char* extensions);
  38. bool hasVersion(const char *name,const char* prefix,int major,int minor);
  39. //-----------------------------------------------------------------------------
  40. // Instantiation of WGL function pointers. These are in the GL namespace to
  41. // avoid problems with definitions in windows.h
  42. #define GL_GROUP_BEGIN(name)
  43. #define GL_FUNCTION(name, type, args) type (__stdcall *name) args;
  44. #define GL_GROUP_END()
  45. #include "../generated/wglfn.h"
  46. #undef GL_GROUP_BEGIN
  47. #undef GL_FUNCTION
  48. #undef GL_GROUP_END
  49. //-----------------------------------------------------------------------------
  50. static bool bindExtension(void *&fnAddress, const char *name)
  51. {
  52. fnAddress = (void*)wglGetProcAddress(name);
  53. if (!fnAddress)
  54. Con::printf("GLExtensions: Extension bind failed for %s",name);
  55. return fnAddress != 0;
  56. }
  57. //-----------------------------------------------------------------------------
  58. bool gglBindCoreFunctions(DLibrary* dll)
  59. {
  60. bool bound = true;
  61. // Bind static functions which are quarenteed to be part of the
  62. // OpenGL library. In this case, OpenGL 1.0 and WGL functions
  63. #define GL_GROUP_BEGIN(name)
  64. #define GL_GROUP_END()
  65. #define GL_FUNCTION(fn_name, fn_return, fn_args) \
  66. bound &= bindFunction(dll, *(void**)&fn_name, #fn_name);
  67. #include "../generated/glcfn.h"
  68. #include "../generated/wglfn.h"
  69. #undef GL_FUNCTION
  70. #undef GL_GROUP_BEGIN
  71. #undef GL_GROUP_END
  72. return bound;
  73. }
  74. bool gglBindExtensions(DLibrary* dll,WGLExtensionPtrs* glp,WGLExtensionFlags* glf,HDC hDC)
  75. {
  76. dMemset(glp,0,sizeof(WGLExtensionPtrs));
  77. dMemset(glf,0,sizeof(WGLExtensionFlags));
  78. // Get WGL extensions, there is no version
  79. const char* wgl = 0;
  80. if (bindExtension(*(void**)&glp->_wglGetExtensionsStringARB,"wglGetExtensionsStringARB"))
  81. wgl = glp->_wglGetExtensionsStringARB(hDC);
  82. else
  83. if (bindExtension(*(void**)&glp->_wglGetExtensionsStringEXT,"wglGetExtensionsStringEXT"))
  84. wgl = glp->_wglGetExtensionsStringEXT();
  85. // Get OpenGL version and extensions
  86. const char* glExtensions = (const char*)glGetString(GL_EXTENSIONS);
  87. const char* glVersion = (const char*) glGetString(GL_VERSION);
  88. if (!glExtensions || !glVersion)
  89. return false;
  90. // Parse the version string major.minor
  91. const char *itr = glVersion;
  92. int glMajor = dAtoi(itr);
  93. while (dIsdigit(*itr))
  94. *itr++;
  95. int glMinor = dAtoi(++itr);
  96. // Test for, and bind, all known extensions. GL and GLX versions ubove 1.0
  97. // are also treated as extensions and bound here.
  98. #define GL_GROUP_BEGIN(name) \
  99. if (hasVersion(#name,"GL_VERSION",glMajor,glMinor) || \
  100. hasExtension(#name,glExtensions) || \
  101. hasExtension(#name,wgl)) \
  102. { \
  103. glf->has_##name = true;
  104. #define GL_FUNCTION(fn_name, fn_return, fn_args) \
  105. bindExtension(*(void**)&glp->_##fn_name, #fn_name);
  106. #define GL_GROUP_END() \
  107. }
  108. #include "../generated/glefn.h"
  109. #include "../generated/wglefn.h"
  110. #undef GL_FUNCTION
  111. #undef GL_GROUP_BEGIN
  112. #undef GL_GROUP_END
  113. glf->bound = glp->bound = true;
  114. return true;
  115. }
  116. static DLibraryRef _hGL = NULL;
  117. void gglPerformBinds()
  118. {
  119. if(!_hGL)
  120. {
  121. _hGL = OsLoadLibrary("opengl32.dll");
  122. gglBindCoreFunctions(_hGL);
  123. }
  124. }
  125. void gglPerformExtensionBinds(void *context)
  126. {
  127. if(!_hGL)
  128. {
  129. _hGL = OsLoadLibrary("opengl32.dll");
  130. }
  131. if(!_GGLptr)
  132. {
  133. static WGLExtensionPtrs ptrs;
  134. static WGLExtensionFlags flags;
  135. _GGLptr = &ptrs;
  136. _GGLflag = &flags;
  137. }
  138. gglBindExtensions(_hGL, (WGLExtensionPtrs*)_GGLptr, (WGLExtensionFlags*)_GGLflag, (HDC)context);
  139. }
  140. } // Namespace