RetroArch/gfx/gl_common.h

341 lines
9.0 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2013-01-01 00:37:37 +00:00
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
*
2012-04-21 21:13:50 +00:00
* RetroArch 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch 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.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
* 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"
#include "math/matrix.h"
2012-09-24 23:26:22 +00:00
#include "gfx_context.h"
#include "scaler/scaler.h"
#include "fonts/gl_font.h"
#include "shader_parse.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
#include <string.h>
#ifdef HAVE_EGL
#include <EGL/egl.h>
#include <EGL/eglext.h>
#endif
#include "glsym/glsym.h"
#define context_get_video_size_func(win, height) gl->ctx_driver->get_video_size(win, height)
#define context_update_window_title_func() gl->ctx_driver->update_window_title()
#define context_destroy_func() gl->ctx_driver->destroy()
#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_post_render_func(gl) gl->ctx_driver->post_render(gl)
#define context_swap_interval_func(var) gl->ctx_driver->swap_interval(var)
#define context_has_focus_func() gl->ctx_driver->has_focus()
2012-12-02 09:49:17 +00:00
#define context_check_window_func(quit, resize, width, height, frame_count) \
gl->ctx_driver->check_window(quit, resize, width, height, frame_count)
#define context_set_video_mode_func(width, height, fullscreen) gl->ctx_driver->set_video_mode(width, height, fullscreen)
#define context_input_driver_func(input, input_data) gl->ctx_driver->input_driver(input, input_data)
#ifdef HAVE_EGL
#define context_init_egl_image_buffer_func(video) gl->ctx_driver->init_egl_image_buffer(video)
2012-12-02 09:49:17 +00:00
#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
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");
break;
case GL_INVALID_VALUE:
RARCH_ERR("GL: Invalid value.\n");
break;
case GL_INVALID_OPERATION:
2012-04-21 21:25:32 +00:00
RARCH_ERR("GL: Invalid operation.\n");
break;
case GL_OUT_OF_MEMORY:
RARCH_ERR("GL: Out of memory.\n");
break;
case GL_NO_ERROR:
return true;
default:
RARCH_ERR("Non specified GL error.\n");
}
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;
}
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;
unsigned width;
unsigned height;
};
2012-05-27 21:14:46 +00:00
struct gl_ortho
{
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
};
struct gl_tex_info
{
GLuint tex;
GLfloat input_size[2];
GLfloat tex_size[2];
GLfloat coord[8];
};
struct gl_coords
{
const GLfloat *vertex;
const GLfloat *color;
const GLfloat *tex_coord;
const GLfloat *lut_tex_coord;
};
typedef struct gl_shader_backend gl_shader_backend_t;
2012-05-26 13:21:51 +00:00
#define MAX_SHADERS 16
#define MAX_TEXTURES 8
2012-05-26 13:21:51 +00:00
typedef struct gl
{
const gfx_ctx_driver_t *ctx_driver;
const gl_shader_backend_t *shader;
2012-05-26 13:21:51 +00:00
bool vsync;
GLuint texture[MAX_TEXTURES];
2012-05-26 13:21:51 +00:00
unsigned tex_index; // For use with PREV.
unsigned textures;
struct gl_tex_info prev_info[MAX_TEXTURES];
2012-05-26 13:21:51 +00:00
GLuint tex_filter;
void *empty_buf;
2012-09-15 13:17:34 +00:00
void *conv_buffer;
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 gfx_fbo_scale fbo_scale[MAX_SHADERS];
2012-05-26 13:21:51 +00:00
int fbo_pass;
bool fbo_inited;
2013-03-27 15:15:15 +00:00
GLuint hw_render_fbo[MAX_TEXTURES];
GLuint hw_render_depth[MAX_TEXTURES];
2013-03-27 15:15:15 +00:00
bool hw_render_fbo_init;
2013-03-29 01:50:42 +00:00
bool hw_render_depth_init;
2012-05-26 13:21:51 +00:00
#endif
bool hw_render_use;
2012-05-26 13:21:51 +00:00
bool should_resize;
bool quitting;
bool fullscreen;
bool keep_aspect;
unsigned rotation;
unsigned full_x, full_y;
unsigned win_width;
unsigned win_height;
struct rarch_viewport vp;
unsigned vp_out_width;
unsigned vp_out_height;
unsigned last_width[MAX_TEXTURES];
unsigned last_height[MAX_TEXTURES];
2012-05-26 13:21:51 +00:00
unsigned tex_w, tex_h;
GLfloat tex_coords[8];
math_matrix mvp, mvp_no_rot;
2012-05-26 13:21:51 +00:00
struct gl_coords coords;
const GLfloat *vertex_ptr;
const GLfloat *white_color_ptr;
GLuint pbo;
GLenum internal_fmt;
GLenum texture_type; // RGB565 or ARGB
2012-05-26 13:21:51 +00:00
GLenum texture_fmt;
GLenum wrap_mode;
2012-05-26 13:21:51 +00:00
unsigned base_size; // 2 or 4
// Fonts
2012-12-14 21:33:04 +00:00
void *font;
2012-12-14 23:07:31 +00:00
const gl_font_renderer_t *font_ctx;
const font_renderer_driver_t *font_driver;
2012-05-26 13:21:51 +00:00
GLuint font_tex;
GLint max_font_size;
2012-05-26 13:21:51 +00:00
int font_tex_w, font_tex_h;
2013-06-23 09:55:21 +00:00
uint32_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];
2012-09-24 23:26:22 +00:00
bool egl_images;
video_info_t video_info;
2012-12-23 17:36:58 +00:00
#ifdef HAVE_OVERLAY
// Overlay rendering
bool overlay_enable;
2013-01-11 15:23:04 +00:00
bool overlay_full_screen;
GLuint tex_overlay;
GLfloat overlay_tex_coord[8];
GLfloat overlay_vertex_coord[8];
2013-01-29 20:51:15 +00:00
GLfloat overlay_alpha_mod;
2012-12-23 17:36:58 +00:00
#endif
#if !defined(HAVE_OPENGLES) && defined(HAVE_FFMPEG)
// PBOs used for asynchronous viewport readbacks.
GLuint pbo_readback[4];
bool pbo_readback_enable;
bool pbo_readback_valid;
unsigned pbo_readback_index;
struct scaler_ctx pbo_readback_scaler;
#endif
2013-09-29 16:04:56 +00:00
#if defined(HAVE_RGUI) || defined(HAVE_RMENU) || defined(HAVE_RMENU_XUI)
GLuint rgui_texture;
bool rgui_texture_enable;
bool rgui_texture_full_screen;
GLfloat rgui_texture_alpha;
#endif
2013-05-03 12:04:29 +00:00
#ifdef HAVE_GL_SYNC
#define MAX_FENCES 4
bool have_sync;
GLsync fences[MAX_FENCES];
unsigned fence_count;
2013-05-03 12:04:29 +00:00
#endif
bool core_context;
GLuint vao;
2012-05-26 13:21:51 +00:00
} gl_t;
#if defined(HAVE_PSGL)
#define RARCH_GL_INTERNAL_FORMAT32 GL_ARGB_SCE
2012-11-18 17:23:20 +00:00
#define RARCH_GL_INTERNAL_FORMAT16 GL_RGB5
#define RARCH_GL_TEXTURE_TYPE32 GL_BGRA
#define RARCH_GL_TEXTURE_TYPE16 GL_BGRA
#define RARCH_GL_FORMAT32 GL_UNSIGNED_INT_8_8_8_8_REV
#define RARCH_GL_FORMAT16 GL_RGB5
#elif defined(HAVE_OPENGLES)
// Imgtec/SGX headers have this missing.
#ifndef GL_BGRA_EXT
#define GL_BGRA_EXT 0x80E1
#endif
#ifdef IOS
#define RARCH_GL_INTERNAL_FORMAT32 GL_RGBA // Stupid Apple
#else
#define RARCH_GL_INTERNAL_FORMAT32 GL_BGRA_EXT
#endif
#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
#define RARCH_GL_FORMAT16 GL_UNSIGNED_SHORT_5_6_5
#else
// 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
#define RARCH_GL_FORMAT16 GL_UNSIGNED_INT_8_8_8_8_REV
#endif
2012-05-26 14:29:02 +00:00
2012-09-15 13:17:34 +00:00
// Platform specific workarounds/hacks.
#if defined(__CELLOS_LV2__)
2012-09-15 13:17:34 +00:00
#define NO_GL_READ_VIEWPORT
#endif
#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
void gl_set_projection(void *data, struct gl_ortho *ortho, bool allow_rotate);
void gl_set_viewport(void *data, unsigned width, unsigned height, bool force_full, bool allow_rotate);
void gl_shader_set_coords(void *data, const struct gl_coords *coords, const math_matrix *mat);
2012-05-27 10:26:43 +00:00
void gl_init_fbo(void *data, unsigned width, unsigned height);
void gl_deinit_fbo(void *data);
2012-06-05 16:11:42 +00:00
static inline GLenum gl_wrap_type_to_enum(enum gfx_wrap_type type)
{
switch (type)
{
#ifndef HAVE_OPENGLES
case RARCH_WRAP_BORDER:
return GL_CLAMP_TO_BORDER;
#else
case RARCH_WRAP_BORDER:
#endif
case RARCH_WRAP_EDGE:
return GL_CLAMP_TO_EDGE;
case RARCH_WRAP_REPEAT:
return GL_REPEAT;
case RARCH_WRAP_MIRRORED_REPEAT:
return GL_MIRRORED_REPEAT;
default:
return 0;
}
}
#endif