POSIXGL.client.cpp 14 KB

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