gfxGLDevice.sdl.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. outMode.bitDepth = SDL_BYTESPERPIXEL( mode.format );
  56. outMode.wideScreen = (mode.w / mode.h) > (4 / 3);
  57. outMode.fullScreen = true;
  58. outModes.push_back( outMode );
  59. }
  60. }
  61. void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
  62. {
  63. AssertFatal( SDL_WasInit(SDL_INIT_VIDEO), "");
  64. PlatformGL::init(); // for hints about context creation
  65. // Create a dummy window & openGL context so that gl functions can be used here
  66. SDL_Window* tempWindow = SDL_CreateWindow(
  67. "", // window title
  68. SDL_WINDOWPOS_UNDEFINED, // initial x position
  69. SDL_WINDOWPOS_UNDEFINED, // initial y position
  70. 640, // width, in pixels
  71. 480, // height, in pixels
  72. SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN // flags - see below
  73. );
  74. SDL_ClearError();
  75. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  76. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  77. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  78. SDL_GLContext tempContext = SDL_GL_CreateContext( tempWindow );
  79. if( !tempContext )
  80. {
  81. const char *err = SDL_GetError();
  82. Con::printf( err );
  83. AssertFatal(0, err );
  84. return;
  85. }
  86. SDL_ClearError();
  87. SDL_GL_MakeCurrent( tempWindow, tempContext );
  88. const char *err = SDL_GetError();
  89. if( err && err[0] )
  90. {
  91. Con::printf( err );
  92. AssertFatal(0, err );
  93. }
  94. // Init GL
  95. loadGLCore();
  96. loadGLExtensions(tempContext);
  97. //check minimun Opengl 3.2
  98. int major, minor;
  99. glGetIntegerv(GL_MAJOR_VERSION, &major);
  100. glGetIntegerv(GL_MINOR_VERSION, &minor);
  101. if( major < 3 || ( major == 3 && minor < 2 ) )
  102. {
  103. return;
  104. }
  105. GFXAdapter *toAdd = new GFXAdapter;
  106. toAdd->mIndex = 0;
  107. const char* renderer = (const char*) glGetString( GL_RENDERER );
  108. AssertFatal( renderer != NULL, "GL_RENDERER returned NULL!" );
  109. if (renderer)
  110. {
  111. dStrcpy(toAdd->mName, renderer);
  112. dStrncat(toAdd->mName, " OpenGL", GFXAdapter::MaxAdapterNameLen);
  113. }
  114. else
  115. dStrcpy(toAdd->mName, "OpenGL");
  116. toAdd->mType = OpenGL;
  117. toAdd->mShaderModel = 0.f;
  118. toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
  119. // Enumerate all available resolutions:
  120. EnumerateVideoModes(toAdd->mAvailableModes);
  121. // Add to the list of available adapters.
  122. adapterList.push_back(toAdd);
  123. // Cleanup window & open gl context
  124. SDL_DestroyWindow( tempWindow );
  125. SDL_GL_DeleteContext( tempContext );
  126. }
  127. void GFXGLDevice::enumerateVideoModes()
  128. {
  129. mVideoModes.clear();
  130. EnumerateVideoModes(mVideoModes);
  131. }
  132. void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
  133. {
  134. AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!");
  135. PlatformWindowSDL* x11Window = dynamic_cast<PlatformWindowSDL*>(window);
  136. AssertFatal(x11Window, "Window is not a valid PlatformWindowSDL object");
  137. // Create OpenGL context
  138. mContext = PlatformGL::CreateContextGL( x11Window );
  139. PlatformGL::MakeCurrentGL( x11Window, mContext );
  140. loadGLCore();
  141. loadGLExtensions(mContext);
  142. // It is very important that extensions be loaded before we call initGLState()
  143. initGLState();
  144. mProjectionMatrix.identity();
  145. mInitialized = true;
  146. deviceInited();
  147. }
  148. bool GFXGLDevice::beginSceneInternal()
  149. {
  150. mCanCurrentlyRender = true;
  151. return true;
  152. }
  153. U32 GFXGLDevice::getTotalVideoMemory()
  154. {
  155. return getTotalVideoMemory_GL_EXT();
  156. }
  157. //------------------------------------------------------------------------------
  158. GFXWindowTarget *GFXGLDevice::allocWindowTarget( PlatformWindow *window )
  159. {
  160. AssertFatal(!mContext, "This GFXGLDevice is already assigned to a window");
  161. GFXGLWindowTarget* ggwt = 0;
  162. if( !mContext )
  163. {
  164. // no context, init the device now
  165. init(window->getVideoMode(), window);
  166. ggwt = new GFXGLWindowTarget(window, this);
  167. ggwt->registerResourceWithDevice(this);
  168. ggwt->mContext = mContext;
  169. }
  170. return ggwt;
  171. }
  172. GFXFence* GFXGLDevice::_createPlatformSpecificFence()
  173. {
  174. return NULL;
  175. }
  176. //-----------------------------------------------------------------------------
  177. void GFXGLWindowTarget::_WindowPresent()
  178. {
  179. SDL_GL_SwapWindow( static_cast<PlatformWindowSDL*>( getWindow() )->getSDLWindow() );
  180. }
  181. void GFXGLWindowTarget::_teardownCurrentMode()
  182. {
  183. }
  184. void GFXGLWindowTarget::_setupNewMode()
  185. {
  186. }
  187. #endif