Skip to content

Commit 8bfdf4b

Browse files
authored
Merge pull request #746 from edge-classic/render-state-consolidation
Consolidate GL state changes and render calls
2 parents 9963dc8 + db97246 commit 8bfdf4b

29 files changed

+1663
-1578
lines changed

source_files/edge/con_con.cc

+118-110
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "r_draw.h"
5151
#include "r_image.h"
5252
#include "r_modes.h"
53+
#include "r_units.h"
5354
#include "r_wipe.h"
5455
#include "stb_sprintf.h"
5556
#include "w_files.h"
@@ -572,25 +573,24 @@ static void CalcSizes()
572573
(FNSZ / console_font->image_character_height_));
573574
}
574575

575-
static void SolidBox(int x, int y, int w, int h, RGBAColor col, float alpha)
576+
static void SolidBox(float x, float y, float w, float h, RGBAColor col, float alpha)
576577
{
577-
if (alpha < 0.99f)
578-
glEnable(GL_BLEND);
578+
RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0,
579+
0, alpha < 0.99f ? kBlendingAlpha : kBlendingNone);
579580

580581
sg_color sgcol = sg_make_color_1i(col);
581-
582-
glColor4f(sgcol.r, sgcol.g, sgcol.b, alpha);
583-
584-
glBegin(GL_QUADS);
585-
586-
glVertex2i(x, y);
587-
glVertex2i(x, y + h);
588-
glVertex2i(x + w, y + h);
589-
glVertex2i(x + w, y);
590-
591-
glEnd();
592-
593-
glDisable(GL_BLEND);
582+
sgcol.a = alpha;
583+
584+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
585+
glvert++->position = {{x, y, 0}};
586+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
587+
glvert++->position = {{x, y + h, 0}};
588+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
589+
glvert++->position = {{x + w, y + h, 0}};
590+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
591+
glvert->position = {{x + w, y, 0}};
592+
593+
EndRenderUnit(4);
594594
}
595595

596596
static void HorizontalLine(int y, RGBAColor col)
@@ -600,15 +600,11 @@ static void HorizontalLine(int y, RGBAColor col)
600600
SolidBox(0, y, current_screen_width - 1, 1, col, alpha);
601601
}
602602

603-
static void DrawChar(int x, int y, char ch, RGBAColor col)
603+
static void DrawChar(float x, float y, char ch, RendererVertex *glvert, sg_color *col)
604604
{
605605
if (x + FNSZ < 0)
606606
return;
607607

608-
sg_color sgcol = sg_make_color_1i(col);
609-
610-
glColor4f(sgcol.r, sgcol.g, sgcol.b, 1.0f);
611-
612608
if (console_font->definition_->type_ == kFontTypeTrueType)
613609
{
614610
float chwidth = console_font->CharWidth(ch);
@@ -618,16 +614,18 @@ static void DrawChar(int x, int y, char ch, RGBAColor col)
618614
float y_adjust = console_font->truetype_glyph_map_.at((uint8_t)ch).y_shift[current_font_size] * FNSZ_ratio;
619615
float height = console_font->truetype_glyph_map_.at((uint8_t)ch).height[current_font_size] * FNSZ_ratio;
620616
stbtt_aligned_quad *q = console_font->truetype_glyph_map_.at((uint8_t)ch).character_quad[current_font_size];
621-
glBegin(GL_POLYGON);
622-
glTexCoord2f(q->s0, q->t0);
623-
glVertex2f(x + x_adjust, y - y_adjust);
624-
glTexCoord2f(q->s1, q->t0);
625-
glVertex2f(x + x_adjust + width, y - y_adjust);
626-
glTexCoord2f(q->s1, q->t1);
627-
glVertex2f(x + x_adjust + width, y - y_adjust - height);
628-
glTexCoord2f(q->s0, q->t1);
629-
glVertex2f(x + x_adjust, y - y_adjust - height);
630-
glEnd();
617+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
618+
glvert->position = {{x + x_adjust, y - y_adjust, 0}};
619+
glvert++->texture_coordinates[0] = {{q->s0, q->t0}};
620+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
621+
glvert->position = {{x + x_adjust + width, y - y_adjust, 0}};
622+
glvert++->texture_coordinates[0] = {{q->s1, q->t0}};
623+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
624+
glvert->position = {{x + x_adjust + width, y - y_adjust - height, 0}};
625+
glvert++->texture_coordinates[0] = {{q->s1, q->t1}};
626+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
627+
glvert->position = {{x + x_adjust, y - y_adjust - height, 0}};
628+
glvert->texture_coordinates[0] = {{q->s0, q->t1}};
631629
return;
632630
}
633631

@@ -639,51 +637,45 @@ static void DrawChar(int x, int y, char ch, RGBAColor col)
639637
float ty1 = (py)*console_font->font_image_->height_ratio_;
640638
float ty2 = (py + 1) * console_font->font_image_->height_ratio_;
641639

642-
glBegin(GL_POLYGON);
643-
644-
glTexCoord2f(tx1, ty1);
645-
glVertex2i(x, y);
646-
647-
glTexCoord2f(tx1, ty2);
648-
glVertex2i(x, y + FNSZ);
649-
650-
glTexCoord2f(tx2, ty2);
651-
glVertex2i(x + FNSZ, y + FNSZ);
652-
653-
glTexCoord2f(tx2, ty1);
654-
glVertex2i(x + FNSZ, y);
655-
656-
glEnd();
640+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
641+
glvert->position = {{x, y, 0}};
642+
glvert++->texture_coordinates[0] = {{tx1, ty1}};
643+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
644+
glvert->position = {{x, y + FNSZ, 0}};
645+
glvert++->texture_coordinates[0] = {{tx1, ty2}};
646+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
647+
glvert->position = {{x + FNSZ, y + FNSZ, 0}};
648+
glvert++->texture_coordinates[0] = {{tx2, ty2}};
649+
memcpy(&glvert->rgba_color, col, 4 * sizeof(float));
650+
glvert->position = {{x + FNSZ, y, 0}};
651+
glvert->texture_coordinates[0] = {{tx2, ty1}};
657652
}
658653

