some optimizations

This commit is contained in:
Eduardo Bart
2012-06-02 11:43:27 -03:00
parent 4c143f4a33
commit 1c5b906b5b
24 changed files with 120 additions and 105 deletions

View File

@@ -65,6 +65,7 @@ void FrameBuffer::resize(const Size& size)
m_texture = TexturePtr(new Texture(size.width(), size.height(), 4));
m_texture->setSmooth(true);
m_texture->setUpsideDown(true);
if(m_fbo) {
internalBind();
@@ -97,8 +98,8 @@ void FrameBuffer::bind()
internalBind();
Matrix3 projectionMatrix = { 2.0f/m_texture->getWidth(), 0.0f, 0.0f,
0.0f, 2.0f/m_texture->getHeight(), 0.0f,
-1.0f, -1.0f, 1.0f };
0.0f, -2.0f/m_texture->getHeight(), 0.0f,
-1.0f, 1.0f, 1.0f };
g_painter->saveAndResetState();
g_painter->setProjectionMatrix(projectionMatrix);

View File

@@ -79,6 +79,13 @@ void Graphics::init()
// blending is always enabled
glEnable(GL_BLEND);
// face culling may improve performance
/*
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
*/
// determine max texture size
static GLint maxTextureSize = -1;
if(maxTextureSize == -1)

View File

@@ -45,7 +45,7 @@ public:
struct PainterState {
Matrix3 projectionMatrix;
Matrix2 textureMatrix;
Matrix3 textureMatrix;
Color color;
float opacity;
Painter::CompositionMode compositionMode;
@@ -75,7 +75,7 @@ public:
virtual void drawBoundingRect(const Rect& dest, int innerLineWidth = 1) = 0;
virtual void setProjectionMatrix(const Matrix3& projectionMatrix) { m_projectionMatrix = projectionMatrix; }
virtual void setTextureMatrix(const Matrix2& textureMatrix) { m_textureMatrix = textureMatrix; }
virtual void setTextureMatrix(const Matrix3& textureMatrix) { m_textureMatrix = textureMatrix; }
virtual void setColor(const Color& color) { m_color = color; }
virtual void setOpacity(float opacity) { m_opacity = opacity; }
virtual void setCompositionMode(CompositionMode compositionMode);
@@ -87,7 +87,7 @@ public:
void setTexture(const TexturePtr& texture) { setTexture(texture.get()); }
Matrix3 getProjectionMatrix() { return m_projectionMatrix; }
Matrix2 getTextureMatrix() { return m_textureMatrix; }
Matrix3 getTextureMatrix() { return m_textureMatrix; }
Color getColor() { return m_color; }
float getOpacity() { return m_opacity; }
CompositionMode getCompositionMode() { return m_compositionMode; }
@@ -109,7 +109,7 @@ protected:
CoordsBuffer m_coordsBuffer;
Matrix3 m_projectionMatrix;
Matrix2 m_textureMatrix;
Matrix3 m_textureMatrix;
Color m_color;
float m_opacity;
CompositionMode m_compositionMode;

View File

@@ -195,7 +195,7 @@ void PainterOGL1::setProjectionMatrix(const Matrix3& projectionMatrix)
updateGlProjectionMatrix();
}
void PainterOGL1::setTextureMatrix(const Matrix2& textureMatrix)
void PainterOGL1::setTextureMatrix(const Matrix3& textureMatrix)
{
// avoid re-updating texture matrix
if(m_textureMatrix == textureMatrix)
@@ -246,10 +246,10 @@ void PainterOGL1::updateGlProjectionMatrix()
void PainterOGL1::updateGlTextureMatrix()
{
float glTextureMatrix[] = {
m_textureMatrix(1,1), m_textureMatrix(1,2), 0.0f, 0.0f,
m_textureMatrix(2,1), m_textureMatrix(2,2), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
m_textureMatrix(1,1), m_textureMatrix(1,2), 0.0f, 0.0f,
m_textureMatrix(2,1), m_textureMatrix(2,2), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
m_textureMatrix(3,1), m_textureMatrix(3,2), 0.0f, m_textureMatrix(3,3),
};
setMatrixMode(MatrixTexture);

View File

@@ -57,7 +57,7 @@ public:
void setMatrixMode(MatrixMode matrixMode);
void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix2& textureMatrix);
void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color);
void setOpacity(float opacity);

View File

@@ -28,13 +28,13 @@ static const std::string glslMainVertexShader = "\n\
static const std::string glslMainWithTexCoordsVertexShader = "\n\
attribute highp vec2 a_texCoord;\n\
uniform highp mat2 textureMatrix;\n\
uniform highp mat3 textureMatrix;\n\
varying highp vec2 texCoord;\n\
highp vec4 calculatePosition();\n\
void main()\n\
{\n\
gl_Position = calculatePosition();\n\
texCoord = textureMatrix * a_texCoord;\n\
texCoord = textureMatrix * vec3(a_texCoord,1);\n\
}\n";
static std::string glslPositionOnlyVertexShader = "\n\

View File

