From aa5ff1edf1786eee36f03dc66c1100a808d04bf4 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 1 Oct 2024 14:58:00 +0300 Subject: [PATCH] twn_gl_15_rendering.c: use display lists to cache common series of calls --- src/rendering/twn_gl_15_rendering.c | 253 ++++++++++++++++------------ 1 file changed, 149 insertions(+), 104 deletions(-) diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index ac6f45d..e810591 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -59,11 +59,34 @@ static Pipeline pipeline_last_used = PIPELINE_NO; void use_space_pipeline(void) { - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); - glShadeModel(GL_SMOOTH); + static GLuint list = 0; + if (!list) { + list = glGenLists(1); - if (GLAD_GL_ARB_depth_clamp) - glDisable(GL_DEPTH_CLAMP); + glNewList(list, GL_COMPILE); { + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + glShadeModel(GL_SMOOTH); + + if (GLAD_GL_ARB_depth_clamp) + glDisable(GL_DEPTH_CLAMP); + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDepthFunc(GL_LESS); + glDepthRange(0, 1); + glDisable(GL_BLEND); + glEnable(GL_ALPHA_TEST); /* TODO: infer its usage? */ + glAlphaFunc(GL_EQUAL, 1.0f); + glEnable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + + /* solid white, no modulation */ + glColor4ub(255, 255, 255, 255); + } glEndList(); + } + + glCallList(list); glMatrixMode(GL_PROJECTION); glLoadMatrixf(&camera_projection_matrix.row[0].x); @@ -71,20 +94,6 @@ void use_space_pipeline(void) { glMatrixMode(GL_MODELVIEW); glLoadMatrixf(&camera_look_at_matrix.row[0].x); - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); - glDepthFunc(GL_LESS); - glDepthRange(0, 1); - glDisable(GL_BLEND); - glEnable(GL_ALPHA_TEST); /* TODO: infer its usage? */ - glAlphaFunc(GL_EQUAL, 1.0f); - glEnable(GL_TEXTURE_2D); - glActiveTexture(GL_TEXTURE0); - - /* solid white, no modulation */ - glColor4ub(255, 255, 255, 255); - pipeline_last_used = PIPELINE_SPACE; } @@ -95,12 +104,26 @@ void use_2d_pipeline(void) { glFlush(); } - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); - glShadeModel(GL_FLAT); + static GLuint list = 0; + if (!list) { + list = glGenLists(1); - /* removes near/far plane comparison and discard */ - if (GLAD_GL_ARB_depth_clamp) - glDisable(GL_DEPTH_CLAMP); + glNewList(list, GL_COMPILE); { + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + glShadeModel(GL_FLAT); + + /* removes near/far plane comparison and discard */ + if (GLAD_GL_ARB_depth_clamp) + glDisable(GL_DEPTH_CLAMP); + + glEnable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + glDisable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + } glEndList(); + } + + glCallList(list); glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -109,11 +132,6 @@ void use_2d_pipeline(void) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glEnable(GL_TEXTURE_2D); - glActiveTexture(GL_TEXTURE0); - glDisable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - pipeline_last_used = PIPELINE_2D; } @@ -183,23 +201,40 @@ void render_circle(const CirclePrimitive *circle) { void use_texture_mode(TextureMode mode) { + GLuint lists = 0; + if (!lists) { + lists = glGenLists(3); + + glNewList(lists + 0, GL_COMPILE); { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDepthFunc(GL_ALWAYS); + glDepthMask(GL_FALSE); + glDisable(GL_ALPHA_TEST); + } glEndList(); + + glNewList(lists + 1, GL_COMPILE); { + glDisable(GL_BLEND); + glDepthFunc(GL_LEQUAL); + glDepthMask(GL_TRUE); + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_EQUAL, 1.0f); + } glEndList(); + + glNewList(lists + 2, GL_COMPILE); { + glDisable(GL_BLEND); + glDepthFunc(GL_LESS); + glDepthMask(GL_TRUE); + glDisable(GL_ALPHA_TEST); + } glEndList(); + } + if (mode == TEXTURE_MODE_GHOSTLY) { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDepthFunc(GL_ALWAYS); - glDepthMask(GL_FALSE); - glDisable(GL_ALPHA_TEST); + glCallList(lists + 0); } else if (mode == TEXTURE_MODE_SEETHROUGH) { - glDisable(GL_BLEND); - glDepthFunc(GL_LEQUAL); - glDepthMask(GL_TRUE); - glEnable(GL_ALPHA_TEST); - glAlphaFunc(GL_EQUAL, 1.0f); + glCallList(lists + 1); } else { - glDisable(GL_BLEND); - glDepthFunc(GL_LESS); - glDepthMask(GL_TRUE); - glDisable(GL_ALPHA_TEST); + glCallList(lists + 2); } } @@ -517,10 +552,6 @@ void finally_render_skybox(char *paths) { glEnable(GL_TEXTURE_CUBE_MAP); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap); - /* note: assumes that space pipeline is applied already */ - glDisable(GL_ALPHA_TEST); - glDepthMask(GL_TRUE); - if (loading_needed) { /* load all the sides */ char *expanded = expand_asterisk(paths, "up"); @@ -548,70 +579,84 @@ void finally_render_skybox(char *paths) { SDL_free(expanded); } - /* TODO: use lists at the very least */ /* TODO: figure out which coordinates to use to not have issues with far z */ + /* TODO: recalculate the list if far z requirement changes */ - glBegin(GL_QUADS); { - /* up */ - glTexCoord3f(50.f, 50.f, 50.f); - glVertex3f(50.f, 50.f, 50.f); - glTexCoord3f(-50.f, 50.f, 50.f); - glVertex3f(-50.f, 50.f, 50.f); - glTexCoord3f(-50.f, 50.f, -50.f); - glVertex3f(-50.f, 50.f, -50.f); - glTexCoord3f(50.f, 50.f, -50.f); - glVertex3f(50.f, 50.f, -50.f); + static GLuint list = 0; + if (!list) { + list = glGenLists(1); - /* down */ - glTexCoord3f(50.f, -50.f, 50.f); - glVertex3f(50.f, -50.f, 50.f); - glTexCoord3f(50.f, -50.f, -50.f); - glVertex3f(50.f, -50.f, -50.f); - glTexCoord3f(-50.f, -50.f, -50.f); - glVertex3f(-50.f, -50.f, -50.f); - glTexCoord3f(-50.f, -50.f, 50.f); - glVertex3f(-50.f, -50.f, 50.f); + glNewList(list, GL_COMPILE); { + /* note: assumes that space pipeline is applied already */ + glDisable(GL_ALPHA_TEST); + glDepthMask(GL_TRUE); - /* east */ - glTexCoord3f(50.f, -50.f, 50.f); - glVertex3f(50.f, -50.f, 50.f); - glTexCoord3f(50.f, 50.f, 50.f); - glVertex3f(50.f, 50.f, 50.f); - glTexCoord3f(50.f, 50.f, -50.f); - glVertex3f(50.f, 50.f, -50.f); - glTexCoord3f(50.f, -50.f, -50.f); - glVertex3f(50.f, -50.f, -50.f); + glBegin(GL_QUADS); { + /* up */ + glTexCoord3f(50.f, 50.f, 50.f); + glVertex3f(50.f, 50.f, 50.f); + glTexCoord3f(-50.f, 50.f, 50.f); + glVertex3f(-50.f, 50.f, 50.f); + glTexCoord3f(-50.f, 50.f, -50.f); + glVertex3f(-50.f, 50.f, -50.f); + glTexCoord3f(50.f, 50.f, -50.f); + glVertex3f(50.f, 50.f, -50.f); - /* west */ - glTexCoord3f(-50.f, -50.f, 50.f); - glVertex3f(-50.f, -50.f, 50.f); - glTexCoord3f(-50.f, -50.f, -50.f); - glVertex3f(-50.f, -50.f, -50.f); - glTexCoord3f(-50.f, 50.f, -50.f); - glVertex3f(-50.f, 50.f, -50.f); - glTexCoord3f(-50.f, 50.f, 50.f); - glVertex3f(-50.f, 50.f, 50.f); + /* down */ + glTexCoord3f(50.f, -50.f, 50.f); + glVertex3f(50.f, -50.f, 50.f); + glTexCoord3f(50.f, -50.f, -50.f); + glVertex3f(50.f, -50.f, -50.f); + glTexCoord3f(-50.f, -50.f, -50.f); + glVertex3f(-50.f, -50.f, -50.f); + glTexCoord3f(-50.f, -50.f, 50.f); + glVertex3f(-50.f, -50.f, 50.f); - /* north */ - glTexCoord3f(-50.f, -50.f, 50.f); - glVertex3f(-50.f, -50.f, 50.f); - glTexCoord3f(-50.f, 50.f, 50.f); - glVertex3f(-50.f, 50.f, 50.f); - glTexCoord3f(50.f, 50.f, 50.f); - glVertex3f(50.f, 50.f, 50.f); - glTexCoord3f(50.f, -50.f, 50.f); - glVertex3f(50.f, -50.f, 50.f); + /* east */ + glTexCoord3f(50.f, -50.f, 50.f); + glVertex3f(50.f, -50.f, 50.f); + glTexCoord3f(50.f, 50.f, 50.f); + glVertex3f(50.f, 50.f, 50.f); + glTexCoord3f(50.f, 50.f, -50.f); + glVertex3f(50.f, 50.f, -50.f); + glTexCoord3f(50.f, -50.f, -50.f); + glVertex3f(50.f, -50.f, -50.f); - /* south */ - glTexCoord3f(-50.f, -50.f, -50.f); - glVertex3f(-50.f, -50.f, -50.f); - glTexCoord3f(50.f, -50.f, -50.f); - glVertex3f(50.f, -50.f, -50.f); - glTexCoord3f(50.f, 50.f, -50.f); - glVertex3f(50.f, 50.f, -50.f); - glTexCoord3f(-50.f, 50.f, -50.f); - glVertex3f(-50.f, 50.f, -50.f); - } glEnd(); + /* west */ + glTexCoord3f(-50.f, -50.f, 50.f); + glVertex3f(-50.f, -50.f, 50.f); + glTexCoord3f(-50.f, -50.f, -50.f); + glVertex3f(-50.f, -50.f, -50.f); + glTexCoord3f(-50.f, 50.f, -50.f); + glVertex3f(-50.f, 50.f, -50.f); + glTexCoord3f(-50.f, 50.f, 50.f); + glVertex3f(-50.f, 50.f, 50.f); - glDisable(GL_TEXTURE_CUBE_MAP); + /* north */ + glTexCoord3f(-50.f, -50.f, 50.f); + glVertex3f(-50.f, -50.f, 50.f); + glTexCoord3f(-50.f, 50.f, 50.f); + glVertex3f(-50.f, 50.f, 50.f); + glTexCoord3f(50.f, 50.f, 50.f); + glVertex3f(50.f, 50.f, 50.f); + glTexCoord3f(50.f, -50.f, 50.f); + glVertex3f(50.f, -50.f, 50.f); + + /* south */ + glTexCoord3f(-50.f, -50.f, -50.f); + glVertex3f(-50.f, -50.f, -50.f); + glTexCoord3f(50.f, -50.f, -50.f); + glVertex3f(50.f, -50.f, -50.f); + glTexCoord3f(50.f, 50.f, -50.f); + glVertex3f(50.f, 50.f, -50.f); + glTexCoord3f(-50.f, 50.f, -50.f); + glVertex3f(-50.f, 50.f, -50.f); + } glEnd(); + + glDisable(GL_TEXTURE_CUBE_MAP); + + } glEndList(); + } + + glCallList(list); }