2011-01-17 19:54:58 +00:00
|
|
|
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
2011-01-23 19:29:28 +00:00
|
|
|
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
|
2010-05-28 16:22:06 +00:00
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
#include "driver.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "libsnes.hpp"
|
|
|
|
#include <stdio.h>
|
2010-08-16 17:49:54 +00:00
|
|
|
#include <sys/time.h>
|
2010-12-26 03:15:12 +00:00
|
|
|
#include <string.h>
|
2010-12-29 19:05:57 +00:00
|
|
|
#include "general.h"
|
2011-03-01 13:46:58 +00:00
|
|
|
#include <assert.h>
|
2011-03-06 18:56:35 +00:00
|
|
|
#include <math.h>
|
2011-01-07 16:59:53 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-12-30 11:54:16 +00:00
|
|
|
#include "config.h"
|
2011-01-07 16:59:53 +00:00
|
|
|
#endif
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-03-14 21:09:35 +00:00
|
|
|
#include "gl_common.h"
|
2011-03-26 17:34:58 +00:00
|
|
|
#include "gfx_common.h"
|
2011-01-06 22:15:49 +00:00
|
|
|
|
2011-02-04 12:46:51 +00:00
|
|
|
#define NO_SDL_GLEXT
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_opengl.h"
|
|
|
|
#include "input/ssnes_sdl_input.h"
|
|
|
|
|
2010-11-12 22:09:43 +00:00
|
|
|
#ifdef HAVE_CG
|
2011-01-05 16:22:12 +00:00
|
|
|
#include "shader_cg.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
#include "shader_glsl.h"
|
2010-11-12 22:09:43 +00:00
|
|
|
#endif
|
2010-11-12 21:32:38 +00:00
|
|
|
|
2011-01-11 21:13:55 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
#include "fonts.h"
|
|
|
|
#endif
|
|
|
|
|
2010-11-08 23:01:17 +00:00
|
|
|
static const GLfloat vertexes[] = {
|
2011-03-12 18:28:56 +00:00
|
|
|
0, 0,
|
|
|
|
0, 1,
|
|
|
|
1, 1,
|
|
|
|
1, 0
|
2010-11-08 23:01:17 +00:00
|
|
|
};
|
|
|
|
|
2011-06-01 10:19:48 +00:00
|
|
|
static const GLfloat vertexes_flipped[] = {
|
|
|
|
0, 1,
|
|
|
|
0, 0,
|
|
|
|
1, 0,
|
|
|
|
1, 1
|
|
|
|
};
|
|
|
|
|
2010-11-12 21:32:38 +00:00
|
|
|
static const GLfloat tex_coords[] = {
|
|
|
|
0, 1,
|
2010-11-08 23:13:57 +00:00
|
|
|
0, 0,
|
2010-11-12 21:32:38 +00:00
|
|
|
1, 0,
|
|
|
|
1, 1
|
2010-11-08 23:13:57 +00:00
|
|
|
};
|
|
|
|
|
2011-03-06 15:52:49 +00:00
|
|
|
static const GLfloat fbo_tex_coords[] = {
|
|
|
|
0, 0,
|
|
|
|
0, 1,
|
|
|
|
1, 1,
|
|
|
|
1, 0
|
|
|
|
};
|
|
|
|
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 21:18:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static PFNGLGENFRAMEBUFFERSPROC pglGenFramebuffers = NULL;
|
|
|
|
static PFNGLBINDFRAMEBUFFERPROC pglBindFramebuffer = NULL;
|
|
|
|
static PFNGLFRAMEBUFFERTEXTURE2DPROC pglFramebufferTexture2D = NULL;
|
|
|
|
static PFNGLCHECKFRAMEBUFFERSTATUSPROC pglCheckFramebufferStatus = NULL;
|
|
|
|
static PFNGLDELETEFRAMEBUFFERSPROC pglDeleteFramebuffers = NULL;
|
|
|
|
|
2011-05-31 13:03:59 +00:00
|
|
|
#define LOAD_SYM(sym) if (!p##sym) { SDL_SYM_WRAP(p##sym, #sym) }
|
2011-03-06 21:18:25 +00:00
|
|
|
static bool load_fbo_proc(void)
|
|
|
|
{
|
|
|
|
LOAD_SYM(glGenFramebuffers);
|
|
|
|
LOAD_SYM(glBindFramebuffer);
|
|
|
|
LOAD_SYM(glFramebufferTexture2D);
|
|
|
|
LOAD_SYM(glCheckFramebufferStatus);
|
|
|
|
LOAD_SYM(glDeleteFramebuffers);
|
|
|
|
|
|
|
|
return pglGenFramebuffers && pglBindFramebuffer && pglFramebufferTexture2D &&
|
|
|
|
pglCheckFramebufferStatus && pglDeleteFramebuffers;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define pglGenFramebuffers glGenFramebuffers
|
|
|
|
#define pglBindFramebuffer glBindFramebuffer
|
|
|
|
#define pglFramebufferTexture2D glFramebufferTexture2D
|
|
|
|
#define pglCheckFramebufferStatus glCheckFramebufferStatus
|
|
|
|
#define pglDeleteFramebuffers glDeleteFramebuffers
|
|
|
|
static bool load_fbo_proc(void) { return true; }
|
|
|
|
#endif
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2011-03-06 21:18:25 +00:00
|
|
|
|
2011-05-22 16:48:43 +00:00
|
|
|
#if (defined(HAVE_XML) || defined(HAVE_CG)) && defined(_WIN32)
|
2011-05-18 20:40:42 +00:00
|
|
|
PFNGLCLIENTACTIVETEXTUREPROC pglClientActiveTexture = NULL;
|
|
|
|
PFNGLACTIVETEXTUREPROC pglActiveTexture = NULL;
|
|
|
|
static inline bool load_gl_proc(void)
|
|
|
|
{
|
|
|
|
LOAD_SYM(glClientActiveTexture);
|
|
|
|
LOAD_SYM(glActiveTexture);
|
|
|
|
return pglClientActiveTexture && pglActiveTexture;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool load_gl_proc(void) { return true; }
|
|
|
|
#endif
|
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
#define MAX_SHADERS 16
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
typedef struct gl
|
|
|
|
{
|
2010-08-16 16:40:17 +00:00
|
|
|
bool vsync;
|
2010-11-13 01:48:32 +00:00
|
|
|
GLuint texture;
|
|
|
|
GLuint tex_filter;
|
2010-12-26 03:15:12 +00:00
|
|
|
|
2011-05-31 21:03:01 +00:00
|
|
|
void *empty_buf;
|
|
|
|
|
2011-05-11 11:24:59 +00:00
|
|
|
unsigned frame_count;
|
|
|
|
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 18:56:35 +00:00
|
|
|
// Render-to-texture, multipass shaders
|
2011-03-12 15:33:01 +00:00
|
|
|
GLuint fbo[MAX_SHADERS];
|
|
|
|
GLuint fbo_texture[MAX_SHADERS];
|
2011-03-14 21:09:35 +00:00
|
|
|
struct gl_fbo_rect fbo_rect[MAX_SHADERS];
|
2011-03-14 22:20:51 +00:00
|
|
|
struct gl_fbo_scale fbo_scale[MAX_SHADERS];
|
2011-03-06 15:52:49 +00:00
|
|
|
bool render_to_tex;
|
2011-03-12 15:33:01 +00:00
|
|
|
int fbo_pass;
|
2011-03-06 18:56:35 +00:00
|
|
|
bool fbo_inited;
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2011-03-06 15:52:49 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
bool should_resize;
|
|
|
|
bool quitting;
|
2011-01-22 23:27:20 +00:00
|
|
|
bool keep_aspect;
|
2011-01-06 19:01:32 +00:00
|
|
|
|
2011-02-28 15:59:31 +00:00
|
|
|
unsigned full_x, full_y;
|
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
unsigned win_width;
|
|
|
|
unsigned win_height;
|
2011-03-27 18:29:47 +00:00
|
|
|
unsigned vp_width, vp_out_width;
|
|
|
|
unsigned vp_height, vp_out_height;
|
2010-12-26 03:15:12 +00:00
|
|
|
unsigned last_width;
|
|
|
|
unsigned last_height;
|
|
|
|
unsigned tex_w, tex_h;
|
|
|
|
GLfloat tex_coords[8];
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 18:56:35 +00:00
|
|
|
GLfloat fbo_tex_coords[8];
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-05-28 12:11:37 +00:00
|
|
|
GLenum texture_type; // XBGR1555 or ARGB
|
2011-03-07 18:12:14 +00:00
|
|
|
GLenum texture_fmt;
|
|
|
|
unsigned base_size; // 2 or 4
|
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
font_renderer_t *font;
|
|
|
|
GLuint font_tex;
|
|
|
|
#endif
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
} gl_t;
|
|
|
|
|
|
|
|
////////////////// Shaders
|
2011-02-28 15:59:31 +00:00
|
|
|
static bool gl_shader_init(void)
|
2011-01-05 16:22:12 +00:00
|
|
|
{
|
2011-02-28 15:59:31 +00:00
|
|
|
switch (g_settings.video.shader_type)
|
|
|
|
{
|
|
|
|
case SSNES_SHADER_AUTO:
|
|
|
|
{
|
|
|
|
if (strlen(g_settings.video.cg_shader_path) > 0 && strlen(g_settings.video.bsnes_shader_path) > 0)
|
|
|
|
SSNES_WARN("Both Cg and bSNES XML shader are defined in config file. Cg shader will be selected by default.\n");
|
|
|
|
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
if (strlen(g_settings.video.cg_shader_path) > 0)
|
|
|
|
return gl_cg_init(g_settings.video.cg_shader_path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
if (strlen(g_settings.video.bsnes_shader_path) > 0)
|
|
|
|
return gl_glsl_init(g_settings.video.bsnes_shader_path);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2011-01-05 18:29:29 +00:00
|
|
|
|
2011-01-05 16:22:12 +00:00
|
|
|
#ifdef HAVE_CG
|
2011-02-28 15:59:31 +00:00
|
|
|
case SSNES_SHADER_CG:
|
|
|
|
{
|
|
|
|
return gl_cg_init(g_settings.video.cg_shader_path);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-05 16:22:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
2011-02-28 15:59:31 +00:00
|
|
|
case SSNES_SHADER_BSNES:
|
|
|
|
{
|
|
|
|
return gl_glsl_init(g_settings.video.bsnes_shader_path);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-05 16:22:12 +00:00
|
|
|
#endif
|
|
|
|
|
2011-02-28 15:59:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-05 16:22:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
static void gl_shader_use(unsigned index)
|
2011-01-22 23:27:20 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
2011-03-06 18:56:35 +00:00
|
|
|
gl_cg_use(index);
|
2011-01-22 23:27:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
2011-03-06 18:56:35 +00:00
|
|
|
gl_glsl_use(index);
|
2011-01-22 23:27:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
static void gl_shader_deinit(void)
|
2011-01-05 16:22:12 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_deinit();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_deinit();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
static void gl_shader_set_proj_matrix(void)
|
2011-01-05 16:22:12 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_set_proj_matrix();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_set_proj_matrix();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
static void gl_shader_set_params(unsigned width, unsigned height,
|
2011-01-05 16:22:12 +00:00
|
|
|
unsigned tex_width, unsigned tex_height,
|
2011-05-11 11:24:59 +00:00
|
|
|
unsigned out_width, unsigned out_height,
|
2011-05-23 15:41:52 +00:00
|
|
|
unsigned frame_count,
|
2011-05-23 17:57:52 +00:00
|
|
|
const struct gl_tex_info *info,
|
|
|
|
const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt)
|
2011-01-05 16:22:12 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
2011-05-23 15:41:52 +00:00
|
|
|
gl_cg_set_params(width, height,
|
|
|
|
tex_width, tex_height,
|
|
|
|
out_width, out_height,
|
2011-05-23 17:57:52 +00:00
|
|
|
frame_count, info, fbo_info, fbo_info_cnt);
|
2011-01-05 16:22:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
2011-05-23 15:41:52 +00:00
|
|
|
gl_glsl_set_params(width, height,
|
|
|
|
tex_width, tex_height,
|
|
|
|
out_width, out_height,
|
2011-05-23 17:57:52 +00:00
|
|
|
frame_count, info, fbo_info, fbo_info_cnt);
|
2011-01-05 16:22:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
static unsigned gl_shader_num(void)
|
2011-03-12 14:30:57 +00:00
|
|
|
{
|
|
|
|
unsigned num = 0;
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
unsigned cg_num = gl_cg_num();
|
|
|
|
if (cg_num > num)
|
|
|
|
num = cg_num;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
unsigned glsl_num = gl_glsl_num();
|
|
|
|
if (glsl_num > num)
|
|
|
|
num = glsl_num;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
2011-03-14 20:28:30 +00:00
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
static bool gl_shader_filter_type(unsigned index, bool *smooth)
|
2011-03-14 21:09:35 +00:00
|
|
|
{
|
|
|
|
bool valid = false;
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
if (!valid)
|
|
|
|
valid = gl_cg_filter_type(index, smooth);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
if (!valid)
|
|
|
|
valid = gl_glsl_filter_type(index, smooth);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-14 21:51:03 +00:00
|
|
|
static void gl_shader_scale(unsigned index, struct gl_fbo_scale *scale)
|
2011-03-14 20:28:30 +00:00
|
|
|
{
|
2011-03-14 21:51:03 +00:00
|
|
|
scale->valid = false;
|
2011-03-14 20:28:30 +00:00
|
|
|
#ifdef HAVE_CG
|
2011-03-14 21:51:03 +00:00
|
|
|
if (!scale->valid)
|
|
|
|
gl_cg_shader_scale(index, scale);
|
2011-03-14 20:28:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
2011-03-14 21:51:03 +00:00
|
|
|
if (!scale->valid)
|
|
|
|
gl_glsl_shader_scale(index, scale);
|
2011-03-14 20:28:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2011-01-22 23:27:20 +00:00
|
|
|
///////////////////
|
|
|
|
|
|
|
|
//////////////// Message rendering
|
2011-01-23 01:48:06 +00:00
|
|
|
static inline void gl_init_font(gl_t *gl, const char *font_path, unsigned font_size)
|
2011-01-22 23:27:20 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_FREETYPE
|
2011-01-23 22:09:54 +00:00
|
|
|
if (strlen(font_path) > 0)
|
2011-01-22 23:27:20 +00:00
|
|
|
{
|
2011-01-23 22:09:54 +00:00
|
|
|
gl->font = font_renderer_new(font_path, font_size);
|
|
|
|
if (gl->font)
|
|
|
|
{
|
|
|
|
glGenTextures(1, &gl->font_tex);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SSNES_WARN("Couldn't init font renderer with font \"%s\"...\n", font_path);
|
2011-01-22 23:27:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-27 18:29:47 +00:00
|
|
|
// Horribly long and complex FBO init :D
|
2011-03-06 18:56:35 +00:00
|
|
|
static void gl_init_fbo(gl_t *gl, unsigned width, unsigned height)
|
|
|
|
{
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-30 21:09:29 +00:00
|
|
|
if (!g_settings.video.render_to_texture && gl_shader_num() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct gl_fbo_scale scale, scale_last;
|
|
|
|
gl_shader_scale(1, &scale);
|
|
|
|
gl_shader_scale(gl_shader_num(), &scale_last);
|
|
|
|
|
|
|
|
// No need to use FBOs.
|
|
|
|
if (gl_shader_num() == 1 && !scale.valid && !g_settings.video.render_to_texture)
|
2011-03-06 18:56:35 +00:00
|
|
|
return;
|
|
|
|
|
2011-03-06 21:18:25 +00:00
|
|
|
if (!load_fbo_proc())
|
2011-03-06 23:03:10 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Failed to locate FBO functions. Won't be able to use render-to-texture.\n");
|
2011-03-06 21:18:25 +00:00
|
|
|
return;
|
2011-03-06 23:03:10 +00:00
|
|
|
}
|
2011-03-06 21:18:25 +00:00
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
gl->fbo_pass = gl_shader_num() - 1;
|
2011-03-30 21:09:29 +00:00
|
|
|
if (scale_last.valid)
|
2011-03-14 22:20:51 +00:00
|
|
|
gl->fbo_pass++;
|
2011-03-23 10:05:18 +00:00
|
|
|
|
2011-03-30 21:09:29 +00:00
|
|
|
if (gl->fbo_pass <= 0)
|
|
|
|
gl->fbo_pass = 1;
|
|
|
|
|
2011-03-23 10:05:18 +00:00
|
|
|
if (!scale.valid)
|
2011-03-15 17:14:01 +00:00
|
|
|
{
|
|
|
|
scale.scale_x = g_settings.video.fbo_scale_x;
|
|
|
|
scale.scale_y = g_settings.video.fbo_scale_y;
|
2011-03-27 18:29:47 +00:00
|
|
|
scale.type_x = scale.type_y = SSNES_SCALE_INPUT;
|
|
|
|
}
|
|
|
|
|
2011-03-27 18:54:14 +00:00
|
|
|
switch (scale.type_x)
|
2011-03-27 18:29:47 +00:00
|
|
|
{
|
|
|
|
case SSNES_SCALE_INPUT:
|
2011-03-27 18:54:14 +00:00
|
|
|
gl->fbo_rect[0].width = width * next_pow2(ceil(scale.scale_x));
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_ABSOLUTE:
|
2011-03-27 19:34:33 +00:00
|
|
|
gl->fbo_rect[0].width = next_pow2(scale.abs_x);
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_VIEWPORT:
|
|
|
|
gl->fbo_rect[0].width = next_pow2(gl->win_width);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-27 18:54:14 +00:00
|
|
|
switch (scale.type_y)
|
2011-03-27 18:29:47 +00:00
|
|
|
{
|
|
|
|
case SSNES_SCALE_INPUT:
|
2011-03-27 18:54:14 +00:00
|
|
|
gl->fbo_rect[0].height = height * next_pow2(ceil(scale.scale_y));
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_ABSOLUTE:
|
2011-03-27 19:34:33 +00:00
|
|
|
gl->fbo_rect[0].height = next_pow2(scale.abs_y);
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_VIEWPORT:
|
|
|
|
gl->fbo_rect[0].height = next_pow2(gl->win_height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2011-03-15 17:14:01 +00:00
|
|
|
}
|
|
|
|
|
2011-03-27 18:54:14 +00:00
|
|
|
unsigned last_width = gl->fbo_rect[0].width, last_height = gl->fbo_rect[0].height;
|
2011-03-15 17:14:01 +00:00
|
|
|
gl->fbo_scale[0] = scale;
|
2011-03-27 18:29:47 +00:00
|
|
|
|
2011-03-16 11:28:02 +00:00
|
|
|
SSNES_LOG("Creating FBO 0 @ %ux%u\n", gl->fbo_rect[0].width, gl->fbo_rect[0].height);
|
2011-03-14 22:20:51 +00:00
|
|
|
|
2011-03-15 17:14:01 +00:00
|
|
|
for (int i = 1; i < gl->fbo_pass; i++)
|
2011-03-14 22:20:51 +00:00
|
|
|
{
|
|
|
|
gl_shader_scale(i + 1, &gl->fbo_scale[i]);
|
|
|
|
if (gl->fbo_scale[i].valid)
|
|
|
|
{
|
2011-03-27 18:29:47 +00:00
|
|
|
switch (gl->fbo_scale[i].type_x)
|
|
|
|
{
|
|
|
|
case SSNES_SCALE_INPUT:
|
2011-03-27 18:54:14 +00:00
|
|
|
gl->fbo_rect[i].width = last_width * next_pow2(ceil(gl->fbo_scale[i].scale_x));
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_ABSOLUTE:
|
|
|
|
gl->fbo_rect[i].width = next_pow2(gl->fbo_scale[i].abs_x);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_VIEWPORT:
|
|
|
|
gl->fbo_rect[i].width = next_pow2(gl->win_width);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (gl->fbo_scale[i].type_y)
|
|
|
|
{
|
|
|
|
case SSNES_SCALE_INPUT:
|
2011-03-27 18:54:14 +00:00
|
|
|
gl->fbo_rect[i].height = last_height * next_pow2(ceil(gl->fbo_scale[i].scale_y));
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_ABSOLUTE:
|
|
|
|
gl->fbo_rect[i].height = next_pow2(gl->fbo_scale[i].abs_y);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_VIEWPORT:
|
|
|
|
gl->fbo_rect[i].height = next_pow2(gl->win_height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-03-27 18:54:14 +00:00
|
|
|
|
|
|
|
last_width = gl->fbo_rect[i].width;
|
|
|
|
last_height = gl->fbo_rect[i].height;
|
2011-03-14 22:20:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use previous values, essentially a 1x scale compared to last shader in chain.
|
2011-03-15 17:14:01 +00:00
|
|
|
gl->fbo_rect[i] = gl->fbo_rect[i - 1];
|
2011-03-27 18:29:47 +00:00
|
|
|
gl->fbo_scale[i].scale_x = gl->fbo_scale[i].scale_y = 1.0;
|
|
|
|
gl->fbo_scale[i].type_x = gl->fbo_scale[i].type_y = SSNES_SCALE_INPUT;
|
2011-03-14 22:20:51 +00:00
|
|
|
}
|
2011-03-14 22:48:19 +00:00
|
|
|
|
|
|
|
SSNES_LOG("Creating FBO %d @ %ux%u\n", i, gl->fbo_rect[i].width, gl->fbo_rect[i].height);
|
2011-03-14 22:20:51 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
glGenTextures(gl->fbo_pass, gl->fbo_texture);
|
2011-03-06 15:52:49 +00:00
|
|
|
|
2011-03-14 20:28:30 +00:00
|
|
|
GLuint base_filt = g_settings.video.second_pass_smooth ? GL_LINEAR : GL_NEAREST;
|
2011-03-12 15:33:01 +00:00
|
|
|
for (int i = 0; i < gl->fbo_pass; i++)
|
2011-03-06 18:56:35 +00:00
|
|
|
{
|
2011-03-12 15:33:01 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
2011-03-14 20:28:30 +00:00
|
|
|
|
|
|
|
GLuint filter_type = base_filt;
|
|
|
|
bool smooth;
|
|
|
|
if (gl_shader_filter_type(i + 2, &smooth))
|
|
|
|
filter_type = smooth ? GL_LINEAR : GL_NEAREST;
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type);
|
2011-03-12 15:33:01 +00:00
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
2011-03-14 22:20:51 +00:00
|
|
|
0, GL_RGBA, gl->fbo_rect[i].width, gl->fbo_rect[i].height, 0, GL_RGBA,
|
|
|
|
GL_UNSIGNED_INT_8_8_8_8, NULL);
|
2011-03-06 18:56:35 +00:00
|
|
|
}
|
2011-03-14 20:28:30 +00:00
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
pglGenFramebuffers(gl->fbo_pass, gl->fbo);
|
|
|
|
for (int i = 0; i < gl->fbo_pass; i++)
|
2011-03-06 18:56:35 +00:00
|
|
|
{
|
2011-03-12 15:33:01 +00:00
|
|
|
pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[i]);
|
|
|
|
pglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl->fbo_texture[i], 0);
|
|
|
|
|
|
|
|
GLenum status = pglCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
goto error;
|
2011-03-06 18:56:35 +00:00
|
|
|
}
|
2011-03-12 15:33:01 +00:00
|
|
|
|
|
|
|
gl->fbo_inited = true;
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
glDeleteTextures(gl->fbo_pass, gl->fbo_texture);
|
|
|
|
pglDeleteFramebuffers(gl->fbo_pass, gl->fbo);
|
|
|
|
SSNES_WARN("Failed to set up FBO. Two-pass shading will not work.\n");
|
2011-03-23 22:48:13 +00:00
|
|
|
#else
|
|
|
|
(void)gl;
|
|
|
|
(void)width;
|
|
|
|
(void)height;
|
|
|
|
#endif
|
2011-03-06 15:52:49 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
static inline void gl_deinit_font(gl_t *gl)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
if (gl->font)
|
|
|
|
{
|
|
|
|
font_renderer_free(gl->font);
|
|
|
|
glDeleteTextures(1, &gl->font_tex);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2011-03-06 21:18:25 +00:00
|
|
|
////////////
|
2011-01-22 23:27:20 +00:00
|
|
|
|
|
|
|
static inline unsigned get_alignment(unsigned pitch)
|
|
|
|
{
|
|
|
|
if (pitch & 1)
|
|
|
|
return 1;
|
|
|
|
if (pitch & 2)
|
|
|
|
return 2;
|
|
|
|
if (pitch & 4)
|
|
|
|
return 4;
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2011-03-20 01:10:02 +00:00
|
|
|
static void set_viewport(gl_t *gl, unsigned width, unsigned height, bool force_full)
|
|
|
|
{
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
if (gl->keep_aspect && !force_full)
|
|
|
|
{
|
|
|
|
float desired_aspect = g_settings.video.aspect_ratio;
|
|
|
|
float device_aspect = (float)width / height;
|
|
|
|
|
|
|
|
// If the aspect ratios of screen and desired aspect ratio are sufficiently equal (floating point stuff),
|
|
|
|
// assume they are actually equal.
|
|
|
|
if (fabs(device_aspect - desired_aspect) < 0.0001)
|
|
|
|
{
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|
|
|
|
else if (device_aspect > desired_aspect)
|
|
|
|
{
|
|
|
|
float delta = (desired_aspect / device_aspect - 1.0) / 2.0 + 0.5;
|
|
|
|
glViewport(width * (0.5 - delta), 0, 2.0 * width * delta, height);
|
|
|
|
width = 2.0 * width * delta;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float delta = (device_aspect / desired_aspect - 1.0) / 2.0 + 0.5;
|
|
|
|
glViewport(0, height * (0.5 - delta), width, 2.0 * height * delta);
|
|
|
|
height = 2.0 * height * delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
|
|
|
|
glOrtho(0, 1, 0, 1, -1, 1);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
gl_shader_set_proj_matrix();
|
|
|
|
|
|
|
|
gl->vp_width = width;
|
|
|
|
gl->vp_height = height;
|
|
|
|
|
2011-03-27 18:29:47 +00:00
|
|
|
// Set last backbuffer viewport.
|
|
|
|
if (!force_full)
|
|
|
|
{
|
|
|
|
gl->vp_out_width = width;
|
|
|
|
gl->vp_out_height = height;
|
|
|
|
}
|
|
|
|
|
2011-03-20 01:10:02 +00:00
|
|
|
//SSNES_LOG("Setting viewport @ %ux%u\n", width, height);
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
static void gl_render_msg(gl_t *gl, const char *msg)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
if (!gl->font)
|
|
|
|
return;
|
|
|
|
|
2011-03-12 18:28:56 +00:00
|
|
|
GLfloat font_vertex[8];
|
2011-01-22 23:27:20 +00:00
|
|
|
|
|
|
|
// Deactivate custom shaders. Enable the font texture.
|
2011-03-06 18:56:35 +00:00
|
|
|
gl_shader_use(0);
|
2011-03-20 01:13:44 +00:00
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
2011-01-22 23:27:20 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
|
2011-03-12 18:28:56 +00:00
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), font_vertex);
|
2011-01-22 23:27:20 +00:00
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), tex_coords); // Use the static one (uses whole texture).
|
|
|
|
|
|
|
|
// Need blending.
|
|
|
|
// Using fixed function pipeline here since we cannot guarantee presence of shaders (would be kinda overkill anyways).
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
|
|
|
struct font_output_list out;
|
|
|
|
font_renderer_msg(gl->font, msg, &out);
|
|
|
|
struct font_output *head = out.head;
|
|
|
|
|
|
|
|
while (head != NULL)
|
|
|
|
{
|
2011-01-23 01:59:44 +00:00
|
|
|
GLfloat lx = (GLfloat)head->off_x / gl->vp_width + g_settings.video.msg_pos_x;
|
|
|
|
GLfloat hx = (GLfloat)(head->off_x + head->width) / gl->vp_width + g_settings.video.msg_pos_x;
|
|
|
|
GLfloat ly = (GLfloat)head->off_y / gl->vp_height + g_settings.video.msg_pos_y;
|
|
|
|
GLfloat hy = (GLfloat)(head->off_y + head->height) / gl->vp_height + g_settings.video.msg_pos_y;
|
2011-01-22 23:27:20 +00:00
|
|
|
|
|
|
|
font_vertex[0] = lx;
|
|
|
|
font_vertex[1] = ly;
|
2011-03-12 18:28:56 +00:00
|
|
|
font_vertex[2] = lx;
|
|
|
|
font_vertex[3] = hy;
|
|
|
|
font_vertex[4] = hx;
|
|
|
|
font_vertex[5] = hy;
|
2011-01-22 23:27:20 +00:00
|
|
|
font_vertex[6] = hx;
|
2011-03-12 18:28:56 +00:00
|
|
|
font_vertex[7] = ly;
|
2011-01-22 23:27:20 +00:00
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, get_alignment(head->pitch));
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, head->pitch);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
2011-02-04 15:24:09 +00:00
|
|
|
0, GL_INTENSITY8, head->width, head->height, 0, GL_LUMINANCE,
|
2011-01-22 23:27:20 +00:00
|
|
|
GL_UNSIGNED_BYTE, head->output);
|
|
|
|
|
|
|
|
head = head->next;
|
|
|
|
glDrawArrays(GL_QUADS, 0, 4);
|
|
|
|
}
|
|
|
|
font_renderer_free_output(&out);
|
|
|
|
|
|
|
|
// Go back to old rendering path.
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->tex_coords);
|
2011-03-12 18:28:56 +00:00
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes);
|
2011-01-22 23:27:20 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-05 16:22:12 +00:00
|
|
|
|
2011-06-05 00:01:44 +00:00
|
|
|
static inline void set_lut_texture_coords(const GLfloat *coords)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_XML) || defined(HAVE_CG)
|
|
|
|
// For texture images.
|
|
|
|
pglClientActiveTexture(GL_TEXTURE1);
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), coords);
|
|
|
|
pglClientActiveTexture(GL_TEXTURE0);
|
|
|
|
#else
|
|
|
|
(void)coords;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-07 18:12:14 +00:00
|
|
|
static bool gl_frame(void *data, const void* frame, unsigned width, unsigned height, unsigned pitch, const char *msg)
|
2010-11-08 22:38:32 +00:00
|
|
|
{
|
2010-11-13 01:48:32 +00:00
|
|
|
gl_t *gl = data;
|
2011-03-06 18:56:35 +00:00
|
|
|
gl_shader_use(1);
|
2011-05-11 11:24:59 +00:00
|
|
|
gl->frame_count++;
|
|
|
|
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 18:56:35 +00:00
|
|
|
// Render to texture in first pass.
|
|
|
|
if (gl->fbo_inited)
|
|
|
|
{
|
2011-03-27 18:29:47 +00:00
|
|
|
unsigned last_width = width;
|
|
|
|
unsigned last_height = height;
|
2011-03-27 19:34:33 +00:00
|
|
|
unsigned last_max_width = gl->tex_w;
|
|
|
|
unsigned last_max_height = gl->tex_h;
|
2011-03-14 22:59:31 +00:00
|
|
|
// Calculate viewports for FBOs.
|
2011-03-14 22:48:19 +00:00
|
|
|
for (int i = 0; i < gl->fbo_pass; i++)
|
|
|
|
{
|
2011-03-27 18:29:47 +00:00
|
|
|
switch (gl->fbo_scale[i].type_x)
|
|
|
|
{
|
|
|
|
case SSNES_SCALE_INPUT:
|
|
|
|
gl->fbo_rect[i].img_width = last_width * gl->fbo_scale[i].scale_x;
|
2011-03-27 19:21:46 +00:00
|
|
|
gl->fbo_rect[i].max_img_width = last_max_width * gl->fbo_scale[i].scale_x;
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_ABSOLUTE:
|
2011-03-27 19:21:46 +00:00
|
|
|
gl->fbo_rect[i].img_width = gl->fbo_rect[i].max_img_width = gl->fbo_scale[i].abs_x;
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_VIEWPORT:
|
2011-03-27 19:21:46 +00:00
|
|
|
gl->fbo_rect[i].img_width = gl->fbo_rect[i].max_img_width = gl->fbo_scale[i].scale_x * gl->vp_out_width;
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (gl->fbo_scale[i].type_y)
|
|
|
|
{
|
|
|
|
case SSNES_SCALE_INPUT:
|
|
|
|
gl->fbo_rect[i].img_height = last_height * gl->fbo_scale[i].scale_y;
|
2011-03-27 19:21:46 +00:00
|
|
|
gl->fbo_rect[i].max_img_height = last_max_height * gl->fbo_scale[i].scale_y;
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_ABSOLUTE:
|
2011-03-27 19:21:46 +00:00
|
|
|
gl->fbo_rect[i].img_height = gl->fbo_rect[i].max_img_height = gl->fbo_scale[i].abs_y;
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSNES_SCALE_VIEWPORT:
|
2011-03-27 19:21:46 +00:00
|
|
|
gl->fbo_rect[i].img_height = gl->fbo_rect[i].max_img_height = gl->fbo_scale[i].scale_y * gl->vp_out_height;
|
2011-03-27 18:29:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_width = gl->fbo_rect[i].img_width;
|
|
|
|
last_height = gl->fbo_rect[i].img_height;
|
2011-03-27 19:21:46 +00:00
|
|
|
last_max_width = gl->fbo_rect[i].max_img_width;
|
|
|
|
last_max_height = gl->fbo_rect[i].max_img_height;
|
2011-03-14 22:48:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-06 18:56:35 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
2011-03-12 15:33:01 +00:00
|
|
|
pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[0]);
|
2011-03-06 18:56:35 +00:00
|
|
|
gl->render_to_tex = true;
|
2011-03-14 22:48:19 +00:00
|
|
|
set_viewport(gl, gl->fbo_rect[0].img_width, gl->fbo_rect[0].img_height, true);
|
2011-03-06 18:56:35 +00:00
|
|
|
}
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2011-03-14 22:48:19 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
if (gl->should_resize)
|
|
|
|
{
|
|
|
|
gl->should_resize = false;
|
2011-01-13 20:25:52 +00:00
|
|
|
SDL_SetVideoMode(gl->win_width, gl->win_height, 0, SDL_OPENGL | SDL_RESIZABLE | (g_settings.video.fullscreen ? SDL_FULLSCREEN : 0));
|
2011-03-06 18:56:35 +00:00
|
|
|
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 18:56:35 +00:00
|
|
|
if (!gl->render_to_tex)
|
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
2011-03-27 18:29:47 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if we have to recreate our FBO textures.
|
|
|
|
for (int i = 0; i < gl->fbo_pass; i++)
|
|
|
|
{
|
2011-03-27 19:34:33 +00:00
|
|
|
// Check proactively since we might suddently get sizes of tex_w width or tex_h height.
|
2011-03-27 19:21:46 +00:00
|
|
|
if (gl->fbo_rect[i].max_img_width > gl->fbo_rect[i].width ||
|
|
|
|
gl->fbo_rect[i].max_img_height > gl->fbo_rect[i].height)
|
2011-03-27 18:29:47 +00:00
|
|
|
{
|
2011-03-27 19:21:46 +00:00
|
|
|
unsigned img_width = gl->fbo_rect[i].max_img_width;
|
|
|
|
unsigned img_height = gl->fbo_rect[i].max_img_height;
|
2011-03-27 18:29:47 +00:00
|
|
|
unsigned max = img_width > img_height ? img_width : img_height;
|
|
|
|
unsigned pow2_size = next_pow2(max);
|
|
|
|
gl->fbo_rect[i].width = gl->fbo_rect[i].height = pow2_size;
|
|
|
|
|
|
|
|
pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[i]);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
|
|
0, GL_RGBA, gl->fbo_rect[i].width, gl->fbo_rect[i].height, 0, GL_RGBA,
|
|
|
|
GL_UNSIGNED_INT_8_8_8_8, NULL);
|
|
|
|
|
|
|
|
pglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl->fbo_texture[i], 0);
|
|
|
|
|
|
|
|
GLenum status = pglCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
SSNES_WARN("Failed to reinit FBO texture!\n");
|
2011-03-27 18:54:14 +00:00
|
|
|
|
|
|
|
SSNES_LOG("Recreating FBO texture #%d: %ux%u\n", i, gl->fbo_rect[i].width, gl->fbo_rect[i].height);
|
2011-03-27 18:29:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go back to what we're supposed to do, render to FBO #0 :D
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
|
|
|
pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[0]);
|
|
|
|
set_viewport(gl, gl->fbo_rect[0].img_width, gl->fbo_rect[0].img_height, true);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
|
|
|
#endif
|
2011-01-06 19:01:32 +00:00
|
|
|
}
|
|
|
|
|
2011-05-31 21:03:01 +00:00
|
|
|
if ((width != gl->last_width || height != gl->last_height) && gl->empty_buf) // Res change. need to clear out texture.
|
2010-12-26 03:15:12 +00:00
|
|
|
{
|
|
|
|
gl->last_width = width;
|
|
|
|
gl->last_height = height;
|
2011-01-22 23:27:20 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, get_alignment(pitch));
|
2011-01-13 20:25:52 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->tex_w);
|
2011-03-07 18:12:14 +00:00
|
|
|
|
2011-03-27 18:33:14 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,
|
|
|
|
0, 0, 0, gl->tex_w, gl->tex_h, gl->texture_type,
|
2011-05-31 21:03:01 +00:00
|
|
|
gl->texture_fmt, gl->empty_buf);
|
2010-12-26 03:15:12 +00:00
|
|
|
|
2011-03-16 11:28:02 +00:00
|
|
|
GLfloat x = (GLfloat)width / gl->tex_w;
|
|
|
|
GLfloat y = (GLfloat)height / gl->tex_h;
|
|
|
|
|
|
|
|
gl->tex_coords[1] = y;
|
|
|
|
gl->tex_coords[4] = x;
|
|
|
|
gl->tex_coords[6] = x;
|
|
|
|
gl->tex_coords[7] = y;
|
2010-12-26 03:15:12 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 10:53:24 +00:00
|
|
|
// Work around a certain issue a Cg where not using TEXUNIT0
|
|
|
|
// in shader causes cgGLEnableTextureParameter() causes it
|
|
|
|
// to bind to TEXUNIT0, to avoid really funny bugs, rebind
|
|
|
|
// our texture.
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
|
|
|
#endif
|
2011-06-01 10:19:48 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FBO
|
|
|
|
// Need to preserve the "flipped" state when in FBO too to have
|
|
|
|
// consistent texture coordinates.
|
|
|
|
if (gl->render_to_tex)
|
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes_flipped);
|
|
|
|
#endif
|
|
|
|
|
2011-03-07 18:12:14 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / gl->base_size);
|
2010-12-26 03:15:12 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,
|
2011-03-07 18:12:14 +00:00
|
|
|
0, 0, 0, width, height, gl->texture_type,
|
|
|
|
gl->texture_fmt, frame);
|
2011-02-26 22:56:45 +00:00
|
|
|
|
2011-05-26 10:53:24 +00:00
|
|
|
struct gl_tex_info tex_info = {
|
|
|
|
.tex = gl->texture,
|
|
|
|
.input_size = {width, height},
|
|
|
|
.tex_size = {gl->tex_w, gl->tex_h}
|
|
|
|
};
|
|
|
|
struct gl_tex_info fbo_tex_info[MAX_SHADERS];
|
|
|
|
unsigned fbo_tex_info_cnt = 0;
|
|
|
|
memcpy(tex_info.coord, gl->tex_coords, sizeof(gl->tex_coords));
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
gl_shader_set_params(width, height, gl->tex_w, gl->tex_h, gl->vp_width, gl->vp_height, gl->frame_count,
|
|
|
|
&tex_info, fbo_tex_info, fbo_tex_info_cnt);
|
|
|
|
|
2010-11-08 22:38:32 +00:00
|
|
|
glDrawArrays(GL_QUADS, 0, 4);
|
2010-12-14 13:28:09 +00:00
|
|
|
|
2011-03-23 22:48:13 +00:00
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 18:56:35 +00:00
|
|
|
if (gl->fbo_inited)
|
|
|
|
{
|
2011-06-01 10:19:48 +00:00
|
|
|
// Go back to non-flipped.
|
|
|
|
if (gl->render_to_tex)
|
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes);
|
|
|
|
|
2011-03-14 22:20:51 +00:00
|
|
|
// Render the rest of our passes.
|
2011-03-06 18:56:35 +00:00
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->fbo_tex_coords);
|
2011-06-05 00:01:44 +00:00
|
|
|
set_lut_texture_coords(fbo_tex_coords);
|
2011-03-06 15:52:49 +00:00
|
|
|
|
2011-03-14 22:59:31 +00:00
|
|
|
// It's kinda handy ... :)
|
2011-03-14 22:48:19 +00:00
|
|
|
const struct gl_fbo_rect *prev_rect;
|
|
|
|
const struct gl_fbo_rect *rect;
|
2011-05-23 17:57:52 +00:00
|
|
|
struct gl_tex_info *fbo_info;
|
2011-03-14 22:48:19 +00:00
|
|
|
|
2011-03-14 22:59:31 +00:00
|
|
|
// Calculate viewports, texture coordinates etc, and render all passes from FBOs, to another FBO.
|
2011-03-12 15:33:01 +00:00
|
|
|
for (int i = 1; i < gl->fbo_pass; i++)
|
|
|
|
{
|
2011-03-14 22:48:19 +00:00
|
|
|
prev_rect = &gl->fbo_rect[i - 1];
|
|
|
|
rect = &gl->fbo_rect[i];
|
2011-05-23 17:57:52 +00:00
|
|
|
fbo_info = &fbo_tex_info[i - 1];
|
2011-03-14 22:48:19 +00:00
|
|
|
|
|
|
|
GLfloat xamt = (GLfloat)prev_rect->img_width / prev_rect->width;
|
|
|
|
GLfloat yamt = (GLfloat)prev_rect->img_height / prev_rect->height;
|
2011-03-14 22:20:51 +00:00
|
|
|
|
|
|
|
gl->fbo_tex_coords[3] = yamt;
|
|
|
|
gl->fbo_tex_coords[4] = xamt;
|
|
|
|
gl->fbo_tex_coords[5] = yamt;
|
|
|
|
gl->fbo_tex_coords[6] = xamt;
|
|
|
|
|
2011-05-23 17:57:52 +00:00
|
|
|
fbo_info->tex = gl->fbo_texture[i - 1];
|
|
|
|
fbo_info->input_size[0] = prev_rect->img_width;
|
|
|
|
fbo_info->input_size[1] = prev_rect->img_height;
|
|
|
|
fbo_info->tex_size[0] = prev_rect->width;
|
|
|
|
fbo_info->tex_size[1] = prev_rect->height;
|
|
|
|
memcpy(fbo_info->coord, gl->fbo_tex_coords, sizeof(gl->fbo_tex_coords));
|
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[i]);
|
|
|
|
gl_shader_use(i + 1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i - 1]);
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2011-03-14 22:59:31 +00:00
|
|
|
|
|
|
|
// Render to FBO with certain size.
|
|
|
|
set_viewport(gl, rect->img_width, rect->img_height, true);
|
2011-05-23 17:57:52 +00:00
|
|
|
gl_shader_set_params(prev_rect->img_width, prev_rect->img_height,
|
|
|
|
prev_rect->width, prev_rect->height,
|
|
|
|
gl->vp_width, gl->vp_height, gl->frame_count,
|
|
|
|
&tex_info, fbo_tex_info, fbo_tex_info_cnt);
|
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
glDrawArrays(GL_QUADS, 0, 4);
|
2011-05-23 17:57:52 +00:00
|
|
|
|
|
|
|
fbo_tex_info_cnt++;
|
2011-03-12 15:33:01 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 22:59:31 +00:00
|
|
|
// Render our last FBO texture directly to screen.
|
2011-03-14 22:48:19 +00:00
|
|
|
prev_rect = &gl->fbo_rect[gl->fbo_pass - 1];
|
|
|
|
GLfloat xamt = (GLfloat)prev_rect->img_width / prev_rect->width;
|
|
|
|
GLfloat yamt = (GLfloat)prev_rect->img_height / prev_rect->height;
|
|
|
|
|
|
|
|
gl->fbo_tex_coords[3] = yamt;
|
|
|
|
gl->fbo_tex_coords[4] = xamt;
|
|
|
|
gl->fbo_tex_coords[5] = yamt;
|
|
|
|
gl->fbo_tex_coords[6] = xamt;
|
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
// Render our FBO texture to back buffer.
|
|
|
|
pglBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
gl_shader_use(gl->fbo_pass + 1);
|
|
|
|
|
2011-05-23 17:57:52 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[gl->fbo_pass - 1]);
|
|
|
|
|
2011-03-12 15:33:01 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
gl->render_to_tex = false;
|
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
2011-05-23 17:57:52 +00:00
|
|
|
gl_shader_set_params(prev_rect->img_width, prev_rect->img_height,
|
|
|
|
prev_rect->width, prev_rect->height,
|
|
|
|
gl->vp_width, gl->vp_height, gl->frame_count,
|
|
|
|
&tex_info, fbo_tex_info, fbo_tex_info_cnt);
|
|
|
|
|
2011-06-01 10:19:48 +00:00
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes_flipped);
|
2011-03-06 18:56:35 +00:00
|
|
|
glDrawArrays(GL_QUADS, 0, 4);
|
2011-06-05 00:01:44 +00:00
|
|
|
|
2011-03-06 18:56:35 +00:00
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->tex_coords);
|
2011-06-05 00:01:44 +00:00
|
|
|
set_lut_texture_coords(tex_coords);
|
2011-06-01 10:19:48 +00:00
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes);
|
2011-03-06 18:56:35 +00:00
|
|
|
}
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2011-03-06 15:52:49 +00:00
|
|
|
|
2011-01-22 23:48:47 +00:00
|
|
|
if (msg)
|
|
|
|
gl_render_msg(gl, msg);
|
2011-01-22 23:27:20 +00:00
|
|
|
|
2011-03-26 17:34:58 +00:00
|
|
|
char buf[128];
|
|
|
|
if (gfx_window_title(buf, sizeof(buf)))
|
|
|
|
SDL_WM_SetCaption(buf, NULL);
|
2011-01-06 17:34:11 +00:00
|
|
|
SDL_GL_SwapBuffers();
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gl_free(void *data)
|
|
|
|
{
|
2010-11-13 01:48:32 +00:00
|
|
|
gl_t *gl = data;
|
2011-01-05 16:22:12 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
gl_deinit_font(gl);
|
2011-01-05 16:22:12 +00:00
|
|
|
gl_shader_deinit();
|
2010-11-08 22:38:32 +00:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
2010-11-13 01:48:32 +00:00
|
|
|
glDeleteTextures(1, &gl->texture);
|
2011-03-23 22:48:13 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FBO
|
2011-03-06 18:56:35 +00:00
|
|
|
if (gl->fbo_inited)
|
|
|
|
{
|
2011-03-12 15:33:01 +00:00
|
|
|
glDeleteTextures(gl->fbo_pass, gl->fbo_texture);
|
|
|
|
pglDeleteFramebuffers(gl->fbo_pass, gl->fbo);
|
2011-03-06 18:56:35 +00:00
|
|
|
}
|
2011-03-23 22:48:13 +00:00
|
|
|
#endif
|
2011-01-06 17:34:11 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
2011-03-13 19:52:06 +00:00
|
|
|
|
2011-05-31 21:03:01 +00:00
|
|
|
if (gl->empty_buf)
|
|
|
|
free(gl->empty_buf);
|
|
|
|
|
2011-03-13 19:52:06 +00:00
|
|
|
free(gl);
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-16 16:40:17 +00:00
|
|
|
static void gl_set_nonblock_state(void *data, bool state)
|
|
|
|
{
|
|
|
|
gl_t *gl = data;
|
|
|
|
if (gl->vsync)
|
|
|
|
{
|
2011-01-06 19:01:32 +00:00
|
|
|
SSNES_LOG("GL VSync => %s\n", state ? "off" : "on");
|
2011-01-06 22:15:49 +00:00
|
|
|
#ifdef _WIN32
|
2011-01-07 16:59:53 +00:00
|
|
|
static BOOL (APIENTRY *wgl_swap_interval)(int) = NULL;
|
2011-05-31 13:03:59 +00:00
|
|
|
if (!wgl_swap_interval)
|
|
|
|
{
|
|
|
|
SDL_SYM_WRAP(wgl_swap_interval, "wglSwapIntervalEXT");
|
|
|
|
}
|
2011-01-06 22:15:49 +00:00
|
|
|
if (wgl_swap_interval) wgl_swap_interval(state ? 0 : 1);
|
|
|
|
#else
|
|
|
|
static int (*glx_swap_interval)(int) = NULL;
|
2011-05-31 13:03:59 +00:00
|
|
|
if (!glx_swap_interval)
|
|
|
|
{
|
|
|
|
SDL_SYM_WRAP(glx_swap_interval, "glXSwapIntervalSGI");
|
|
|
|
}
|
|
|
|
if (!glx_swap_interval)
|
|
|
|
{
|
2011-05-31 21:03:01 +00:00
|
|
|
SDL_SYM_WRAP(glx_swap_interval, "glXSwapIntervalMESA");
|
2011-05-31 13:03:59 +00:00
|
|
|
}
|
2011-01-06 22:15:49 +00:00
|
|
|
if (glx_swap_interval) glx_swap_interval(state ? 0 : 1);
|
|
|
|
#endif
|
2010-08-16 16:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 01:23:44 +00:00
|
|
|
static void* gl_init(const video_info_t *video, const input_driver_t **input, void **input_data)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2011-01-06 17:34:11 +00:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-28 15:59:31 +00:00
|
|
|
const SDL_VideoInfo *video_info = SDL_GetVideoInfo();
|
2011-03-01 13:46:58 +00:00
|
|
|
assert(video_info);
|
2011-02-28 15:59:31 +00:00
|
|
|
unsigned full_x = video_info->current_w;
|
|
|
|
unsigned full_y = video_info->current_h;
|
2011-03-01 13:46:58 +00:00
|
|
|
SSNES_LOG("Detecting desktop resolution %ux%u.\n", full_x, full_y);
|
2011-02-28 15:59:31 +00:00
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, video->vsync ? 1 : 0);
|
|
|
|
|
2011-04-21 11:12:45 +00:00
|
|
|
if (!SDL_SetVideoMode(video->width, video->height, g_settings.video.force_16bit ? 16 : 0, SDL_OPENGL | SDL_RESIZABLE | (video->fullscreen ? SDL_FULLSCREEN : 0)))
|
2011-01-06 17:34:11 +00:00
|
|
|
return NULL;
|
2011-01-11 21:33:28 +00:00
|
|
|
|
2011-03-12 14:30:57 +00:00
|
|
|
// Remove that ugly mouse :D
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2011-03-12 18:09:25 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2011-03-12 14:30:57 +00:00
|
|
|
|
2011-05-31 21:03:01 +00:00
|
|
|
#if (defined(HAVE_XML) || defined(HAVE_CG)) && defined(_WIN32)
|
2011-05-18 20:40:42 +00:00
|
|
|
// Win32 GL lib doesn't have some functions needed for XML shaders.
|
|
|
|
// Need to load dynamically :(
|
|
|
|
if (!load_gl_proc())
|
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-11 21:33:28 +00:00
|
|
|
|
2011-01-11 20:16:57 +00:00
|
|
|
gl_t *gl = calloc(1, sizeof(gl_t));
|
|
|
|
if (!gl)
|
2011-05-18 20:40:42 +00:00
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
2011-01-11 20:16:57 +00:00
|
|
|
return NULL;
|
2011-05-18 20:40:42 +00:00
|
|
|
}
|
2011-01-11 20:16:57 +00:00
|
|
|
|
2011-05-31 21:03:01 +00:00
|
|
|
gl->vsync = video->vsync;
|
|
|
|
int attr = 0;
|
|
|
|
SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &attr);
|
|
|
|
if (attr <= 0 && video->vsync)
|
|
|
|
{
|
2011-05-31 21:08:02 +00:00
|
|
|
SSNES_WARN("SDL failed to setup VSync, attempting to recover using native calls!\n");
|
2011-05-31 21:03:01 +00:00
|
|
|
gl_set_nonblock_state(gl, false);
|
|
|
|
}
|
|
|
|
attr = 0;
|
|
|
|
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &attr);
|
|
|
|
if (attr <= 0)
|
|
|
|
SSNES_WARN("GL double buffer has not been enabled!\n");
|
|
|
|
|
2011-02-28 15:59:31 +00:00
|
|
|
gl->full_x = full_x;
|
|
|
|
gl->full_y = full_y;
|
|
|
|
|
|
|
|
if (video->fullscreen)
|
|
|
|
{
|
|
|
|
gl->win_width = video->width ? video->width : gl->full_x;
|
|
|
|
gl->win_height = video->height ? video->height : gl->full_y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gl->win_width = video->width;
|
|
|
|
gl->win_height = video->height;
|
|
|
|
}
|
2011-03-06 15:52:49 +00:00
|
|
|
|
2011-03-01 13:46:58 +00:00
|
|
|
SSNES_LOG("GL: Using resolution %ux%u\n", gl->win_width, gl->win_height);
|
2011-02-28 15:59:31 +00:00
|
|
|
|
2011-01-11 18:23:21 +00:00
|
|
|
if (!gl_shader_init())
|
|
|
|
{
|
|
|
|
SSNES_ERR("Shader init failed.\n");
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
2011-01-11 20:16:57 +00:00
|
|
|
free(gl);
|
2011-01-11 18:23:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-28 09:29:40 +00:00
|
|
|
SSNES_LOG("GL: Loaded %u program(s).\n", gl_shader_num());
|
2011-03-12 14:30:57 +00:00
|
|
|
|
|
|
|
// Set up render to texture.
|
|
|
|
gl_init_fbo(gl, 256 * video->input_scale, 256 * video->input_scale);
|
|
|
|
|
2011-03-30 12:57:45 +00:00
|
|
|
gl->keep_aspect = video->force_aspect;
|
|
|
|
|
2011-03-16 11:47:31 +00:00
|
|
|
// Apparently need to set viewport for passes when we aren't using FBOs.
|
2011-05-26 20:25:30 +00:00
|
|
|
gl_shader_use(0);
|
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
2011-03-16 11:47:31 +00:00
|
|
|
gl_shader_use(1);
|
2011-03-12 14:30:57 +00:00
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
2011-01-06 19:01:32 +00:00
|
|
|
|
2011-03-14 20:28:30 +00:00
|
|
|
bool force_smooth;
|
|
|
|
if (gl_shader_filter_type(1, &force_smooth))
|
|
|
|
gl->tex_filter = force_smooth ? GL_LINEAR : GL_NEAREST;
|
2010-05-29 12:45:40 +00:00
|
|
|
else
|
2011-03-14 20:28:30 +00:00
|
|
|
gl->tex_filter = video->smooth ? GL_LINEAR : GL_NEAREST;
|
2010-05-28 00:54:20 +00:00
|
|
|
|
2011-05-28 12:11:37 +00:00
|
|
|
gl->texture_type = GL_BGRA;
|
|
|
|
gl->texture_fmt = video->rgb32 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_SHORT_1_5_5_5_REV;
|
2011-03-07 18:12:14 +00:00
|
|
|
gl->base_size = video->rgb32 ? sizeof(uint32_t) : sizeof(uint16_t);
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2010-11-08 22:38:32 +00:00
|
|
|
glDisable(GL_DEPTH_TEST);
|
2011-03-12 18:28:56 +00:00
|
|
|
glDisable(GL_DITHER);
|
2011-02-04 15:24:09 +00:00
|
|
|
glColor4f(1, 1, 1, 1);
|
2011-03-12 18:09:25 +00:00
|
|
|
glClearColor(0, 0, 0, 1);
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-03-26 17:34:58 +00:00
|
|
|
char buf[128];
|
|
|
|
if (gfx_window_title(buf, sizeof(buf)))
|
|
|
|
SDL_WM_SetCaption(buf, NULL);
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2010-11-13 01:48:32 +00:00
|
|
|
glGenTextures(1, &gl->texture);
|
2010-11-08 22:38:32 +00:00
|
|
|
|
2010-11-13 01:48:32 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
2010-11-08 22:38:32 +00:00
|
|
|
|
2011-01-05 18:25:27 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
2011-03-14 20:28:30 +00:00
|
|
|
|
2010-11-13 01:48:32 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->tex_filter);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->tex_filter);
|
2010-11-08 22:38:32 +00:00
|
|
|
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
2011-03-12 18:28:56 +00:00
|
|
|
glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes);
|
2010-12-26 03:15:12 +00:00
|
|
|
|
|
|
|
memcpy(gl->tex_coords, tex_coords, sizeof(tex_coords));
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->tex_coords);
|
|
|
|
|
2011-06-05 00:01:44 +00:00
|
|
|
set_lut_texture_coords(tex_coords);
|
2011-05-18 18:38:04 +00:00
|
|
|
|
2010-12-26 03:15:12 +00:00
|
|
|
gl->tex_w = 256 * video->input_scale;
|
|
|
|
gl->tex_h = 256 * video->input_scale;
|
2011-03-07 18:12:14 +00:00
|
|
|
|
2010-12-26 03:15:12 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
2011-03-07 18:12:14 +00:00
|
|
|
0, GL_RGBA, gl->tex_w, gl->tex_h, 0, gl->texture_type,
|
2011-03-14 22:48:19 +00:00
|
|
|
gl->texture_fmt, NULL);
|
2011-03-07 18:12:14 +00:00
|
|
|
|
2011-05-31 21:03:01 +00:00
|
|
|
// Empty buffer that we use to clear out the texture with on res change.
|
|
|
|
gl->empty_buf = calloc(gl->base_size, gl->tex_w * gl->tex_h);
|
|
|
|
|
2010-12-26 03:15:12 +00:00
|
|
|
gl->last_width = gl->tex_w;
|
|
|
|
gl->last_height = gl->tex_h;
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
// Hook up SDL input driver to get SDL_QUIT events and RESIZE.
|
|
|
|
sdl_input_t *sdl_input = input_sdl.init();
|
|
|
|
if (sdl_input)
|
|
|
|
{
|
|
|
|
sdl_input->quitting = &gl->quitting;
|
|
|
|
sdl_input->should_resize = &gl->should_resize;
|
|
|
|
sdl_input->new_width = &gl->win_width;
|
|
|
|
sdl_input->new_height = &gl->win_height;
|
|
|
|
*input = &input_sdl;
|
|
|
|
*input_data = sdl_input;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*input = NULL;
|
2011-01-22 23:27:20 +00:00
|
|
|
|
2011-01-23 01:48:06 +00:00
|
|
|
gl_init_font(gl, g_settings.video.font_path, g_settings.video.font_size);
|
2011-01-13 20:25:52 +00:00
|
|
|
|
2011-01-11 21:33:28 +00:00
|
|
|
if (!gl_check_error())
|
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
free(gl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-16 16:40:17 +00:00
|
|
|
return gl;
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
static bool gl_alive(void *data)
|
|
|
|
{
|
|
|
|
gl_t *gl = data;
|
|
|
|
return !gl->quitting;
|
|
|
|
}
|
|
|
|
|
2011-02-05 20:45:44 +00:00
|
|
|
static bool gl_focus(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return (SDL_GetAppState() & (SDL_APPINPUTFOCUS | SDL_APPACTIVE)) == (SDL_APPINPUTFOCUS | SDL_APPACTIVE);
|
|
|
|
}
|
|
|
|
|
2011-03-29 16:59:06 +00:00
|
|
|
#ifdef HAVE_XML
|
2011-03-29 16:28:31 +00:00
|
|
|
static bool gl_xml_shader(void *data, const char *path)
|
|
|
|
{
|
2011-03-29 16:59:06 +00:00
|
|
|
gl_t *gl = data;
|
|
|
|
gl_shader_deinit();
|
|
|
|
|
|
|
|
#ifdef HAVE_FBO
|
|
|
|
if (gl->fbo_inited)
|
|
|
|
{
|
|
|
|
pglDeleteFramebuffers(gl->fbo_pass, gl->fbo);
|
2011-03-30 09:05:56 +00:00
|
|
|
glDeleteTextures(gl->fbo_pass, gl->fbo_texture);
|
2011-03-30 09:12:27 +00:00
|
|
|
memset(gl->fbo_texture, 0, sizeof(gl->fbo_texture));
|
|
|
|
memset(gl->fbo, 0, sizeof(gl->fbo));
|
2011-03-29 16:59:06 +00:00
|
|
|
gl->fbo_inited = false;
|
2011-03-30 09:05:56 +00:00
|
|
|
gl->render_to_tex = false;
|
|
|
|
gl->fbo_pass = 0;
|
|
|
|
|
|
|
|
if (!gl_check_error())
|
|
|
|
SSNES_WARN("Failed to deinit FBO properly!\n");
|
2011-03-30 09:12:27 +00:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
2011-03-29 16:59:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!gl_glsl_init(path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Set up render to texture.
|
|
|
|
gl_init_fbo(gl, gl->tex_w, gl->tex_h);
|
|
|
|
|
|
|
|
// Apparently need to set viewport for passes when we aren't using FBOs.
|
2011-05-26 20:25:30 +00:00
|
|
|
gl_shader_use(0);
|
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
2011-03-29 16:59:06 +00:00
|
|
|
gl_shader_use(1);
|
|
|
|
set_viewport(gl, gl->win_width, gl->win_height, false);
|
|
|
|
|
2011-03-29 16:28:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-03-29 16:59:06 +00:00
|
|
|
#endif
|
2011-03-29 16:28:31 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
const video_driver_t video_gl = {
|
|
|
|
.init = gl_init,
|
|
|
|
.frame = gl_frame,
|
2011-01-06 19:01:32 +00:00
|
|
|
.alive = gl_alive,
|
2010-08-16 16:40:17 +00:00
|
|
|
.set_nonblock_state = gl_set_nonblock_state,
|
2011-02-05 20:45:44 +00:00
|
|
|
.focus = gl_focus,
|
2010-12-29 19:05:57 +00:00
|
|
|
.free = gl_free,
|
2011-03-29 16:59:06 +00:00
|
|
|
#ifdef HAVE_XML
|
2011-03-29 16:28:31 +00:00
|
|
|
.xml_shader = gl_xml_shader,
|
2011-03-29 16:59:06 +00:00
|
|
|
#endif
|
2011-01-06 17:34:11 +00:00
|
|
|
.ident = "gl"
|
2010-05-28 00:45:18 +00:00
|
|
|
};
|
2010-12-14 13:28:09 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
|