optimizations

master
Eduardo Bart 12 years ago
parent 49727c573f
commit 4f15da695c

@ -2,11 +2,10 @@ uniform float opacity; // painter opacity
uniform vec4 color; // painter color uniform vec4 color; // painter color
uniform float time; // time in seconds since shader linkage uniform float time; // time in seconds since shader linkage
uniform sampler2D texture; // map texture uniform sampler2D texture; // map texture
varying vec2 textureCoords; // map texture coords
//uniform int itemId; // item id //uniform int itemId; // item id
varying vec2 textureCoords; // map texture coords
void main() void main()
{ {
vec4 outColor = texture2D(texture, textureCoords); gl_FragColor = texture2D(texture, textureCoords);
gl_FragColor = outColor * opacity;
} }

@ -6,5 +6,5 @@ varying vec2 textureCoords; // map texture coords
void main() void main()
{ {
gl_FragColor = texture2D(texture, textureCoords) * color * opacity; gl_FragColor = texture2D(texture, textureCoords);
} }

@ -57,6 +57,6 @@ vec4 calcOutfitPixel()
*/ */
void main() void main()
{ {
gl_FragColor = calcOutfitPixel() * color * opacity; gl_FragColor = calcOutfitPixel();
} }

@ -42,6 +42,7 @@
namespace Fw namespace Fw
{ {
constexpr float pi = 3.14159265; constexpr float pi = 3.14159265;
constexpr float MIN_ALPHA = 0.003f;
enum Key : uint8 { enum Key : uint8 {
KeyUnknown = 0, KeyUnknown = 0,

@ -69,4 +69,26 @@ extern Logger g_logger;
#define logTraceWarning(...) g_logger.logFunc(Fw::LogWarning, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__) #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 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 #endif

@ -30,6 +30,7 @@ PainterShaderProgram::PainterShaderProgram()
{ {
m_textures.fill(std::make_tuple(-1, 0)); m_textures.fill(std::make_tuple(-1, 0));
m_startTime = g_clock.time(); m_startTime = g_clock.time();
m_lastTexture = -1;
} }
bool PainterShaderProgram::link() bool PainterShaderProgram::link()
@ -72,6 +73,7 @@ void PainterShaderProgram::setUniformTexture(int location, const TexturePtr& tex
assert(index >= 0 && index <= 1); assert(index >= 0 && index <= 1);
m_textures[index] = std::make_tuple(location, texture ? texture->getId() : 0); m_textures[index] = std::make_tuple(location, texture ? texture->getId() : 0);
m_lastTexture = std::max(m_lastTexture, index);
} }
void PainterShaderProgram::setTexture(const TexturePtr& texture) void PainterShaderProgram::setTexture(const TexturePtr& texture)
@ -102,7 +104,8 @@ void PainterShaderProgram::draw(CoordsBuffer& coordsBuffer, DrawMode drawMode)
coordsBuffer.getHardwareVertexBuffer()->bind(); coordsBuffer.getHardwareVertexBuffer()->bind();
setAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR, hardwareCached ? 0 : coordsBuffer.getVertexBuffer(), 2); 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); enableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR);
if(hardwareCached) if(hardwareCached)
coordsBuffer.getHardwareTextureVertexBuffer()->bind(); coordsBuffer.getHardwareTextureVertexBuffer()->bind();
@ -112,23 +115,21 @@ void PainterShaderProgram::draw(CoordsBuffer& coordsBuffer, DrawMode drawMode)
if(hardwareCached) if(hardwareCached)
HardwareBuffer::unbind(HardwareBuffer::VertexBuffer); 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]); int location = std::get<0>(m_textures[i]);
if(location == -1) uint id = std::get<1>(m_textures[i]);
break;
int id = std::get<1>(m_textures[i]);
setUniformValue(location, i); setUniformValue(location, i);
glActiveTexture(GL_TEXTURE0+i); if(m_lastTexture > 0)
glActiveTexture(GL_TEXTURE0+i);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
} }
glActiveTexture(GL_TEXTURE0);
glDrawArrays(drawMode, 0, vertexCount); glDrawArrays(drawMode, 0, vertexCount);
disableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR); disableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR);
if(coordsBuffer.getTextureVertexCount() != 0) if(hasTexture)
disableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR); disableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR);
} }

@ -59,7 +59,8 @@ public:
private: private:
DrawMode m_drawMode; DrawMode m_drawMode;
float m_startTime; float m_startTime;
std::array<std::tuple<int, int>, 4> m_textures; std::array<std::tuple<int, uint>, 4> m_textures;
int m_lastTexture;
}; };
#endif #endif

