From b6b8703ecd693f177864c3bc9e216e4a252ab1be Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Sat, 1 Jun 2024 12:38:23 -0700 Subject: [PATCH] Use pre-allocated array, remove cvector_float --- portablegl.h | 377 ++----------------------------------------- portablegl_unsafe.h | 375 ++---------------------------------------- src/generate_gl_h.py | 5 +- src/gl_impl.c | 14 +- src/gl_impl_unsafe.c | 12 +- src/gl_internal.c | 6 +- src/gl_types.h | 2 +- src/pgl_ext.c | 5 +- 8 files changed, 53 insertions(+), 743 deletions(-) diff --git a/portablegl.h b/portablegl.h index 504b5e2..4c90ace 100644 --- a/portablegl.h +++ b/portablegl.h @@ -1979,58 +1979,6 @@ static inline std::ostream& operator<<(std::ostream& stream, const vec4& a) /* CRSW_MATH_H */ #endif -#ifndef CVEC_SIZE_T -#include -#define CVEC_SIZE_T size_t -#endif - -#ifndef CVEC_SZ -#define CVEC_SZ -typedef CVEC_SIZE_T cvec_sz; -#endif - - -/** Data structure for float vector. */ -typedef struct cvector_float -{ - float* a; /**< Array. */ - cvec_sz size; /**< Current size (amount you use when manipulating array directly). */ - cvec_sz capacity; /**< Allocated size of array; always >= size. */ -} cvector_float; - - - -extern cvec_sz CVEC_float_SZ; - -int cvec_float(cvector_float* vec, cvec_sz size, cvec_sz capacity); -int cvec_init_float(cvector_float* vec, float* vals, cvec_sz num); - -cvector_float* cvec_float_heap(cvec_sz size, cvec_sz capacity); -cvector_float* cvec_init_float_heap(float* vals, cvec_sz num); -int cvec_copyc_float(void* dest, void* src); -int cvec_copy_float(cvector_float* dest, cvector_float* src); - -int cvec_push_float(cvector_float* vec, float a); -float cvec_pop_float(cvector_float* vec); - -int cvec_extend_float(cvector_float* vec, cvec_sz num); -int cvec_insert_float(cvector_float* vec, cvec_sz i, float a); -int cvec_insert_array_float(cvector_float* vec, cvec_sz i, float* a, cvec_sz num); -float cvec_replace_float(cvector_float* vec, cvec_sz i, float a); -void cvec_erase_float(cvector_float* vec, cvec_sz start, cvec_sz end); -int cvec_reserve_float(cvector_float* vec, cvec_sz size); -#define cvec_shrink_to_fit_float(vec) cvec_set_cap_float((vec), (vec)->size) -int cvec_set_cap_float(cvector_float* vec, cvec_sz size); -void cvec_set_val_sz_float(cvector_float* vec, float val); -void cvec_set_val_cap_float(cvector_float* vec, float val); - -float* cvec_back_float(cvector_float* vec); - -void cvec_clear_float(cvector_float* vec); -void cvec_free_float_heap(void* vec); -void cvec_free_float(void* vec); - - #include @@ -2649,7 +2597,7 @@ typedef struct Vertex_Shader_output // but still easily add back functions as needed... // // or like comment in init_glContext says just allocate to the max size and be done - cvector_float output_buf; + float* output_buf; } Vertex_Shader_output; @@ -5525,304 +5473,6 @@ void cvec_free_glVertex(void* vec) } -#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC) -/* ok */ -#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC) -/* ok */ -#else -#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC." -#endif - -#ifndef CVEC_MALLOC -#include -#define CVEC_MALLOC(sz) malloc(sz) -#define CVEC_REALLOC(p, sz) realloc(p, sz) -#define CVEC_FREE(p) free(p) -#endif - -#ifndef CVEC_MEMMOVE -#include -#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz) -#endif - -#ifndef CVEC_ASSERT -#include -#define CVEC_ASSERT(x) assert(x) -#endif - -cvec_sz CVEC_float_SZ = 50; - -#define CVEC_float_ALLOCATOR(x) ((x+1) * 2) - -cvector_float* cvec_float_heap(cvec_sz size, cvec_sz capacity) -{ - cvector_float* vec; - if (!(vec = (cvector_float*)CVEC_MALLOC(sizeof(cvector_float)))) { - CVEC_ASSERT(vec != NULL); - return NULL; - } - - vec->size = size; - vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_float_SZ; - - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - CVEC_FREE(vec); - return NULL; - } - - return vec; -} - -cvector_float* cvec_init_float_heap(float* vals, cvec_sz num) -{ - cvector_float* vec; - - if (!(vec = (cvector_float*)CVEC_MALLOC(sizeof(cvector_float)))) { - CVEC_ASSERT(vec != NULL); - return NULL; - } - - vec->capacity = num + CVEC_float_SZ; - vec->size = num; - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - CVEC_FREE(vec); - return NULL; - } - - CVEC_MEMMOVE(vec->a, vals, sizeof(float)*num); - - return vec; -} - -int cvec_float(cvector_float* vec, cvec_sz size, cvec_sz capacity) -{ - vec->size = size; - vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_float_SZ; - - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - vec->size = vec->capacity = 0; - return 0; - } - - return 1; -} - -int cvec_init_float(cvector_float* vec, float* vals, cvec_sz num) -{ - vec->capacity = num + CVEC_float_SZ; - vec->size = num; - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - vec->size = vec->capacity = 0; - return 0; - } - - CVEC_MEMMOVE(vec->a, vals, sizeof(float)*num); - - return 1; -} - -int cvec_copyc_float(void* dest, void* src) -{ - cvector_float* vec1 = (cvector_float*)dest; - cvector_float* vec2 = (cvector_float*)src; - - vec1->a = NULL; - vec1->size = 0; - vec1->capacity = 0; - - return cvec_copy_float(vec1, vec2); -} - -int cvec_copy_float(cvector_float* dest, cvector_float* src) -{ - float* tmp = NULL; - if (!(tmp = (float*)CVEC_REALLOC(dest->a, src->capacity*sizeof(float)))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - dest->a = tmp; - - CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(float)); - dest->size = src->size; - dest->capacity = src->capacity; - return 1; -} - - -int cvec_push_float(cvector_float* vec, float a) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity > vec->size) { - vec->a[vec->size++] = a; - } else { - tmp_sz = CVEC_float_ALLOCATOR(vec->capacity); - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->a[vec->size++] = a; - vec->capacity = tmp_sz; - } - return 1; -} - -float cvec_pop_float(cvector_float* vec) -{ - return vec->a[--vec->size]; -} - -float* cvec_back_float(cvector_float* vec) -{ - return &vec->a[vec->size-1]; -} - -int cvec_extend_float(cvector_float* vec, cvec_sz num) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity < vec->size + num) { - tmp_sz = vec->capacity + num + CVEC_float_SZ; - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = tmp_sz; - } - - vec->size += num; - return 1; -} - -int cvec_insert_float(cvector_float* vec, cvec_sz i, float a) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity > vec->size) { - CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(float)); - vec->a[i] = a; - } else { - tmp_sz = CVEC_float_ALLOCATOR(vec->capacity); - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(float)); - vec->a[i] = a; - vec->capacity = tmp_sz; - } - - vec->size++; - return 1; -} - -int cvec_insert_array_float(cvector_float* vec, cvec_sz i, float* a, cvec_sz num) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity < vec->size + num) { - tmp_sz = vec->capacity + num + CVEC_float_SZ; - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = tmp_sz; - } - - CVEC_MEMMOVE(&vec->a[i+num], &vec->a[i], (vec->size-i)*sizeof(float)); - CVEC_MEMMOVE(&vec->a[i], a, num*sizeof(float)); - vec->size += num; - return 1; -} - -float cvec_replace_float(cvector_float* vec, cvec_sz i, float a) -{ - float tmp = vec->a[i]; - vec->a[i] = a; - return tmp; -} - -void cvec_erase_float(cvector_float* vec, cvec_sz start, cvec_sz end) -{ - cvec_sz d = end - start + 1; - CVEC_MEMMOVE(&vec->a[start], &vec->a[end+1], (vec->size-1-end)*sizeof(float)); - vec->size -= d; -} - - -int cvec_reserve_float(cvector_float* vec, cvec_sz size) -{ - float* tmp; - if (vec->capacity < size) { - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*(size+CVEC_float_SZ)))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = size + CVEC_float_SZ; - } - return 1; -} - -int cvec_set_cap_float(cvector_float* vec, cvec_sz size) -{ - float* tmp; - if (size < vec->size) { - vec->size = size; - } - - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*size))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = size; - return 1; -} - -void cvec_set_val_sz_float(cvector_float* vec, float val) -{ - cvec_sz i; - for (i=0; isize; i++) { - vec->a[i] = val; - } -} - -void cvec_set_val_cap_float(cvector_float* vec, float val) -{ - cvec_sz i; - for (i=0; icapacity; i++) { - vec->a[i] = val; - } -} - -void cvec_clear_float(cvector_float* vec) { vec->size = 0; } - -void cvec_free_float_heap(void* vec) -{ - cvector_float* tmp = (cvector_float*)vec; - if (!tmp) return; - CVEC_FREE(tmp->a); - CVEC_FREE(tmp); -} - -void cvec_free_float(void* vec) -{ - cvector_float* tmp = (cvector_float*)vec; - CVEC_FREE(tmp->a); - tmp->size = 0; - tmp->capacity = 0; -} - - static glContext* c; static Color blend_pixel(vec4 src, vec4 dst); @@ -5972,7 +5622,7 @@ static void do_vertex(glVertex_Attrib* v, int* enabled, unsigned int num_enabled c->vertex_attribs_vs[enabled[j]] = get_v_attrib(&v[enabled[j]], i); } - float* vs_out = &c->vs_output.output_buf.a[vert*c->vs_output.size]; + float* vs_out = &c->vs_output.output_buf[vert*c->vs_output.size]; c->programs.a[c->cur_program].vertex_shader(vs_out, c->vertex_attribs_vs, &c->builtins, c->programs.a[c->cur_program].uniform); c->glverts.a[vert].vs_out = vs_out; @@ -6217,7 +5867,7 @@ static int depthtest(float zval, float zbufval) static void setup_fs_input(float t, float* v1_out, float* v2_out, float wa, float wb, unsigned int provoke) { - float* vs_output = &c->vs_output.output_buf.a[0]; + float* vs_output = &c->vs_output.output_buf[0]; float inv_wa = 1.0/wa; float inv_wb = 1.0/wb; @@ -7570,7 +7220,7 @@ static void draw_triangle_fill(glVertex* v0, glVertex* v1, glVertex* v2, unsigne float alpha, beta, gamma, tmp, tmp2, z; float fs_input[GL_MAX_VERTEX_OUTPUT_COMPONENTS]; float perspective[GL_MAX_VERTEX_OUTPUT_COMPONENTS*3]; - float* vs_output = &c->vs_output.output_buf.a[0]; + float* vs_output = &c->vs_output.output_buf[0]; for (int i=0; ivs_output.size; ++i) { perspective[i] = v0->vs_out[i]/p0.w; @@ -8262,9 +7912,8 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u cvec_glTexture(&c->textures, 0, 1); cvec_glVertex(&c->glverts, 0, 10); - //TODO might as well just set it to PGL_MAX_VERTICES * MAX_OUTPUT_COMPONENTS - cvec_float(&c->vs_output.output_buf, 0, 0); - + // If not pre-allocating max, need to track size and edit glUseProgram and pglSetInterp + c->vs_output.output_buf = (float*)PGL_MALLOC(PGL_MAX_VERTICES * GL_MAX_VERTEX_OUTPUT_COMPONENTS * sizeof(float)); c->clear_stencil = 0; c->clear_color = make_Color(0, 0, 0, 0); @@ -8276,7 +7925,6 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u c->depth_range_far = 1.0f; make_viewport_matrix(c->vp_mat, 0, 0, w, h, 1); - //set flags //TODO match order in structure definition c->provoking_vert = GL_LAST_VERTEX_CONVENTION; @@ -8400,7 +8048,7 @@ void free_glContext(glContext* ctx) cvec_free_glTexture(&ctx->textures); cvec_free_glVertex(&ctx->glverts); - cvec_free_float(&ctx->vs_output.output_buf); + PGL_FREE(ctx->vs_output.output_buf); if (c == ctx) { c = NULL; @@ -10265,7 +9913,7 @@ void glDeleteProgram(GLuint program) void glUseProgram(GLuint program) { - // Not a problem is program is marked "deleted" already + // Not a problem if program is marked "deleted" already if (program >= c->programs.size) { if (!c->error) c->error = GL_INVALID_VALUE; @@ -10273,7 +9921,9 @@ void glUseProgram(GLuint program) } c->vs_output.size = c->programs.a[program].vs_output_size; - cvec_reserve_float(&c->vs_output.output_buf, c->vs_output.size * PGL_MAX_VERTICES); + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here c->vs_output.interpolation = c->programs.a[program].interpolation; c->fragdepth_or_discard = c->programs.a[program].fragdepth_or_discard; @@ -11290,7 +10940,10 @@ void pglSetInterp(GLsizei n, GLenum* interpolation) c->vs_output.size = n; memcpy(c->programs.a[c->cur_program].interpolation, interpolation, n*sizeof(GLenum)); - cvec_reserve_float(&c->vs_output.output_buf, n * PGL_MAX_VERTICES); + + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here //vs_output.interpolation would be already pointing at current program's array //unless the programs array was realloced since the last glUseProgram because diff --git a/portablegl_unsafe.h b/portablegl_unsafe.h index d678dd9..8398d92 100644 --- a/portablegl_unsafe.h +++ b/portablegl_unsafe.h @@ -1979,58 +1979,6 @@ static inline std::ostream& operator<<(std::ostream& stream, const vec4& a) /* CRSW_MATH_H */ #endif -#ifndef CVEC_SIZE_T -#include -#define CVEC_SIZE_T size_t -#endif - -#ifndef CVEC_SZ -#define CVEC_SZ -typedef CVEC_SIZE_T cvec_sz; -#endif - - -/** Data structure for float vector. */ -typedef struct cvector_float -{ - float* a; /**< Array. */ - cvec_sz size; /**< Current size (amount you use when manipulating array directly). */ - cvec_sz capacity; /**< Allocated size of array; always >= size. */ -} cvector_float; - - - -extern cvec_sz CVEC_float_SZ; - -int cvec_float(cvector_float* vec, cvec_sz size, cvec_sz capacity); -int cvec_init_float(cvector_float* vec, float* vals, cvec_sz num); - -cvector_float* cvec_float_heap(cvec_sz size, cvec_sz capacity); -cvector_float* cvec_init_float_heap(float* vals, cvec_sz num); -int cvec_copyc_float(void* dest, void* src); -int cvec_copy_float(cvector_float* dest, cvector_float* src); - -int cvec_push_float(cvector_float* vec, float a); -float cvec_pop_float(cvector_float* vec); - -int cvec_extend_float(cvector_float* vec, cvec_sz num); -int cvec_insert_float(cvector_float* vec, cvec_sz i, float a); -int cvec_insert_array_float(cvector_float* vec, cvec_sz i, float* a, cvec_sz num); -float cvec_replace_float(cvector_float* vec, cvec_sz i, float a); -void cvec_erase_float(cvector_float* vec, cvec_sz start, cvec_sz end); -int cvec_reserve_float(cvector_float* vec, cvec_sz size); -#define cvec_shrink_to_fit_float(vec) cvec_set_cap_float((vec), (vec)->size) -int cvec_set_cap_float(cvector_float* vec, cvec_sz size); -void cvec_set_val_sz_float(cvector_float* vec, float val); -void cvec_set_val_cap_float(cvector_float* vec, float val); - -float* cvec_back_float(cvector_float* vec); - -void cvec_clear_float(cvector_float* vec); -void cvec_free_float_heap(void* vec); -void cvec_free_float(void* vec); - - #include @@ -2649,7 +2597,7 @@ typedef struct Vertex_Shader_output // but still easily add back functions as needed... // // or like comment in init_glContext says just allocate to the max size and be done - cvector_float output_buf; + float* output_buf; } Vertex_Shader_output; @@ -5525,304 +5473,6 @@ void cvec_free_glVertex(void* vec) } -#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC) -/* ok */ -#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC) -/* ok */ -#else -#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC." -#endif - -#ifndef CVEC_MALLOC -#include -#define CVEC_MALLOC(sz) malloc(sz) -#define CVEC_REALLOC(p, sz) realloc(p, sz) -#define CVEC_FREE(p) free(p) -#endif - -#ifndef CVEC_MEMMOVE -#include -#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz) -#endif - -#ifndef CVEC_ASSERT -#include -#define CVEC_ASSERT(x) assert(x) -#endif - -cvec_sz CVEC_float_SZ = 50; - -#define CVEC_float_ALLOCATOR(x) ((x+1) * 2) - -cvector_float* cvec_float_heap(cvec_sz size, cvec_sz capacity) -{ - cvector_float* vec; - if (!(vec = (cvector_float*)CVEC_MALLOC(sizeof(cvector_float)))) { - CVEC_ASSERT(vec != NULL); - return NULL; - } - - vec->size = size; - vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_float_SZ; - - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - CVEC_FREE(vec); - return NULL; - } - - return vec; -} - -cvector_float* cvec_init_float_heap(float* vals, cvec_sz num) -{ - cvector_float* vec; - - if (!(vec = (cvector_float*)CVEC_MALLOC(sizeof(cvector_float)))) { - CVEC_ASSERT(vec != NULL); - return NULL; - } - - vec->capacity = num + CVEC_float_SZ; - vec->size = num; - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - CVEC_FREE(vec); - return NULL; - } - - CVEC_MEMMOVE(vec->a, vals, sizeof(float)*num); - - return vec; -} - -int cvec_float(cvector_float* vec, cvec_sz size, cvec_sz capacity) -{ - vec->size = size; - vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_float_SZ; - - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - vec->size = vec->capacity = 0; - return 0; - } - - return 1; -} - -int cvec_init_float(cvector_float* vec, float* vals, cvec_sz num) -{ - vec->capacity = num + CVEC_float_SZ; - vec->size = num; - if (!(vec->a = (float*)CVEC_MALLOC(vec->capacity*sizeof(float)))) { - CVEC_ASSERT(vec->a != NULL); - vec->size = vec->capacity = 0; - return 0; - } - - CVEC_MEMMOVE(vec->a, vals, sizeof(float)*num); - - return 1; -} - -int cvec_copyc_float(void* dest, void* src) -{ - cvector_float* vec1 = (cvector_float*)dest; - cvector_float* vec2 = (cvector_float*)src; - - vec1->a = NULL; - vec1->size = 0; - vec1->capacity = 0; - - return cvec_copy_float(vec1, vec2); -} - -int cvec_copy_float(cvector_float* dest, cvector_float* src) -{ - float* tmp = NULL; - if (!(tmp = (float*)CVEC_REALLOC(dest->a, src->capacity*sizeof(float)))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - dest->a = tmp; - - CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(float)); - dest->size = src->size; - dest->capacity = src->capacity; - return 1; -} - - -int cvec_push_float(cvector_float* vec, float a) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity > vec->size) { - vec->a[vec->size++] = a; - } else { - tmp_sz = CVEC_float_ALLOCATOR(vec->capacity); - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->a[vec->size++] = a; - vec->capacity = tmp_sz; - } - return 1; -} - -float cvec_pop_float(cvector_float* vec) -{ - return vec->a[--vec->size]; -} - -float* cvec_back_float(cvector_float* vec) -{ - return &vec->a[vec->size-1]; -} - -int cvec_extend_float(cvector_float* vec, cvec_sz num) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity < vec->size + num) { - tmp_sz = vec->capacity + num + CVEC_float_SZ; - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = tmp_sz; - } - - vec->size += num; - return 1; -} - -int cvec_insert_float(cvector_float* vec, cvec_sz i, float a) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity > vec->size) { - CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(float)); - vec->a[i] = a; - } else { - tmp_sz = CVEC_float_ALLOCATOR(vec->capacity); - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(float)); - vec->a[i] = a; - vec->capacity = tmp_sz; - } - - vec->size++; - return 1; -} - -int cvec_insert_array_float(cvector_float* vec, cvec_sz i, float* a, cvec_sz num) -{ - float* tmp; - cvec_sz tmp_sz; - if (vec->capacity < vec->size + num) { - tmp_sz = vec->capacity + num + CVEC_float_SZ; - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*tmp_sz))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = tmp_sz; - } - - CVEC_MEMMOVE(&vec->a[i+num], &vec->a[i], (vec->size-i)*sizeof(float)); - CVEC_MEMMOVE(&vec->a[i], a, num*sizeof(float)); - vec->size += num; - return 1; -} - -float cvec_replace_float(cvector_float* vec, cvec_sz i, float a) -{ - float tmp = vec->a[i]; - vec->a[i] = a; - return tmp; -} - -void cvec_erase_float(cvector_float* vec, cvec_sz start, cvec_sz end) -{ - cvec_sz d = end - start + 1; - CVEC_MEMMOVE(&vec->a[start], &vec->a[end+1], (vec->size-1-end)*sizeof(float)); - vec->size -= d; -} - - -int cvec_reserve_float(cvector_float* vec, cvec_sz size) -{ - float* tmp; - if (vec->capacity < size) { - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*(size+CVEC_float_SZ)))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = size + CVEC_float_SZ; - } - return 1; -} - -int cvec_set_cap_float(cvector_float* vec, cvec_sz size) -{ - float* tmp; - if (size < vec->size) { - vec->size = size; - } - - if (!(tmp = (float*)CVEC_REALLOC(vec->a, sizeof(float)*size))) { - CVEC_ASSERT(tmp != NULL); - return 0; - } - vec->a = tmp; - vec->capacity = size; - return 1; -} - -void cvec_set_val_sz_float(cvector_float* vec, float val) -{ - cvec_sz i; - for (i=0; isize; i++) { - vec->a[i] = val; - } -} - -void cvec_set_val_cap_float(cvector_float* vec, float val) -{ - cvec_sz i; - for (i=0; icapacity; i++) { - vec->a[i] = val; - } -} - -void cvec_clear_float(cvector_float* vec) { vec->size = 0; } - -void cvec_free_float_heap(void* vec) -{ - cvector_float* tmp = (cvector_float*)vec; - if (!tmp) return; - CVEC_FREE(tmp->a); - CVEC_FREE(tmp); -} - -void cvec_free_float(void* vec) -{ - cvector_float* tmp = (cvector_float*)vec; - CVEC_FREE(tmp->a); - tmp->size = 0; - tmp->capacity = 0; -} - - static glContext* c; static Color blend_pixel(vec4 src, vec4 dst); @@ -5972,7 +5622,7 @@ static void do_vertex(glVertex_Attrib* v, int* enabled, unsigned int num_enabled c->vertex_attribs_vs[enabled[j]] = get_v_attrib(&v[enabled[j]], i); } - float* vs_out = &c->vs_output.output_buf.a[vert*c->vs_output.size]; + float* vs_out = &c->vs_output.output_buf[vert*c->vs_output.size]; c->programs.a[c->cur_program].vertex_shader(vs_out, c->vertex_attribs_vs, &c->builtins, c->programs.a[c->cur_program].uniform); c->glverts.a[vert].vs_out = vs_out; @@ -6217,7 +5867,7 @@ static int depthtest(float zval, float zbufval) static void setup_fs_input(float t, float* v1_out, float* v2_out, float wa, float wb, unsigned int provoke) { - float* vs_output = &c->vs_output.output_buf.a[0]; + float* vs_output = &c->vs_output.output_buf[0]; float inv_wa = 1.0/wa; float inv_wb = 1.0/wb; @@ -7570,7 +7220,7 @@ static void draw_triangle_fill(glVertex* v0, glVertex* v1, glVertex* v2, unsigne float alpha, beta, gamma, tmp, tmp2, z; float fs_input[GL_MAX_VERTEX_OUTPUT_COMPONENTS]; float perspective[GL_MAX_VERTEX_OUTPUT_COMPONENTS*3]; - float* vs_output = &c->vs_output.output_buf.a[0]; + float* vs_output = &c->vs_output.output_buf[0]; for (int i=0; ivs_output.size; ++i) { perspective[i] = v0->vs_out[i]/p0.w; @@ -8262,9 +7912,8 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u cvec_glTexture(&c->textures, 0, 1); cvec_glVertex(&c->glverts, 0, 10); - //TODO might as well just set it to PGL_MAX_VERTICES * MAX_OUTPUT_COMPONENTS - cvec_float(&c->vs_output.output_buf, 0, 0); - + // If not pre-allocating max, need to track size and edit glUseProgram and pglSetInterp + c->vs_output.output_buf = (float*)PGL_MALLOC(PGL_MAX_VERTICES * GL_MAX_VERTEX_OUTPUT_COMPONENTS * sizeof(float)); c->clear_stencil = 0; c->clear_color = make_Color(0, 0, 0, 0); @@ -8276,7 +7925,6 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u c->depth_range_far = 1.0f; make_viewport_matrix(c->vp_mat, 0, 0, w, h, 1); - //set flags //TODO match order in structure definition c->provoking_vert = GL_LAST_VERTEX_CONVENTION; @@ -8400,7 +8048,7 @@ void free_glContext(glContext* ctx) cvec_free_glTexture(&ctx->textures); cvec_free_glVertex(&ctx->glverts); - cvec_free_float(&ctx->vs_output.output_buf); + PGL_FREE(ctx->vs_output.output_buf); if (c == ctx) { c = NULL; @@ -9662,7 +9310,9 @@ void glDeleteProgram(GLuint program) void glUseProgram(GLuint program) { c->vs_output.size = c->programs.a[program].vs_output_size; - cvec_reserve_float(&c->vs_output.output_buf, c->vs_output.size * PGL_MAX_VERTICES); + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here c->vs_output.interpolation = c->programs.a[program].interpolation; c->fragdepth_or_discard = c->programs.a[program].fragdepth_or_discard; @@ -10547,7 +10197,10 @@ void pglSetInterp(GLsizei n, GLenum* interpolation) c->vs_output.size = n; memcpy(c->programs.a[c->cur_program].interpolation, interpolation, n*sizeof(GLenum)); - cvec_reserve_float(&c->vs_output.output_buf, n * PGL_MAX_VERTICES); + + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here //vs_output.interpolation would be already pointing at current program's array //unless the programs array was realloced since the last glUseProgram because diff --git a/src/generate_gl_h.py b/src/generate_gl_h.py index 5afcc5d..dacb33c 100755 --- a/src/generate_gl_h.py +++ b/src/generate_gl_h.py @@ -219,7 +219,7 @@ def get_gl_funcs(): gl_h.write(open("crsw_math.h").read()) # we actually use this for output_buf in gl_types..for now - gl_h.write(open("cvector_float.h").read()) + #gl_h.write(open("cvector_float.h").read()) gl_h.write(open("gl_types.h").read()) @@ -247,7 +247,8 @@ def get_gl_funcs(): # maybe I should stick to using cvector_macro.h and use the macros # maybe an option to add vectors for commonly used types gl_h.write(open("cvector_combined.c").read()) - gl_h.write(open("cvector_float.c").read()) + + #gl_h.write(open("cvector_float.c").read()) # maybe this should go last? does it matter beyond aesthetics? gl_h.write(open("gl_internal.c").read()) diff --git a/src/gl_impl.c b/src/gl_impl.c index bcdd6a8..8421d57 100644 --- a/src/gl_impl.c +++ b/src/gl_impl.c @@ -207,9 +207,8 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u cvec_glTexture(&c->textures, 0, 1); cvec_glVertex(&c->glverts, 0, 10); - //TODO might as well just set it to PGL_MAX_VERTICES * MAX_OUTPUT_COMPONENTS - cvec_float(&c->vs_output.output_buf, 0, 0); - + // If not pre-allocating max, need to track size and edit glUseProgram and pglSetInterp + c->vs_output.output_buf = (float*)PGL_MALLOC(PGL_MAX_VERTICES * GL_MAX_VERTEX_OUTPUT_COMPONENTS * sizeof(float)); c->clear_stencil = 0; c->clear_color = make_Color(0, 0, 0, 0); @@ -221,7 +220,6 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u c->depth_range_far = 1.0f; make_viewport_matrix(c->vp_mat, 0, 0, w, h, 1); - //set flags //TODO match order in structure definition c->provoking_vert = GL_LAST_VERTEX_CONVENTION; @@ -345,7 +343,7 @@ void free_glContext(glContext* ctx) cvec_free_glTexture(&ctx->textures); cvec_free_glVertex(&ctx->glverts); - cvec_free_float(&ctx->vs_output.output_buf); + PGL_FREE(ctx->vs_output.output_buf); if (c == ctx) { c = NULL; @@ -2210,7 +2208,7 @@ void glDeleteProgram(GLuint program) void glUseProgram(GLuint program) { - // Not a problem is program is marked "deleted" already + // Not a problem if program is marked "deleted" already if (program >= c->programs.size) { if (!c->error) c->error = GL_INVALID_VALUE; @@ -2218,7 +2216,9 @@ void glUseProgram(GLuint program) } c->vs_output.size = c->programs.a[program].vs_output_size; - cvec_reserve_float(&c->vs_output.output_buf, c->vs_output.size * PGL_MAX_VERTICES); + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here c->vs_output.interpolation = c->programs.a[program].interpolation; c->fragdepth_or_discard = c->programs.a[program].fragdepth_or_discard; diff --git a/src/gl_impl_unsafe.c b/src/gl_impl_unsafe.c index b0b8f4f..2f397d9 100644 --- a/src/gl_impl_unsafe.c +++ b/src/gl_impl_unsafe.c @@ -207,9 +207,8 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u cvec_glTexture(&c->textures, 0, 1); cvec_glVertex(&c->glverts, 0, 10); - //TODO might as well just set it to PGL_MAX_VERTICES * MAX_OUTPUT_COMPONENTS - cvec_float(&c->vs_output.output_buf, 0, 0); - + // If not pre-allocating max, need to track size and edit glUseProgram and pglSetInterp + c->vs_output.output_buf = (float*)PGL_MALLOC(PGL_MAX_VERTICES * GL_MAX_VERTEX_OUTPUT_COMPONENTS * sizeof(float)); c->clear_stencil = 0; c->clear_color = make_Color(0, 0, 0, 0); @@ -221,7 +220,6 @@ int init_glContext(glContext* context, u32** back, int w, int h, int bitdepth, u c->depth_range_far = 1.0f; make_viewport_matrix(c->vp_mat, 0, 0, w, h, 1); - //set flags //TODO match order in structure definition c->provoking_vert = GL_LAST_VERTEX_CONVENTION; @@ -345,7 +343,7 @@ void free_glContext(glContext* ctx) cvec_free_glTexture(&ctx->textures); cvec_free_glVertex(&ctx->glverts); - cvec_free_float(&ctx->vs_output.output_buf); + PGL_FREE(ctx->vs_output.output_buf); if (c == ctx) { c = NULL; @@ -1607,7 +1605,9 @@ void glDeleteProgram(GLuint program) void glUseProgram(GLuint program) { c->vs_output.size = c->programs.a[program].vs_output_size; - cvec_reserve_float(&c->vs_output.output_buf, c->vs_output.size * PGL_MAX_VERTICES); + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here c->vs_output.interpolation = c->programs.a[program].interpolation; c->fragdepth_or_discard = c->programs.a[program].fragdepth_or_discard; diff --git a/src/gl_internal.c b/src/gl_internal.c index c7819d8..f0af8db 100644 --- a/src/gl_internal.c +++ b/src/gl_internal.c @@ -148,7 +148,7 @@ static void do_vertex(glVertex_Attrib* v, int* enabled, unsigned int num_enabled c->vertex_attribs_vs[enabled[j]] = get_v_attrib(&v[enabled[j]], i); } - float* vs_out = &c->vs_output.output_buf.a[vert*c->vs_output.size]; + float* vs_out = &c->vs_output.output_buf[vert*c->vs_output.size]; c->programs.a[c->cur_program].vertex_shader(vs_out, c->vertex_attribs_vs, &c->builtins, c->programs.a[c->cur_program].uniform); c->glverts.a[vert].vs_out = vs_out; @@ -393,7 +393,7 @@ static int depthtest(float zval, float zbufval) static void setup_fs_input(float t, float* v1_out, float* v2_out, float wa, float wb, unsigned int provoke) { - float* vs_output = &c->vs_output.output_buf.a[0]; + float* vs_output = &c->vs_output.output_buf[0]; float inv_wa = 1.0/wa; float inv_wb = 1.0/wb; @@ -1746,7 +1746,7 @@ static void draw_triangle_fill(glVertex* v0, glVertex* v1, glVertex* v2, unsigne float alpha, beta, gamma, tmp, tmp2, z; float fs_input[GL_MAX_VERTEX_OUTPUT_COMPONENTS]; float perspective[GL_MAX_VERTEX_OUTPUT_COMPONENTS*3]; - float* vs_output = &c->vs_output.output_buf.a[0]; + float* vs_output = &c->vs_output.output_buf[0]; for (int i=0; ivs_output.size; ++i) { perspective[i] = v0->vs_out[i]/p0.w; diff --git a/src/gl_types.h b/src/gl_types.h index 0e24804..7fd8d50 100644 --- a/src/gl_types.h +++ b/src/gl_types.h @@ -617,7 +617,7 @@ typedef struct Vertex_Shader_output // but still easily add back functions as needed... // // or like comment in init_glContext says just allocate to the max size and be done - cvector_float output_buf; + float* output_buf; } Vertex_Shader_output; diff --git a/src/pgl_ext.c b/src/pgl_ext.c index 600768a..f992fed 100644 --- a/src/pgl_ext.c +++ b/src/pgl_ext.c @@ -23,7 +23,10 @@ void pglSetInterp(GLsizei n, GLenum* interpolation) c->vs_output.size = n; memcpy(c->programs.a[c->cur_program].interpolation, interpolation, n*sizeof(GLenum)); - cvec_reserve_float(&c->vs_output.output_buf, n * PGL_MAX_VERTICES); + + // c->vs_output.output_buf was pre-allocated to max size needed in init_glContext + // otherwise would need to assure it's at least + // c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here //vs_output.interpolation would be already pointing at current program's array //unless the programs array was realloced since the last glUseProgram because