EmscriptenOGLVideo.cpp 15 KB

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