From 539e77b122e091d0a79c9f1a16d659b4dfd93ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Wed, 25 Mar 2015 09:48:09 -0300 Subject: [PATCH] (gl) Add gl_coord_array_t --- gfx/gl_common.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ gfx/gl_common.h | 22 +++++++++++++-- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/gfx/gl_common.c b/gfx/gl_common.c index 8a0ae3c111..1f63fedd1b 100644 --- a/gfx/gl_common.c +++ b/gfx/gl_common.c @@ -125,3 +125,77 @@ bool gl_load_luts(const struct video_shader *shader, glBindTexture(GL_TEXTURE_2D, 0); return true; } + +static INLINE bool realloc_checked(void **ptr, size_t size) +{ + void *nptr; + + if (*ptr) + nptr = realloc(*ptr, size); + else + nptr = malloc(size); + + if (nptr) + *ptr = nptr; + + return *ptr == nptr; +} + +bool gl_coord_array_add(gl_coord_array_t *ca, const gl_coords_t *coords, unsigned count) +{ + bool success = false; + count = min(count, coords->vertices); + + if (ca->coords.vertices + count >= ca->allocated) + { + unsigned alloc_size = next_pow2(ca->coords.vertices + count); + size_t base_size = sizeof(GLfloat) * alloc_size; + + bool vert_ok = realloc_checked((void**)&ca->coords.vertex, 2 * base_size); + bool color_ok = realloc_checked((void**)&ca->coords.color, 4 * base_size); + bool tex_ok = realloc_checked((void**)&ca->coords.tex_coord, 2 * base_size); + bool lut_ok = realloc_checked((void**)&ca->coords.lut_tex_coord, 2 * base_size); + + if (vert_ok && color_ok && tex_ok && lut_ok) + { + ca->allocated = alloc_size; + success = true; + } + } + else + success = true; + + if (success) + { + size_t base_size = coords->vertices * sizeof(GLfloat); + size_t offset = ca->coords.vertices; + + /* XXX: i wish we used interlaced arrays so we could call memcpy only once */ + memcpy(ca->coords.vertex + offset * 2, coords->vertex, base_size * 2); + memcpy(ca->coords.color + offset * 4, coords->color, base_size * 4); + memcpy(ca->coords.tex_coord + offset * 2, coords->tex_coord, base_size * 2); + memcpy(ca->coords.lut_tex_coord + offset * 2, coords->lut_tex_coord, base_size * 2); + + ca->coords.vertices += count; + } + else + RARCH_WARN("Allocation failed."); + + return success; +} + +void gl_coord_array_release(gl_coord_array_t *ca) +{ + free(ca->coords.vertex); + free(ca->coords.color); + free(ca->coords.tex_coord); + free(ca->coords.lut_tex_coord); + + ca->coords.vertex = NULL; + ca->coords.color = NULL; + ca->coords.tex_coord = NULL; + ca->coords.lut_tex_coord = NULL; + + ca->coords.vertices = 0; + ca->allocated = 0; +} diff --git a/gfx/gl_common.h b/gfx/gl_common.h index 30ed555f62..0589670899 100644 --- a/gfx/gl_common.h +++ b/gfx/gl_common.h @@ -231,14 +231,29 @@ struct gl_tex_info GLfloat coord[8]; }; -struct gl_coords +typedef struct gl_coords { const GLfloat *vertex; const GLfloat *color; const GLfloat *tex_coord; const GLfloat *lut_tex_coord; unsigned vertices; -}; +} gl_coords_t; + +typedef struct gl_mut_coords +{ + GLfloat *vertex; + GLfloat *color; + GLfloat *tex_coord; + GLfloat *lut_tex_coord; + unsigned vertices; +} gl_mut_coords_t; + +typedef struct gl_coord_array +{ + gl_mut_coords_t coords; + unsigned allocated; +} gl_coord_array_t; typedef struct gl { @@ -422,4 +437,7 @@ static INLINE unsigned gl_wrap_type_to_enum(enum gfx_wrap_type type) return 0; } +bool gl_coord_array_add(gl_coord_array_t *ca, const gl_coords_t *coords, unsigned count); +void gl_coord_array_release(gl_coord_array_t *ca); + #endif