gfxGLDevice.win.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "platformWin32/platformWin32.h"
  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/win32/win32Window.h"
  36. #include "gfx/gl/tGL/tWGL.h"
  37. #include "postFx/postEffect.h"
  38. #include "gfx/gl/gfxGLUtils.h"
  39. #define GETHWND(x) static_cast<Win32Window*>(x)->getHWND()
  40. // yonked from winWindow.cc
  41. void CreatePixelFormat( PIXELFORMATDESCRIPTOR *pPFD, S32 colorBits, S32 depthBits, S32 stencilBits, bool stereo )
  42. {
  43. PIXELFORMATDESCRIPTOR src =
  44. {
  45. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  46. 1, // version number
  47. PFD_DRAW_TO_WINDOW | // support window
  48. PFD_SUPPORT_OPENGL | // support OpenGL
  49. PFD_DOUBLEBUFFER, // double buffered
  50. PFD_TYPE_RGBA, // RGBA type
  51. colorBits, // color depth
  52. 0, 0, 0, 0, 0, 0, // color bits ignored
  53. 0, // no alpha buffer
  54. 0, // shift bit ignored
  55. 0, // no accumulation buffer
  56. 0, 0, 0, 0, // accum bits ignored
  57. depthBits, // z-buffer
  58. stencilBits, // stencil buffer
  59. 0, // no auxiliary buffer
  60. PFD_MAIN_PLANE, // main layer
  61. 0, // reserved
  62. 0, 0, 0 // layer masks ignored
  63. };
  64. if ( stereo )
  65. {
  66. //ri.Printf( PRINT_ALL, "...attempting to use stereo\n" );
  67. src.dwFlags |= PFD_STEREO;
  68. //glConfig.stereoEnabled = true;
  69. }
  70. else
  71. {
  72. //glConfig.stereoEnabled = qfalse;
  73. }
  74. *pPFD = src;
  75. }
  76. extern void loadGLCore();
  77. extern void loadGLExtensions(void* context);
  78. void GFXGLDevice::enumerateAdapters( Vector<GFXAdapter*> &adapterList )
  79. {
  80. // GL_ERROR_CHECK();
  81. WNDCLASS windowclass;
  82. dMemset( &windowclass, 0, sizeof( WNDCLASS ) );
  83. windowclass.lpszClassName = L"GFX-OpenGL";
  84. windowclass.style = CS_OWNDC;
  85. windowclass.lpfnWndProc = DefWindowProc;
  86. windowclass.hInstance = winState.appInstance;
  87. if( !RegisterClass( &windowclass ) )
  88. AssertFatal( false, "Failed to register the window class for the GL test window." );
  89. // Now create a window
  90. HWND hwnd = CreateWindow( L"GFX-OpenGL", L"", WS_POPUP, 0, 0, 640, 480,
  91. NULL, NULL, winState.appInstance, NULL );
  92. AssertFatal( hwnd != NULL, "Failed to create the window for the GL test window." );
  93. // Create a device context
  94. HDC tempDC = GetDC( hwnd );
  95. AssertFatal( tempDC != NULL, "Failed to create device context" );
  96. // Create pixel format descriptor...
  97. PIXELFORMATDESCRIPTOR pfd;
  98. CreatePixelFormat( &pfd, 32, 0, 0, false );
  99. if( !SetPixelFormat( tempDC, ChoosePixelFormat( tempDC, &pfd ), &pfd ) )
  100. AssertFatal( false, "I don't know who's responcible for this, but I want caught..." );
  101. // Create a rendering context!
  102. HGLRC tempGLRC = wglCreateContext( tempDC );
  103. if( !wglMakeCurrent( tempDC, tempGLRC ) )
  104. AssertFatal( false, "I want them caught and killed." );
  105. // Add the GL renderer
  106. loadGLCore();
  107. loadGLExtensions(tempDC);
  108. GFXAdapter *toAdd = new GFXAdapter;
  109. toAdd->mIndex = 0;
  110. const char* renderer = (const char*) glGetString( GL_RENDERER );
  111. AssertFatal( renderer != NULL, "GL_RENDERER returned NULL!" );
  112. if (renderer)
  113. {
  114. dStrcpy(toAdd->mName, renderer, GFXAdapter::MaxAdapterNameLen);
  115. dStrncat(toAdd->mName, " OpenGL", GFXAdapter::MaxAdapterNameLen);
  116. }
  117. else
  118. dStrcpy(toAdd->mName, "OpenGL", GFXAdapter::MaxAdapterNameLen);
  119. toAdd->mType = OpenGL;
  120. toAdd->mShaderModel = 0.f;
  121. toAdd->mCreateDeviceInstanceDelegate = mCreateDeviceInstance;
  122. // Enumerate all available resolutions:
  123. DEVMODE devMode;
  124. U32 modeNum = 0;
  125. U32 stillGoing = true;
  126. while ( stillGoing )
  127. {
  128. dMemset( &devMode, 0, sizeof( devMode ) );
  129. devMode.dmSize = sizeof( devMode );
  130. stillGoing = EnumDisplaySettings( NULL, modeNum++, &devMode );
  131. if (( devMode.dmPelsWidth >= 480) && (devMode.dmPelsHeight >= 360 )
  132. && ( devMode.dmBitsPerPel == 16 || devMode.dmBitsPerPel == 32 ))
  133. {
  134. GFXVideoMode vmAdd;
  135. vmAdd.bitDepth = devMode.dmBitsPerPel;
  136. vmAdd.fullScreen = true;
  137. vmAdd.refreshRate = devMode.dmDisplayFrequency;
  138. vmAdd.resolution.x = devMode.dmPelsWidth;
  139. vmAdd.resolution.y = devMode.dmPelsHeight;
  140. // Only add this resolution if it is not already in the list:
  141. bool alreadyInList = false;
  142. for (Vector<GFXVideoMode>::iterator i = toAdd->mAvailableModes.begin(); i != toAdd->mAvailableModes.end(); i++)
  143. {
  144. if (vmAdd == *i)
  145. {
  146. alreadyInList = true;
  147. break;
  148. }
  149. }
  150. if(alreadyInList)
  151. continue;
  152. toAdd->mAvailableModes.push_back( vmAdd );
  153. }
  154. }
  155. // Add to the list of available adapters.
  156. adapterList.push_back(toAdd);
  157. // Cleanup our window
  158. wglMakeCurrent(NULL, NULL);
  159. wglDeleteContext(tempGLRC);
  160. ReleaseDC(hwnd, tempDC);
  161. DestroyWindow(hwnd);
  162. UnregisterClass(L"GFX-OpenGL", winState.appInstance);
  163. }
  164. void GFXGLDevice::enumerateVideoModes()
  165. {
  166. mVideoModes.clear();
  167. // Enumerate all available resolutions:
  168. DEVMODE devMode;
  169. U32 modeNum = 0;
  170. U32 stillGoing = true;
  171. while ( stillGoing )
  172. {
  173. dMemset( &devMode, 0, sizeof( devMode ) );
  174. devMode.dmSize = sizeof( devMode );
  175. stillGoing = EnumDisplaySettings( NULL, modeNum++, &devMode );
  176. if (( devMode.dmPelsWidth >= 480) && (devMode.dmPelsHeight >= 360 )
  177. && ( devMode.dmBitsPerPel == 16 || devMode.dmBitsPerPel == 32 ))
  178. //( smCanSwitchBitDepth || devMode.dmBitsPerPel == winState.desktopBitsPixel ) )
  179. {
  180. GFXVideoMode toAdd;
  181. toAdd.bitDepth = devMode.dmBitsPerPel;
  182. toAdd.fullScreen = false;
  183. toAdd.refreshRate = devMode.dmDisplayFrequency;
  184. toAdd.resolution.x = devMode.dmPelsWidth;
  185. toAdd.resolution.y = devMode.dmPelsHeight;
  186. // Only add this resolution if it is not already in the list:
  187. bool alreadyInList = false;
  188. for (Vector<GFXVideoMode>::iterator i = mVideoModes.begin(); i != mVideoModes.end(); i++)
  189. {
  190. if (toAdd == *i)
  191. {
  192. alreadyInList = true;
  193. break;
  194. }
  195. }
  196. if ( !alreadyInList )
  197. {
  198. //Con::printf("Resolution: %dx%d %d bpp %d Refresh rate: %d", toAdd.resolution.x, toAdd.resolution.y, toAdd.bitDepth, toAdd.refreshRate);
  199. mVideoModes.push_back( toAdd );
  200. }
  201. }
  202. }
  203. }
  204. void GFXGLDevice::init( const GFXVideoMode &mode, PlatformWindow *window )
  205. {
  206. AssertFatal(window, "GFXGLDevice::init - no window specified, can't init device without a window!");
  207. AssertFatal(dynamic_cast<Win32Window*>(window), "Invalid window class type!");
  208. HWND hwnd = GETHWND(window);
  209. mWindowRT = &static_cast<Win32Window*>(window)->mTarget;
  210. RECT rect;
  211. GetClientRect(hwnd, &rect);
  212. Point2I resolution;
  213. resolution.x = rect.right - rect.left;
  214. resolution.y = rect.bottom - rect.top;
  215. // Create a device context
  216. HDC hdcGL = GetDC( hwnd );
  217. AssertFatal( hdcGL != NULL, "Failed to create device context" );
  218. // Create pixel format descriptor...
  219. PIXELFORMATDESCRIPTOR pfd;
  220. CreatePixelFormat( &pfd, 32, 0, 0, false ); // 32 bit color... We do not need depth or stencil, OpenGL renders into a FBO and then copy the image to window
  221. if( !SetPixelFormat( hdcGL, ChoosePixelFormat( hdcGL, &pfd ), &pfd ) )
  222. {
  223. AssertFatal( false, "GFXGLDevice::init - cannot get the one and only pixel format we check for." );
  224. }
  225. int OGL_MAJOR = 3;
  226. int OGL_MINOR = 2;
  227. #if TORQUE_DEBUG
  228. int debugFlag = WGL_CONTEXT_DEBUG_BIT_ARB;
  229. #else
  230. int debugFlag = 0;
  231. #endif
  232. // Create a temp rendering context, needed a current context to use wglCreateContextAttribsARB
  233. HGLRC tempGLRC = wglCreateContext(hdcGL);
  234. if (!wglMakeCurrent(hdcGL, tempGLRC))
  235. AssertFatal(false, "Couldn't make temp GL context.");
  236. if( gglHasWExtension(hdcGL, ARB_create_context) )
  237. {
  238. int const create_attribs[] = {
  239. WGL_CONTEXT_MAJOR_VERSION_ARB, OGL_MAJOR,
  240. WGL_CONTEXT_MINOR_VERSION_ARB, OGL_MINOR,
  241. WGL_CONTEXT_FLAGS_ARB, /*WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB |*/ debugFlag,
  242. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
  243. //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
  244. 0
  245. };
  246. mContext = wglCreateContextAttribsARB(hdcGL, 0, create_attribs);
  247. if(!mContext)
  248. {
  249. AssertFatal(0,"");
  250. }
  251. }
  252. else
  253. mContext = wglCreateContext( hdcGL );
  254. // Delete temp rendering context
  255. wglMakeCurrent(NULL, NULL);
  256. wglDeleteContext(tempGLRC);
  257. if( !wglMakeCurrent( hdcGL, (HGLRC)mContext ) )
  258. AssertFatal( false , "GFXGLDevice::init - cannot make our context current. Or maybe we can't create it." );
  259. loadGLCore();
  260. loadGLExtensions(hdcGL);
  261. // It is very important that extensions be loaded
  262. // before we call initGLState()
  263. initGLState();
  264. mProjectionMatrix.identity();
  265. mInitialized = true;
  266. deviceInited();
  267. }
  268. bool GFXGLDevice::beginSceneInternal()
  269. {
  270. mCanCurrentlyRender = true;
  271. return true;
  272. }
  273. U32 GFXGLDevice::getTotalVideoMemory()
  274. {
  275. return getTotalVideoMemory_GL_EXT();
  276. }
  277. //------------------------------------------------------------------------------
  278. GFXWindowTarget *GFXGLDevice::allocWindowTarget( PlatformWindow *window )
  279. {
  280. AssertFatal(!mContext, "");
  281. init(window->getVideoMode(), window);
  282. GFXGLWindowTarget *ggwt = new GFXGLWindowTarget(window, this);
  283. ggwt->registerResourceWithDevice(this);
  284. ggwt->mContext = mContext;
  285. AssertFatal(ggwt->mContext, "GFXGLDevice::allocWindowTarget - failed to allocate window target!");
  286. return ggwt;
  287. }
  288. GFXFence* GFXGLDevice::_createPlatformSpecificFence()
  289. {
  290. return NULL;
  291. }
  292. //-----------------------------------------------------------------------------
  293. void GFXGLWindowTarget::_WindowPresent()
  294. {
  295. HWND hwnd = GETHWND(getWindow());
  296. SwapBuffers(GetDC(hwnd));
  297. }
  298. void GFXGLWindowTarget::_teardownCurrentMode()
  299. {
  300. }
  301. void GFXGLWindowTarget::_setupNewMode()
  302. {
  303. }
  304. void GFXGLWindowTarget::_makeContextCurrent()
  305. {
  306. HWND hwnd = GETHWND(getWindow());
  307. HDC hdc = GetDC(hwnd);
  308. if (!wglMakeCurrent(hdc, (HGLRC)mContext))
  309. {
  310. //HRESULT if needed for debug
  311. //HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  312. AssertFatal(false, "GFXGLWindowTarget::_makeContextCurrent() - cannot make our context current.");
  313. }
  314. }