@ -72,7 +72,7 @@ void UIWidget::draw(const Rect& visibleRect)
void UIWidget::drawSelf() void UIWidget::drawSelf()
{ {
// draw style components in order // draw style components in order
if(m_backgroundColor.aF() != 0.0f) { if(m_backgroundColor.aF() > Fw::MIN_ALPHA) {
Rect backgroundDestRect = m_rect; Rect backgroundDestRect = m_rect;
backgroundDestRect.expand(-m_borderWidth.top, -m_borderWidth.right, -m_borderWidth.bottom, -m_borderWidth.left); backgroundDestRect.expand(-m_borderWidth.top, -m_borderWidth.right, -m_borderWidth.bottom, -m_borderWidth.left);
drawBackground(m_rect); drawBackground(m_rect);
@ -89,7 +89,7 @@ void UIWidget::drawChildren(const Rect& visibleRect)
// draw children // draw children
for(const UIWidgetPtr& child : m_children) { for(const UIWidgetPtr& child : m_children) {
// render only visible children with a valid rect inside parent rect // 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; continue;
Rect childVisibleRect = visibleRect.intersection(child->getRect()); Rect childVisibleRect = visibleRect.intersection(child->getRect());

@ -314,7 +314,7 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
void UIWidget::drawBackground(const Rect& screenCoords) void UIWidget::drawBackground(const Rect& screenCoords)
{ {
if(m_backgroundColor.aF() != 0.0f) { if(m_backgroundColor.aF() > 0.0f) {
Rect drawRect = screenCoords; Rect drawRect = screenCoords;
drawRect.translate(m_backgroundRect.topLeft()); drawRect.translate(m_backgroundRect.topLeft());
if(m_backgroundRect.isValid()) if(m_backgroundRect.isValid())
@ -327,28 +327,28 @@ void UIWidget::drawBackground(const Rect& screenCoords)
void UIWidget::drawBorder(const Rect& screenCoords) void UIWidget::drawBorder(const Rect& screenCoords)
{ {
// top // top
if(m_borderWidth.top > 0 && m_borderColor.top.aF() != 0.0f) { if(m_borderWidth.top > 0) {
g_painter.setColor(m_borderColor.top); g_painter.setColor(m_borderColor.top);
Rect borderRect(screenCoords.topLeft(), screenCoords.width(), m_borderWidth.top); Rect borderRect(screenCoords.topLeft(), screenCoords.width(), m_borderWidth.top);
g_painter.drawFilledRect(borderRect); g_painter.drawFilledRect(borderRect);
} }
// right // right
if(m_borderWidth.right > 0 && m_borderColor.right.aF() != 0.0f) { if(m_borderWidth.right > 0) {
g_painter.setColor(m_borderColor.right); g_painter.setColor(m_borderColor.right);
Rect borderRect(screenCoords.topRight() - Point(m_borderWidth.right - 1, 0), m_borderWidth.right, screenCoords.height()); Rect borderRect(screenCoords.topRight() - Point(m_borderWidth.right - 1, 0), m_borderWidth.right, screenCoords.height());
g_painter.drawFilledRect(borderRect); g_painter.drawFilledRect(borderRect);
} }
// bottom // bottom
if(m_borderWidth.bottom > 0 && m_borderColor.bottom.aF() != 0.0f) { if(m_borderWidth.bottom > 0) {
g_painter.setColor(m_borderColor.bottom); g_painter.setColor(m_borderColor.bottom);
Rect borderRect(screenCoords.bottomLeft() - Point(0, m_borderWidth.bottom - 1), screenCoords.width(), m_borderWidth.bottom); Rect borderRect(screenCoords.bottomLeft() - Point(0, m_borderWidth.bottom - 1), screenCoords.width(), m_borderWidth.bottom);
g_painter.drawFilledRect(borderRect); g_painter.drawFilledRect(borderRect);
} }
// left // left
if(m_borderWidth.left > 0 && m_borderColor.left.aF() != 0.0f) { if(m_borderWidth.left > 0) {
g_painter.setColor(m_borderColor.left); g_painter.setColor(m_borderColor.left);
Rect borderRect(screenCoords.topLeft(), m_borderWidth.left, screenCoords.height()); 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) void UIWidget::drawIcon(const Rect& screenCoords)
{ {
if(m_icon && m_iconColor.aF() != 0.0f) { if(m_icon) {
Rect drawRect; Rect drawRect;
if(m_iconRect.isValid()) { if(m_iconRect.isValid()) {
drawRect = screenCoords; drawRect = screenCoords;

@ -76,7 +76,7 @@ void UIWidget::parseImageStyle(const OTMLNodePtr& styleNode)
void UIWidget::drawImage(const Rect& screenCoords) void UIWidget::drawImage(const Rect& screenCoords)
{ {
if(!m_imageTexture || m_imageColor.aF() == 0.0f || !screenCoords.isValid()) if(!m_imageTexture || !screenCoords.isValid())
return; return;
// cache vertex buffers // cache vertex buffers

Loading…
Cancel
Save