From 4f15da695cad7c0c4dfcf37380d9f7748f25e787 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 5 Apr 2012 16:08:46 -0300 Subject: [PATCH] optimizations --- modules/game_shaders/item.frag | 5 ++--- modules/game_shaders/map.frag | 2 +- modules/game_shaders/outfit.frag | 2 +- src/framework/const.h | 1 + src/framework/core/logger.h | 22 +++++++++++++++++++ .../graphics/paintershaderprogram.cpp | 17 +++++++------- src/framework/graphics/paintershaderprogram.h | 3 ++- src/framework/ui/uiwidget.cpp | 4 ++-- src/framework/ui/uiwidgetbasestyle.cpp | 12 +++++----- src/framework/ui/uiwidgetimage.cpp | 2 +- 10 files changed, 47 insertions(+), 23 deletions(-) diff --git a/modules/game_shaders/item.frag b/modules/game_shaders/item.frag index f7a3bfa6..ade31f15 100644 --- a/modules/game_shaders/item.frag +++ b/modules/game_shaders/item.frag @@ -2,11 +2,10 @@ uniform float opacity; // painter opacity uniform vec4 color; // painter color uniform float time; // time in seconds since shader linkage uniform sampler2D texture; // map texture -varying vec2 textureCoords; // map texture coords //uniform int itemId; // item id +varying vec2 textureCoords; // map texture coords void main() { - vec4 outColor = texture2D(texture, textureCoords); - gl_FragColor = outColor * opacity; + gl_FragColor = texture2D(texture, textureCoords); } diff --git a/modules/game_shaders/map.frag b/modules/game_shaders/map.frag index f407109c..c88317ba 100644 --- a/modules/game_shaders/map.frag +++ b/modules/game_shaders/map.frag @@ -6,5 +6,5 @@ varying vec2 textureCoords; // map texture coords void main() { - gl_FragColor = texture2D(texture, textureCoords) * color * opacity; + gl_FragColor = texture2D(texture, textureCoords); } diff --git a/modules/game_shaders/outfit.frag b/modules/game_shaders/outfit.frag index 86fa0622..31033ce5 100644 --- a/modules/game_shaders/outfit.frag +++ b/modules/game_shaders/outfit.frag @@ -57,6 +57,6 @@ vec4 calcOutfitPixel() */ void main() { - gl_FragColor = calcOutfitPixel() * color * opacity; + gl_FragColor = calcOutfitPixel(); } diff --git a/src/framework/const.h b/src/framework/const.h index 1cabd0ea..c75ad042 100644 --- a/src/framework/const.h +++ b/src/framework/const.h @@ -42,6 +42,7 @@ namespace Fw { constexpr float pi = 3.14159265; + constexpr float MIN_ALPHA = 0.003f; enum Key : uint8 { KeyUnknown = 0, diff --git a/src/framework/core/logger.h b/src/framework/core/logger.h index d06cad7d..edd6b42c 100644 --- a/src/framework/core/logger.h +++ b/src/framework/core/logger.h @@ -69,4 +69,26 @@ extern Logger g_logger; #define logTraceWarning(...) g_logger.logFunc(Fw::LogWarning, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) #define logTraceError(...) g_logger.logFunc(Fw::LogError, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) +#define logTraceCounter() { \ + static int __count = 0; \ + static Timer __timer; \ + __count++; \ + if(__timer.ticksElapsed() >= 1000) { \ + logTraceDebug(__count); \ + __count = 0; \ + __timer.restart(); \ + } \ +} + +#define logTraceFrameCounter() { \ + static int __count = 0; \ + static Timer __timer; \ + __count++; \ + if(__timer.ticksElapsed() > 0) { \ + logTraceDebug(__count); \ + __count = 0; \ + __timer.restart(); \ + } \ +} + #endif diff --git a/src/framework/graphics/paintershaderprogram.cpp b/src/framework/graphics/paintershaderprogram.cpp index a70a333b..3f53343e 100644 --- a/src/framework/graphics/paintershaderprogram.cpp +++ b/src/framework/graphics/paintershaderprogram.cpp @@ -30,6 +30,7 @@ PainterShaderProgram::PainterShaderProgram() { m_textures.fill(std::make_tuple(-1, 0)); m_startTime = g_clock.time(); + m_lastTexture = -1; } bool PainterShaderProgram::link() @@ -72,6 +73,7 @@ void PainterShaderProgram::setUniformTexture(int location, const TexturePtr& tex assert(index >= 0 && index <= 1); m_textures[index] = std::make_tuple(location, texture ? texture->getId() : 0); + m_lastTexture = std::max(m_lastTexture, index); } void PainterShaderProgram::setTexture(const TexturePtr& texture) @@ -102,7 +104,8 @@ void PainterShaderProgram::draw(CoordsBuffer& coordsBuffer, DrawMode drawMode) coordsBuffer.getHardwareVertexBuffer()->bind(); setAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR, hardwareCached ? 0 : coordsBuffer.getVertexBuffer(), 2); - if(coordsBuffer.getTextureVertexCount() != 0) { + bool hasTexture = coordsBuffer.getTextureVertexCount() != 0; + if(hasTexture) { enableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR); if(hardwareCached) coordsBuffer.getHardwareTextureVertexBuffer()->bind(); @@ -112,23 +115,21 @@ void PainterShaderProgram::draw(CoordsBuffer& coordsBuffer, DrawMode drawMode) if(hardwareCached) HardwareBuffer::unbind(HardwareBuffer::VertexBuffer); - for(int i=0;i<(int)m_textures.size();++i) { + for(int i=m_lastTexture;i>=0;--i) { int location = std::get<0>(m_textures[i]); - if(location == -1) - break; - int id = std::get<1>(m_textures[i]); + uint id = std::get<1>(m_textures[i]); setUniformValue(location, i); - glActiveTexture(GL_TEXTURE0+i); + if(m_lastTexture > 0) + glActiveTexture(GL_TEXTURE0+i); glBindTexture(GL_TEXTURE_2D, id); } - glActiveTexture(GL_TEXTURE0); glDrawArrays(drawMode, 0, vertexCount); disableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR); - if(coordsBuffer.getTextureVertexCount() != 0) + if(hasTexture) disableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR); } diff --git a/src/framework/graphics/paintershaderprogram.h b/src/framework/graphics/paintershaderprogram.h index 0c9a0f6c..14423932 100644 --- a/src/framework/graphics/paintershaderprogram.h +++ b/src/framework/graphics/paintershaderprogram.h @@ -59,7 +59,8 @@ public: private: DrawMode m_drawMode; float m_startTime; - std::array, 4> m_textures; + std::array, 4> m_textures; + int m_lastTexture; }; #endif diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index 2fc7d923..fabf5d2e 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -72,7 +72,7 @@ void UIWidget::draw(const Rect& visibleRect) void UIWidget::drawSelf() { // draw style components in order - if(m_backgroundColor.aF() != 0.0f) { + if(m_backgroundColor.aF() > Fw::MIN_ALPHA) { Rect backgroundDestRect = m_rect; backgroundDestRect.expand(-m_borderWidth.top, -m_borderWidth.right, -m_borderWidth.bottom, -m_borderWidth.left); drawBackground(m_rect); @@ -89,7 +89,7 @@ void UIWidget::drawChildren(const Rect& visibleRect) // draw children for(const UIWidgetPtr& child : m_children) { // render only visible children with a valid rect inside parent rect - if(!child->isExplicitlyVisible() || !child->getRect().isValid() || child->getOpacity() == 0.0f) + if(!child->isExplicitlyVisible() || !child->getRect().isValid() || child->getOpacity() < Fw::MIN_ALPHA) continue; Rect childVisibleRect = visibleRect.intersection(child->getRect()); diff --git a/src/framework/ui/uiwidgetbasestyle.cpp b/src/framework/ui/uiwidgetbasestyle.cpp index b11a0e2b..6b743fa2 100644 --- a/src/framework/ui/uiwidgetbasestyle.cpp +++ b/src/framework/ui/uiwidgetbasestyle.cpp @@ -314,7 +314,7 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode) void UIWidget::drawBackground(const Rect& screenCoords) { - if(m_backgroundColor.aF() != 0.0f) { + if(m_backgroundColor.aF() > 0.0f) { Rect drawRect = screenCoords; drawRect.translate(m_backgroundRect.topLeft()); if(m_backgroundRect.isValid()) @@ -327,28 +327,28 @@ void UIWidget::drawBackground(const Rect& screenCoords) void UIWidget::drawBorder(const Rect& screenCoords) { // top - if(m_borderWidth.top > 0 && m_borderColor.top.aF() != 0.0f) { + if(m_borderWidth.top > 0) { g_painter.setColor(m_borderColor.top); Rect borderRect(screenCoords.topLeft(), screenCoords.width(), m_borderWidth.top); g_painter.drawFilledRect(borderRect); } // right - if(m_borderWidth.right > 0 && m_borderColor.right.aF() != 0.0f) { + if(m_borderWidth.right > 0) { g_painter.setColor(m_borderColor.right); Rect borderRect(screenCoords.topRight() - Point(m_borderWidth.right - 1, 0), m_borderWidth.right, screenCoords.height()); g_painter.drawFilledRect(borderRect); } // bottom - if(m_borderWidth.bottom > 0 && m_borderColor.bottom.aF() != 0.0f) { + if(m_borderWidth.bottom > 0) { g_painter.setColor(m_borderColor.bottom); Rect borderRect(screenCoords.bottomLeft() - Point(0, m_borderWidth.bottom - 1), screenCoords.width(), m_borderWidth.bottom); g_painter.drawFilledRect(borderRect); } // left - if(m_borderWidth.left > 0 && m_borderColor.left.aF() != 0.0f) { + if(m_borderWidth.left > 0) { g_painter.setColor(m_borderColor.left); Rect borderRect(screenCoords.topLeft(), m_borderWidth.left, screenCoords.height()); @@ -358,7 +358,7 @@ void UIWidget::drawBorder(const Rect& screenCoords) void UIWidget::drawIcon(const Rect& screenCoords) { - if(m_icon && m_iconColor.aF() != 0.0f) { + if(m_icon) { Rect drawRect; if(m_iconRect.isValid()) { drawRect = screenCoords; diff --git a/src/framework/ui/uiwidgetimage.cpp b/src/framework/ui/uiwidgetimage.cpp index 574dc52a..be3a900d 100644 --- a/src/framework/ui/uiwidgetimage.cpp +++ b/src/framework/ui/uiwidgetimage.cpp @@ -76,7 +76,7 @@ void UIWidget::parseImageStyle(const OTMLNodePtr& styleNode) void UIWidget::drawImage(const Rect& screenCoords) { - if(!m_imageTexture || m_imageColor.aF() == 0.0f || !screenCoords.isValid()) + if(!m_imageTexture || !screenCoords.isValid()) return; // cache vertex buffers