RetroArch/gfx/drivers/gl_common.h

364 lines
9.4 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 18:06:50 +01:00
* copyright (c) 2011-2015 - Daniel De Matteis
*
2012-04-21 23:13:50 +02: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 23:13:50 +02: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 23:31:57 +02: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
2015-07-12 04:20:48 +02:00
#include <string.h>
2011-05-18 22:11:34 +02:00
#ifdef HAVE_CONFIG_H
2015-04-09 04:57:17 +02:00
#include "../../config.h"
2011-05-18 22:11:34 +02:00
#endif
#ifdef HAVE_EGL
#include <EGL/egl.h>
#include <EGL/eglext.h>
#endif
#include <glsym/glsym.h>
2015-07-12 04:20:48 +02:00
#include <retro_inline.h>
#include <gfx/math/matrix_4x4.h>
#include <gfx/scaler/scaler.h>
#include <formats/image.h>
#include "../../general.h"
#include "../font_renderer_driver.h"
2015-07-12 04:23:27 +02:00
#include "../video_common.h"
2015-07-12 04:20:48 +02:00
#include "../video_context_driver.h"
#include "../video_shader_driver.h"
#include "../video_shader_parse.h"
#if (!defined(HAVE_OPENGLES) || defined(HAVE_OPENGLES3))
#ifdef GL_PIXEL_PACK_BUFFER
#define HAVE_GL_ASYNC_READBACK
#endif
#endif
#if defined(HAVE_PSGL)
#define RARCH_GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
#define RARCH_GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE_OES
#define RARCH_GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_EXT
2014-05-22 20:30:21 +02:00
#elif defined(OSX_PPC)
#define RARCH_GL_FRAMEBUFFER GL_FRAMEBUFFER_EXT
#define RARCH_GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE_EXT
#define RARCH_GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_EXT
#else
#define RARCH_GL_FRAMEBUFFER GL_FRAMEBUFFER
#define RARCH_GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE
#define RARCH_GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0
#endif
#if defined(HAVE_OPENGLES2)
#define RARCH_GL_RENDERBUFFER GL_RENDERBUFFER
#define RARCH_GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#define RARCH_GL_DEPTH_ATTACHMENT GL_DEPTH_ATTACHMENT
#define RARCH_GL_STENCIL_ATTACHMENT GL_STENCIL_ATTACHMENT
#elif defined(OSX_PPC)
#define RARCH_GL_RENDERBUFFER GL_RENDERBUFFER_EXT
#define RARCH_GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_EXT
#define RARCH_GL_DEPTH_ATTACHMENT GL_DEPTH_ATTACHMENT_EXT
#define RARCH_GL_STENCIL_ATTACHMENT GL_STENCIL_ATTACHMENT_EXT
#elif defined(HAVE_PSGL)
2014-05-27 22:15:25 +02:00
#define RARCH_GL_RENDERBUFFER GL_RENDERBUFFER_OES
#define RARCH_GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_SCE
#define RARCH_GL_DEPTH_ATTACHMENT GL_DEPTH_ATTACHMENT_OES
#define RARCH_GL_STENCIL_ATTACHMENT GL_STENCIL_ATTACHMENT_OES
#else
#define RARCH_GL_RENDERBUFFER GL_RENDERBUFFER
#define RARCH_GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8
#define RARCH_GL_DEPTH_ATTACHMENT GL_DEPTH_ATTACHMENT
#define RARCH_GL_STENCIL_ATTACHMENT GL_STENCIL_ATTACHMENT
#endif
#ifdef OSX_PPC
#define RARCH_GL_MAX_RENDERBUFFER_SIZE GL_MAX_RENDERBUFFER_SIZE_EXT
#elif defined(HAVE_PSGL)
2014-05-27 22:15:25 +02:00
#define RARCH_GL_MAX_RENDERBUFFER_SIZE GL_MAX_RENDERBUFFER_SIZE_OES
#else
#define RARCH_GL_MAX_RENDERBUFFER_SIZE GL_MAX_RENDERBUFFER_SIZE
#endif
#if defined(HAVE_PSGL)
2014-05-27 22:15:25 +02:00
#define glGenerateMipmap glGenerateMipmapOES
#endif
2014-05-27 22:27:16 +02:00
#ifdef HAVE_FBO
#if defined(__APPLE__) || defined(HAVE_PSGL)
#define GL_RGBA32F GL_RGBA32F_ARB
#endif
#endif
2015-01-12 07:02:55 +01:00
#if defined(HAVE_PSGL)
#define RARCH_GL_INTERNAL_FORMAT32 GL_ARGB_SCE
#define RARCH_GL_INTERNAL_FORMAT16 GL_RGB5 /* TODO: Verify if this is really 565 or just 555. */
#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
/* Stupid Apple. */
#define RARCH_GL_INTERNAL_FORMAT32 GL_RGBA
#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
#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_RGBA8
#define RARCH_GL_INTERNAL_FORMAT16 GL_RGBA8
#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_UNSIGNED_INT_8_8_8_8_REV
/* GL_RGB565 internal format isn't in desktop GL
* until 4.1 core (ARB_ES2_compatibility).
* Check for this. */
#ifndef GL_RGB565
#define GL_RGB565 0x8D62
#endif
#define RARCH_GL_INTERNAL_FORMAT16_565 GL_RGB565
#define RARCH_GL_TEXTURE_TYPE16_565 GL_RGB
#define RARCH_GL_FORMAT16_565 GL_UNSIGNED_SHORT_5_6_5
#endif
/* Platform specific workarounds/hacks. */
#if defined(__CELLOS_LV2__)
#define NO_GL_READ_PIXELS
#endif
#if defined(HAVE_OPENGL_MODERN) || defined(HAVE_OPENGLES2) || defined(HAVE_PSGL)
#ifndef NO_GL_FF_VERTEX
#define NO_GL_FF_VERTEX
#endif
#endif
#if defined(HAVE_OPENGL_MODERN) || defined(HAVE_OPENGLES2) || defined(HAVE_PSGL)
#ifndef NO_GL_FF_MATRIX
#define NO_GL_FF_MATRIX
#endif
#endif
#if defined(HAVE_OPENGLES2) /* TODO: Figure out exactly what. */
#define NO_GL_CLAMP_TO_BORDER
#endif
#if defined(HAVE_OPENGLES)
#ifndef GL_UNPACK_ROW_LENGTH
#define GL_UNPACK_ROW_LENGTH 0x0CF2
#endif
#ifndef GL_SRGB_ALPHA_EXT
#define GL_SRGB_ALPHA_EXT 0x8C42
#endif
#endif
struct gl_font_renderer;
2012-05-26 15:21:51 +02:00
typedef struct gl
{
uint64_t frame_count;
const shader_backend_t *shader;
2012-05-26 15:21:51 +02:00
bool vsync;
GLuint texture[GFX_MAX_TEXTURES];
2014-09-12 02:51:23 +02:00
unsigned tex_index; /* For use with PREV. */
unsigned textures;
2015-07-12 04:16:40 +02:00
struct gfx_tex_info tex_info;
struct gfx_tex_info prev_info[GFX_MAX_TEXTURES];
2014-05-11 13:13:38 +02:00
GLuint tex_mag_filter;
GLuint tex_min_filter;
bool tex_mipmap;
2012-05-26 15:21:51 +02:00
void *empty_buf;
2012-09-15 15:17:34 +02:00
void *conv_buffer;
struct scaler_ctx scaler;
2012-05-26 15:21:51 +02:00
#ifdef HAVE_FBO
2014-09-12 02:51:23 +02:00
/* Render-to-texture, multipass shaders. */
GLuint fbo[GFX_MAX_SHADERS];
GLuint fbo_texture[GFX_MAX_SHADERS];
2015-07-12 04:04:56 +02:00
struct gfx_fbo_rect fbo_rect[GFX_MAX_SHADERS];
struct gfx_fbo_scale fbo_scale[GFX_MAX_SHADERS];
2012-05-26 15:21:51 +02:00
int fbo_pass;
bool fbo_inited;
2013-03-27 16:15:15 +01:00
bool fbo_feedback_enable;
unsigned fbo_feedback_pass;
GLuint fbo_feedback;
GLuint fbo_feedback_texture;
GLuint hw_render_fbo[GFX_MAX_TEXTURES];
GLuint hw_render_depth[GFX_MAX_TEXTURES];
2013-03-27 16:15:15 +01:00
bool hw_render_fbo_init;
2013-03-29 02:50:42 +01:00
bool hw_render_depth_init;
bool has_fp_fbo;
2014-05-11 13:13:38 +02:00
bool has_srgb_fbo;
bool has_srgb_fbo_gles3;
2012-05-26 15:21:51 +02:00
#endif
bool hw_render_use;
2014-04-19 16:59:26 +02:00
bool shared_context_use;
2012-05-26 15:21:51 +02:00
bool should_resize;
bool quitting;
bool fullscreen;
bool keep_aspect;
unsigned rotation;
unsigned full_x, full_y;
2015-02-14 05:52:05 +01:00
struct video_viewport vp;
unsigned vp_out_width;
unsigned vp_out_height;
unsigned last_width[GFX_MAX_TEXTURES];
unsigned last_height[GFX_MAX_TEXTURES];
2012-05-26 15:21:51 +02:00
unsigned tex_w, tex_h;
math_matrix_4x4 mvp, mvp_no_rot;
2012-05-26 15:21:51 +02:00
2015-07-12 03:57:06 +02:00
struct gfx_coords coords;
const float *vertex_ptr;
const float *white_color_ptr;
GLuint pbo;
GLenum internal_fmt;
2014-09-12 02:51:23 +02:00
GLenum texture_type; /* RGB565 or ARGB */
2012-05-26 15:21:51 +02:00
GLenum texture_fmt;
GLenum wrap_mode;
2014-09-12 02:51:23 +02:00
unsigned base_size; /* 2 or 4 */
#ifdef HAVE_OPENGLES
bool support_unpack_row_length;
#else
bool have_es2_compat;
#endif
2012-05-26 15:21:51 +02:00
bool egl_images;
video_info_t video_info;
2012-12-23 18:36:58 +01:00
#ifdef HAVE_OVERLAY
unsigned overlays;
bool overlay_enable;
2013-01-11 16:23:04 +01:00
bool overlay_full_screen;
GLuint *overlay_tex;
2015-07-12 03:57:06 +02:00
float *overlay_vertex_coord;
float *overlay_tex_coord;
float *overlay_color_coord;
2012-12-23 18:36:58 +01:00
#endif
#ifdef HAVE_GL_ASYNC_READBACK
2014-09-12 02:51:23 +02:00
/* PBOs used for asynchronous viewport readbacks. */
GLuint pbo_readback[4];
bool pbo_readback_valid[4];
bool pbo_readback_enable;
unsigned pbo_readback_index;
struct scaler_ctx pbo_readback_scaler;
#endif
void *readback_buffer_screenshot;
#if defined(HAVE_MENU)
GLuint menu_texture;
bool menu_texture_enable;
bool menu_texture_full_screen;
2015-07-12 03:57:06 +02:00
float menu_texture_alpha;
#endif
2013-05-03 14:04:29 +02:00
#ifdef HAVE_GL_SYNC
#define MAX_FENCES 4
bool have_sync;
GLsync fences[MAX_FENCES];
unsigned fence_count;
2013-05-03 14:04:29 +02:00
#endif
bool core_context;
GLuint vao;
2012-05-26 15:21:51 +02:00
} gl_t;
2015-03-30 02:53:11 +02:00
static INLINE void context_bind_hw_render(gl_t *gl, bool enable)
{
if (!gl)
return;
2015-04-10 09:07:53 +02:00
if (gl->shared_context_use)
gfx_ctx_bind_hw_render(gl, enable);
2015-03-30 02:53:11 +02:00
}
2015-03-15 04:52:46 +01:00
static INLINE bool gl_check_error(void)
2015-01-12 07:02:55 +01:00
{
int error = glGetError();
switch (error)
{
case GL_INVALID_ENUM:
RARCH_ERR("GL: Invalid enum.\n");
break;
case GL_INVALID_VALUE:
RARCH_ERR("GL: Invalid value.\n");
break;
case GL_INVALID_OPERATION:
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;
}
2015-01-12 07:02:55 +01:00
RARCH_ERR("Non specified GL error.\n");
return false;
}
2015-01-19 21:24:08 +01:00
bool gl_load_luts(const struct video_shader *generic_shader,
2014-10-02 13:32:42 +02:00
GLuint *lut_textures);
static INLINE unsigned 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;
}
return 0;
}
void gl_ff_vertex(const void *data);
void gl_ff_matrix(const void *data);
2014-10-02 13:32:42 +02:00
#endif