glxBind.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "glx.h"
  23. #include "util/utilAssert.h"
  24. #include "util/utilStr.h"
  25. #include "util/utilString.h"
  26. #include "util/utilMemory.h"
  27. #include "os/osLog.h"
  28. #include "os/osDlibrary.h"
  29. //-----------------------------------------------------------------------------
  30. // Instantiation of GL function pointers in global namespace.
  31. #define GL_GROUP_BEGIN(name)
  32. #define GL_FUNCTION(name, type, args) type (XGL_DLL *name) args;
  33. #define GL_GROUP_END()
  34. #include "../generated/glcfn.h"
  35. #undef GL_GROUP_BEGIN
  36. #undef GL_FUNCTION
  37. #undef GL_GROUP_END
  38. namespace GL
  39. {
  40. using namespace Torque;
  41. bool bindFunction(DLibrary* dll,void *&fnAddress, const char *name);
  42. bool hasExtension(const char *name,const char* extensions);
  43. bool hasVersion(const char *name,const char* prefix,int major,int minor);
  44. //-----------------------------------------------------------------------------
  45. // Instantiation of GLX function pointers.
  46. #define GL_GROUP_BEGIN(name)
  47. #define GL_FUNCTION(name, type, args) type (XGL_DLL *name) args;
  48. #define GL_GROUP_END()
  49. #include "../generated/glxfn.h"
  50. #undef GL_GROUP_BEGIN
  51. #undef GL_FUNCTION
  52. #undef GL_GROUP_END
  53. //-----------------------------------------------------------------------------
  54. bool bindFunction(DLibrary* dll,GLFunction (*gproc)(const GLubyte*),
  55. void *&fnAddress, const char *name)
  56. {
  57. // Use the getProcAddress if we have it.
  58. fnAddress = gproc? (void*)(*gproc)((const GLubyte*)name): dll->bind(name);
  59. return fnAddress != 0;
  60. }
  61. //-----------------------------------------------------------------------------
  62. bool gglBindCoreFunctions(DLibrary* dll,GLXExtensionPtrs* glp)
  63. {
  64. bool bound = true;
  65. // Bind static functions which are quarenteed to be part of the
  66. // OpenGL library. In this case, OpenGL 1.0 and GLX 1.0 functions
  67. #define GL_GROUP_BEGIN(name)
  68. #define GL_GROUP_END()
  69. #define GL_FUNCTION(fn_name, fn_return, fn_args) \
  70. bound &= bindFunction(dll,*(void**)&fn_name, #fn_name);
  71. #include "../generated/glcfn.h"
  72. #include "../generated/glxfn.h"
  73. #undef GL_FUNCTION
  74. #undef GL_GROUP_BEGIN
  75. #undef GL_GROUP_END
  76. // Check for the getProcAddress first, otherwise we'll expect everything
  77. // to be in the DLL.
  78. memset(glp,0,sizeof(GLXExtensionPtrs));
  79. glp->_glXGetProcAddressARB = (GLFunction (*)(const GLubyte*))dll->bind("glXGetProcAddressARB");
  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(dll,glp->_glXGetProcAddressARB,*(void**)&glp->_##fn_name, #fn_name);
  85. #define GL_GROUP_END()
  86. #include "../generated/glefn.h"
  87. #include "../generated/glxefn.h"
  88. #undef GL_FUNCTION
  89. #undef GL_GROUP_BEGIN
  90. #undef GL_GROUP_END
  91. return bound;
  92. }
  93. //-----------------------------------------------------------------------------
  94. void gglBindGLX(::Display* display,int screen,GLXExtensionFlags* glx)
  95. {
  96. // Check GLX version and glx extensions
  97. int glxMajor,glxMinor;
  98. glXQueryVersion(display,&glxMajor,&glxMinor);
  99. const char* glxExtensions = glXQueryExtensionsString(display,screen);
  100. static LogCategory log("/Gfx/Device/GL");
  101. log.print(format("GLX Version: %d.%d",glxMajor,glxMinor));
  102. Assert(glxMajor == 1 && glxMinor >= 1,"GLXBind: Need GLX version 1.1 or greater");
  103. #define GL_GROUP_BEGIN(name) \
  104. glx->has_##name = hasVersion(#name,"GLX_VERSION",glxMajor,glxMinor) || \
  105. hasExtension(#name,glxExtensions);
  106. #define GL_FUNCTION(fn_name, fn_return, fn_args)
  107. #define GL_GROUP_END()
  108. #include "../generated/glxefn.h"
  109. #undef GL_FUNCTION
  110. #undef GL_GROUP_BEGIN
  111. #undef GL_GROUP_END
  112. }
  113. //-----------------------------------------------------------------------------
  114. bool gglBindExtensions(GLExtensionFlags* gl)
  115. {
  116. memset(gl,0,sizeof(GLExtensionFlags));
  117. // Get GL version and extensions
  118. const char* glExtensions = (const char*)glGetString(GL_EXTENSIONS);
  119. const char* glVersion = (const char*) glGetString(GL_VERSION);
  120. if (!glExtensions || !glVersion)
  121. return false;
  122. // Parse the GL version string "major.minor"
  123. const char *itr = glVersion;
  124. int glMajor = atoi(itr);
  125. while (isDigit(*itr))
  126. *itr++;
  127. int glMinor = atoi(++itr);
  128. // Check which extensions are available on the active context.
  129. // GL and GLX versions ubove 1.0 are also tested here.
  130. #define GL_GROUP_BEGIN(name) \
  131. gl->has_##name = hasVersion(#name,"GL_VERSION",glMajor,glMinor) || \
  132. hasExtension(#name,glExtensions);
  133. #define GL_FUNCTION(fn_name, fn_return, fn_args)
  134. #define GL_GROUP_END()
  135. #include "../generated/glefn.h"
  136. #undef GL_FUNCTION
  137. #undef GL_GROUP_BEGIN
  138. #undef GL_GROUP_END
  139. gl->bound = true;
  140. return true;
  141. }
  142. } // Namespace