From 446340cbb80a217bbb7ebdfcfed85f252bad5282 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Thu, 29 Dec 2022 00:00:45 +0300 Subject: [PATCH] gl: cache texture id and color --- src/game/renders/opengl/r_draw.pas | 62 +++++++++++++++++--------- src/game/renders/opengl/r_map.pas | 4 +- src/game/renders/opengl/r_textures.pas | 21 +++++++-- 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/game/renders/opengl/r_draw.pas b/src/game/renders/opengl/r_draw.pas index 67bd230..940b672 100644 --- a/src/game/renders/opengl/r_draw.pas +++ b/src/game/renders/opengl/r_draw.pas @@ -41,6 +41,8 @@ interface procedure r_Draw_SetRect (l, t, r, b: Integer); procedure r_Draw_GetRect (out l, t, r, b: Integer); + procedure r_Draw_EnableTexture2D (enable: Boolean); + implementation uses @@ -63,6 +65,30 @@ implementation sl, st, sr, sb: Integer; ScreenWidth, ScreenHeight: Integer; + enableTexture2D: Boolean; + curR, curG, curB, curA: Byte; + + procedure r_Draw_EnableTexture2D (enable: Boolean); + begin + if enable <> enableTexture2D then + begin + if enable then glEnable(GL_TEXTURE_2D) else glDisable(GL_TEXTURE_2D); + enableTexture2D := enable; + end; + end; + + procedure r_Draw_SetColor (r, g, b, a: Byte); + begin + if (r <> curR) or (g <> curG) or (b <> curB) or (curA <> a) then + begin + glColor4ub(r, g, b, a); + curR := r; + curG := g; + curB := b; + curA := a; + end; + end; + procedure r_Draw_Setup (w, h: Integer); begin ASSERT(w >= 0); @@ -95,9 +121,9 @@ implementation begin if tile = nil then begin - glColor4ub(rr, gg, bb, aa); + r_Draw_SetColor(rr, gg, bb, aa); if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_TEXTURE_2D); + r_Draw_EnableTexture2D(false); glEnable(GL_BLEND); DrawQuad(x, y, w, h); end @@ -110,10 +136,10 @@ implementation ay := (tile.t) / nw; by := (tile.b + 1) / nh; l := x; t := y; r := x + w; b := y + h; - glBindTexture(GL_TEXTURE_2D, tile.id); - glColor4ub(rr, gg, bb, aa); + r_Textures_GL_Bind(tile.id); + r_Draw_SetColor(rr, gg, bb, aa); + r_Draw_EnableTexture2D(true); if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBegin(GL_QUADS); glTexCoord2f(ax, ay); glVertex2i(r, t); @@ -121,8 +147,6 @@ implementation glTexCoord2f(bx, by); glVertex2i(l, b); glTexCoord2f(ax, by); glVertex2i(r, b); glEnd(); - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); end end; @@ -134,10 +158,10 @@ implementation ay := 0 / nw; by := h / nh; l := x; t := y; r := x + w; b := y + h; - glBindTexture(GL_TEXTURE_2D, gltex); - glColor4ub(rr, gg, bb, aa); + r_Textures_GL_Bind(gltex); + r_Draw_SetColor(rr, gg, bb, aa); + r_Draw_EnableTexture2D(true); if blend then glBlendFunc(GL_SRC_ALPHA, GL_ONE) else glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBegin(GL_QUADS); glTexCoord2f(ax, ay); glVertex2i(r, t); @@ -145,8 +169,6 @@ implementation glTexCoord2f(bx, by); glVertex2i(l, b); glTexCoord2f(ax, by); glVertex2i(r, b); glEnd(); - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); end; procedure r_Draw_Texture (img: TGLTexture; x, y, w, h: Integer; flip: Boolean; r, g, b, a: Byte; blend: Boolean); @@ -267,8 +289,8 @@ implementation ASSERT(b >= t); glEnable(GL_BLEND); glBlendFunc(GL_ZERO, GL_SRC_COLOR); - glDisable(GL_TEXTURE_2D); - glColor4ub(rr, gg, bb, aa); + r_Draw_EnableTexture2D(false); + r_Draw_SetColor(rr, gg, bb, aa); glBegin(GL_QUADS); glVertex2i(l, t); glVertex2i(r, t); @@ -283,8 +305,8 @@ implementation ASSERT(b >= t); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_TEXTURE_2D); - glColor4ub(rr, gg, bb, aa); + r_Draw_EnableTexture2D(false); + r_Draw_SetColor(rr, gg, bb, aa); glBegin(GL_LINE_LOOP); { glVertex2i(l, t); @@ -305,8 +327,8 @@ implementation ASSERT(b >= t); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_TEXTURE_2D); - glColor4ub(rr, gg, bb, aa); + r_Draw_EnableTexture2D(false); + r_Draw_SetColor(rr, gg, bb, aa); glBegin(GL_QUADS); { glVertex2i(l, t); @@ -333,8 +355,8 @@ implementation ASSERT(b >= t); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); - glDisable(GL_TEXTURE_2D); - glColor4ub(rr, gg, bb, aa); + r_Draw_EnableTexture2D(false); + r_Draw_SetColor(rr, gg, bb, aa); glBegin(GL_QUADS); glVertex2i(l, t); glVertex2i(r, t); diff --git a/src/game/renders/opengl/r_map.pas b/src/game/renders/opengl/r_map.pas index 028c724..a1d781c 100644 --- a/src/game/renders/opengl/r_map.pas +++ b/src/game/renders/opengl/r_map.pas @@ -1113,7 +1113,7 @@ implementation begin if gpart_dbg_enabled and (Particles <> nil) then begin - glDisable(GL_TEXTURE_2D); + r_Draw_EnableTexture2D(false); if (g_dbg_scale < 0.6) then glPointSize(1) else if (g_dbg_scale > 1.3) then @@ -1132,7 +1132,7 @@ implementation fx := nlerp(Particles[i].oldX, Particles[i].x, gLerpFactor); fy := nlerp(Particles[i].oldY, Particles[i].y, gLerpFactor); glColor4ub(Particles[i].red, Particles[i].green, Particles[i].blue, Particles[i].alpha); - glVertex2f(fx, fy); + glVertex2i(fx, fy); end; end; glEnd; diff --git a/src/game/renders/opengl/r_textures.pas b/src/game/renders/opengl/r_textures.pas index 34b5b9e..1025aba 100644 --- a/src/game/renders/opengl/r_textures.pas +++ b/src/game/renders/opengl/r_textures.pas @@ -139,6 +139,8 @@ interface function r_Textures_LoadFontFromFile (const filename: AnsiString; constref f: TFontInfo; font2enc: TConvProc; log: Boolean = true): TGLFont; + procedure r_Textures_GL_Bind (id: GLuint); + implementation uses @@ -154,6 +156,16 @@ implementation r_GL_RepeatOpt: Boolean; maxTileSize: Integer; atl, ratl: array of TGLAtlas; + currentTexture2D: GLuint; + + procedure r_Textures_GL_Bind (id: GLuint); + begin + if id <> currentTexture2D then + begin + glBindTexture(GL_TEXTURE_2D, id); + currentTexture2D := id; + end + end; (* --------- TGLAtlasNode --------- *) @@ -185,9 +197,9 @@ implementation ASSERT(n.l + x + w - 1 <= n.r); ASSERT(n.t + y + h - 1 <= n.b); ASSERT(n.id > 0); - glBindTexture(GL_TEXTURE_2D, n.id); + r_Textures_GL_Bind(n.id); glTexSubImage2D(GL_TEXTURE_2D, 0, n.l + x, n.t + y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, data); - glBindTexture(GL_TEXTURE_2D, 0); + r_Textures_GL_Bind(0); end; (* --------- TGLAtlas --------- *) @@ -221,13 +233,13 @@ implementation glGenTextures(1, @id); if id <> 0 then begin - glBindTexture(GL_TEXTURE_2D, id); + r_Textures_GL_Bind(id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil); - glBindTexture(GL_TEXTURE_2D, 0); + r_Textures_GL_Bind(0); end; result := id end; @@ -472,6 +484,7 @@ implementation procedure r_Textures_Initialize; begin + currentTexture2D := 0; maxTileSize := r_Textures_GetMaxHardwareSize(); e_LogWritefln('TEXTURE SIZE: %s', [maxTileSize]); end; -- 2.29.2