Skip to content

Commit

Permalink
Use pre-allocated array, remove cvector_float
Browse files Browse the repository at this point in the history
  • Loading branch information
rswinkle committed Jun 1, 2024
1 parent 886236c commit b6b8703
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 743 deletions.
377 changes: 15 additions & 362 deletions portablegl.h

Large diffs are not rendered by default.

375 changes: 14 additions & 361 deletions portablegl_unsafe.h

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/generate_gl_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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())
Expand Down
14 changes: 7 additions & 7 deletions src/gl_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2210,15 +2208,17 @@ 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;
return;
}

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;

Expand Down
12 changes: 6 additions & 6 deletions src/gl_impl_unsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/gl_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; i<c->vs_output.size; ++i) {
perspective[i] = v0->vs_out[i]/p0.w;
Expand Down
2 changes: 1 addition & 1 deletion src/gl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
5 changes: 4 additions & 1 deletion src/pgl_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b6b8703

Please sign in to comment.