2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2012-01-08 00:08:18 +00:00
|
|
|
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
2011-01-11 21:13:55 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-01-11 21:13:55 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-01-11 21:13:55 +00:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-01-11 21:13:55 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __GL_COMMON_H
|
|
|
|
#define __GL_COMMON_H
|
|
|
|
|
2011-11-30 17:31:09 +00:00
|
|
|
#include "../general.h"
|
2012-05-27 00:04:50 +00:00
|
|
|
#include "fonts/fonts.h"
|
2012-08-09 20:33:32 +00:00
|
|
|
#include "math/matrix.h"
|
2012-09-24 23:26:22 +00:00
|
|
|
#include "gfx_context.h"
|
2012-10-01 22:58:43 +00:00
|
|
|
#include "scaler/scaler.h"
|
2011-05-18 20:11:34 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-11-30 17:31:09 +00:00
|
|
|
#include "../config.h"
|
2011-05-18 20:11:34 +00:00
|
|
|
#endif
|
2011-01-11 21:13:55 +00:00
|
|
|
|
2012-09-10 21:17:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-10-20 03:08:53 +00:00
|
|
|
#ifdef HAVE_EGL
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
#endif
|
|
|
|
|
2011-12-07 00:13:06 +00:00
|
|
|
#if defined(__APPLE__)
|
2011-03-14 21:09:35 +00:00
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#include <OpenGL/glext.h>
|
2012-09-11 07:55:03 +00:00
|
|
|
#elif defined(HAVE_PSGL)
|
2011-11-30 17:32:45 +00:00
|
|
|
#include <PSGL/psgl.h>
|
|
|
|
#include <PSGL/psglu.h>
|
|
|
|
#include <GLES/glext.h>
|
2012-09-11 07:55:03 +00:00
|
|
|
#elif defined(HAVE_OPENGL_MODERN)
|
2012-07-01 20:44:33 +00:00
|
|
|
#include <GL3/gl3.h>
|
|
|
|
#include <GL3/gl3ext.h>
|
2012-09-09 22:36:25 +00:00
|
|
|
#elif defined(HAVE_OPENGLES2)
|
|
|
|
#include <GLES2/gl2.h>
|
2012-09-29 18:06:48 +00:00
|
|
|
#include <GLES2/gl2ext.h>
|
2012-09-09 22:36:25 +00:00
|
|
|
#elif defined(HAVE_OPENGLES1)
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
#include <GLES/glext.h>
|
2011-11-30 17:32:45 +00:00
|
|
|
#else
|
2011-03-14 21:09:35 +00:00
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glext.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-03 09:18:08 +00:00
|
|
|
#if defined(ANDROID) && defined(HAVE_GRIFFIN)
|
|
|
|
#include "../console/griffin/hook_context.h"
|
|
|
|
#else
|
2012-11-03 08:18:31 +00:00
|
|
|
#define context_get_video_size_func(win, height) gl->ctx_driver->get_video_size(win, height)
|
|
|
|
#define context_update_window_title_func(var) gl->ctx_driver->update_window_title(var)
|
|
|
|
#define context_destroy_func() gl->ctx_driver->destroy()
|
2012-11-18 23:01:25 +00:00
|
|
|
#define context_set_fbo_func(var) gl->ctx_driver->set_fbo(var)
|
2012-11-03 08:18:31 +00:00
|
|
|
#define context_get_available_resolutions_func() gl->ctx_driver->get_available_resolutions()
|
|
|
|
#define context_translate_aspect_func(width, height) gl->ctx_driver->translate_aspect(width, height)
|
|
|
|
#define context_set_resize_func(width, height) gl->ctx_driver->set_resize(width, height)
|
|
|
|
#define context_swap_buffers_func() gl->ctx_driver->swap_buffers()
|
|
|
|
#define context_swap_interval_func(var) gl->ctx_driver->swap_interval(var)
|
|
|
|
#define context_has_focus_func() gl->ctx_driver->has_focus()
|
|
|
|
#define context_check_window_func(quit, resize, width, height, frame_count) gl->ctx_driver->check_window(quit, resize, width, height, frame_count)
|
|
|
|
#define context_update_window_title_func(var) gl->ctx_driver->update_window_title(var)
|
2012-11-20 22:23:18 +00:00
|
|
|
#define context_set_video_mode_func(width, height, fullscreen) gl->ctx_driver->set_video_mode(width, height, fullscreen)
|
2012-11-03 08:18:31 +00:00
|
|
|
#define context_input_driver_func(input, input_data) gl->ctx_driver->input_driver(input, input_data)
|
|
|
|
|
|
|
|
#ifdef HAVE_RMENU
|
|
|
|
#define context_rmenu_init_func() gl->ctx_driver->rmenu_init()
|
|
|
|
#define context_rmenu_frame_func(ctx) gl->ctx_driver->rmenu_frame(ctx)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_EGL
|
|
|
|
#define context_init_egl_image_buffer_func(video) gl->ctx_driver->init_egl_image_buffer(video)
|
|
|
|
|
|
|
|
#define context_write_egl_image_func(frame, width, height, pitch, base_size, tex_index, img) gl->ctx_driver->write_egl_image(frame, width, height, pitch, base_size, tex_index,img)
|
|
|
|
#endif
|
2012-11-03 09:18:08 +00:00
|
|
|
#endif
|
2012-11-03 08:18:31 +00:00
|
|
|
|
2012-09-10 21:17:48 +00:00
|
|
|
static inline bool gl_query_extension(const char *ext)
|
|
|
|
{
|
|
|
|
const char *str = (const char*)glGetString(GL_EXTENSIONS);
|
|
|
|
bool ret = str && strstr(str, ext);
|
|
|
|
RARCH_LOG("Querying GL extension: %s => %s\n",
|
|
|
|
ext, ret ? "exists" : "doesn't exist");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-11 21:13:55 +00:00
|
|
|
static inline bool gl_check_error(void)
|
|
|
|
{
|
|
|
|
int error = glGetError();
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case GL_INVALID_ENUM:
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("GL: Invalid enum.\n");
|
2011-01-11 21:13:55 +00:00
|
|
|
break;
|
|
|
|
case GL_INVALID_VALUE:
|
2012-09-10 21:17:48 +00:00
|
|
|
RARCH_ERR("GL: Invalid value.\n");
|
2011-01-11 21:13:55 +00:00
|
|
|
break;
|
|
|
|
case GL_INVALID_OPERATION:
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("GL: Invalid operation.\n");
|
2011-01-11 21:13:55 +00:00
|
|
|
break;
|
|
|
|
case GL_OUT_OF_MEMORY:
|
2012-09-10 21:17:48 +00:00
|
|
|
RARCH_ERR("GL: Out of memory.\n");
|
2011-01-11 21:13:55 +00:00
|
|
|
break;
|
|
|
|
case GL_NO_ERROR:
|
|
|
|
return true;
|
|
|
|
default:
|
2012-09-10 21:17:48 +00:00
|
|
|
RARCH_ERR("Non specified GL error.\n");
|
2011-01-11 21:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-26 13:21:51 +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-14 21:09:35 +00:00
|
|
|
struct gl_fbo_rect
|
|
|
|
{
|
2011-03-14 22:48:19 +00:00
|
|
|
unsigned img_width;
|
|
|
|
unsigned img_height;
|
2011-03-27 19:21:46 +00:00
|
|
|
unsigned max_img_width;
|
|
|
|
unsigned max_img_height;
|
2011-03-14 21:09:35 +00:00
|
|
|
unsigned width;
|
|
|
|
unsigned height;
|
2011-03-14 21:51:03 +00:00
|
|
|
};
|
2011-03-14 21:09:35 +00:00
|
|
|
|
2011-03-27 18:29:47 +00:00
|
|
|
enum gl_scale_type
|
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_SCALE_ABSOLUTE,
|
|
|
|
RARCH_SCALE_INPUT,
|
|
|
|
RARCH_SCALE_VIEWPORT
|
2011-03-27 18:29:47 +00:00
|
|
|
};
|
|
|
|
|
2011-03-14 21:51:03 +00:00
|
|
|
struct gl_fbo_scale
|
|
|
|
{
|
2011-03-27 18:29:47 +00:00
|
|
|
enum gl_scale_type type_x;
|
|
|
|
enum gl_scale_type type_y;
|
2011-03-14 21:51:03 +00:00
|
|
|
float scale_x;
|
|
|
|
float scale_y;
|
2011-03-27 18:29:47 +00:00
|
|
|
unsigned abs_x;
|
|
|
|
unsigned abs_y;
|
2011-03-14 21:51:03 +00:00
|
|
|
bool valid;
|
2011-03-14 21:09:35 +00:00
|
|
|
};
|
|
|
|
|
2012-05-27 21:14:46 +00:00
|
|
|
struct gl_ortho
|
2012-05-27 20:39:29 +00:00
|
|
|
{
|
2012-05-27 21:14:46 +00:00
|
|
|
GLfloat left;
|
|
|
|
GLfloat right;
|
|
|
|
GLfloat bottom;
|
|
|
|
GLfloat top;
|
2012-05-27 21:33:31 +00:00
|
|
|
GLfloat znear;
|
|
|
|
GLfloat zfar;
|
2012-05-27 21:14:46 +00:00
|
|
|
};
|
2012-05-27 20:39:29 +00:00
|
|
|
|
2011-05-23 15:41:52 +00:00
|
|
|
struct gl_tex_info
|
|
|
|
{
|
|
|
|
GLuint tex;
|
2011-07-03 13:39:35 +00:00
|
|
|
GLfloat input_size[2];
|
|
|
|
GLfloat tex_size[2];
|
|
|
|
GLfloat coord[8];
|
2011-05-23 15:41:52 +00:00
|
|
|
};
|
|
|
|
|
2012-08-14 21:32:55 +00:00
|
|
|
struct gl_coords
|
|
|
|
{
|
|
|
|
const GLfloat *vertex;
|
|
|
|
const GLfloat *color;
|
|
|
|
const GLfloat *tex_coord;
|
2012-09-11 21:32:00 +00:00
|
|
|
const GLfloat *lut_tex_coord;
|
2012-08-14 21:32:55 +00:00
|
|
|
};
|
|
|
|
|
2012-11-14 21:51:08 +00:00
|
|
|
typedef struct gl_shader_backend gl_shader_backend_t;
|
|
|
|
|
2012-05-26 13:21:51 +00:00
|
|
|
#define MAX_SHADERS 16
|
|
|
|
|
2012-10-20 10:22:10 +00:00
|
|
|
#if (defined(HAVE_GLSL) || defined(HAVE_CG))
|
2012-05-26 13:21:51 +00:00
|
|
|
#define TEXTURES 8
|
|
|
|
#else
|
|
|
|
#define TEXTURES 1
|
|
|
|
#endif
|
|
|
|
#define TEXTURES_MASK (TEXTURES - 1)
|
|
|
|
|
|
|
|
typedef struct gl
|
|
|
|
{
|
2012-10-09 15:47:48 +00:00
|
|
|
const gfx_ctx_driver_t *ctx_driver;
|
2012-11-14 21:51:08 +00:00
|
|
|
const gl_shader_backend_t *shader;
|
|
|
|
|
2012-05-26 13:21:51 +00:00
|
|
|
bool vsync;
|
|
|
|
GLuint texture[TEXTURES];
|
|
|
|
unsigned tex_index; // For use with PREV.
|
|
|
|
struct gl_tex_info prev_info[TEXTURES];
|
|
|
|
GLuint tex_filter;
|
|
|
|
|
|
|
|
void *empty_buf;
|
2012-10-01 22:58:43 +00:00
|
|
|
|
2012-09-15 13:17:34 +00:00
|
|
|
void *conv_buffer;
|
2012-10-01 22:58:43 +00:00
|
|
|
struct scaler_ctx scaler;
|
2012-05-26 13:21:51 +00:00
|
|
|
|
|
|
|
unsigned frame_count;
|
|
|
|
|
|
|
|
#ifdef HAVE_FBO
|
|
|
|
// Render-to-texture, multipass shaders
|
|
|
|
GLuint fbo[MAX_SHADERS];
|
|
|
|
GLuint fbo_texture[MAX_SHADERS];
|
|
|
|
struct gl_fbo_rect fbo_rect[MAX_SHADERS];
|
|
|
|
struct gl_fbo_scale fbo_scale[MAX_SHADERS];
|
|
|
|
int fbo_pass;
|
|
|
|
bool fbo_inited;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool should_resize;
|
|
|
|
bool quitting;
|
|
|
|
bool fullscreen;
|
|
|
|
bool keep_aspect;
|
|
|
|
unsigned rotation;
|
|
|
|
|
|
|
|
unsigned full_x, full_y;
|
|
|
|
|
|
|
|
unsigned win_width;
|
|
|
|
unsigned win_height;
|
2012-10-27 22:38:31 +00:00
|
|
|
struct rarch_viewport vp;
|
|
|
|
unsigned vp_out_width;
|
|
|
|
unsigned vp_out_height;
|
2012-05-26 13:21:51 +00:00
|
|
|
unsigned last_width[TEXTURES];
|
|
|
|
unsigned last_height[TEXTURES];
|
|
|
|
unsigned tex_w, tex_h;
|
|
|
|
GLfloat tex_coords[8];
|
2012-08-09 20:33:32 +00:00
|
|
|
math_matrix mvp;
|
2012-05-26 13:21:51 +00:00
|
|
|
|
2012-08-14 21:32:55 +00:00
|
|
|
struct gl_coords coords;
|
|
|
|
|
2012-05-26 13:45:00 +00:00
|
|
|
GLuint pbo;
|
2012-10-19 23:12:02 +00:00
|
|
|
GLenum internal_fmt;
|
|
|
|
GLenum texture_type; // RGB565 or ARGB
|
2012-05-26 13:21:51 +00:00
|
|
|
GLenum texture_fmt;
|
2012-09-11 10:32:50 +00:00
|
|
|
GLenum border_type;
|
2012-05-26 13:21:51 +00:00
|
|
|
unsigned base_size; // 2 or 4
|
|
|
|
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
font_renderer_t *font;
|
|
|
|
GLuint font_tex;
|
|
|
|
int font_tex_w, font_tex_h;
|
2012-09-15 21:15:19 +00:00
|
|
|
uint16_t *font_tex_buf;
|
2012-05-26 13:21:51 +00:00
|
|
|
char font_last_msg[256];
|
|
|
|
int font_last_width, font_last_height;
|
|
|
|
GLfloat font_color[16];
|
|
|
|
GLfloat font_color_dark[16];
|
|
|
|
#endif
|
2012-09-24 23:26:22 +00:00
|
|
|
|
|
|
|
#ifdef RARCH_CONSOLE
|
|
|
|
bool block_swap;
|
|
|
|
#endif
|
2012-10-22 04:12:12 +00:00
|
|
|
#ifdef HAVE_RMENU
|
|
|
|
bool draw_rmenu;
|
2012-09-24 23:26:22 +00:00
|
|
|
#endif
|
2012-10-20 03:08:53 +00:00
|
|
|
|
|
|
|
bool egl_images;
|
2012-11-21 15:24:28 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_OPENGLES
|
|
|
|
// PBOs used for asynchronous viewport readbacks.
|
|
|
|
GLuint pbo_readback[4];
|
|
|
|
bool pbo_readback_enable;
|
|
|
|
bool pbo_readback_valid;
|
|
|
|
unsigned pbo_readback_index;
|
|
|
|
#endif
|
2012-05-26 13:21:51 +00:00
|
|
|
} gl_t;
|
|
|
|
|
2011-05-18 20:11:34 +00:00
|
|
|
// Windows ... <_<
|
2012-08-16 08:09:44 +00:00
|
|
|
#ifdef _WIN32
|
2011-05-18 20:40:42 +00:00
|
|
|
extern PFNGLCLIENTACTIVETEXTUREPROC pglClientActiveTexture;
|
|
|
|
extern PFNGLACTIVETEXTUREPROC pglActiveTexture;
|
2011-05-18 20:11:34 +00:00
|
|
|
#else
|
|
|
|
#define pglClientActiveTexture glClientActiveTexture
|
|
|
|
#define pglActiveTexture glActiveTexture
|
|
|
|
#endif
|
|
|
|
|
2012-09-11 07:55:03 +00:00
|
|
|
#if defined(HAVE_PSGL)
|
2012-10-19 23:12:02 +00:00
|
|
|
#define RARCH_GL_INTERNAL_FORMAT32 GL_ARGB_SCE
|
2012-11-18 17:23:20 +00:00
|
|
|
#define RARCH_GL_INTERNAL_FORMAT16 GL_RGB5
|
2012-10-19 23:12:02 +00:00
|
|
|
#define RARCH_GL_TEXTURE_TYPE32 GL_BGRA
|
|
|
|
#define RARCH_GL_TEXTURE_TYPE16 GL_BGRA
|
2012-10-02 18:16:55 +00:00
|
|
|
#define RARCH_GL_FORMAT32 GL_UNSIGNED_INT_8_8_8_8_REV
|
2012-10-22 01:56:28 +00:00
|
|
|
#define RARCH_GL_FORMAT16 GL_RGB5
|
2012-09-09 22:36:25 +00:00
|
|
|
#elif defined(HAVE_OPENGLES)
|
2012-10-19 23:12:02 +00:00
|
|
|
#define RARCH_GL_INTERNAL_FORMAT32 GL_BGRA_EXT
|
|
|
|
#define RARCH_GL_INTERNAL_FORMAT16 GL_RGB
|
|
|
|
#define RARCH_GL_TEXTURE_TYPE32 GL_BGRA_EXT
|
|
|
|
#define RARCH_GL_TEXTURE_TYPE16 GL_RGB
|
2012-09-15 13:17:34 +00:00
|
|
|
#define RARCH_GL_FORMAT32 GL_UNSIGNED_BYTE
|
2012-10-19 23:12:02 +00:00
|
|
|
#define RARCH_GL_FORMAT16 GL_UNSIGNED_SHORT_5_6_5
|
2012-05-27 16:46:20 +00:00
|
|
|
#else
|
2012-10-19 23:12:02 +00:00
|
|
|
// On desktop, we always use 32-bit.
|
|
|
|
#define RARCH_GL_INTERNAL_FORMAT32 GL_RGBA
|
|
|
|
#define RARCH_GL_INTERNAL_FORMAT16 GL_RGBA
|
|
|
|
#define RARCH_GL_TEXTURE_TYPE32 GL_BGRA
|
|
|
|
#define RARCH_GL_TEXTURE_TYPE16 GL_BGRA
|
2012-05-26 14:29:02 +00:00
|
|
|
#define RARCH_GL_FORMAT32 GL_UNSIGNED_INT_8_8_8_8_REV
|
2012-10-01 22:58:43 +00:00
|
|
|
#define RARCH_GL_FORMAT16 GL_UNSIGNED_INT_8_8_8_8_REV
|
2012-05-27 16:46:20 +00:00
|
|
|
#endif
|
2012-05-26 14:29:02 +00:00
|
|
|
|
2012-09-15 13:17:34 +00:00
|
|
|
// Platform specific workarounds/hacks.
|
2012-10-22 23:03:57 +00:00
|
|
|
#if defined(__CELLOS_LV2__)
|
2012-09-15 13:17:34 +00:00
|
|
|
#define NO_GL_READ_VIEWPORT
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 02:07:36 +00:00
|
|
|
#if defined(HAVE_OPENGL_MODERN) || defined(HAVE_OPENGLES2) || defined(HAVE_PSGL)
|
2012-09-15 13:17:34 +00:00
|
|
|
#define NO_GL_FF_VERTEX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENGL_MODERN) || defined(HAVE_OPENGLES2) || defined(HAVE_PSGL)
|
|
|
|
#define NO_GL_FF_MATRIX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENGLES2) // TODO: Figure out exactly what.
|
|
|
|
#define NO_GL_CLAMP_TO_BORDER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENGLES2) // It's an extension. Don't bother checking for it atm.
|
|
|
|
#undef GL_UNPACK_ROW_LENGTH
|
|
|
|
#endif
|
|
|
|
|
2012-10-26 19:09:30 +00:00
|
|
|
extern const GLfloat vertexes_flipped[];
|
|
|
|
extern const GLfloat white_color[];
|
|
|
|
|
2012-11-14 21:51:08 +00:00
|
|
|
void gl_shader_use(gl_t *gl, unsigned index);
|
2012-05-27 21:14:46 +00:00
|
|
|
void gl_set_projection(gl_t *gl, struct gl_ortho *ortho, bool allow_rotate);
|
2012-05-27 10:26:43 +00:00
|
|
|
void gl_set_viewport(gl_t *gl, unsigned width, unsigned height, bool force_full, bool allow_rotate);
|
2012-11-14 21:51:08 +00:00
|
|
|
void gl_shader_set_coords(gl_t *gl, const struct gl_coords *coords, const math_matrix *mat);
|
2012-05-27 10:26:43 +00:00
|
|
|
|
2012-06-08 20:39:18 +00:00
|
|
|
void gl_init_fbo(gl_t *gl, unsigned width, unsigned height);
|
2012-06-05 16:11:42 +00:00
|
|
|
void gl_deinit_fbo(gl_t *gl);
|
|
|
|
|
2011-01-11 21:13:55 +00:00
|
|
|
#endif
|