659-
static void DrawEndoomChar(float x, float y, char ch, RGBAColor col, RGBAColor col2, bool blink, int enwidth)
654+
static void DrawEndoomChar(float x, float y, char ch, RGBAColor col, RGBAColor col2, bool blink, int enwidth, GLuint tex_id)
660655
{
661656
if (x + FNSZ < 0)
662657
return;
663658

664659
sg_color sgcol = sg_make_color_1i(col2);
660+
sgcol.a = 1.0f;
665661

666-
glDisable(GL_TEXTURE_2D);
667-
668-
glColor4f(sgcol.r, sgcol.g, sgcol.b, 1.0f);
669-
670-
glBegin(GL_QUADS);
671-
672-
glVertex2i(x - (enwidth / 2), y);
673-
674-
glVertex2i(x - (enwidth / 2), y + FNSZ);
675-
676-
glVertex2i(x + (enwidth / 2), y + FNSZ);
662+
RendererVertex *glvert = BeginRenderUnit(GL_QUADS, 4, GL_MODULATE, 0, (GLuint)kTextureEnvironmentDisable, 0,
663+
0, kBlendingNone);
677664

678-
glVertex2i(x + (enwidth / 2), y);
665+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
666+
glvert++->position = {{x - (enwidth / 2), y}};
667+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
668+
glvert++->position = {{x - (enwidth / 2), y + FNSZ}};
669+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
670+
glvert++->position = {{x + (enwidth / 2), y + FNSZ}};
671+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
672+
glvert->position = {{x + (enwidth / 2), y}};
679673

680-
glEnd();
681674

682-
glEnable(GL_TEXTURE_2D);
675+
EndRenderUnit(4);
683676

684677
sgcol = sg_make_color_1i(col);
685-
686-
glColor4f(sgcol.r, sgcol.g, sgcol.b, 1.0f);
678+
sgcol.a = 1.0f;
687679

688680
if (blink && console_cursor >= 16)
689681
ch = 0x20;
@@ -697,49 +689,47 @@ static void DrawEndoomChar(float x, float y, char ch, RGBAColor col, RGBAColor c
697689
float ty1 = (py)*endoom_font->font_image_->height_ratio_;
698690
float ty2 = (py + 1) * endoom_font->font_image_->height_ratio_;
699691

700-
glBegin(GL_POLYGON);
701-
702-
glTexCoord2f(tx1, ty1);
703-
glVertex2i(x - enwidth, y);
704-
705-
glTexCoord2f(tx1, ty2);
706-
glVertex2i(x - enwidth, y + FNSZ);
707-
708-
glTexCoord2f(tx2, ty2);
709-
glVertex2i(x + enwidth, y + FNSZ);
710-
711-
glTexCoord2f(tx2, ty1);
712-
glVertex2i(x + enwidth, y);
713-
714-
glEnd();
692+
glvert = BeginRenderUnit(GL_POLYGON, 4, GL_MODULATE, tex_id, (GLuint)kTextureEnvironmentDisable, 0,
693+
0, kBlendingMasked);
694+
695+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
696+
glvert->texture_coordinates[0] = {{tx1, ty1}};
697+
glvert++->position = {{x - enwidth, y}};
698+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
699+
glvert->texture_coordinates[0] = {{tx1, ty2}};
700+
glvert++->position = {{x - enwidth, y + FNSZ}};
701+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
702+
glvert->texture_coordinates[0] = {{tx2, ty2}};
703+
glvert++->position = {{x + enwidth, y + FNSZ}};
704+
memcpy(&glvert->rgba_color, &sgcol, 4 * sizeof(float));
705+
glvert->texture_coordinates[0] = {{tx2, ty1}};
706+
glvert->position = {{x + enwidth, y}};
707+
708+
EndRenderUnit(4);
715709
}
716710

717711
// writes the text on coords (x,y) of the console
718-
static void DrawText(int x, int y, const char *s, RGBAColor col)
712+
static void DrawText(float x, float y, const char *s, RGBAColor col)
719713
{
714+
GLuint tex_id = 0;
715+
BlendingMode blend = kBlendingNone;
716+
720717
if (console_font->definition_->type_ == kFontTypeImage)
721718
{
722719
// Always whiten the font when used with console output
723-
GLuint tex_id = ImageCache(console_font->font_image_, true, (const Colormap *)0, true);
724-
725-
glEnable(GL_TEXTURE_2D);
726-
glBindTexture(GL_TEXTURE_2D, tex_id);
727-
728-
glEnable(GL_BLEND);
729-
glEnable(GL_ALPHA_TEST);
730-
glAlphaFunc(GL_GREATER, 0);
720+
tex_id = ImageCache(console_font->font_image_, true, (const Colormap *)0, true);
721+
blend = kBlendingMasked;
731722
}
732723
else if (console_font->definition_->type_ == kFontTypeTrueType)
733724
{
734-
glEnable(GL_BLEND);
735-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
736-
glEnable(GL_TEXTURE_2D);
737725
if ((image_smoothing &&
738726
console_font->definition_->truetype_smoothing_ == FontDefinition::kTrueTypeSmoothOnDemand) ||
739727
console_font->definition_->truetype_smoothing_ == FontDefinition::kTrueTypeSmoothAlways)
740-
glBindTexture(GL_TEXTURE_2D, console_font->truetype_smoothed_texture_id_[current_font_size]);
728+
tex_id = console_font->truetype_smoothed_texture_id_[current_font_size];
741729
else
742-
glBindTexture(GL_TEXTURE_2D, console_font->truetype_texture_id_[current_font_size]);
730+
tex_id = console_font->truetype_texture_id_[current_font_size];
731+
732+
blend = kBlendingAlpha;
743733
}
744734

745735
bool draw_cursor = false;
@@ -750,10 +740,18 @@ static void DrawText(int x, int y, const char *s, RGBAColor col)
750740
draw_cursor = true;
751741
}
752742

743+
sg_color sgcol = sg_make_color_1i(col);
744+
RendererVertex *glvert = nullptr;
745+
753746
int pos = 0;
754747
for (; *s; s++, pos++)
755748
{
756-
DrawChar(x, y, *s, col);
749+
glvert = BeginRenderUnit(GL_POLYGON, 4, GL_MODULATE, tex_id, (GLuint)kTextureEnvironmentDisable, 0,
750+
0, blend);
751+
752+
DrawChar(x, y, *s, glvert, &sgcol);
753+
754+
EndRenderUnit(4);
757755

758756
if (console_font->definition_->type_ == kFontTypeTrueType)
759757
{
@@ -767,7 +765,13 @@ static void DrawText(int x, int y, const char *s, RGBAColor col)
767765

768766
if (pos == input_position && draw_cursor)
769767
{
770-
DrawChar(x, y, 95, col);
768+
glvert = BeginRenderUnit(GL_POLYGON, 4, GL_MODULATE, tex_id, (GLuint)kTextureEnvironmentDisable, 0,
769+
0, blend);
770+
771+
DrawChar(x, y, 95, glvert, &sgcol);
772+
773+
EndRenderUnit(4);
774+
771775
draw_cursor = false;
772776
}
773777

@@ -778,11 +782,14 @@ static void DrawText(int x, int y, const char *s, RGBAColor col)
778782
}
779783

780784
if (draw_cursor)
781-
DrawChar(x, y, 95, col);
785+
{
786+
glvert = BeginRenderUnit(GL_POLYGON, 4, GL_MODULATE, tex_id, (GLuint)kTextureEnvironmentDisable, 0,
787+
0, blend);
782788

783-
glDisable(GL_TEXTURE_2D);
784-
glDisable(GL_ALPHA_TEST);
785-
glDisable(GL_BLEND);
789+
DrawChar(x, y, 95, glvert, &sgcol);
790+
791+
EndRenderUnit(4);
792+
}
786793
}
787794

788795
static void EndoomDrawText(int x, int y, ConsoleLine *endoom_line)
@@ -793,29 +800,18 @@ static void EndoomDrawText(int x, int y, ConsoleLine *endoom_line)
793800
int enwidth = RoundToInteger((float)endoom_font->image_monospace_width_ *
794801
((float)FNSZ / endoom_font->image_monospace_width_) / 2);
795802

796-
glEnable(GL_TEXTURE_2D);
797-
glBindTexture(GL_TEXTURE_2D, tex_id);
798-
799-
glEnable(GL_BLEND);
800-
glEnable(GL_ALPHA_TEST);
801-
glAlphaFunc(GL_GREATER, 0);
802-
803803
for (int i = 0; i < 80; i++)
804804
{
805805
uint8_t info = endoom_line->endoom_bytes_.at(i);
806806

807807
DrawEndoomChar(x, y, endoom_line->line_.at(i), endoom_colors[info & 15], endoom_colors[(info >> 4) & 7],
808-
info & 128, enwidth);
808+
info & 128, enwidth, tex_id);
809809

810810
x += enwidth;
811811

812812
if (x >= current_screen_width)
813813
break;
814814
}
815-
816-
glDisable(GL_TEXTURE_2D);
817-
glDisable(GL_ALPHA_TEST);
818-
glDisable(GL_BLEND);
819815
}
820816

