From 63e0f13f1b3b3505a17f852263231f57680df11a Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Thu, 11 Feb 2021 19:15:35 +0000 Subject: [PATCH] Reduces the number of GL state changes in GUI. This was SVN commit r24885. --- source/graphics/TextRenderer.cpp | 9 +++++++-- source/gui/GUIRenderer.cpp | 22 +++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/source/graphics/TextRenderer.cpp b/source/graphics/TextRenderer.cpp index 490d922e70..3e1fca0780 100644 --- a/source/graphics/TextRenderer.cpp +++ b/source/graphics/TextRenderer.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -244,13 +244,18 @@ void CTextRenderer::Render() ++it; } + CTexture* lastTexture = nullptr; for (std::list::iterator it = m_Batches.begin(); it != m_Batches.end(); ++it) { SBatch& batch = *it; const CFont::GlyphMap& glyphs = batch.font->GetGlyphs(); - m_Shader->BindTexture(str_tex, batch.font->GetTexture()); + if (lastTexture != batch.font->GetTexture().get()) + { + lastTexture = batch.font->GetTexture().get(); + m_Shader->BindTexture(str_tex, batch.font->GetTexture()); + } m_Shader->Uniform(str_transform, batch.transform); diff --git a/source/gui/GUIRenderer.cpp b/source/gui/GUIRenderer.cpp index 89e558b697..439c48f1f7 100644 --- a/source/gui/GUIRenderer.cpp +++ b/source/gui/GUIRenderer.cpp @@ -333,6 +333,9 @@ CRect SDrawCall::ComputeTexCoords() const void GUIRenderer::Draw(DrawCalls& Calls, float Z) { + if (Calls.empty()) + return; + // Called every frame, to draw the object (based on cached calculations) // TODO: batching by shader/texture/etc would be nice @@ -340,6 +343,7 @@ void GUIRenderer::Draw(DrawCalls& Calls, float Z) CMatrix3D matrix = GetDefaultGuiMatrix(); glDisable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Iterate through each DrawCall, and execute whatever drawing code is being called for (DrawCalls::const_iterator cit = Calls.begin(); cit != Calls.end(); ++cit) @@ -353,11 +357,10 @@ void GUIRenderer::Draw(DrawCalls& Calls, float Z) shader->Uniform(str_color, cit->m_ShaderColorParameter); shader->BindTexture(str_tex, cit->m_Texture); - if (cit->m_EnableBlending || cit->m_Texture->HasAlpha()) // (shouldn't call HasAlpha before BindTexture) - { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // Shouldn't call HasAlpha before BindTexture. + const bool needsBlend = cit->m_EnableBlending || cit->m_Texture->HasAlpha(); + if (needsBlend) glEnable(GL_BLEND); - } CRect TexCoords = cit->ComputeTexCoords(); @@ -388,16 +391,16 @@ void GUIRenderer::Draw(DrawCalls& Calls, float Z) shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 5*sizeof(float), &data[0]); shader->VertexPointer(3, GL_FLOAT, 5*sizeof(float), &data[2]); glDrawArrays(GL_TRIANGLES, 0, 6); + + if (needsBlend) + glDisable(GL_BLEND); } else { shader->Uniform(str_color, *cit->m_BackColor); if (cit->m_EnableBlending) - { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); - } // Ensure the quad has the correct winding order CRect Verts = cit->m_Vertices; @@ -432,11 +435,12 @@ void GUIRenderer::Draw(DrawCalls& Calls, float Z) shader->VertexPointer(3, GL_FLOAT, 3*sizeof(float), &data[0]); glDrawArrays(GL_LINE_LOOP, 0, 4); } + + if (cit->m_EnableBlending) + glDisable(GL_BLEND); #undef ADD } cit->m_Shader->EndPass(); - - glDisable(GL_BLEND); } }