(gl) Add gl_coord_array_t

This commit is contained in:
Higor Eurípedes 2015-03-25 09:48:09 -03:00
parent 7d1bffc350
commit 539e77b122
2 changed files with 94 additions and 2 deletions

View File

@ -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;
}

View File

@ -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