From 4f69b5e34b10f2ef510b98ab302ceec931fe1e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Fri, 17 Jul 2015 21:55:58 -0300 Subject: [PATCH 1/4] (gl_raster_font) Use a smaller pixel format to store the font atlas --- gfx/drivers_font/gl_raster_font.c | 95 ++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 32 deletions(-) diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index c52b720745..0a65530f7b 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -47,13 +47,68 @@ typedef struct gfx_font_raster_block_t *block; } gl_raster_t; +static void gl_raster_font_free_font(void *data); + +static bool gl_raster_font_upload_atlas(gl_raster_t *font, + const struct font_atlas *atlas, + unsigned width, unsigned height) +{ + unsigned i, j; + GLint gl_internal = GL_LUMINANCE_ALPHA; + GLenum gl_format = GL_LUMINANCE_ALPHA; + size_t ncomponents = 2; + uint8_t *tmp = NULL; + +#ifndef HAVE_OPENGLES + if (font->gl->core_context) + { + GLint swizzle[] = { GL_ONE, GL_ONE, GL_ONE, GL_RED }; + glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle); + + gl_internal = GL_R8; + gl_format = GL_RED; + ncomponents = 1; + } +#endif + + tmp = (uint8_t*)calloc(height, width * ncomponents); + + if (!tmp) + return false; + + for (i = 0; i < atlas->height; ++i) + { + const uint8_t *src = &atlas->buffer[i * atlas->width]; + uint8_t *dst = &tmp[i * width * ncomponents]; + + if (ncomponents == 1) + { + memcpy(dst, src, atlas->width); + src += atlas->width; + } + else if (ncomponents == 2) + { + for (j = 0; j < atlas->width; ++j) + { + *dst++ = 0xff; + *dst++ = *src++; + } + } + } + + glTexImage2D(GL_TEXTURE_2D, 0, gl_internal, width, height, + 0, gl_format, GL_UNSIGNED_BYTE, tmp); + + free(tmp); + + return true; +} + static void *gl_raster_font_init_font(void *data, const char *font_path, float font_size) { - unsigned width, height; - uint8_t *tmp_buffer; const struct font_atlas *atlas = NULL; - gl_raster_t *font = (gl_raster_t*)calloc(1, sizeof(*font)); + gl_raster_t *font = (gl_raster_t*)calloc(1, sizeof(*font)); if (!font) return NULL; @@ -77,39 +132,15 @@ static void *gl_raster_font_init_font(void *data, atlas = font->font_driver->get_atlas(font->font_data); - width = next_pow2(atlas->width); - height = next_pow2(atlas->height); + font->tex_width = next_pow2(atlas->width);; + font->tex_height = next_pow2(atlas->height);; - /* Ideally, we'd use single component textures, but the - * difference in ways to do that between core GL and GLES/legacy GL - * is too great to bother going down that route. */ - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - - tmp_buffer = (uint8_t*)malloc(atlas->width * atlas->height * 4); - - if (tmp_buffer) + if (!gl_raster_font_upload_atlas(font, atlas, font->tex_width, font->tex_height)) { - unsigned i; - uint8_t *dst = tmp_buffer; - const uint8_t *src = atlas->buffer; - - for (i = 0; i < atlas->width * atlas->height; i++) - { - *dst++ = 0xff; - *dst++ = 0xff; - *dst++ = 0xff; - *dst++ = *src++; - } - - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, atlas->width, - atlas->height, GL_RGBA, GL_UNSIGNED_BYTE, tmp_buffer); - free(tmp_buffer); + gl_raster_font_free_font(font); + font = NULL; } - font->tex_width = width; - font->tex_height = height; - glBindTexture(GL_TEXTURE_2D, font->gl->texture[font->gl->tex_index]); return font; From f9f98f9f2e44ce596f38308745e323fb85063080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Sat, 18 Jul 2015 14:19:31 -0300 Subject: [PATCH 2/4] (gl_raster_font) Enable GL_R8 atlas for GL >= 3 --- gfx/drivers_font/gl_raster_font.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index 0a65530f7b..39d6926449 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -60,7 +60,12 @@ static bool gl_raster_font_upload_atlas(gl_raster_t *font, uint8_t *tmp = NULL; #ifndef HAVE_OPENGLES - if (font->gl->core_context) + struct retro_hw_render_callback *cb = video_driver_callback(); + bool modern = font->gl->core_context || + (cb->context_type == RETRO_HW_CONTEXT_OPENGL && + cb->version_major >= 3); + + if (modern) { GLint swizzle[] = { GL_ONE, GL_ONE, GL_ONE, GL_RED }; glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle); From 10345e08996a9efc33790bc987c67114679beb1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Sun, 19 Jul 2015 12:42:36 -0300 Subject: [PATCH 3/4] (gl_raster_font) Add back the old compat code --- gfx/drivers_font/gl_raster_font.c | 45 ++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index 39d6926449..f6b749df79 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -58,9 +58,10 @@ static bool gl_raster_font_upload_atlas(gl_raster_t *font, GLenum gl_format = GL_LUMINANCE_ALPHA; size_t ncomponents = 2; uint8_t *tmp = NULL; + struct retro_hw_render_callback *cb = video_driver_callback(); + bool ancient = false; /* add a check here if needed */ #ifndef HAVE_OPENGLES - struct retro_hw_render_callback *cb = video_driver_callback(); bool modern = font->gl->core_context || (cb->context_type == RETRO_HW_CONTEXT_OPENGL && cb->version_major >= 3); @@ -74,7 +75,13 @@ static bool gl_raster_font_upload_atlas(gl_raster_t *font, gl_format = GL_RED; ncomponents = 1; } + else #endif + if (ancient) + { + gl_internal = gl_format = GL_RGBA; + ncomponents = 4; + } tmp = (uint8_t*)calloc(height, width * ncomponents); @@ -86,18 +93,32 @@ static bool gl_raster_font_upload_atlas(gl_raster_t *font, const uint8_t *src = &atlas->buffer[i * atlas->width]; uint8_t *dst = &tmp[i * width * ncomponents]; - if (ncomponents == 1) + switch (ncomponents) { - memcpy(dst, src, atlas->width); - src += atlas->width; - } - else if (ncomponents == 2) - { - for (j = 0; j < atlas->width; ++j) - { - *dst++ = 0xff; - *dst++ = *src++; - } + case 1: + memcpy(dst, src, atlas->width); + src += atlas->width; + break; + case 2: + for (j = 0; j < atlas->width; ++j) + { + *dst++ = 0xff; + *dst++ = *src++; + } + break; + case 4: + for (j = 0; j < atlas->width; ++j) + { + *dst++ = 0xff; + *dst++ = 0xff; + *dst++ = 0xff; + *dst++ = *src++; + } + break; + default: + RARCH_ERR("Unsupported number of components: %u\n", (unsigned)ncomponents); + free(tmp); + return false; } } From b1c9d483688ee24fd2b71b417e106207f1b390c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Sun, 19 Jul 2015 13:01:28 -0300 Subject: [PATCH 4/4] (gl_raster_font) Reorder ancient/modern checks --- gfx/drivers_font/gl_raster_font.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index f6b749df79..5d057747b4 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -60,13 +60,19 @@ static bool gl_raster_font_upload_atlas(gl_raster_t *font, uint8_t *tmp = NULL; struct retro_hw_render_callback *cb = video_driver_callback(); bool ancient = false; /* add a check here if needed */ - -#ifndef HAVE_OPENGLES bool modern = font->gl->core_context || (cb->context_type == RETRO_HW_CONTEXT_OPENGL && cb->version_major >= 3); - if (modern) + if (ancient) + { + gl_internal = gl_format = GL_RGBA; + ncomponents = 4; + } +#ifdef HAVE_OPENGLES + (void)modern; +#else + else if (modern) { GLint swizzle[] = { GL_ONE, GL_ONE, GL_ONE, GL_RED }; glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle); @@ -75,13 +81,7 @@ static bool gl_raster_font_upload_atlas(gl_raster_t *font, gl_format = GL_RED; ncomponents = 1; } - else #endif - if (ancient) - { - gl_internal = gl_format = GL_RGBA; - ncomponents = 4; - } tmp = (uint8_t*)calloc(height, width * ncomponents);