From 61b0811501f0b975e306d7447e378e175232ad8c Mon Sep 17 00:00:00 2001 From: Twinaphex Date: Mon, 10 Sep 2012 00:36:25 +0200 Subject: [PATCH] (Android/GLES) Add HAVE_OPENGLES defines to gl.c - remaining stuff todo - GL_QUADS not supported on GLES - GL_UNPACK_ROW_LENGTH/GL_PACK_ROW_LENGTH not supported on GLES without extensions - all client state array stuff unsupported - - Query extensions and see if GL_UNPACK_ROW_LENGTH and GL_PACK_ROW_LENGTH are available (on Tegra SOCs they should) --- android/jni/Android.mk | 4 +- gfx/gl.c | 117 ++++++++++++++++++++++++++++++++++++++--- gfx/gl_common.h | 11 ++++ 3 files changed, 125 insertions(+), 7 deletions(-) diff --git a/android/jni/Android.mk b/android/jni/Android.mk index 7604e18bdf..20570b913c 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -7,6 +7,8 @@ include $(CLEAR_VARS) LOCAL_MODULE := retroarch LOCAL_SRC_FILES = ../../console/griffin/griffin.c ../../console/rzlib/rzlib.c -LOCAL_CFLAGS = -DANDROID -DHAVE_DYNAMIC -DHAVE_ZLIB -DINLINE=inline -DRARCH_CONSOLE -DLSB_FIRST -D__LIBRETRO__ -DHAVE_CONFIGFILE=1 -DHAVE_GRIFFIN=1 -DPACKAGE_VERSION=\"$(RARCH_VERSION)\" -Dmain=rarch_main -std=gnu99 +LOCAL_CFLAGS = -DANDROID -DHAVE_DYNAMIC -DHAVE_OPENGL -DHAVE_OPENGLES -DHAVE_OPENGLES2 -DHAVE_ZLIB -DINLINE=inline -DRARCH_CONSOLE -DLSB_FIRST -D__LIBRETRO__ -DHAVE_CONFIGFILE=1 -DHAVE_GRIFFIN=1 -DPACKAGE_VERSION=\"$(RARCH_VERSION)\" -Dmain=rarch_main -std=gnu99 + +LOCAL_LDLIBS := -lGLESv2 include $(BUILD_SHARED_LIBRARY) diff --git a/gfx/gl.c b/gfx/gl.c index dc897ab54b..3cf67e9248 100644 --- a/gfx/gl.c +++ b/gfx/gl.c @@ -48,6 +48,11 @@ #include "shader_glsl.h" #endif +#ifdef HAVE_OPENGLES +bool use_unpack_row_length = false; +bool use_pack_row_length = false; +#endif + extern const GLfloat vertexes_flipped[]; extern const GLfloat white_color[]; @@ -108,7 +113,7 @@ static bool load_fbo_proc(void) return pglGenFramebuffers && pglBindFramebuffer && pglFramebufferTexture2D && pglCheckFramebufferStatus && pglDeleteFramebuffers; } -#elif defined(HAVE_OPENGLES11) +#elif defined(HAVE_OPENGLES) #define pglGenFramebuffers glGenFramebuffersOES #define pglBindFramebuffer glBindFramebufferOES #define pglFramebufferTexture2D glFramebufferTexture2DOES @@ -354,8 +359,14 @@ static void gl_create_fbo_textures(gl_t *gl) for (int i = 0; i < gl->fbo_pass; i++) { glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[i]); +#ifdef HAVE_OPENGLES + /* Doesn't support GL_CLAMP_TO_BORDER */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +#else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); +#endif GLuint filter_type = base_filt; bool smooth = false; @@ -480,6 +491,12 @@ void gl_init_fbo(gl_t *gl, unsigned width, unsigned height) //////////// +#ifdef HAVE_OPENGLES +void gl_set_coords(const struct gl_coords *coords, unsigned unit) +{ + /* stub */ +} +#else void gl_set_coords(const struct gl_coords *coords, unsigned unit) { pglClientActiveTexture(GL_TEXTURE0 + unit); @@ -504,6 +521,7 @@ void gl_set_coords(const struct gl_coords *coords, unsigned unit) pglClientActiveTexture(GL_TEXTURE0); } +#endif void gl_set_projection(gl_t *gl, struct gl_ortho *ortho, bool allow_rotate) { @@ -705,7 +723,11 @@ static void gl_frame_fbo(gl_t *gl, const struct gl_tex_info *tex_info) tex_info, gl->prev_info, fbo_tex_info, fbo_tex_info_cnt); gl_set_coords(&gl->coords, 0); +#ifdef HAVE_OPENGLES + /* stub - Doesn't support GL_QUADS */ +#else glDrawArrays(GL_QUADS, 0, 4); +#endif fbo_tex_info_cnt++; } @@ -734,7 +756,11 @@ static void gl_frame_fbo(gl_t *gl, const struct gl_tex_info *tex_info) gl->coords.vertex = vertex_ptr; gl_set_coords(&gl->coords, 0); +#ifdef HAVE_OPENGLES + /* stub - Doesn't support GL_QUADS */ +#else glDrawArrays(GL_QUADS, 0, 4); +#endif gl->coords.tex_coord = gl->tex_coords; } @@ -772,7 +798,13 @@ static void gl_update_input_size(gl_t *gl, unsigned width, unsigned height, unsi gl->empty_buf); #else glPixelStorei(GL_UNPACK_ALIGNMENT, get_alignment(pitch)); +#ifdef HAVE_OPENGLES + /* stub - Doesn't support GL_UNPACK_ROW_LENGTH without extension */ + if(!use_unpack_row_length) { } + else +#else glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->tex_w); +#endif glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, gl->tex_w, gl->tex_h, gl->texture_type, @@ -825,8 +857,14 @@ static void gl_init_textures(gl_t *gl) { glBindTexture(GL_TEXTURE_2D, gl->texture[i]); +#ifdef HAVE_OPENGLES + /* Doesn't support GL_CLAMP_TO_BORDER */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +#else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); +#endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->tex_filter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->tex_filter); @@ -841,7 +879,13 @@ static void gl_init_textures(gl_t *gl) #else static inline void gl_copy_frame(gl_t *gl, const void *frame, unsigned width, unsigned height, unsigned pitch) { +#ifdef HAVE_OPENGLES + /* stub - Doesn't support GL_UNPACK_ROW_LENGTH without extension */ + if(!use_unpack_row_length) { } + else +#else glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / gl->base_size); +#endif glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, gl->texture_type, gl->texture_fmt, frame); @@ -854,12 +898,23 @@ static void gl_init_textures(gl_t *gl) { glBindTexture(GL_TEXTURE_2D, gl->texture[i]); +#ifdef HAVE_OPENGLES + /* Doesn't support GL_CLAMP_TO_BORDER */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +#else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); +#endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->tex_filter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->tex_filter); +#ifdef HAVE_OPENGLES + if(!use_unpack_row_length) { } + else +#else glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->tex_w); +#endif glTexImage2D(GL_TEXTURE_2D, 0, RARCH_GL_INTERNAL_FORMAT, gl->tex_w, gl->tex_h, 0, gl->texture_type, gl->texture_fmt, gl->empty_buf ? gl->empty_buf : NULL); @@ -892,7 +947,11 @@ static void gl_render_menu(gl_t *gl) gl->coords.vertex = default_vertex_ptr; gl_set_coords(&gl->coords, 0); +#ifdef HAVE_OPENGLES + /* Doesn't support GL_QUADS */ +#else glDrawArrays(GL_QUADS, 0, 4); +#endif glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); } #endif @@ -949,7 +1008,11 @@ static bool gl_frame(void *data, const void *frame, unsigned width, unsigned hei &tex_info, gl->prev_info, NULL, 0); gl_set_coords(&gl->coords, 0); +#ifdef HAVE_OPENGLES + /* Doesn't support GL_QUADS */ +#else glDrawArrays(GL_QUADS, 0, 4); +#endif #ifdef HAVE_FBO if (gl->fbo_inited) @@ -978,6 +1041,15 @@ static bool gl_frame(void *data, const void *frame, unsigned width, unsigned hei return true; } +static void gl_free_arrays(void) +{ +#ifndef HAVE_OPENGLES + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); +#endif +} + static void gl_free(void *data) { #ifdef RARCH_CONSOLE @@ -989,9 +1061,7 @@ static void gl_free(void *data) gl_deinit_font(gl); gl_shader_deinit(); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); + gl_free_arrays(); glDeleteTextures(TEXTURES, gl->texture); #ifdef HAVE_OPENGL_TEXREF @@ -1019,8 +1089,34 @@ static void gl_set_nonblock_state(void *data, bool state) gfx_ctx_set_swap_interval(state ? 0 : 1, true); } +#ifdef HAVE_OPENGLES +static bool gl_check_extensions(char *extension_name) +{ + char *p = (char *) glGetString(GL_EXTENSIONS); + char *end; + int extNameLen; + + extNameLen = strlen(extension_name); + end = p + strlen(p); + + while (p < end) + { + int n = strcspn(p, " "); + if ((extNameLen == n) && (strncmp(extension_name, p, n) == 0)) + return true; + + p += (n + 1); + } + return false; +} +#endif + static void *gl_init(const video_info_t *video, const input_driver_t **input, void **input_data) { +#ifdef HAVE_OPENGLES + if(gl_check_extensions("GL_EXT_unpack_subimage")) + use_unpack_row_length = true; +#endif #ifdef _WIN32 gfx_set_dwm(); #endif @@ -1256,11 +1352,17 @@ static bool gl_read_viewport(void *data, uint8_t *buffer) glGetIntegerv(GL_VIEWPORT, vp); glPixelStorei(GL_PACK_ALIGNMENT, get_alignment(vp[2])); +#ifdef HAVE_OPENGLES + /* stub - Doesn't support GL_PACK_ROW_LENGTH without extension */ + if(!use_pack_row_length) { } + else +#else glPixelStorei(GL_PACK_ROW_LENGTH, vp[2]); +#endif glReadPixels(vp[0], vp[1], vp[2], vp[3], - GL_BGR, GL_UNSIGNED_BYTE, buffer); + RARCH_GL_TEXTURE_TYPE, GL_UNSIGNED_BYTE, buffer); return true; } @@ -1294,6 +1396,7 @@ static void gl_start(void) gfx_ctx_menu_init(); #endif +#ifdef HAVE_FBO // FBO mode has to be enabled once even if FBO mode has to be // turned off if (!g_console.fbo_enabled) @@ -1302,6 +1405,7 @@ static void gl_start(void) gfx_ctx_apply_fbo_state_changes(FBO_INIT); gfx_ctx_apply_fbo_state_changes(FBO_DEINIT); } +#endif } static void gl_stop(void) @@ -1332,7 +1436,9 @@ static void gl_restart(void) #endif gl_stop(); +#ifdef HAVE_CG gl_cg_invalidate_context(); +#endif gl_start(); #ifdef HAVE_CG_MENU @@ -1388,4 +1494,3 @@ const video_driver_t video_gl = { NULL, #endif }; - diff --git a/gfx/gl_common.h b/gfx/gl_common.h index 15e377dbfc..2bc3212140 100644 --- a/gfx/gl_common.h +++ b/gfx/gl_common.h @@ -36,6 +36,12 @@ #include #include #define GL_QUADS 0x0007 +#elif defined(HAVE_OPENGLES2) +#include +#include +#elif defined(HAVE_OPENGLES1) +#include +#include #else #define GL_GLEXT_PROTOTYPES #include @@ -224,6 +230,11 @@ extern PFNGLACTIVETEXTUREPROC pglActiveTexture; #define RARCH_GL_TEXTURE_TYPE GL_ARGB_SCE #define RARCH_GL_FORMAT32 GL_UNSIGNED_INT_8_8_8_8 #define RARCH_GL_FORMAT16 GL_RGB5_A1 +#elif defined(HAVE_OPENGLES) +#define RARCH_GL_INTERNAL_FORMAT GL_RGBA +#define RARCH_GL_TEXTURE_TYPE GL_RGBA +#define RARCH_GL_FORMAT32 GL_UNSIGNED_INT +#define RARCH_GL_FORMAT16 GL_UNSIGNED_SHORT #else #define RARCH_GL_INTERNAL_FORMAT GL_RGBA #define RARCH_GL_TEXTURE_TYPE GL_BGRA