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"
34 COGLGraphicsContext::COGLGraphicsContext() :
35 m_bSupportMultiTexture(false),
36 m_bSupportTextureEnvCombine(false),
37 m_bSupportSeparateSpecularColor(false),
38 m_bSupportSecondColor(false),
39 m_bSupportFogCoord(false),
40 m_bSupportTextureObject(false),
41 m_bSupportRescaleNormal(false),
42 m_bSupportLODBias(false),
43 m_bSupportTextureMirrorRepeat(false),
44 m_bSupportTextureLOD(false),
45 m_bSupportNVRegisterCombiner(false),
46 m_bSupportBlendColor(false),
47 m_bSupportBlendSubtract(false),
48 m_bSupportNVTextureEnvCombine4(false),
51 m_pExtensionStr(NULL),
57 COGLGraphicsContext::~COGLGraphicsContext()
61 bool COGLGraphicsContext::Initialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
63 DebugMessage(M64MSG_INFO, "Initializing OpenGL Device Context.");
66 CGraphicsContext::Get()->m_supportTextureMirror = false;
67 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
71 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
72 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
76 windowSetting.statusBarHeightToUse = 0;
77 windowSetting.toolbarHeightToUse = 0;
80 int depthBufferDepth = options.OpenglDepthBufferSetting;
81 int colorBufferDepth = 32;
82 int bVerticalSync = windowSetting.bVerticalSync;
83 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
86 DebugMessage(M64MSG_VERBOSE, "Initializing video subsystem...");
87 if (CoreVideo_Init() != M64ERR_SUCCESS)
90 /* hard-coded attribute values */
91 const int iDOUBLEBUFFER = 1;
93 /* set opengl attributes */
94 CoreVideo_GL_SetAttribute(M64P_GL_DOUBLEBUFFER, iDOUBLEBUFFER);
95 CoreVideo_GL_SetAttribute(M64P_GL_SWAP_CONTROL, bVerticalSync);
96 CoreVideo_GL_SetAttribute(M64P_GL_BUFFER_SIZE, colorBufferDepth);
97 CoreVideo_GL_SetAttribute(M64P_GL_DEPTH_SIZE, depthBufferDepth);
99 /* set multisampling */
100 if (options.multiSampling > 0)
102 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLEBUFFERS, 1);
103 if (options.multiSampling <= 2)
104 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 2);
105 else if (options.multiSampling <= 4)
106 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 4);
107 else if (options.multiSampling <= 8)
108 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 8);
110 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
113 /* Set the video mode */
114 m64p_video_mode ScreenMode = bWindowed ? M64VIDEO_WINDOWED : M64VIDEO_FULLSCREEN;
115 m64p_video_flags flags = M64VIDEOFLAG_SUPPORT_RESIZING;
117 if (CoreVideo_SetVideoMode(800, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
119 if (CoreVideo_SetVideoMode(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
122 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
127 /* check that our opengl attributes were properly set */
129 if (CoreVideo_GL_GetAttribute(M64P_GL_DOUBLEBUFFER, &iActual) == M64ERR_SUCCESS)
130 if (iActual != iDOUBLEBUFFER)
131 DebugMessage(M64MSG_WARNING, "Failed to set GL_DOUBLEBUFFER to %i. (it's %i)", iDOUBLEBUFFER, iActual);
132 if (CoreVideo_GL_GetAttribute(M64P_GL_SWAP_CONTROL, &iActual) == M64ERR_SUCCESS)
133 if (iActual != bVerticalSync)
134 DebugMessage(M64MSG_WARNING, "Failed to set GL_SWAP_CONTROL to %i. (it's %i)", bVerticalSync, iActual);
135 if (CoreVideo_GL_GetAttribute(M64P_GL_BUFFER_SIZE, &iActual) == M64ERR_SUCCESS)
136 if (iActual != colorBufferDepth)
137 DebugMessage(M64MSG_WARNING, "Failed to set GL_BUFFER_SIZE to %i. (it's %i)", colorBufferDepth, iActual);
138 if (CoreVideo_GL_GetAttribute(M64P_GL_DEPTH_SIZE, &iActual) == M64ERR_SUCCESS)
139 if (iActual != depthBufferDepth)
140 DebugMessage(M64MSG_WARNING, "Failed to set GL_DEPTH_SIZE to %i. (it's %i)", depthBufferDepth, iActual);
143 /* Get function pointers to OpenGL extensions (blame Microsoft Windows for this) */
144 OGLExtensions_Init();
148 sprintf(caption, "%s v%i.%i.%i", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION));
149 CoreVideo_SetCaption(caption);
154 sprintf(m_strDeviceStats, "%.60s - %.128s : %.60s", m_pVendorStr, m_pRenderStr, m_pVersionStr);
155 TRACE0(m_strDeviceStats);
156 DebugMessage(M64MSG_INFO, "Using OpenGL: %s", m_strDeviceStats);
160 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
162 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
166 status.isVertexShaderEnabled = false;
171 bool COGLGraphicsContext::ResizeInitialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
175 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
177 int depthBufferDepth = options.OpenglDepthBufferSetting;
178 int colorBufferDepth = 32;
179 int bVerticalSync = windowSetting.bVerticalSync;
180 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
182 /* hard-coded attribute values */
183 const int iDOUBLEBUFFER = 1;
185 /* set opengl attributes */
186 CoreVideo_GL_SetAttribute(M64P_GL_DOUBLEBUFFER, iDOUBLEBUFFER);
187 CoreVideo_GL_SetAttribute(M64P_GL_SWAP_CONTROL, bVerticalSync);
188 CoreVideo_GL_SetAttribute(M64P_GL_BUFFER_SIZE, colorBufferDepth);
189 CoreVideo_GL_SetAttribute(M64P_GL_DEPTH_SIZE, depthBufferDepth);
191 /* set multisampling */
192 if (options.multiSampling > 0)
194 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLEBUFFERS, 1);
195 if (options.multiSampling <= 2)
196 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 2);
197 else if (options.multiSampling <= 4)
198 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 4);
199 else if (options.multiSampling <= 8)
200 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 8);
202 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
205 /* Call Mupen64plus core Video Extension to resize the window, which will create a new OpenGL Context under SDL */
206 if (CoreVideo_ResizeWindow(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight) != M64ERR_SUCCESS)
208 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
216 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
218 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
224 void COGLGraphicsContext::InitState(void)
226 m_pRenderStr = glGetString(GL_RENDERER);
227 m_pExtensionStr = glGetString(GL_EXTENSIONS);
228 m_pVersionStr = glGetString(GL_VERSION);
229 m_pVendorStr = glGetString(GL_VENDOR);
230 glMatrixMode(GL_PROJECTION);
235 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
241 glShadeModel(GL_SMOOTH);
245 //glMatrixMode(GL_MODELVIEW);
248 glDisable(GL_ALPHA_TEST);
252 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
259 glDisable(GL_CULL_FACE);
262 glDisable(GL_NORMALIZE);
266 glDepthFunc(GL_LEQUAL);
268 glEnable(GL_DEPTH_TEST);
272 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
279 glEnable(GL_ALPHA_TEST);
282 glMatrixMode(GL_PROJECTION);
289 #elif SDL_VIDEO_OPENGL_ES2
290 glDepthRangef(0.0f, 1.0f);
295 void COGLGraphicsContext::InitOGLExtension(void)
297 // important extension features, it is very bad not to have these feature
298 m_bSupportMultiTexture = IsExtensionSupported(OSAL_GL_ARB_MULTITEXTURE);
299 m_bSupportTextureEnvCombine = IsExtensionSupported("GL_EXT_texture_env_combine");
301 m_bSupportSeparateSpecularColor = IsExtensionSupported("GL_EXT_separate_specular_color");
302 m_bSupportSecondColor = IsExtensionSupported("GL_EXT_secondary_color");
303 m_bSupportFogCoord = IsExtensionSupported("GL_EXT_fog_coord");
304 m_bSupportTextureObject = IsExtensionSupported("GL_EXT_texture_object");
306 // Optional extension features
307 m_bSupportRescaleNormal = IsExtensionSupported("GL_EXT_rescale_normal");
308 m_bSupportLODBias = IsExtensionSupported("GL_EXT_texture_lod_bias");
309 m_bSupportAnisotropicFiltering = IsExtensionSupported("GL_EXT_texture_filter_anisotropic");
311 // Compute maxAnisotropicFiltering
312 m_maxAnisotropicFiltering = 0;
314 if( m_bSupportAnisotropicFiltering
315 && (options.anisotropicFiltering == 2
316 || options.anisotropicFiltering == 4
317 || options.anisotropicFiltering == 8
318 || options.anisotropicFiltering == 16))
320 //Get the max value of aniso that the graphic card support
321 glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropicFiltering);
324 // If user want more aniso than hardware can do
325 if(options.anisotropicFiltering > (uint32) m_maxAnisotropicFiltering)
327 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);
330 //check if user want less anisotropy than hardware can do
331 if((uint32) m_maxAnisotropicFiltering > options.anisotropicFiltering)
332 m_maxAnisotropicFiltering = options.anisotropicFiltering;
335 // Nvidia only extension features (optional)
336 m_bSupportNVRegisterCombiner = IsExtensionSupported("GL_NV_register_combiners");
337 m_bSupportTextureMirrorRepeat = IsExtensionSupported("GL_IBM_texture_mirrored_repeat") || IsExtensionSupported("ARB_texture_mirrored_repeat");
338 m_supportTextureMirror = m_bSupportTextureMirrorRepeat;
339 m_bSupportTextureLOD = IsExtensionSupported("GL_EXT_texture_lod");
340 m_bSupportBlendColor = IsExtensionSupported("GL_EXT_blend_color");
341 m_bSupportBlendSubtract = IsExtensionSupported("GL_EXT_blend_subtract");
342 m_bSupportNVTextureEnvCombine4 = IsExtensionSupported("GL_NV_texture_env_combine4");
346 bool COGLGraphicsContext::IsExtensionSupported(const char* pExtName)
348 if (strstr((const char*)m_pExtensionStr, pExtName) != NULL)
350 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is supported.", pExtName);
355 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is NOT supported.", pExtName);
360 bool COGLGraphicsContext::IsWglExtensionSupported(const char* pExtName)
362 if( m_pWglExtensionStr == NULL )
365 if( strstr((const char*)m_pWglExtensionStr, pExtName) != NULL )
372 void COGLGraphicsContext::CleanUp()
379 void COGLGraphicsContext::Clear(ClearFlag dwFlags, uint32 color, float depth)
382 if( dwFlags&CLEAR_COLOR_BUFFER ) flag |= GL_COLOR_BUFFER_BIT;
383 if( dwFlags&CLEAR_DEPTH_BUFFER ) flag |= GL_DEPTH_BUFFER_BIT;
385 float r = ((color>>16)&0xFF)/255.0f;
386 float g = ((color>> 8)&0xFF)/255.0f;
387 float b = ((color )&0xFF)/255.0f;
388 float a = ((color>>24)&0xFF)/255.0f;
389 glClearColor(r, g, b, a);
393 glClear(flag); //Clear color buffer and depth buffer
397 void COGLGraphicsContext::UpdateFrame(bool swaponly)
399 status.gFrameCount++;
404 //wglSwapIntervalEXT(0);
407 if (debuggerPauseCount == countToPause)
409 static int iShotNum = 0;
410 // get width, height, allocate buffer to store image
411 int width = windowSetting.uDisplayWidth;
412 int height = windowSetting.uDisplayHeight;
413 printf("Saving debug images: width=%i height=%i\n", width, height);
414 short *buffer = (short *) malloc(((width+3)&~3)*(height+1)*4);
415 glReadBuffer( GL_FRONT );
416 // set up a BMGImage struct
417 struct BMGImageStruct img;
418 memset(&img, 0, sizeof(BMGImageStruct));
420 img.bits = (unsigned char *) buffer;
421 img.bits_per_pixel = 32;
424 img.scan_width = width * 4;
425 // store the RGB color image
427 sprintf(chFilename, "dbg_rgb_%03i.png", iShotNum);
428 glReadPixels(0,0,width,height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
429 WritePNG(chFilename, img);
430 // store the Z buffer
431 sprintf(chFilename, "dbg_Z_%03i.png", iShotNum);
432 glReadPixels(0,0,width,height, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buffer);
433 //img.bits_per_pixel = 16;
434 //img.scan_width = width * 2;
435 WritePNG(chFilename, img);
436 // dump a subset of the Z data
437 for (int y = 0; y < 480; y += 16)
439 for (int x = 0; x < 640; x+= 16)
440 printf("%4hx ", buffer[y*640 + x]);
444 // free memory and get out of here
451 // if emulator defined a render callback function, call it before buffer swap
453 (*renderCallback)(status.bScreenIsDrawn);
455 CoreVideo_GL_SwapBuffers();
457 /*if(options.bShowFPS)
459 static unsigned int lastTick=0;
461 unsigned int nowTick = SDL_GetTicks();
463 if(lastTick + 5000 <= nowTick)
466 sprintf(caption, "%s v%i.%i.%i - %.3f VI/S", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION), frames/5.0);
467 CoreVideo_SetCaption(caption);
473 glDepthMask(GL_TRUE);
477 if( !g_curRomInfo.bForceScreenClear )
479 glClear(GL_DEPTH_BUFFER_BIT);
483 needCleanScene = true;
485 status.bScreenIsDrawn = false;
488 bool COGLGraphicsContext::SetFullscreenMode()
490 windowSetting.statusBarHeightToUse = 0;
491 windowSetting.toolbarHeightToUse = 0;
495 bool COGLGraphicsContext::SetWindowMode()
497 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
498 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
501 int COGLGraphicsContext::ToggleFullscreen()
503 if (CoreVideo_ToggleFullScreen() == M64ERR_SUCCESS)
505 m_bWindowed = !m_bWindowed;
512 return m_bWindowed?0:1;
515 // This is a static function, will be called when the plugin DLL is initialized
516 void COGLGraphicsContext::InitDeviceParameters()
518 status.isVertexShaderEnabled = false; // Disable it for now
522 bool COGLGraphicsContext::IsSupportAnisotropicFiltering()
524 return m_bSupportAnisotropicFiltering;
527 int COGLGraphicsContext::getMaxAnisotropicFiltering()
529 return m_maxAnisotropicFiltering;