optimizations

This commit is contained in:
Eduardo Bart 2012-04-05 16:08:46 -03:00
parent 49727c573f
commit 4f15da695c
10 changed files with 47 additions and 23 deletions

View File

@ -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);
}

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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);
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);
}

View File

@ -59,7 +59,8 @@ public:
private:
DrawMode m_drawMode;
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

View File

@ -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());

View File

@ -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;

View File

@ -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