821817
void ConsoleSetupFont(void)
@@ -860,6 +856,8 @@ void ConsoleDrawer(void)
860856

861857
// -- background --
862858

859+
StartUnitBatch(false);
860+
863861
int CON_GFX_HT = (current_screen_height * 3 / 5);
864862

865863
int y = current_screen_height;
@@ -939,6 +937,8 @@ void ConsoleDrawer(void)
939937
if (y >= current_screen_height)
940938
break;
941939
}
940+
941+
FinishUnitBatch();
942942
}
943943

944944
static void GotoEndOfLine(void)
@@ -1624,6 +1624,8 @@ void ConsoleShowFPS(void)
16241624
if (debug_fps.d_ == 0)
16251625
return;
16261626

1627+
StartUnitBatch(false);
1628+
16271629
ConsoleSetupFont();
16281630

16291631
// -AJA- 2022: reworked for better accuracy, ability to show WORST time
@@ -1723,13 +1725,17 @@ void ConsoleShowFPS(void)
17231725
stbsp_sprintf(textbuf, "%i texture", ec_frame_stats.draw_texture_change);
17241726
DrawText(x, y, textbuf, SG_WEB_GRAY_RGBA32);
17251727
}
1728+
1729+
FinishUnitBatch();
17261730
}
17271731

17281732
void ConsoleShowPosition(void)
17291733
{
17301734
if (debug_position.d_ <= 0)
17311735
return;
17321736

1737+
StartUnitBatch(false);
1738+
17331739
ConsoleSetupFont();
17341740

17351741
Player *p = players[display_player];
@@ -1779,6 +1785,8 @@ void ConsoleShowPosition(void)
17791785
y -= FNSZ;
17801786
stbsp_sprintf(textbuf, " sub: %d", (int)(p->map_object_->subsector_ - level_subsectors));
17811787
DrawText(x, y, textbuf, SG_WEB_GRAY_RGBA32);
1788+
1789+
FinishUnitBatch();
17821790
}
17831791

17841792
void ConsolePrintEndoom()

0 commit comments

Comments
 (0)