GLE2RICE: Some fixes from muppen64plus-ae
[mupen64plus-pandora.git] / source / gles2rice / src / OGLGraphicsContext.cpp
CommitLineData
292f9317 1/*
2Copyright (C) 2003 Rice1964
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#include "osal_opengl.h"
20
21#define M64P_PLUGIN_PROTOTYPES 1
22#include "m64p_plugin.h"
23#include "Config.h"
24#include "Debugger.h"
25#if SDL_VIDEO_OPENGL
26#include "OGLExtensions.h"
27#endif
28#include "OGLDebug.h"
29#include "OGLGraphicsContext.h"
30#include "TextureManager.h"
31#include "Video.h"
32#include "version.h"
33
34COGLGraphicsContext::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),
49 m_pVendorStr(NULL),
50 m_pRenderStr(NULL),
51 m_pExtensionStr(NULL),
52 m_pVersionStr(NULL)
53{
54}
55
56
57COGLGraphicsContext::~COGLGraphicsContext()
58{
59}
60
61bool COGLGraphicsContext::Initialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
62{
63 DebugMessage(M64MSG_INFO, "Initializing OpenGL Device Context.");
64 Lock();
65
66 CGraphicsContext::Get()->m_supportTextureMirror = false;
67 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
68
69 if( bWindowed )
70 {
71 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
72 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
73 }
74 else
75 {
76 windowSetting.statusBarHeightToUse = 0;
77 windowSetting.toolbarHeightToUse = 0;
78 }
79
80 int depthBufferDepth = options.OpenglDepthBufferSetting;
81 int colorBufferDepth = 32;
82 int bVerticalSync = windowSetting.bVerticalSync;
83 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
84
85 // init sdl & gl
86 DebugMessage(M64MSG_VERBOSE, "Initializing video subsystem...");
87 if (CoreVideo_Init() != M64ERR_SUCCESS)
88 return false;
89
90 /* hard-coded attribute values */
91 const int iDOUBLEBUFFER = 1;
92
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);
98
99 /* set multisampling */
100 if (options.multiSampling > 0)
101 {
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);
109 else
110 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
111 }
112
113 /* Set the video mode */
114 m64p_video_mode ScreenMode = bWindowed ? M64VIDEO_WINDOWED : M64VIDEO_FULLSCREEN;
115 m64p_video_flags flags = M64VIDEOFLAG_SUPPORT_RESIZING;
ca22e7b7 116#if 1
117 if (CoreVideo_SetVideoMode(800, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
118#else
292f9317 119 if (CoreVideo_SetVideoMode(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
ca22e7b7 120#endif
292f9317 121 {
122 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
123 CoreVideo_Quit();
124 return false;
125 }
126
127 /* check that our opengl attributes were properly set */
128 int iActual;
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);
141
142#if SDL_VIDEO_OPENGL
143 /* Get function pointers to OpenGL extensions (blame Microsoft Windows for this) */
144 OGLExtensions_Init();
145#endif
146
147 char caption[500];
148 sprintf(caption, "%s v%i.%i.%i", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION));
149 CoreVideo_SetCaption(caption);
150 SetWindowMode();
151
152 InitState();
153 InitOGLExtension();
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);
157
158 Unlock();
159
160 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
161 UpdateFrame();
162 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
163 UpdateFrame();
164
165 m_bReady = true;
166 status.isVertexShaderEnabled = false;
167
168 return true;
169}
170
171bool COGLGraphicsContext::ResizeInitialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
172{
173 Lock();
174
175 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
176
177 int depthBufferDepth = options.OpenglDepthBufferSetting;
178 int colorBufferDepth = 32;
179 int bVerticalSync = windowSetting.bVerticalSync;
180 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
181
182 /* hard-coded attribute values */
183 const int iDOUBLEBUFFER = 1;
184
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);
190
191 /* set multisampling */
192 if (options.multiSampling > 0)
193 {
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);
201 else
202 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
203 }
204
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)
207 {
208 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
209 CoreVideo_Quit();
210 return false;
211 }
212
213 InitState();
214 Unlock();
215
216 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
217 UpdateFrame();
218 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
219 UpdateFrame();
220
221 return true;
222}
223
224void COGLGraphicsContext::InitState(void)
225{
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);
231 OPENGL_CHECK_ERRORS;
232 glLoadIdentity();
233 OPENGL_CHECK_ERRORS;
234
235 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
236 OPENGL_CHECK_ERRORS;
237 glClearDepth(1.0f);
238 OPENGL_CHECK_ERRORS;
239
240#if SDL_VIDEO_OPENGL
241 glShadeModel(GL_SMOOTH);
242 OPENGL_CHECK_ERRORS;
243
244 //position viewer
245 //glMatrixMode(GL_MODELVIEW);
246 //glLoadIdentity();
247
248 glDisable(GL_ALPHA_TEST);
249 OPENGL_CHECK_ERRORS;
250#endif
251
252 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
253 OPENGL_CHECK_ERRORS;
254 glDisable(GL_BLEND);
255 OPENGL_CHECK_ERRORS;
256
257 glFrontFace(GL_CCW);
258 OPENGL_CHECK_ERRORS;
259 glDisable(GL_CULL_FACE);
260 OPENGL_CHECK_ERRORS;
261#if SDL_VIDEO_OPENGL
262 glDisable(GL_NORMALIZE);
263 OPENGL_CHECK_ERRORS;
264#endif
265
266 glDepthFunc(GL_LEQUAL);
267 OPENGL_CHECK_ERRORS;
268 glEnable(GL_DEPTH_TEST);
269 OPENGL_CHECK_ERRORS;
270
271#if SDL_VIDEO_OPENGL
272 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
273 OPENGL_CHECK_ERRORS;
274#endif
275
276 glEnable(GL_BLEND);
277 OPENGL_CHECK_ERRORS;
278#if SDL_VIDEO_OPENGL
279 glEnable(GL_ALPHA_TEST);
280 OPENGL_CHECK_ERRORS;
281
282 glMatrixMode(GL_PROJECTION);
283 OPENGL_CHECK_ERRORS;
284 glLoadIdentity();
285 OPENGL_CHECK_ERRORS;
286
287 glDepthRange(-1, 1);
288
289#elif SDL_VIDEO_OPENGL_ES2
0c9dca78 290 glDepthRangef(-1.0f, 1.0f);
292f9317 291#endif
292 OPENGL_CHECK_ERRORS;
293}
294
295void COGLGraphicsContext::InitOGLExtension(void)
296{
0c9dca78 297#if SDL_VIDEO_OPENGL
292f9317 298 // important extension features, it is very bad not to have these feature
299 m_bSupportMultiTexture = IsExtensionSupported(OSAL_GL_ARB_MULTITEXTURE);
300 m_bSupportTextureEnvCombine = IsExtensionSupported("GL_EXT_texture_env_combine");
301
302 m_bSupportSeparateSpecularColor = IsExtensionSupported("GL_EXT_separate_specular_color");
303 m_bSupportSecondColor = IsExtensionSupported("GL_EXT_secondary_color");
304 m_bSupportFogCoord = IsExtensionSupported("GL_EXT_fog_coord");
305 m_bSupportTextureObject = IsExtensionSupported("GL_EXT_texture_object");
306
307 // Optional extension features
308 m_bSupportRescaleNormal = IsExtensionSupported("GL_EXT_rescale_normal");
309 m_bSupportLODBias = IsExtensionSupported("GL_EXT_texture_lod_bias");
310 m_bSupportAnisotropicFiltering = IsExtensionSupported("GL_EXT_texture_filter_anisotropic");
0c9dca78 311#else
312 m_bSupportMultiTexture = true;
313 m_bSupportFogCoord = false;
314 m_bSupportAnisotropicFiltering = true;
315#endif
292f9317 316 // Compute maxAnisotropicFiltering
317 m_maxAnisotropicFiltering = 0;
318
319 if( m_bSupportAnisotropicFiltering
320 && (options.anisotropicFiltering == 2
321 || options.anisotropicFiltering == 4
322 || options.anisotropicFiltering == 8
323 || options.anisotropicFiltering == 16))
324 {
325 //Get the max value of aniso that the graphic card support
326 glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropicFiltering);
327 OPENGL_CHECK_ERRORS;
328
329 // If user want more aniso than hardware can do
330 if(options.anisotropicFiltering > (uint32) m_maxAnisotropicFiltering)
331 {
332 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);
333 }
334
335 //check if user want less anisotropy than hardware can do
336 if((uint32) m_maxAnisotropicFiltering > options.anisotropicFiltering)
337 m_maxAnisotropicFiltering = options.anisotropicFiltering;
338 }
0c9dca78 339
340#if SDL_VIDEO_OPENGL
292f9317 341 // Nvidia only extension features (optional)
342 m_bSupportNVRegisterCombiner = IsExtensionSupported("GL_NV_register_combiners");
343 m_bSupportTextureMirrorRepeat = IsExtensionSupported("GL_IBM_texture_mirrored_repeat") || IsExtensionSupported("ARB_texture_mirrored_repeat");
344 m_supportTextureMirror = m_bSupportTextureMirrorRepeat;
345 m_bSupportTextureLOD = IsExtensionSupported("GL_EXT_texture_lod");
346 m_bSupportBlendColor = IsExtensionSupported("GL_EXT_blend_color");
347 m_bSupportBlendSubtract = IsExtensionSupported("GL_EXT_blend_subtract");
348 m_bSupportNVTextureEnvCombine4 = IsExtensionSupported("GL_NV_texture_env_combine4");
0c9dca78 349#else
350 m_supportTextureMirror = true;
351#endif
292f9317 352}
353
354bool COGLGraphicsContext::IsExtensionSupported(const char* pExtName)
355{
356 if (strstr((const char*)m_pExtensionStr, pExtName) != NULL)
357 {
358 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is supported.", pExtName);
359 return true;
360 }
361 else
362 {
363 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is NOT supported.", pExtName);
364 return false;
365 }
366}
367
368bool COGLGraphicsContext::IsWglExtensionSupported(const char* pExtName)
369{
370 if( m_pWglExtensionStr == NULL )
371 return false;
372
373 if( strstr((const char*)m_pWglExtensionStr, pExtName) != NULL )
374 return true;
375 else
376 return false;
377}
378
379
380void COGLGraphicsContext::CleanUp()
381{
382 CoreVideo_Quit();
383 m_bReady = false;
384}
385
386
387void COGLGraphicsContext::Clear(ClearFlag dwFlags, uint32 color, float depth)
388{
389 uint32 flag=0;
390 if( dwFlags&CLEAR_COLOR_BUFFER ) flag |= GL_COLOR_BUFFER_BIT;
391 if( dwFlags&CLEAR_DEPTH_BUFFER ) flag |= GL_DEPTH_BUFFER_BIT;
392
393 float r = ((color>>16)&0xFF)/255.0f;
394 float g = ((color>> 8)&0xFF)/255.0f;
395 float b = ((color )&0xFF)/255.0f;
396 float a = ((color>>24)&0xFF)/255.0f;
397 glClearColor(r, g, b, a);
398 OPENGL_CHECK_ERRORS;
399 glClearDepth(depth);
400 OPENGL_CHECK_ERRORS;
401 glClear(flag); //Clear color buffer and depth buffer
402 OPENGL_CHECK_ERRORS;
403}
404
405void COGLGraphicsContext::UpdateFrame(bool swaponly)
406{
407 status.gFrameCount++;
408
409 glFlush();
410 OPENGL_CHECK_ERRORS;
411 //glFinish();
412 //wglSwapIntervalEXT(0);
413
414 /*
415 if (debuggerPauseCount == countToPause)
416 {
417 static int iShotNum = 0;
418 // get width, height, allocate buffer to store image
419 int width = windowSetting.uDisplayWidth;
420 int height = windowSetting.uDisplayHeight;
421 printf("Saving debug images: width=%i height=%i\n", width, height);
422 short *buffer = (short *) malloc(((width+3)&~3)*(height+1)*4);
423 glReadBuffer( GL_FRONT );
424 // set up a BMGImage struct
425 struct BMGImageStruct img;
426 memset(&img, 0, sizeof(BMGImageStruct));
427 InitBMGImage(&img);
428 img.bits = (unsigned char *) buffer;
429 img.bits_per_pixel = 32;
430 img.height = height;
431 img.width = width;
432 img.scan_width = width * 4;
433 // store the RGB color image
434 char chFilename[64];
435 sprintf(chFilename, "dbg_rgb_%03i.png", iShotNum);
436 glReadPixels(0,0,width,height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
437 WritePNG(chFilename, img);
438 // store the Z buffer
439 sprintf(chFilename, "dbg_Z_%03i.png", iShotNum);
440 glReadPixels(0,0,width,height, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buffer);
441 //img.bits_per_pixel = 16;
442 //img.scan_width = width * 2;
443 WritePNG(chFilename, img);
444 // dump a subset of the Z data
445 for (int y = 0; y < 480; y += 16)
446 {
447 for (int x = 0; x < 640; x+= 16)
448 printf("%4hx ", buffer[y*640 + x]);
449 printf("\n");
450 }
451 printf("\n");
452 // free memory and get out of here
453 free(buffer);
454 iShotNum++;
455 }
456 */
457
458
459 // if emulator defined a render callback function, call it before buffer swap
460 if(renderCallback)
461 (*renderCallback)(status.bScreenIsDrawn);
462
463 CoreVideo_GL_SwapBuffers();
464
465 /*if(options.bShowFPS)
466 {
467 static unsigned int lastTick=0;
468 static int frames=0;
469 unsigned int nowTick = SDL_GetTicks();
470 frames++;
471 if(lastTick + 5000 <= nowTick)
472 {
473 char caption[200];
474 sprintf(caption, "%s v%i.%i.%i - %.3f VI/S", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION), frames/5.0);
475 CoreVideo_SetCaption(caption);
476 frames = 0;
477 lastTick = nowTick;
478 }
479 }*/
480
481 glDepthMask(GL_TRUE);
482 OPENGL_CHECK_ERRORS;
483 glClearDepth(1.0f);
484 OPENGL_CHECK_ERRORS;
485 if( !g_curRomInfo.bForceScreenClear )
486 {
487 glClear(GL_DEPTH_BUFFER_BIT);
488 OPENGL_CHECK_ERRORS;
489 }
490 else
491 needCleanScene = true;
492
493 status.bScreenIsDrawn = false;
494}
495
496bool COGLGraphicsContext::SetFullscreenMode()
497{
498 windowSetting.statusBarHeightToUse = 0;
499 windowSetting.toolbarHeightToUse = 0;
500 return true;
501}
502
503bool COGLGraphicsContext::SetWindowMode()
504{
505 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
506 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
507 return true;
508}
509int COGLGraphicsContext::ToggleFullscreen()
510{
511 if (CoreVideo_ToggleFullScreen() == M64ERR_SUCCESS)
512 {
513 m_bWindowed = !m_bWindowed;
514 if(m_bWindowed)
515 SetWindowMode();
516 else
517 SetFullscreenMode();
518 }
519
520 return m_bWindowed?0:1;
521}
522
523// This is a static function, will be called when the plugin DLL is initialized
524void COGLGraphicsContext::InitDeviceParameters()
525{
526 status.isVertexShaderEnabled = false; // Disable it for now
527}
528
529// Get methods
530bool COGLGraphicsContext::IsSupportAnisotropicFiltering()
531{
532 return m_bSupportAnisotropicFiltering;
533}
534
535int COGLGraphicsContext::getMaxAnisotropicFiltering()
536{
537 return m_maxAnisotropicFiltering;
538}