2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include "osal_opengl.h"
21 #define M64P_PLUGIN_PROTOTYPES 1
22 #include "m64p_plugin.h"
26 #include "OGLExtensions.h"
29 #include "OGLGraphicsContext.h"
30 #include "TextureManager.h"
37 COGLGraphicsContext::COGLGraphicsContext() :
38 m_bSupportMultiTexture(false),
39 m_bSupportTextureEnvCombine(false),
40 m_bSupportSeparateSpecularColor(false),
41 m_bSupportSecondColor(false),
42 m_bSupportFogCoord(false),
43 m_bSupportTextureObject(false),
44 m_bSupportRescaleNormal(false),
45 m_bSupportLODBias(false),
46 m_bSupportTextureMirrorRepeat(false),
47 m_bSupportTextureLOD(false),
48 m_bSupportNVRegisterCombiner(false),
49 m_bSupportBlendColor(false),
50 m_bSupportBlendSubtract(false),
51 m_bSupportNVTextureEnvCombine4(false),
54 m_pExtensionStr(NULL),
60 COGLGraphicsContext::~COGLGraphicsContext()
64 bool COGLGraphicsContext::Initialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
66 DebugMessage(M64MSG_INFO, "Initializing OpenGL Device Context.");
72 CGraphicsContext::Get()->m_supportTextureMirror = false;
73 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
77 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
78 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
82 windowSetting.statusBarHeightToUse = 0;
83 windowSetting.toolbarHeightToUse = 0;
86 int depthBufferDepth = options.OpenglDepthBufferSetting;
87 int colorBufferDepth = 32;
88 int bVerticalSync = windowSetting.bVerticalSync;
89 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
92 DebugMessage(M64MSG_VERBOSE, "Initializing video subsystem...");
95 if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
98 if (CoreVideo_Init() != M64ERR_SUCCESS)
102 /* hard-coded attribute values */
103 const int iDOUBLEBUFFER = 1;
106 SDL_SetVideoMode(800, 480, 0, (bWindowed)?0:SDL_FULLSCREEN);
107 SDL_ShowCursor(SDL_DISABLE);
110 /* set opengl attributes */
111 CoreVideo_GL_SetAttribute(M64P_GL_DOUBLEBUFFER, iDOUBLEBUFFER);
112 CoreVideo_GL_SetAttribute(M64P_GL_SWAP_CONTROL, bVerticalSync);
113 CoreVideo_GL_SetAttribute(M64P_GL_BUFFER_SIZE, colorBufferDepth);
114 CoreVideo_GL_SetAttribute(M64P_GL_DEPTH_SIZE, depthBufferDepth);
116 /* set multisampling */
117 if (options.multiSampling > 0)
119 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLEBUFFERS, 1);
120 if (options.multiSampling <= 2)
121 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 2);
122 else if (options.multiSampling <= 4)
123 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 4);
124 else if (options.multiSampling <= 8)
125 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 8);
127 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
130 /* Set the video mode */
131 m64p_video_mode ScreenMode = bWindowed ? M64VIDEO_WINDOWED : M64VIDEO_FULLSCREEN;
132 m64p_video_flags flags = M64VIDEOFLAG_SUPPORT_RESIZING;
133 if (CoreVideo_SetVideoMode(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
135 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
140 /* check that our opengl attributes were properly set */
142 if (CoreVideo_GL_GetAttribute(M64P_GL_DOUBLEBUFFER, &iActual) == M64ERR_SUCCESS)
143 if (iActual != iDOUBLEBUFFER)
144 DebugMessage(M64MSG_WARNING, "Failed to set GL_DOUBLEBUFFER to %i. (it's %i)", iDOUBLEBUFFER, iActual);
145 if (CoreVideo_GL_GetAttribute(M64P_GL_SWAP_CONTROL, &iActual) == M64ERR_SUCCESS)
146 if (iActual != bVerticalSync)
147 DebugMessage(M64MSG_WARNING, "Failed to set GL_SWAP_CONTROL to %i. (it's %i)", bVerticalSync, iActual);
148 if (CoreVideo_GL_GetAttribute(M64P_GL_BUFFER_SIZE, &iActual) == M64ERR_SUCCESS)
149 if (iActual != colorBufferDepth)
150 DebugMessage(M64MSG_WARNING, "Failed to set GL_BUFFER_SIZE to %i. (it's %i)", colorBufferDepth, iActual);
151 if (CoreVideo_GL_GetAttribute(M64P_GL_DEPTH_SIZE, &iActual) == M64ERR_SUCCESS)
152 if (iActual != depthBufferDepth)
153 DebugMessage(M64MSG_WARNING, "Failed to set GL_DEPTH_SIZE to %i. (it's %i)", depthBufferDepth, iActual);
156 /* Get function pointers to OpenGL extensions (blame Microsoft Windows for this) */
157 OGLExtensions_Init();
161 sprintf(caption, "%s v%i.%i.%i", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION));
162 CoreVideo_SetCaption(caption);
167 sprintf(m_strDeviceStats, "%.60s - %.128s : %.60s", m_pVendorStr, m_pRenderStr, m_pVersionStr);
168 TRACE0(m_strDeviceStats);
169 DebugMessage(M64MSG_INFO, "Using OpenGL: %s", m_strDeviceStats);
173 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
175 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
179 status.isVertexShaderEnabled = false;
184 bool COGLGraphicsContext::ResizeInitialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
189 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
191 int depthBufferDepth = options.OpenglDepthBufferSetting;
192 int colorBufferDepth = 32;
193 int bVerticalSync = windowSetting.bVerticalSync;
194 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
196 /* hard-coded attribute values */
197 const int iDOUBLEBUFFER = 1;
199 /* set opengl attributes */
200 CoreVideo_GL_SetAttribute(M64P_GL_DOUBLEBUFFER, iDOUBLEBUFFER);
201 CoreVideo_GL_SetAttribute(M64P_GL_SWAP_CONTROL, bVerticalSync);
202 CoreVideo_GL_SetAttribute(M64P_GL_BUFFER_SIZE, colorBufferDepth);
203 CoreVideo_GL_SetAttribute(M64P_GL_DEPTH_SIZE, depthBufferDepth);
205 /* set multisampling */
206 if (options.multiSampling > 0)
208 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLEBUFFERS, 1);
209 if (options.multiSampling <= 2)
210 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 2);
211 else if (options.multiSampling <= 4)
212 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 4);
213 else if (options.multiSampling <= 8)
214 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 8);
216 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
219 /* Call Mupen64plus core Video Extension to resize the window, which will create a new OpenGL Context under SDL */
220 if (CoreVideo_ResizeWindow(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight) != M64ERR_SUCCESS)
222 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
230 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
232 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
238 void COGLGraphicsContext::InitState(void)
240 m_pRenderStr = glGetString(GL_RENDERER);
241 m_pExtensionStr = glGetString(GL_EXTENSIONS);
242 m_pVersionStr = glGetString(GL_VERSION);
243 m_pVendorStr = glGetString(GL_VENDOR);
244 glMatrixMode(GL_PROJECTION);
249 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
255 glShadeModel(GL_SMOOTH);
259 //glMatrixMode(GL_MODELVIEW);
262 glDisable(GL_ALPHA_TEST);
266 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
273 glDisable(GL_CULL_FACE);
276 glDisable(GL_NORMALIZE);
280 glDepthFunc(GL_LEQUAL);
282 glEnable(GL_DEPTH_TEST);
286 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
293 glEnable(GL_ALPHA_TEST);
296 glMatrixMode(GL_PROJECTION);
303 #elif SDL_VIDEO_OPENGL_ES2
304 glDepthRangef(0.0f, 1.0f);
309 void COGLGraphicsContext::InitOGLExtension(void)
311 // important extension features, it is very bad not to have these feature
313 m_bSupportMultiTexture = true;
314 m_bSupportTextureEnvCombine = true;
315 m_bSupportSeparateSpecularColor = true;
316 m_bSupportSecondColor = true;
317 m_bSupportFogCoord = false;
318 m_bSupportTextureObject = false;
319 m_bSupportRescaleNormal = true;
320 m_bSupportLODBias = false;
321 m_bSupportAnisotropicFiltering = true;
323 m_bSupportMultiTexture = IsExtensionSupported(OSAL_GL_ARB_MULTITEXTURE);
324 m_bSupportTextureEnvCombine = IsExtensionSupported("GL_EXT_texture_env_combine");
326 m_bSupportSeparateSpecularColor = IsExtensionSupported("GL_EXT_separate_specular_color");
327 m_bSupportSecondColor = IsExtensionSupported("GL_EXT_secondary_color");
328 m_bSupportFogCoord = IsExtensionSupported("GL_EXT_fog_coord");
329 m_bSupportTextureObject = IsExtensionSupported("GL_EXT_texture_object");
331 // Optional extension features
332 m_bSupportRescaleNormal = IsExtensionSupported("GL_EXT_rescale_normal");
333 m_bSupportLODBias = IsExtensionSupported("GL_EXT_texture_lod_bias");
334 m_bSupportAnisotropicFiltering = IsExtensionSupported("GL_EXT_texture_filter_anisotropic");
336 // Compute maxAnisotropicFiltering
337 m_maxAnisotropicFiltering = 0;
339 if( m_bSupportAnisotropicFiltering
340 && (options.anisotropicFiltering == 2
341 || options.anisotropicFiltering == 4
342 || options.anisotropicFiltering == 8
343 || options.anisotropicFiltering == 16))
345 //Get the max value of aniso that the graphic card support
346 glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropicFiltering);
349 // If user want more aniso than hardware can do
350 if(options.anisotropicFiltering > (uint32) m_maxAnisotropicFiltering)
352 DebugMessage(M64MSG_INFO, "A value of '%i' is set for AnisotropicFiltering option but the hardware has a maximum value of '%i' so this will be used", options.anisotropicFiltering, m_maxAnisotropicFiltering);
355 //check if user want less anisotropy than hardware can do
356 if((uint32) m_maxAnisotropicFiltering > options.anisotropicFiltering)
357 m_maxAnisotropicFiltering = options.anisotropicFiltering;
360 // Nvidia only extension features (optional)
361 m_bSupportNVRegisterCombiner = IsExtensionSupported("GL_NV_register_combiners");
363 m_bSupportTextureMirrorRepeat = true;
365 m_bSupportTextureMirrorRepeat = IsExtensionSupported("GL_IBM_texture_mirrored_repeat") || IsExtensionSupported("ARB_texture_mirrored_repeat");
367 m_supportTextureMirror = m_bSupportTextureMirrorRepeat;
368 m_bSupportTextureLOD = IsExtensionSupported("GL_EXT_texture_lod");
370 m_bSupportBlendColor = true;
371 m_bSupportBlendSubtract = true;
373 m_bSupportBlendColor = IsExtensionSupported("GL_EXT_blend_color");
374 m_bSupportBlendSubtract = IsExtensionSupported("GL_EXT_blend_subtract");
376 m_bSupportNVTextureEnvCombine4 = IsExtensionSupported("GL_NV_texture_env_combine4");
380 bool COGLGraphicsContext::IsExtensionSupported(const char* pExtName)
382 if (strstr((const char*)m_pExtensionStr, pExtName) != NULL)
384 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is supported.", pExtName);
389 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is NOT supported.", pExtName);
394 bool COGLGraphicsContext::IsWglExtensionSupported(const char* pExtName)
396 if( m_pWglExtensionStr == NULL )
399 if( strstr((const char*)m_pWglExtensionStr, pExtName) != NULL )
406 void COGLGraphicsContext::CleanUp()
410 SDL_ShowCursor(SDL_ENABLE);
411 SDL_QuitSubSystem(SDL_INIT_VIDEO);
419 void COGLGraphicsContext::Clear(ClearFlag dwFlags, uint32 color, float depth)
422 if( dwFlags&CLEAR_COLOR_BUFFER ) flag |= GL_COLOR_BUFFER_BIT;
423 if( dwFlags&CLEAR_DEPTH_BUFFER ) flag |= GL_DEPTH_BUFFER_BIT;
425 float r = ((color>>16)&0xFF)/255.0f;
426 float g = ((color>> 8)&0xFF)/255.0f;
427 float b = ((color )&0xFF)/255.0f;
428 float a = ((color>>24)&0xFF)/255.0f;
429 glClearColor(r, g, b, a);
433 glClear(flag); //Clear color buffer and depth buffer
437 void COGLGraphicsContext::UpdateFrame(bool swaponly)
439 status.gFrameCount++;
444 //wglSwapIntervalEXT(0);
447 if (debuggerPauseCount == countToPause)
449 static int iShotNum = 0;
450 // get width, height, allocate buffer to store image
451 int width = windowSetting.uDisplayWidth;
452 int height = windowSetting.uDisplayHeight;
453 printf("Saving debug images: width=%i height=%i\n", width, height);
454 short *buffer = (short *) malloc(((width+3)&~3)*(height+1)*4);
455 glReadBuffer( GL_FRONT );
456 // set up a BMGImage struct
457 struct BMGImageStruct img;
458 memset(&img, 0, sizeof(BMGImageStruct));
460 img.bits = (unsigned char *) buffer;
461 img.bits_per_pixel = 32;
464 img.scan_width = width * 4;
465 // store the RGB color image
467 sprintf(chFilename, "dbg_rgb_%03i.png", iShotNum);
468 glReadPixels(0,0,width,height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
469 WritePNG(chFilename, img);
470 // store the Z buffer
471 sprintf(chFilename, "dbg_Z_%03i.png", iShotNum);
472 glReadPixels(0,0,width,height, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buffer);
473 //img.bits_per_pixel = 16;
474 //img.scan_width = width * 2;
475 WritePNG(chFilename, img);
476 // dump a subset of the Z data
477 for (int y = 0; y < 480; y += 16)
479 for (int x = 0; x < 640; x+= 16)
480 printf("%4hx ", buffer[y*640 + x]);
484 // free memory and get out of here
491 // if emulator defined a render callback function, call it before buffer swap
493 (*renderCallback)(status.bScreenIsDrawn);
497 CoreVideo_GL_SwapBuffers();
500 /*if(options.bShowFPS)
502 static unsigned int lastTick=0;
504 unsigned int nowTick = SDL_GetTicks();
506 if(lastTick + 5000 <= nowTick)
509 sprintf(caption, "%s v%i.%i.%i - %.3f VI/S", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION), frames/5.0);
510 CoreVideo_SetCaption(caption);
516 glDepthMask(GL_TRUE);
520 if( !g_curRomInfo.bForceScreenClear )
522 glClear(GL_DEPTH_BUFFER_BIT);
526 needCleanScene = true;
528 status.bScreenIsDrawn = false;
531 bool COGLGraphicsContext::SetFullscreenMode()
533 windowSetting.statusBarHeightToUse = 0;
534 windowSetting.toolbarHeightToUse = 0;
538 bool COGLGraphicsContext::SetWindowMode()
540 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
541 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
544 int COGLGraphicsContext::ToggleFullscreen()
547 if (CoreVideo_ToggleFullScreen() == M64ERR_SUCCESS)
549 m_bWindowed = !m_bWindowed;
556 return m_bWindowed?0:1;
559 // This is a static function, will be called when the plugin DLL is initialized
560 void COGLGraphicsContext::InitDeviceParameters()
562 status.isVertexShaderEnabled = false; // Disable it for now
566 bool COGLGraphicsContext::IsSupportAnisotropicFiltering()
568 return m_bSupportAnisotropicFiltering;
571 int COGLGraphicsContext::getMaxAnisotropicFiltering()
573 return m_maxAnisotropicFiltering;