Add a per rom config to Notaz Audio Plugin
[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;
116 if (CoreVideo_SetVideoMode(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
117 {
118 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
119 CoreVideo_Quit();
120 return false;
121 }
122
123 /* check that our opengl attributes were properly set */
124 int iActual;
125 if (CoreVideo_GL_GetAttribute(M64P_GL_DOUBLEBUFFER, &iActual) == M64ERR_SUCCESS)
126 if (iActual != iDOUBLEBUFFER)
127 DebugMessage(M64MSG_WARNING, "Failed to set GL_DOUBLEBUFFER to %i. (it's %i)", iDOUBLEBUFFER, iActual);
128 if (CoreVideo_GL_GetAttribute(M64P_GL_SWAP_CONTROL, &iActual) == M64ERR_SUCCESS)
129 if (iActual != bVerticalSync)
130 DebugMessage(M64MSG_WARNING, "Failed to set GL_SWAP_CONTROL to %i. (it's %i)", bVerticalSync, iActual);
131 if (CoreVideo_GL_GetAttribute(M64P_GL_BUFFER_SIZE, &iActual) == M64ERR_SUCCESS)
132 if (iActual != colorBufferDepth)
133 DebugMessage(M64MSG_WARNING, "Failed to set GL_BUFFER_SIZE to %i. (it's %i)", colorBufferDepth, iActual);
134 if (CoreVideo_GL_GetAttribute(M64P_GL_DEPTH_SIZE, &iActual) == M64ERR_SUCCESS)
135 if (iActual != depthBufferDepth)
136 DebugMessage(M64MSG_WARNING, "Failed to set GL_DEPTH_SIZE to %i. (it's %i)", depthBufferDepth, iActual);
137
138#if SDL_VIDEO_OPENGL
139 /* Get function pointers to OpenGL extensions (blame Microsoft Windows for this) */
140 OGLExtensions_Init();
141#endif
142
143 char caption[500];
144 sprintf(caption, "%s v%i.%i.%i", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION));
145 CoreVideo_SetCaption(caption);
146 SetWindowMode();
147
148 InitState();
149 InitOGLExtension();
150 sprintf(m_strDeviceStats, "%.60s - %.128s : %.60s", m_pVendorStr, m_pRenderStr, m_pVersionStr);
151 TRACE0(m_strDeviceStats);
152 DebugMessage(M64MSG_INFO, "Using OpenGL: %s", m_strDeviceStats);
153
154 Unlock();
155
156 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
157 UpdateFrame();
158 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
159 UpdateFrame();
160
161 m_bReady = true;
162 status.isVertexShaderEnabled = false;
163
164 return true;
165}
166
167bool COGLGraphicsContext::ResizeInitialize(uint32 dwWidth, uint32 dwHeight, BOOL bWindowed )
168{
169 Lock();
170
171 CGraphicsContext::Initialize(dwWidth, dwHeight, bWindowed );
172
173 int depthBufferDepth = options.OpenglDepthBufferSetting;
174 int colorBufferDepth = 32;
175 int bVerticalSync = windowSetting.bVerticalSync;
176 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) colorBufferDepth = 16;
177
178 /* hard-coded attribute values */
179 const int iDOUBLEBUFFER = 1;
180
181 /* set opengl attributes */
182 CoreVideo_GL_SetAttribute(M64P_GL_DOUBLEBUFFER, iDOUBLEBUFFER);
183 CoreVideo_GL_SetAttribute(M64P_GL_SWAP_CONTROL, bVerticalSync);
184 CoreVideo_GL_SetAttribute(M64P_GL_BUFFER_SIZE, colorBufferDepth);
185 CoreVideo_GL_SetAttribute(M64P_GL_DEPTH_SIZE, depthBufferDepth);
186
187 /* set multisampling */
188 if (options.multiSampling > 0)
189 {
190 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLEBUFFERS, 1);
191 if (options.multiSampling <= 2)
192 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 2);
193 else if (options.multiSampling <= 4)
194 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 4);
195 else if (options.multiSampling <= 8)
196 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 8);
197 else
198 CoreVideo_GL_SetAttribute(M64P_GL_MULTISAMPLESAMPLES, 16);
199 }
200
201 /* Call Mupen64plus core Video Extension to resize the window, which will create a new OpenGL Context under SDL */
202 if (CoreVideo_ResizeWindow(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight) != M64ERR_SUCCESS)
203 {
204 DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
205 CoreVideo_Quit();
206 return false;
207 }
208
209 InitState();
210 Unlock();
211
212 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER); // Clear buffers
213 UpdateFrame();
214 Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
215 UpdateFrame();
216
217 return true;
218}
219
220void COGLGraphicsContext::InitState(void)
221{
222 m_pRenderStr = glGetString(GL_RENDERER);
223 m_pExtensionStr = glGetString(GL_EXTENSIONS);
224 m_pVersionStr = glGetString(GL_VERSION);
225 m_pVendorStr = glGetString(GL_VENDOR);
226 glMatrixMode(GL_PROJECTION);
227 OPENGL_CHECK_ERRORS;
228 glLoadIdentity();
229 OPENGL_CHECK_ERRORS;
230
231 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
232 OPENGL_CHECK_ERRORS;
233 glClearDepth(1.0f);
234 OPENGL_CHECK_ERRORS;
235
236#if SDL_VIDEO_OPENGL
237 glShadeModel(GL_SMOOTH);
238 OPENGL_CHECK_ERRORS;
239
240 //position viewer
241 //glMatrixMode(GL_MODELVIEW);
242 //glLoadIdentity();
243
244 glDisable(GL_ALPHA_TEST);
245 OPENGL_CHECK_ERRORS;
246#endif
247
248 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
249 OPENGL_CHECK_ERRORS;
250 glDisable(GL_BLEND);
251 OPENGL_CHECK_ERRORS;
252
253 glFrontFace(GL_CCW);
254 OPENGL_CHECK_ERRORS;
255 glDisable(GL_CULL_FACE);
256 OPENGL_CHECK_ERRORS;
257#if SDL_VIDEO_OPENGL
258 glDisable(GL_NORMALIZE);
259 OPENGL_CHECK_ERRORS;
260#endif
261
262 glDepthFunc(GL_LEQUAL);
263 OPENGL_CHECK_ERRORS;
264 glEnable(GL_DEPTH_TEST);
265 OPENGL_CHECK_ERRORS;
266
267#if SDL_VIDEO_OPENGL
268 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
269 OPENGL_CHECK_ERRORS;
270#endif
271
272 glEnable(GL_BLEND);
273 OPENGL_CHECK_ERRORS;
274#if SDL_VIDEO_OPENGL
275 glEnable(GL_ALPHA_TEST);
276 OPENGL_CHECK_ERRORS;
277
278 glMatrixMode(GL_PROJECTION);
279 OPENGL_CHECK_ERRORS;
280 glLoadIdentity();
281 OPENGL_CHECK_ERRORS;
282
283 glDepthRange(-1, 1);
284
285#elif SDL_VIDEO_OPENGL_ES2
286 glDepthRangef(0.0f, 1.0f);
287#endif
288 OPENGL_CHECK_ERRORS;
289}
290
291void COGLGraphicsContext::InitOGLExtension(void)
292{
293 // important extension features, it is very bad not to have these feature
294 m_bSupportMultiTexture = IsExtensionSupported(OSAL_GL_ARB_MULTITEXTURE);
295 m_bSupportTextureEnvCombine = IsExtensionSupported("GL_EXT_texture_env_combine");
296
297 m_bSupportSeparateSpecularColor = IsExtensionSupported("GL_EXT_separate_specular_color");
298 m_bSupportSecondColor = IsExtensionSupported("GL_EXT_secondary_color");
299 m_bSupportFogCoord = IsExtensionSupported("GL_EXT_fog_coord");
300 m_bSupportTextureObject = IsExtensionSupported("GL_EXT_texture_object");
301
302 // Optional extension features
303 m_bSupportRescaleNormal = IsExtensionSupported("GL_EXT_rescale_normal");
304 m_bSupportLODBias = IsExtensionSupported("GL_EXT_texture_lod_bias");
305 m_bSupportAnisotropicFiltering = IsExtensionSupported("GL_EXT_texture_filter_anisotropic");
306
307 // Compute maxAnisotropicFiltering
308 m_maxAnisotropicFiltering = 0;
309
310 if( m_bSupportAnisotropicFiltering
311 && (options.anisotropicFiltering == 2
312 || options.anisotropicFiltering == 4
313 || options.anisotropicFiltering == 8
314 || options.anisotropicFiltering == 16))
315 {
316 //Get the max value of aniso that the graphic card support
317 glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropicFiltering);
318 OPENGL_CHECK_ERRORS;
319
320 // If user want more aniso than hardware can do
321 if(options.anisotropicFiltering > (uint32) m_maxAnisotropicFiltering)
322 {
323 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);
324 }
325
326 //check if user want less anisotropy than hardware can do
327 if((uint32) m_maxAnisotropicFiltering > options.anisotropicFiltering)
328 m_maxAnisotropicFiltering = options.anisotropicFiltering;
329 }
330
331 // Nvidia only extension features (optional)
332 m_bSupportNVRegisterCombiner = IsExtensionSupported("GL_NV_register_combiners");
333 m_bSupportTextureMirrorRepeat = IsExtensionSupported("GL_IBM_texture_mirrored_repeat") || IsExtensionSupported("ARB_texture_mirrored_repeat");
334 m_supportTextureMirror = m_bSupportTextureMirrorRepeat;
335 m_bSupportTextureLOD = IsExtensionSupported("GL_EXT_texture_lod");
336 m_bSupportBlendColor = IsExtensionSupported("GL_EXT_blend_color");
337 m_bSupportBlendSubtract = IsExtensionSupported("GL_EXT_blend_subtract");
338 m_bSupportNVTextureEnvCombine4 = IsExtensionSupported("GL_NV_texture_env_combine4");
339
340}
341
342bool COGLGraphicsContext::IsExtensionSupported(const char* pExtName)
343{
344 if (strstr((const char*)m_pExtensionStr, pExtName) != NULL)
345 {
346 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is supported.", pExtName);
347 return true;
348 }
349 else
350 {
351 DebugMessage(M64MSG_VERBOSE, "OpenGL Extension '%s' is NOT supported.", pExtName);
352 return false;
353 }
354}
355
356bool COGLGraphicsContext::IsWglExtensionSupported(const char* pExtName)
357{
358 if( m_pWglExtensionStr == NULL )
359 return false;
360
361 if( strstr((const char*)m_pWglExtensionStr, pExtName) != NULL )
362 return true;
363 else
364 return false;
365}
366
367
368void COGLGraphicsContext::CleanUp()
369{
370 CoreVideo_Quit();
371 m_bReady = false;
372}
373
374
375void COGLGraphicsContext::Clear(ClearFlag dwFlags, uint32 color, float depth)
376{
377 uint32 flag=0;
378 if( dwFlags&CLEAR_COLOR_BUFFER ) flag |= GL_COLOR_BUFFER_BIT;
379 if( dwFlags&CLEAR_DEPTH_BUFFER ) flag |= GL_DEPTH_BUFFER_BIT;
380
381 float r = ((color>>16)&0xFF)/255.0f;
382 float g = ((color>> 8)&0xFF)/255.0f;
383 float b = ((color )&0xFF)/255.0f;
384 float a = ((color>>24)&0xFF)/255.0f;
385 glClearColor(r, g, b, a);
386 OPENGL_CHECK_ERRORS;
387 glClearDepth(depth);
388 OPENGL_CHECK_ERRORS;
389 glClear(flag); //Clear color buffer and depth buffer
390 OPENGL_CHECK_ERRORS;
391}
392
393void COGLGraphicsContext::UpdateFrame(bool swaponly)
394{
395 status.gFrameCount++;
396
397 glFlush();
398 OPENGL_CHECK_ERRORS;
399 //glFinish();
400 //wglSwapIntervalEXT(0);
401
402 /*
403 if (debuggerPauseCount == countToPause)
404 {
405 static int iShotNum = 0;
406 // get width, height, allocate buffer to store image
407 int width = windowSetting.uDisplayWidth;
408 int height = windowSetting.uDisplayHeight;
409 printf("Saving debug images: width=%i height=%i\n", width, height);
410 short *buffer = (short *) malloc(((width+3)&~3)*(height+1)*4);
411 glReadBuffer( GL_FRONT );
412 // set up a BMGImage struct
413 struct BMGImageStruct img;
414 memset(&img, 0, sizeof(BMGImageStruct));
415 InitBMGImage(&img);
416 img.bits = (unsigned char *) buffer;
417 img.bits_per_pixel = 32;
418 img.height = height;
419 img.width = width;
420 img.scan_width = width * 4;
421 // store the RGB color image
422 char chFilename[64];
423 sprintf(chFilename, "dbg_rgb_%03i.png", iShotNum);
424 glReadPixels(0,0,width,height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
425 WritePNG(chFilename, img);
426 // store the Z buffer
427 sprintf(chFilename, "dbg_Z_%03i.png", iShotNum);
428 glReadPixels(0,0,width,height, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buffer);
429 //img.bits_per_pixel = 16;
430 //img.scan_width = width * 2;
431 WritePNG(chFilename, img);
432 // dump a subset of the Z data
433 for (int y = 0; y < 480; y += 16)
434 {
435 for (int x = 0; x < 640; x+= 16)
436 printf("%4hx ", buffer[y*640 + x]);
437 printf("\n");
438 }
439 printf("\n");
440 // free memory and get out of here
441 free(buffer);
442 iShotNum++;
443 }
444 */
445
446
447 // if emulator defined a render callback function, call it before buffer swap
448 if(renderCallback)
449 (*renderCallback)(status.bScreenIsDrawn);
450
451 CoreVideo_GL_SwapBuffers();
452
453 /*if(options.bShowFPS)
454 {
455 static unsigned int lastTick=0;
456 static int frames=0;
457 unsigned int nowTick = SDL_GetTicks();
458 frames++;
459 if(lastTick + 5000 <= nowTick)
460 {
461 char caption[200];
462 sprintf(caption, "%s v%i.%i.%i - %.3f VI/S", PLUGIN_NAME, VERSION_PRINTF_SPLIT(PLUGIN_VERSION), frames/5.0);
463 CoreVideo_SetCaption(caption);
464 frames = 0;
465 lastTick = nowTick;
466 }
467 }*/
468
469 glDepthMask(GL_TRUE);
470 OPENGL_CHECK_ERRORS;
471 glClearDepth(1.0f);
472 OPENGL_CHECK_ERRORS;
473 if( !g_curRomInfo.bForceScreenClear )
474 {
475 glClear(GL_DEPTH_BUFFER_BIT);
476 OPENGL_CHECK_ERRORS;
477 }
478 else
479 needCleanScene = true;
480
481 status.bScreenIsDrawn = false;
482}
483
484bool COGLGraphicsContext::SetFullscreenMode()
485{
486 windowSetting.statusBarHeightToUse = 0;
487 windowSetting.toolbarHeightToUse = 0;
488 return true;
489}
490
491bool COGLGraphicsContext::SetWindowMode()
492{
493 windowSetting.statusBarHeightToUse = windowSetting.statusBarHeight;
494 windowSetting.toolbarHeightToUse = windowSetting.toolbarHeight;
495 return true;
496}
497int COGLGraphicsContext::ToggleFullscreen()
498{
499 if (CoreVideo_ToggleFullScreen() == M64ERR_SUCCESS)
500 {
501 m_bWindowed = !m_bWindowed;
502 if(m_bWindowed)
503 SetWindowMode();
504 else
505 SetFullscreenMode();
506 }
507
508 return m_bWindowed?0:1;
509}
510
511// This is a static function, will be called when the plugin DLL is initialized
512void COGLGraphicsContext::InitDeviceParameters()
513{
514 status.isVertexShaderEnabled = false; // Disable it for now
515}
516
517// Get methods
518bool COGLGraphicsContext::IsSupportAnisotropicFiltering()
519{
520 return m_bSupportAnisotropicFiltering;
521}
522
523int COGLGraphicsContext::getMaxAnisotropicFiltering()
524{
525 return m_maxAnisotropicFiltering;
526}