@@ -28,7 +28,7 @@
PainterShaderProgram::PainterShaderProgram()
{
m_startTime = g_clock.time();
m_startTime = g_clock.seconds();
m_opacity = 1;
m_color = Color::white;
m_time = 0;
@@ -36,7 +36,7 @@ PainterShaderProgram::PainterShaderProgram()
bool PainterShaderProgram::link()
{
m_startTime = g_clock.time();
m_startTime = g_clock.seconds();
bindAttributeLocation(VERTEX_ATTR, "a_vertex");
bindAttributeLocation(TEXCOORD_ATTR, "a_texCoord");
if(ShaderProgram::link()) {
@@ -72,7 +72,7 @@ void PainterShaderProgram::setProjectionMatrix(const Matrix3& projectionMatrix)
m_projectionMatrix = projectionMatrix;
}
void PainterShaderProgram::setTextureMatrix(const Matrix2& textureMatrix)
void PainterShaderProgram::setTextureMatrix(const Matrix3& textureMatrix)
{
if(textureMatrix == m_textureMatrix)
return;
@@ -104,7 +104,7 @@ void PainterShaderProgram::setOpacity(float opacity)
void PainterShaderProgram::updateTime()
{
float time = g_clock.timeElapsed(m_startTime);
float time = g_clock.seconds() - m_startTime;
if(m_time == time)
return;

View File

@@ -52,7 +52,7 @@ public:
bool link();
void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix2& textureMatrix);
void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color);
void setOpacity(float opacity);
void updateTime();
@@ -63,7 +63,7 @@ private:
Color m_color;
float m_opacity;
Matrix3 m_projectionMatrix;
Matrix2 m_textureMatrix;
Matrix3 m_textureMatrix;
float m_time;
};

View File

@@ -27,7 +27,7 @@
ParticleSystem::ParticleSystem()
{
m_finished = false;
m_lastUpdateTime = g_clock.time();
m_lastUpdateTime = g_clock.seconds();
}
bool ParticleSystem::load(const OTMLNodePtr& node)
@@ -79,10 +79,10 @@ void ParticleSystem::update()
}
// check time
float elapsedTime = g_clock.timeElapsed(m_lastUpdateTime);
float elapsedTime = g_clock.seconds() - m_lastUpdateTime;
if(elapsedTime < delay)
return;
m_lastUpdateTime = g_clock.time() - std::fmod(elapsedTime, delay);
m_lastUpdateTime = g_clock.seconds() - std::fmod(elapsedTime, delay);
for(int i = 0; i < elapsedTime / delay; ++i) {

View File

@@ -53,7 +53,6 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
m_size.resize(width, height);
// convert texture pixel data to power of two size, only required for OpenGL 1.5 or older
Size glSize;
std::vector<uint8> tmp;
if(!g_graphics.canUseNonPowerOfTwoTextures()) {
int glWidth = 1;
@@ -64,7 +63,7 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
while(glHeight < height)
glHeight = glHeight << 1;
if(m_size != glSize && pixels) {
if(m_size != m_glSize && pixels) {
tmp.resize(glHeight*glWidth*channels, 0);
for(int y=0; y<height; ++y)
for(int x=0; x<width; ++x)
@@ -73,15 +72,14 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
pixels = &tmp[0];
}
glSize.resize(glWidth, glHeight);
m_glSize.resize(glWidth, glHeight);
} else
glSize = m_size;
m_glSize = m_size;
m_transformMatrix = { 1.0f/glSize.width(), 0.0f,
0.0f, 1.0f/glSize.height() };
setupTranformMatrix();
// checks texture max size
if(std::max(glSize.width(), glSize.height()) > g_graphics.getMaxTextureSize()) {
if(std::max(m_glSize.width(), m_glSize.height()) > g_graphics.getMaxTextureSize()) {
g_logger.error(stdext::format("loading texture with size %dx%d failed, "
"the maximum size allowed by the graphics card is %dx%d,"
"to prevent crashes the texture will be displayed as a blank texture",
@@ -115,7 +113,7 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
}
// load pixels into gl memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glSize.width(), glSize.height(), 0, format, GL_UNSIGNED_BYTE, pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_glSize.width(), m_glSize.height(), 0, format, GL_UNSIGNED_BYTE, pixels);
GLint texParam = GL_REPEAT;
if(g_graphics.canUseClampToEdge())
@@ -182,6 +180,14 @@ void Texture::setSmooth(bool smooth)
setupFilters();
}
void Texture::setUpsideDown(bool upsideDown)
{
if(m_upsideDown == upsideDown)
return;
m_upsideDown = upsideDown;
setupTranformMatrix();
}
void Texture::generateSoftwareMipmaps(std::vector<uint8> inPixels)
{
bind();
@@ -261,3 +267,16 @@ void Texture::setupFilters()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
}
void Texture::setupTranformMatrix()
{
if(m_upsideDown) {
m_transformMatrix = { 1.0f/m_glSize.width(), 0.0f, 0.0f,
0.0f, -1.0f/m_glSize.height(), 0.0f,
0.0f, 1.0f, 1.0f };
} else {
m_transformMatrix = { 1.0f/m_glSize.width(), 0.0f, 0.0f,
0.0f, 1.0f/m_glSize.height(), 0.0f,
0.0f, 0.0f, 1.0f };
}
}

View File

@@ -46,26 +46,30 @@ public:
/// Activate texture anti-aliasing giving a better look when they are resized
void setSmooth(bool smooth);
void setUpsideDown(bool upsideDown);
GLuint getId() { return m_textureId; }
int getWidth() { return m_size.width(); }
int getHeight() { return m_size.height(); }
const Size& getSize() { return m_size; }
const Matrix2& getTransformMatrix() { return m_transformMatrix; }
const Matrix3& getTransformMatrix() { return m_transformMatrix; }
bool isEmpty() { return m_textureId == 0; }
bool hasMipmaps() { return m_hasMipmaps; }
protected:
void setupFilters();
void setupTranformMatrix();
GLuint internalLoadGLTexture(uchar* pixels, int channels, int w, int h);
GLuint m_textureId;
Size m_size;
Matrix2 m_transformMatrix;
Size m_glSize;
Matrix3 m_transformMatrix;
Boolean<false> m_hasMipmaps;
Boolean<false> m_smooth;
Boolean<false> m_upsideDown;
};
#endif