x86UNIXOGLVideo.client.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 "console/console.h"
  23. #include "platform/event.h"
  24. #include "platform/gameInterface.h"
  25. #include "platformX86UNIX/platformX86UNIX.h"
  26. #include "platformX86UNIX/platformGL.h"
  27. #include "platformX86UNIX/x86UNIXOGLVideo.h"
  28. #include "platformX86UNIX/x86UNIXState.h"
  29. #include <SDL/SDL.h>
  30. #include <SDL/SDL_syswm.h>
  31. #include <SDL/SDL_version.h>
  32. //------------------------------------------------------------------------------
  33. bool InitOpenGL()
  34. {
  35. DisplayDevice::init();
  36. // Get the video settings from the prefs:
  37. const char* resString = Con::getVariable( "$pref::Video::resolution" );
  38. char* tempBuf = new char[dStrlen( resString ) + 1];
  39. dStrcpy( tempBuf, resString );
  40. char* temp = dStrtok( tempBuf, " x\0" );
  41. U32 width = ( temp ? dAtoi( temp ) : 800 );
  42. temp = dStrtok( NULL, " x\0" );
  43. U32 height = ( temp ? dAtoi( temp ) : 600 );
  44. temp = dStrtok( NULL, "\0" );
  45. U32 bpp = ( temp ? dAtoi( temp ) : 16 );
  46. delete [] tempBuf;
  47. bool fullScreen = Con::getBoolVariable( "$pref::Video::fullScreen" );
  48. // the only supported video device in unix is OpenGL
  49. if ( !Video::setDevice( "OpenGL", width, height, bpp, fullScreen ) )
  50. {
  51. Con::errorf("Unable to create default OpenGL mode: %d %d %d %d",
  52. width, height, bpp, fullScreen);
  53. // if we can't create the default, attempt to create a "safe" window
  54. if ( !Video::setDevice( "OpenGL", 640, 480, 16, true ) )
  55. {
  56. DisplayErrorAlert("Could not find a compatible OpenGL display " \
  57. "resolution. Please check your driver configuration.");
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. //------------------------------------------------------------------------------
  64. bool OpenGLDevice::smCanSwitchBitDepth = false;
  65. //------------------------------------------------------------------------------
  66. OpenGLDevice::OpenGLDevice()
  67. {
  68. initDevice();
  69. }
  70. //------------------------------------------------------------------------------
  71. OpenGLDevice::~OpenGLDevice()
  72. {
  73. }
  74. //------------------------------------------------------------------------------
  75. void OpenGLDevice::addResolution(S32 width, S32 height, bool check)
  76. {
  77. Point2I desktopSize = x86UNIXState->getDesktopSize();
  78. U32 desktopBpp = x86UNIXState->getDesktopBpp();
  79. // don't allow any resolution under this size
  80. if (width < 640 || height < 480)
  81. return;
  82. if (check)
  83. {
  84. // don't allow resolutions that exceed the current desktop size
  85. if (width > desktopSize.x || height > desktopSize.y)
  86. return;
  87. }
  88. if (smCanSwitchBitDepth)
  89. {
  90. // add both 16 and 32 bit resolutions
  91. mResolutionList.push_back(Resolution(width, height, 16));
  92. mResolutionList.push_back(Resolution(width, height, 32));
  93. }
  94. else
  95. {
  96. // add just the desktop resolution
  97. mResolutionList.push_back(Resolution(width, height, desktopBpp));
  98. }
  99. }
  100. //------------------------------------------------------------------------------
  101. void OpenGLDevice::initDevice()
  102. {
  103. mDeviceName = "OpenGL";
  104. mFullScreenOnly = false;
  105. }
  106. //------------------------------------------------------------------------------
  107. void OpenGLDevice::loadResolutions()
  108. {
  109. mResolutionList.clear();
  110. // X cannot switch bit depths on the fly. In case this feature is
  111. // implemented someday, calling this function will let you take
  112. // advantage of it
  113. if (Con::getBoolVariable("$pref::Unix::CanSwitchBitDepth"))
  114. smCanSwitchBitDepth = true;
  115. // add some default resolutions
  116. addResolution(640, 480);
  117. addResolution(800, 600);
  118. addResolution(1024, 768);
  119. addResolution(1152, 864);
  120. addResolution(1280, 1024);
  121. addResolution(1600, 1200);
  122. // specifying full screen should give us the resolutions that the
  123. // X server allows
  124. SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
  125. if (modes &&
  126. (modes != (SDL_Rect **)-1))
  127. {
  128. for (int i = 0; modes[i] != NULL; ++i)
  129. {
  130. // do we already have this mode?
  131. bool found = false;
  132. for (Vector<Resolution>::iterator iter = mResolutionList.begin();
  133. iter != mResolutionList.end();
  134. ++iter)
  135. {
  136. if (iter->w == modes[i]->w && iter->h == modes[i]->h)
  137. {
  138. found = true;
  139. break;
  140. }
  141. }
  142. if (!found)
  143. // don't check these resolutions because they should be OK
  144. // (and checking might drop resolutions that are higher than the
  145. // current desktop bpp)
  146. addResolution(modes[i]->w, modes[i]->h, false);
  147. }
  148. }
  149. }
  150. //------------------------------------------------------------------------------
  151. bool OpenGLDevice::activate( U32 width, U32 height, U32 bpp, bool fullScreen )
  152. {
  153. if (!setScreenMode(width, height, bpp, fullScreen))
  154. {
  155. Con::printf("Unable to set screen mode.");
  156. return false;
  157. }
  158. // Output some driver info to the console
  159. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  160. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  161. const char* versionString = (const char*) glGetString( GL_VERSION );
  162. Con::printf( "OpenGL driver information:" );
  163. if ( vendorString )
  164. Con::printf( " Vendor: %s", vendorString );
  165. if ( rendererString )
  166. Con::printf( " Renderer: %s", rendererString );
  167. if ( versionString )
  168. Con::printf( " Version: %s", versionString );
  169. GL_EXT_Init();
  170. Con::setVariable( "$pref::Video::displayDevice", mDeviceName );
  171. // Do this here because we now know about the extensions:
  172. if ( gGLState.suppSwapInterval )
  173. setVerticalSync(
  174. !Con::getBoolVariable( "$pref::Video::disableVerticalSync" ) );
  175. Con::setBoolVariable("$pref::OpenGL::allowTexGen", true);
  176. return true;
  177. }
  178. //------------------------------------------------------------------------------
  179. void OpenGLDevice::shutdown()
  180. {
  181. // Shutdown is deferred to Platform::shutdown()
  182. }
  183. //------------------------------------------------------------------------------
  184. static void PrintGLAttributes()
  185. {
  186. int doubleBuf;
  187. int bufferSize, depthSize, stencilSize;
  188. int red, green, blue, alpha;
  189. int aRed, aGreen, aBlue, aAlpha;
  190. SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doubleBuf);
  191. SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE, &bufferSize);
  192. SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depthSize);
  193. SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencilSize);
  194. SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
  195. SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
  196. SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &blue);
  197. SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &alpha);
  198. SDL_GL_GetAttribute(SDL_GL_ACCUM_RED_SIZE, &aRed);
  199. SDL_GL_GetAttribute(SDL_GL_ACCUM_GREEN_SIZE, &aGreen);
  200. SDL_GL_GetAttribute(SDL_GL_ACCUM_BLUE_SIZE, &aBlue);
  201. SDL_GL_GetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, &aAlpha);
  202. Con::printf("OpenGL Attributes:");
  203. Con::printf(" DoubleBuffer: %d", doubleBuf);
  204. Con::printf(" BufferSize: %d, DepthSize: %d, StencilSize: %d",
  205. bufferSize, depthSize, stencilSize);
  206. Con::printf(" Red: %d, Green: %d, Blue: %d, Alpha: %d",
  207. red, green, blue, alpha);
  208. Con::printf(" Accum Red: %d, Green: %d, Blue: %d, Alpha: %d",
  209. aRed, aGreen, aBlue, aAlpha);
  210. }
  211. //------------------------------------------------------------------------------
  212. bool OpenGLDevice::setScreenMode( U32 width, U32 height, U32 bpp,
  213. bool fullScreen, bool forceIt, bool repaint )
  214. {
  215. // load resolutions, this is done lazily so that we can check the setting
  216. // of smCanSwitchBitDepth, which may be overridden by console
  217. if (mResolutionList.size()==0)
  218. loadResolutions();
  219. if (mResolutionList.size()==0)
  220. {
  221. Con::printf("No resolutions available!");
  222. return false;
  223. }
  224. if (bpp == 0)
  225. {
  226. // bpp comes in as "0" when it is set to "Default"
  227. bpp = x86UNIXState->getDesktopBpp();
  228. }
  229. if (height == 0 || width == 0)
  230. {
  231. // paranoia check. set it to the default to prevent crashing
  232. width = 800;
  233. height = 600;
  234. }
  235. U32 desktopDepth = x86UNIXState->getDesktopBpp();
  236. // if we can't switch bit depths and the requested bpp is not equal to
  237. // the desktop bpp, set bpp to the desktop bpp
  238. if (!smCanSwitchBitDepth &&
  239. bpp != desktopDepth)
  240. {
  241. bpp = desktopDepth;
  242. }
  243. bool IsInList = false;
  244. Resolution NewResolution( width, height, bpp );
  245. // See if the desired resolution is in the list
  246. if ( mResolutionList.size() )
  247. {
  248. for ( int i = 0; i < mResolutionList.size(); i++ )
  249. {
  250. if ( width == mResolutionList[i].w
  251. && height == mResolutionList[i].h
  252. && bpp == mResolutionList[i].bpp )
  253. {
  254. IsInList = true;
  255. break;
  256. }
  257. }
  258. if ( !IsInList )
  259. {
  260. Con::printf( "Selected resolution not available: %d %d %d",
  261. width, height, bpp);
  262. return false;
  263. }
  264. }
  265. else
  266. {
  267. AssertFatal( false, "No resolution list found!!" );
  268. }
  269. // Here if we found a matching resolution in the list
  270. bool needResurrect = false;
  271. if (x86UNIXState->windowCreated())
  272. {
  273. Con::printf( "Killing the texture manager..." );
  274. Game->textureKill();
  275. needResurrect = true;
  276. }
  277. // Set the desired GL Attributes
  278. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  279. // JMQ: NVIDIA 2802+ doesn't like this setting for stencil size
  280. // SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  281. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  282. SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0);
  283. SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0);
  284. SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0);
  285. SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0);
  286. // SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  287. // SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  288. // SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
  289. // SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  290. U32 flags = SDL_OPENGL;
  291. if (fullScreen)
  292. flags |= SDL_FULLSCREEN;
  293. Con::printf( "Setting screen mode to %dx%dx%d (%s)...", width, height,
  294. bpp, ( fullScreen ? "fs" : "w" ) );
  295. // set the new video mode
  296. if (SDL_SetVideoMode(width, height, bpp, flags) == NULL)
  297. {
  298. Con::printf("Unable to set SDL Video Mode: %s", SDL_GetError());
  299. return false;
  300. }
  301. PrintGLAttributes();
  302. // clear screen here to prevent buffer garbage from being displayed when
  303. // video mode is switched
  304. glClearColor(0.0, 0.0, 0.0, 0.0);
  305. glClear(GL_COLOR_BUFFER_BIT);
  306. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  307. if ( needResurrect )
  308. {
  309. // Reload the textures:
  310. Con::printf( "Resurrecting the texture manager..." );
  311. Game->textureResurrect();
  312. }
  313. if ( gGLState.suppSwapInterval )
  314. setVerticalSync( !Con::getBoolVariable( "$pref::Video::disableVerticalSync" ) );
  315. // reset the window in platform state
  316. SDL_SysWMinfo sysinfo;
  317. SDL_VERSION(&sysinfo.version);
  318. if (SDL_GetWMInfo(&sysinfo) == 0)
  319. {
  320. Con::printf("Unable to set SDL Video Mode: %s", SDL_GetError());
  321. return false;
  322. }
  323. x86UNIXState->setWindow(sysinfo.info.x11.window);
  324. // set various other parameters
  325. x86UNIXState->setWindowCreated(true);
  326. smCurrentRes = NewResolution;
  327. Platform::setWindowSize ( width, height );
  328. smIsFullScreen = fullScreen;
  329. Con::setBoolVariable( "$pref::Video::fullScreen", smIsFullScreen );
  330. char tempBuf[15];
  331. dSprintf( tempBuf, sizeof( tempBuf ), "%d %d %d",
  332. smCurrentRes.w, smCurrentRes.h, smCurrentRes.bpp );
  333. Con::setVariable( "$pref::Video::resolution", tempBuf );
  334. // post a TORQUE_SETVIDEOMODE user event
  335. SDL_Event event;
  336. event.type = SDL_USEREVENT;
  337. event.user.code = TORQUE_SETVIDEOMODE;
  338. event.user.data1 = NULL;
  339. event.user.data2 = NULL;
  340. SDL_PushEvent(&event);
  341. // reset the caption
  342. SDL_WM_SetCaption(x86UNIXState->getWindowName(), NULL);
  343. // repaint
  344. if ( repaint )
  345. Con::evaluate( "resetCanvas();" );
  346. return true;
  347. }
  348. //------------------------------------------------------------------------------
  349. void OpenGLDevice::swapBuffers()
  350. {
  351. SDL_GL_SwapBuffers();
  352. }
  353. //------------------------------------------------------------------------------
  354. const char* OpenGLDevice::getDriverInfo()
  355. {
  356. const char* vendorString = (const char*) glGetString( GL_VENDOR );
  357. const char* rendererString = (const char*) glGetString( GL_RENDERER );
  358. const char* versionString = (const char*) glGetString( GL_VERSION );
  359. const char* extensionsString = (const char*) glGetString( GL_EXTENSIONS );
  360. U32 bufferLen = ( vendorString ? dStrlen( vendorString ) : 0 )
  361. + ( rendererString ? dStrlen( rendererString ) : 0 )
  362. + ( versionString ? dStrlen( versionString ) : 0 )
  363. + ( extensionsString ? dStrlen( extensionsString ) : 0 )
  364. + 4;
  365. char* returnString = Con::getReturnBuffer( bufferLen );
  366. dSprintf( returnString, bufferLen, "%s\t%s\t%s\t%s",
  367. ( vendorString ? vendorString : "" ),
  368. ( rendererString ? rendererString : "" ),
  369. ( versionString ? versionString : "" ),
  370. ( extensionsString ? extensionsString : "" ) );
  371. return( returnString );
  372. }
  373. //------------------------------------------------------------------------------
  374. bool OpenGLDevice::getGammaCorrection(F32 &g)
  375. {
  376. U16 redtable[256];
  377. U16 greentable[256];
  378. U16 bluetable[256];
  379. if (SDL_GetGammaRamp(redtable, greentable, bluetable) == -1)
  380. {
  381. Con::warnf("getGammaCorrection error: %s", SDL_GetError());
  382. return false;
  383. }
  384. F32 csum = 0.0;
  385. U32 ccount = 0;
  386. for (U16 i = 0; i < 256; ++i)
  387. {
  388. if (i != 0 && redtable[i] != 0 && redtable[i] != 65535)
  389. {
  390. F64 b = (F64) i/256.0;
  391. F64 a = (F64) redtable[i]/65535.0;
  392. F32 c = (F32) (mLog(a)/mLog(b));
  393. csum += c;
  394. ++ccount;
  395. }
  396. }
  397. g = csum/ccount;
  398. return true;
  399. }
  400. //------------------------------------------------------------------------------
  401. bool OpenGLDevice::setGammaCorrection(F32 g)
  402. {
  403. U16 redtable[256];
  404. U16 greentable[256];
  405. U16 bluetable[256];
  406. for (U16 i = 0; i < 256; ++i)
  407. redtable[i] = static_cast<U16>(mPow((F32) i/256.0f, g) * 65535.0f);
  408. dMemcpy(greentable,redtable,256*sizeof(U16));
  409. dMemcpy(bluetable,redtable,256*sizeof(U16));
  410. S32 ok = SDL_SetGammaRamp(redtable, greentable, bluetable);
  411. if (ok == -1)
  412. Con::warnf("Error setting gamma correction: %s", SDL_GetError());
  413. return ok != -1;
  414. }
  415. //------------------------------------------------------------------------------
  416. bool OpenGLDevice::setVerticalSync( bool on )
  417. {
  418. Con::printf("WARNING: OpenGLDevice::setVerticalSync is unimplemented %s %d\n", __FILE__, __LINE__);
  419. return false;
  420. #if 0
  421. if ( !gGLState.suppSwapInterval )
  422. return( false );
  423. return( qwglSwapIntervalEXT( on ? 1 : 0 ) );
  424. #endif
  425. }
  426. //------------------------------------------------------------------------------
  427. DisplayDevice* OpenGLDevice::create()
  428. {
  429. return new OpenGLDevice();
  430. }