Torque3D Documentation / _generateds / gfxGLDevice.sdl.cpp

gfxGLDevice.sdl.cpp

Engine/source/gfx/gl/sdl/gfxGLDevice.sdl.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23#if defined( TORQUE_SDL ) && !defined( TORQUE_DEDICATED )
 24
 25#include "gfx/gfxCubemap.h"
 26#include "gfx/screenshot.h"
 27
 28#include "gfx/gl/gfxGLDevice.h"
 29#include "gfx/gl/gfxGLEnumTranslate.h"
 30#include "gfx/gl/gfxGLVertexBuffer.h"
 31#include "gfx/gl/gfxGLPrimitiveBuffer.h"
 32#include "gfx/gl/gfxGLTextureTarget.h"
 33#include "gfx/gl/gfxGLWindowTarget.h"
 34#include "gfx/gl/gfxGLTextureManager.h"
 35#include "gfx/gl/gfxGLTextureObject.h"
 36#include "gfx/gl/gfxGLCubemap.h"
 37#include "gfx/gl/gfxGLCardProfiler.h"
 38
 39#include "windowManager/sdl/sdlWindow.h"
 40#include "platform/platformGL.h"
 41#include "SDL.h"
 42
 43extern void loadGLCore();
 44extern void loadGLExtensions(void* context);
 45
 46void EnumerateVideoModes(Vector<GFXVideoMode>& outModes)
 47{
 48   int count = SDL_GetNumDisplayModes( 0 );
 49   if( count < 0)
 50   {
 51      AssertFatal(0, "");
 52      return;     
 53   }
 54   
 55   SDL_DisplayMode mode;
 56   for(int i = 0; i < count; ++i)
 57   {
 58      SDL_GetDisplayMode( 0, i, &mode);
 59      GFXVideoMode outMode;
 60      outMode.resolution.set( mode.w, mode.h );
 61      outMode.refreshRate = mode.refresh_rate;
 62      outMode.bitDepth = SDL_BYTESPERPIXEL( mode.format );
 63      outMode.wideScreen = (mode.w / mode.h) > (4 / 3);
 64      outMode.fullScreen = true;
 65      
 66      outModes.push_back( outMode );
 67   }
 68}
 69
 70void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
 71{
 72   AssertFatal( SDL_WasInit(SDL_INIT_VIDEO), "");
 73
 74   PlatformGL::init(); // for hints about context creation
 75
 76    // Create a dummy window & openGL context so that gl functions can be used here
 77   SDL_Window* tempWindow =  SDL_CreateWindow(
 78        "",                                // window title
 79        SDL_WINDOWPOS_UNDEFINED,           // initial x position
 80        SDL_WINDOWPOS_UNDEFINED,           // initial y position
 81        640,                               // width, in pixels
 82        480,                               // height, in pixels
 83        SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN // flags - see below
 84    );
 85
 86   SDL_ClearError();
 87   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
 88   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
 89   SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
 90   SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
 91
 92   SDL_GLContext tempContext = SDL_GL_CreateContext( tempWindow );
 93   if( !tempContext )
 94   {
 95       const char *err = SDL_GetError();
 96       Con::printf( err );
 97       AssertFatal(0, err );
 98       return;
 99   }
100
101   SDL_ClearError();
102   SDL_GL_MakeCurrent( tempWindow, tempContext );
103
104   const char *err = SDL_GetError();
105   if( err && err[0] )
106   {
107       Con::printf( err );
108       AssertFatal(0, err );
109   }
110
111   // Init GL
112   loadGLCore();
113   loadGLExtensions(tempContext);
114
115   //check minimun Opengl 3.3
116   int major, minor;
117   glGetIntegerv(GL_MAJOR_VERSION, &major);
118   glGetIntegerv(GL_MINOR_VERSION, &minor);
119   if( major < 3 || ( major == 3 && minor < 3 ) )
120   {
121      return;
122   }
123
124   //check for required extensions
125   if (!gglHasExtension(ARB_texture_cube_map_array))
126   {
127      Con::warnf("Adapater supports OpenGL 3.3 but doesnt support GL_ARB_texture_cube_map_array");
128      return;
129   }
130
131   if (!gglHasExtension(ARB_gpu_shader5))
132   {
133      Con::warnf("Adapater supports OpenGL 3.3 but doesnt support GL_ARB_gpu_shader5");
134      return;
135   }
136    
137   GFXAdapter *toAdd = new GFXAdapter;
138   toAdd->mIndex = 0;
139
140   const char* renderer = (const char*) glGetString( GL_RENDERER );
141   AssertFatal( renderer != NULL, "GL_RENDERER returned NULL!" );
142
143   if (renderer)
144   {
145      dStrcpy(toAdd->mName, renderer, GFXAdapter::MaxAdapterNameLen);
146      dStrcat(toAdd->mName, " OpenGL", GFXAdapter::MaxAdapterNameLen);
147   }
148   else
149      dStrcpy(toAdd->mName, "OpenGL", GFXAdapter::MaxAdapterNameLen);
150
151   toAdd->mType = OpenGL;
152   toAdd->mShaderModel = 0.f;
153   toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
154
155   // Enumerate all available resolutions:
156   EnumerateVideoModes(toAdd->mAvailableModes);
157
158   // Add to the list of available adapters.
159   adapterList.push_back(toAdd);
160
161   // Cleanup window & open gl context
162   SDL_DestroyWindow( tempWindow );
163   SDL_GL_DeleteContext( tempContext );
164}
165
166void GFXGLDevice::enumerateVideoModes() 
167{
168    mVideoModes.clear();
169    EnumerateVideoModes(mVideoModes);
170}
171
172void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
173{
174    AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!");
175    PlatformWindowSDL* sdlWindow = dynamic_cast<PlatformWindowSDL*>(window);
176    AssertFatal(sdlWindow, "Window is not a valid PlatformWindowSDL object");
177
178    // Create OpenGL context
179    mContext = PlatformGL::CreateContextGL( sdlWindow );
180    PlatformGL::MakeCurrentGL( sdlWindow, mContext );
181        
182    loadGLCore();
183    loadGLExtensions(mContext);
184    
185    // It is very important that extensions be loaded before we call initGLState()
186    initGLState();
187    
188    mProjectionMatrix.identity();
189    
190    mInitialized = true;
191    deviceInited();
192}
193
194bool GFXGLDevice::beginSceneInternal() 
195{
196   mCanCurrentlyRender = true;
197   return true;
198}
199
200U32 GFXGLDevice::getTotalVideoMemory()
201{
202   return getTotalVideoMemory_GL_EXT();
203}
204
205//------------------------------------------------------------------------------
206
207GFXWindowTarget *GFXGLDevice::allocWindowTarget( PlatformWindow *window )
208{
209   GFXGLWindowTarget* ggwt = new GFXGLWindowTarget(window, this);
210
211   //first window
212   if (!mContext)
213   {
214      init(window->getVideoMode(), window);
215      ggwt->mSecondaryWindow = false;
216   }
217   else
218      ggwt->mSecondaryWindow = true;
219
220   ggwt->registerResourceWithDevice(this);
221   ggwt->mContext = mContext;
222
223   return ggwt;
224}
225
226GFXFence* GFXGLDevice::_createPlatformSpecificFence()
227{
228    return NULL;
229}
230
231
232//-----------------------------------------------------------------------------
233
234void GFXGLWindowTarget::_WindowPresent()
235{   
236   SDL_GL_SwapWindow( static_cast<PlatformWindowSDL*>( getWindow() )->getSDLWindow() );
237}
238
239void GFXGLWindowTarget::_teardownCurrentMode()
240{
241
242}
243
244void GFXGLWindowTarget::_setupNewMode()
245{
246}
247
248void GFXGLWindowTarget::_makeContextCurrent()
249{
250   PlatformGL::MakeCurrentGL(mWindow, mContext);
251}
252
253#endif
254