x86UNIXGL.client.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 "platformX86UNIX/platformGL.h"
  23. #include "platformX86UNIX/platformX86UNIX.h"
  24. #include "console/console.h"
  25. #include <dlfcn.h>
  26. #include <SDL/SDL.h>
  27. // declare stub functions
  28. #define GL_FUNCTION(fn_return, fn_name, fn_args, fn_value) fn_return stub_##fn_name fn_args{ fn_value }
  29. #include "platform/GLCoreFunc.h"
  30. #include "platform/GLExtFunc.h"
  31. #undef GL_FUNCTION
  32. // point gl function pointers at stub functions
  33. #define GL_FUNCTION(fn_return,fn_name,fn_args, fn_value) fn_return (*fn_name)fn_args = stub_##fn_name;
  34. #include "platform/GLCoreFunc.h"
  35. #include "platform/GLExtFunc.h"
  36. #undef GL_FUNCTION
  37. static void* dlHandle = NULL;
  38. //------------------------------------------------------------------
  39. //bind functions for each function prototype
  40. //------------------------------------------------------------------
  41. //GL_EXT/ARB
  42. enum {
  43. ARB_multitexture = BIT(0),
  44. ARB_texture_compression = BIT(1),
  45. EXT_compiled_vertex_array = BIT(2),
  46. EXT_fog_coord = BIT(3),
  47. EXT_paletted_texture = BIT(4),
  48. NV_vertex_array_range = BIT(5),
  49. EXT_blend_color = BIT(6),
  50. EXT_blend_minmax = BIT(7)
  51. };
  52. //WGL_ARB
  53. enum {
  54. WGL_ARB_extensions_string = BIT(0),
  55. WGL_EXT_swap_control = BIT(1),
  56. WGL_3DFX_gamma_control = BIT(2)
  57. };
  58. static bool isFnOk( const char *name)
  59. {
  60. bool ok = false;
  61. // JMQ: these are specific to torque's d3d->gl wrapper. They are not used under linux.
  62. if (dStrcmp(name, "glAvailableVertexBufferEXT")==0)
  63. ok = true;
  64. else if (dStrcmp(name, "glAllocateVertexBufferEXT")==0)
  65. ok = true;
  66. else if (dStrcmp(name, "glLockVertexBufferEXT")==0)
  67. ok = true;
  68. else if (dStrcmp(name, "glUnlockVertexBufferEXT")==0)
  69. ok = true;
  70. else if (dStrcmp(name, "glSetVertexBufferEXT")==0)
  71. ok = true;
  72. else if (dStrcmp(name, "glOffsetVertexBufferEXT")==0)
  73. ok = true;
  74. else if (dStrcmp(name, "glFillVertexBufferEXT")==0)
  75. ok = true;
  76. else if (dStrcmp(name, "glFreeVertexBufferEXT")==0)
  77. ok = true;
  78. return ok;
  79. }
  80. //------------------------------------------------------------------
  81. //bind functions for each function prototype
  82. //------------------------------------------------------------------
  83. static bool bindGLFunction( void *&fnAddress, const char *name )
  84. {
  85. void* addr = (void*)SDL_GL_GetProcAddress(name);
  86. bool ok = (bool)addr;
  87. if( !ok )
  88. {
  89. if (!isFnOk(name))
  90. Con::errorf(ConsoleLogEntry::General, " Missing OpenGL function '%s'", name);
  91. else
  92. ok = true;
  93. }
  94. else
  95. fnAddress = addr;
  96. return ok;
  97. }
  98. static bool bindEXTFunction( void *&fnAddress, const char *name )
  99. {
  100. void* addr = (void*)SDL_GL_GetProcAddress(name);
  101. if( !addr )
  102. Con::errorf(ConsoleLogEntry::General, " Missing OpenGL extension '%s'", name);
  103. else
  104. fnAddress = addr;
  105. return (addr != NULL);
  106. }
  107. //------------------------------------------------------------------
  108. //binds for each function group
  109. //------------------------------------------------------------------
  110. static bool bindGLFunctions()
  111. {
  112. bool result = true;
  113. #define GL_FUNCTION(fn_return, fn_name, fn_args, fn_value) \
  114. result &= bindGLFunction( *(void**)&fn_name, #fn_name);
  115. #include "platform/GLCoreFunc.h"
  116. #undef GL_FUNCTION
  117. return result;
  118. }
  119. static bool bindEXTFunctions(U32 extMask)
  120. {
  121. bool result = true;
  122. #define GL_GROUP_BEGIN( flag ) \
  123. if( extMask & flag ) {
  124. #define GL_GROUP_END() }
  125. #define GL_FUNCTION(fn_return, fn_name, fn_args, fn_value) \
  126. result &= bindEXTFunction( *(void**)&fn_name, #fn_name);
  127. #include "platform/GLExtFunc.h"
  128. #undef GL_FUNCTION
  129. #undef GL_GROUP_BEGIN
  130. #undef GL_GROUP_END
  131. return result;
  132. }
  133. static void unbindGLFunctions()
  134. {
  135. // point gl function pointers at stub functions
  136. #define GL_FUNCTION(fn_return, fn_name, fn_args, fn_value) fn_name = stub_##fn_name;
  137. #include "platform/GLCoreFunc.h"
  138. #include "platform/GLExtFunc.h"
  139. #undef GL_FUNCTION
  140. }
  141. namespace GLLoader
  142. {
  143. bool OpenGLInit()
  144. {
  145. return OpenGLDLLInit();
  146. }
  147. void OpenGLShutdown()
  148. {
  149. OpenGLDLLShutdown();
  150. }
  151. bool OpenGLDLLInit()
  152. {
  153. OpenGLDLLShutdown();
  154. // load libGL.so
  155. if (SDL_GL_LoadLibrary("libGL.so") == -1 &&
  156. SDL_GL_LoadLibrary("libGL.so.1") == -1)
  157. {
  158. Con::errorf("Error loading GL library: %s", SDL_GetError());
  159. return false;
  160. }
  161. // bind functions
  162. if (!bindGLFunctions())
  163. {
  164. Con::errorf("Error binding GL functions");
  165. OpenGLDLLShutdown();
  166. return false;
  167. }
  168. return true;
  169. }
  170. void OpenGLDLLShutdown()
  171. {
  172. // there is no way to tell SDL to unload the library
  173. if (dlHandle != NULL)
  174. {
  175. dlclose(dlHandle);
  176. dlHandle = NULL;
  177. }
  178. unbindGLFunctions();
  179. }
  180. }
  181. GLState gGLState;
  182. bool gOpenGLDisablePT = false;
  183. bool gOpenGLDisableCVA = false;
  184. bool gOpenGLDisableTEC = false;
  185. bool gOpenGLDisableARBMT = false;
  186. bool gOpenGLDisableFC = false;
  187. bool gOpenGLDisableTCompress = false;
  188. bool gOpenGLNoEnvColor = false;
  189. float gOpenGLGammaCorrection = 0.5;
  190. bool gOpenGLNoDrawArraysAlpha = false;
  191. // JMQTODO: really need a platform-shared version of this nastiness
  192. bool GL_EXT_Init( )
  193. {
  194. // Load extensions...
  195. //
  196. const char* pExtString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
  197. gGLState.primMode = 0;
  198. U32 extBitMask = 0;
  199. // GL_EXT_paletted_texture
  200. if (pExtString && dStrstr(pExtString, (const char*)"GL_EXT_paletted_texture") != NULL)
  201. {
  202. extBitMask |= EXT_paletted_texture;
  203. gGLState.suppPalettedTexture = true;
  204. }
  205. else
  206. gGLState.suppPalettedTexture = false;
  207. // EXT_compiled_vertex_array
  208. if (pExtString && dStrstr(pExtString, (const char*)"GL_EXT_compiled_vertex_array") != NULL)
  209. {
  210. extBitMask |= EXT_compiled_vertex_array;
  211. gGLState.suppLockedArrays = true;
  212. }
  213. else
  214. {
  215. gGLState.suppLockedArrays = false;
  216. }
  217. // ARB_multitexture
  218. if (pExtString && dStrstr(pExtString, (const char*)"GL_ARB_multitexture") != NULL)
  219. {
  220. extBitMask |= ARB_multitexture;
  221. gGLState.suppARBMultitexture = true;
  222. } else {
  223. gGLState.suppARBMultitexture = false;
  224. }
  225. // EXT_blend_color
  226. if(pExtString && dStrstr(pExtString, (const char*)"GL_EXT_blend_color") != NULL)
  227. {
  228. extBitMask |= EXT_blend_color;
  229. gGLState.suppEXTblendcolor = true;
  230. } else {
  231. gGLState.suppEXTblendcolor = false;
  232. }
  233. // EXT_blend_minmax
  234. if(pExtString && dStrstr(pExtString, (const char*)"GL_EXT_blend_minmax") != NULL)
  235. {
  236. extBitMask |= EXT_blend_color;
  237. gGLState.suppEXTblendminmax = true;
  238. } else {
  239. gGLState.suppEXTblendminmax = false;
  240. }
  241. // EXT_fog_coord
  242. if (pExtString && dStrstr(pExtString, (const char*)"GL_EXT_fog_coord") != NULL)
  243. {
  244. extBitMask |= EXT_fog_coord;
  245. gGLState.suppFogCoord = true;
  246. } else {
  247. gGLState.suppFogCoord = false;
  248. }
  249. // EXT_texture_compression_s3tc
  250. if (pExtString && dStrstr(pExtString, (const char*)"GL_EXT_texture_compression_s3tc") != NULL)
  251. gGLState.suppS3TC = true;
  252. else
  253. gGLState.suppS3TC = false;
  254. // ARB_texture_compression
  255. if (pExtString && dStrstr(pExtString, (const char*)"GL_ARB_texture_compression") != NULL)
  256. {
  257. extBitMask |= ARB_texture_compression;
  258. gGLState.suppTextureCompression = true;
  259. } else {
  260. gGLState.suppTextureCompression = false;
  261. }
  262. // NV_vertex_array_range (not on *nix)
  263. gGLState.suppVertexArrayRange = false;
  264. // 3DFX_texture_compression_FXT1
  265. if (pExtString && dStrstr(pExtString, (const char*)"3DFX_texture_compression_FXT1") != NULL)
  266. gGLState.suppFXT1 = true;
  267. else
  268. gGLState.suppFXT1 = false;
  269. if (!bindEXTFunctions(extBitMask))
  270. Con::warnf("You are missing some OpenGL Extensions. You may experience rendering problems.");
  271. // Binary states, i.e., no supporting functions
  272. // EXT_packed_pixels
  273. // EXT_texture_env_combine
  274. //
  275. // dhc note: a number of these can have multiple matching 'versions', private, ext, and arb.
  276. gGLState.suppPackedPixels = pExtString? (dStrstr(pExtString, (const char*)"GL_EXT_packed_pixels") != NULL) : false;
  277. gGLState.suppTextureEnvCombine = pExtString? (dStrstr(pExtString, (const char*)"GL_EXT_texture_env_combine") != NULL) : false;
  278. gGLState.suppEdgeClamp = pExtString? (dStrstr(pExtString, (const char*)"GL_EXT_texture_edge_clamp") != NULL) : false;
  279. gGLState.suppEdgeClamp |= pExtString? (dStrstr(pExtString, (const char*)"GL_SGIS_texture_edge_clamp") != NULL) : false;
  280. gGLState.suppTexEnvAdd = pExtString? (dStrstr(pExtString, (const char*)"GL_ARB_texture_env_add") != NULL) : false;
  281. gGLState.suppTexEnvAdd |= pExtString? (dStrstr(pExtString, (const char*)"GL_EXT_texture_env_add") != NULL) : false;
  282. // Anisotropic filtering
  283. gGLState.suppTexAnisotropic = pExtString? (dStrstr(pExtString, (const char*)"GL_EXT_texture_filter_anisotropic") != NULL) : false;
  284. if (gGLState.suppTexAnisotropic)
  285. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gGLState.maxAnisotropy);
  286. if (gGLState.suppARBMultitexture)
  287. glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &gGLState.maxTextureUnits);
  288. else
  289. gGLState.maxTextureUnits = 1;
  290. // JMQ: vsync/swap interval skipped
  291. gGLState.suppSwapInterval = false;
  292. Con::printf("OpenGL Init: Enabled Extensions");
  293. if (gGLState.suppARBMultitexture) Con::printf(" ARB_multitexture (Max Texture Units: %d)", gGLState.maxTextureUnits);
  294. if (gGLState.suppEXTblendcolor) Con::printf(" EXT_blend_color");
  295. if (gGLState.suppEXTblendminmax) Con::printf(" EXT_blend_minmax");
  296. if (gGLState.suppPalettedTexture) Con::printf(" EXT_paletted_texture");
  297. if (gGLState.suppLockedArrays) Con::printf(" EXT_compiled_vertex_array");
  298. if (gGLState.suppVertexArrayRange) Con::printf(" NV_vertex_array_range");
  299. if (gGLState.suppTextureEnvCombine) Con::printf(" EXT_texture_env_combine");
  300. if (gGLState.suppPackedPixels) Con::printf(" EXT_packed_pixels");
  301. if (gGLState.suppFogCoord) Con::printf(" EXT_fog_coord");
  302. if (gGLState.suppTextureCompression) Con::printf(" ARB_texture_compression");
  303. if (gGLState.suppS3TC) Con::printf(" EXT_texture_compression_s3tc");
  304. if (gGLState.suppFXT1) Con::printf(" 3DFX_texture_compression_FXT1");
  305. if (gGLState.suppTexEnvAdd) Con::printf(" (ARB|EXT)_texture_env_add");
  306. if (gGLState.suppTexAnisotropic) Con::printf(" EXT_texture_filter_anisotropic (Max anisotropy: %f)", gGLState.maxAnisotropy);
  307. if (gGLState.suppSwapInterval) Con::printf(" WGL_EXT_swap_control");
  308. Con::warnf("OpenGL Init: Disabled Extensions");
  309. if (!gGLState.suppARBMultitexture) Con::warnf(" ARB_multitexture");
  310. if (!gGLState.suppEXTblendcolor) Con::warnf(" EXT_blend_color");
  311. if (!gGLState.suppEXTblendminmax) Con::warnf(" EXT_blend_minmax");
  312. if (!gGLState.suppPalettedTexture) Con::warnf(" EXT_paletted_texture");
  313. if (!gGLState.suppLockedArrays) Con::warnf(" EXT_compiled_vertex_array");
  314. if (!gGLState.suppVertexArrayRange) Con::warnf(" NV_vertex_array_range");
  315. if (!gGLState.suppTextureEnvCombine) Con::warnf(" EXT_texture_env_combine");
  316. if (!gGLState.suppPackedPixels) Con::warnf(" EXT_packed_pixels");
  317. if (!gGLState.suppFogCoord) Con::warnf(" EXT_fog_coord");
  318. if (!gGLState.suppTextureCompression) Con::warnf(" ARB_texture_compression");
  319. if (!gGLState.suppS3TC) Con::warnf(" EXT_texture_compression_s3tc");
  320. if (!gGLState.suppFXT1) Con::warnf(" 3DFX_texture_compression_FXT1");
  321. if (!gGLState.suppTexEnvAdd) Con::warnf(" (ARB|EXT)_texture_env_add");
  322. if (!gGLState.suppTexAnisotropic) Con::warnf(" EXT_texture_filter_anisotropic");
  323. if (!gGLState.suppSwapInterval) Con::warnf(" WGL_EXT_swap_control");
  324. Con::printf(" ");
  325. // Set some console variables:
  326. Con::setBoolVariable( "$FogCoordSupported", gGLState.suppFogCoord );
  327. Con::setBoolVariable( "$TextureCompressionSupported", gGLState.suppTextureCompression );
  328. Con::setBoolVariable( "$AnisotropySupported", gGLState.suppTexAnisotropic );
  329. Con::setBoolVariable( "$PalettedTextureSupported", gGLState.suppPalettedTexture );
  330. Con::setBoolVariable( "$SwapIntervalSupported", gGLState.suppSwapInterval );
  331. if (!gGLState.suppPalettedTexture && Con::getBoolVariable("$pref::OpenGL::forcePalettedTexture",false))
  332. {
  333. Con::setBoolVariable("$pref::OpenGL::forcePalettedTexture", false);
  334. Con::setBoolVariable("$pref::OpenGL::force16BitTexture", true);
  335. }
  336. return true;
  337. }