gfxGLDevice.sdl.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 defined( TORQUE_SDL ) && !defined( TORQUE_DEDICATED )
  23. #include "gfx/gfxCubemap.h"
  24. #include "gfx/screenshot.h"
  25. #include "gfx/gl/gfxGLDevice.h"
  26. #include "gfx/gl/gfxGLEnumTranslate.h"
  27. #include "gfx/gl/gfxGLVertexBuffer.h"
  28. #include "gfx/gl/gfxGLPrimitiveBuffer.h"
  29. #include "gfx/gl/gfxGLTextureTarget.h"
  30. #include "gfx/gl/gfxGLWindowTarget.h"
  31. #include "gfx/gl/gfxGLTextureManager.h"
  32. #include "gfx/gl/gfxGLTextureObject.h"
  33. #include "gfx/gl/gfxGLCubemap.h"
  34. #include "gfx/gl/gfxGLCardProfiler.h"
  35. #include "windowManager/sdl/sdlWindow.h"
  36. #include "platform/platformGL.h"
  37. #include "SDL.h"
  38. extern void loadGLCore();
  39. extern void loadGLExtensions(void* context);
  40. void EnumerateVideoModes(Vector<GFXVideoMode>& outModes)
  41. {
  42. int count = SDL_GetNumDisplayModes( 0 );
  43. if( count < 0)
  44. {
  45. AssertFatal(0, "");
  46. return;
  47. }
  48. SDL_DisplayMode mode;
  49. for(int i = 0; i < count; ++i)
  50. {
  51. SDL_GetDisplayMode( 0, i, &mode);
  52. GFXVideoMode outMode;
  53. outMode.resolution.set( mode.w, mode.h );
  54. outMode.refreshRate = mode.refresh_rate;
  55. // BBP = 32 for some reason the engine knows it should be 32, but then we
  56. // add some extra code to break what the engine knows.
  57. //outMode.bitDepth = SDL_BYTESPERPIXEL( mode.format ); // sets bitdepths to 4
  58. //outMode.bitDepth = SDL_BITSPERPIXEL(mode.format); // sets bitdepth to 24
  59. // hardcoded magic numbers ftw
  60. // This value is hardcoded in DX, probably to avoid the shenanigans going on here
  61. outMode.bitDepth = 32;
  62. outMode.wideScreen = (mode.w / mode.h) > (4 / 3);
  63. outMode.fullScreen = true;
  64. outModes.push_back( outMode );
  65. }
  66. }
  67. void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
  68. {
  69. #ifdef TORQUE_TESTS_ENABLED
  70. return;
  71. #endif
  72. AssertFatal( SDL_WasInit(SDL_INIT_VIDEO), "");
  73. PlatformGL::init(); // for hints about context creation
  74. // Create a dummy window & openGL context so that gl functions can be used here
  75. SDL_Window* tempWindow = SDL_CreateWindow(
  76. "", // window title
  77. SDL_WINDOWPOS_UNDEFINED, // initial x position
  78. SDL_WINDOWPOS_UNDEFINED, // initial y position
  79. 640, // width, in pixels
  80. 480, // height, in pixels
  81. SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN // flags - see below
  82. );
  83. if (!tempWindow)
  84. {
  85. const char* err = SDL_GetError();
  86. Con::printf(err);
  87. AssertFatal(0, err);
  88. return;
  89. }
  90. SDL_ClearError();
  91. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  92. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  93. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  94. SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
  95. SDL_GLContext tempContext = SDL_GL_CreateContext( tempWindow );
  96. if( !tempContext )
  97. {
  98. const char *err = SDL_GetError();
  99. Con::printf( err );
  100. AssertFatal(0, err );
  101. return;
  102. }
  103. SDL_ClearError();
  104. SDL_GL_MakeCurrent( tempWindow, tempContext );
  105. const char *err = SDL_GetError();
  106. if( err && err[0] )
  107. {
  108. Con::printf( err );
  109. AssertFatal(0, err );
  110. }
  111. // Init GL
  112. loadGLCore();
  113. loadGLExtensions(tempContext);
  114. //check minimun Opengl 3.3
  115. int major, minor;
  116. glGetIntegerv(GL_MAJOR_VERSION, &major);
  117. glGetIntegerv(GL_MINOR_VERSION, &minor);
  118. if( major < 3 || ( major == 3 && minor < 3 ) )
  119. {
  120. return;
  121. }
  122. //check for required extensions
  123. if (!gglHasExtension(ARB_texture_cube_map_array))
  124. {
  125. Con::warnf("Adapater supports OpenGL 3.3 but doesnt support GL_ARB_texture_cube_map_array");
  126. return;
  127. }
  128. if (!gglHasExtension(ARB_gpu_shader5))
  129. {
  130. Con::warnf("Adapater supports OpenGL 3.3 but doesnt support GL_ARB_gpu_shader5");
  131. return;
  132. }
  133. GFXAdapter *toAdd = new GFXAdapter;
  134. toAdd->mIndex = 0;
  135. const char* renderer = (const char*) glGetString( GL_RENDERER );
  136. AssertFatal( renderer != NULL, "GL_RENDERER returned NULL!" );
  137. if (renderer)
  138. {
  139. dStrcpy(toAdd->mName, renderer, GFXAdapter::MaxAdapterNameLen);
  140. dStrcat(toAdd->mName, " OpenGL", GFXAdapter::MaxAdapterNameLen);
  141. }
  142. else
  143. dStrcpy(toAdd->mName, "OpenGL", GFXAdapter::MaxAdapterNameLen);
  144. toAdd->mType = OpenGL;
  145. toAdd->mShaderModel = 0.f;
  146. toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
  147. // Enumerate all available resolutions:
  148. EnumerateVideoModes(toAdd->mAvailableModes);
  149. // Add to the list of available adapters.
  150. adapterList.push_back(toAdd);
  151. // Cleanup window & open gl context
  152. SDL_DestroyWindow( tempWindow );
  153. SDL_GL_DeleteContext( tempContext );
  154. }
  155. void GFXGLDevice::enumerateVideoModes()
  156. {
  157. mVideoModes.clear();
  158. EnumerateVideoModes(mVideoModes);
  159. }
  160. void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
  161. {
  162. AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!");
  163. PlatformWindowSDL* sdlWindow = dynamic_cast<PlatformWindowSDL*>(window);
  164. AssertFatal(sdlWindow, "Window is not a valid PlatformWindowSDL object");
  165. // Create OpenGL context
  166. mContext = PlatformGL::CreateContextGL( sdlWindow );
  167. PlatformGL::MakeCurrentGL( sdlWindow, mContext );
  168. loadGLCore();
  169. loadGLExtensions(mContext);
  170. // It is very important that extensions be loaded before we call initGLState()
  171. initGLState();
  172. mProjectionMatrix.identity();
  173. mInitialized = true;
  174. deviceInited();
  175. }
  176. bool GFXGLDevice::beginSceneInternal()
  177. {
  178. mCanCurrentlyRender = true;
  179. return true;
  180. }
  181. U32 GFXGLDevice::getTotalVideoMemory()
  182. {
  183. return getTotalVideoMemory_GL_EXT();
  184. }
  185. //------------------------------------------------------------------------------
  186. GFXWindowTarget *GFXGLDevice::allocWindowTarget( PlatformWindow *window )
  187. {
  188. GFXGLWindowTarget* ggwt = new GFXGLWindowTarget(window, this);
  189. //first window
  190. if (!mContext)
  191. {
  192. init(window->getVideoMode(), window);
  193. ggwt->mSecondaryWindow = false;
  194. }
  195. else
  196. ggwt->mSecondaryWindow = true;
  197. ggwt->registerResourceWithDevice(this);
  198. ggwt->mContext = mContext;
  199. return ggwt;
  200. }
  201. GFXFence* GFXGLDevice::_createPlatformSpecificFence()
  202. {
  203. return NULL;
  204. }
  205. //-----------------------------------------------------------------------------
  206. void GFXGLWindowTarget::_WindowPresent()
  207. {
  208. SDL_GL_SwapWindow( static_cast<PlatformWindowSDL*>( getWindow() )->getSDLWindow() );
  209. }
  210. void GFXGLWindowTarget::_teardownCurrentMode()
  211. {
  212. }
  213. void GFXGLWindowTarget::_setupNewMode()
  214. {
  215. }
  216. void GFXGLWindowTarget::_makeContextCurrent()
  217. {
  218. PlatformGL::MakeCurrentGL(mWindow, mContext);
  219. }
  220. #endif