Code that needs to be optimized

Started by
21 comments, last by data2 16 years, 4 months ago
Wow,

first of all many many thanks for all the suggestions. That's much more than I've expected :-).

I currently implemented special functions for power-of-two textures:
Point2D TilePow2(const Point2D& coord) const{    return Point2D(coord.X & ((int)m_width - 1), coord.Y & (m_height - 1));}Point2D MirrorPow2(const Point2D& coord) const{    Point2D result;        result.X = coord.X & ((int)m_width - 1);   // fast tiling    // handle negative coordinates     if(coord.X < 0)    {        int x = -coord.X - 1;    // note that negative coords do not start with 0        // even tiles are flipped        if(((x / m_width) % 2) == 0)        {            result.X = m_width - result.X - 1;        }        // odd tiles simply need to be shifted into positive range        else        {            result.X += m_width;        }    }    // odd tiles are flipped    else if(((coord.X / m_width) % 2) != 0)    {        result.X = m_width - result.X - 1;    }        result.Y = coord.Y & ((int)m_height - 1);   // fast tiling    // handle negative coordinates     if(coord.Y < 0)    {        int y = -coord.Y - 1;    // note that negative coords do not start with 0        // even tiles are flipped        if(((y / m_height) % 2) == 0)        {            result.Y = m_height - result.Y - 1;        }        // odd tiles simply need to be shifted into positive range        else        {            result.Y += m_height;        }    }    // odd tiles are flipped    else if(((coord.Y / m_height) % 2) != 0)    {        result.Y = m_height - result.Y - 1;    }    return result;}


This already gave me some 10% more performance :-). But still, the divides and modulus in the if-statements are the bottlenecks.


@Adam_42 I tried out the code and it gives exeptions. The implementation is as follows:
Point2D MirrorPow2(const Point2D& coord){    Point2D result;        result.X = coord.X & ((2 << m_powerWidth)  - 1);   // fast tiling    result.Y = coord.Y & ((2 << m_powerHeight) - 1);   // fast tiling    // Mirror X    const int bMirrorX = (result.X << (31 - m_powerWidth)) >> 31;    result.X ^= bMirrorX;    result.X += ((2 << m_powerWidth) + 1) & bMirrorX;    // Mirror Y    const int bMirrorY = (result.Y << (31 - m_powerHeight)) >> 31;    result.Y ^= bMirrorY;    result.Y += ((2 << m_powerHeight) + 1) & bMirrorY;    return result;}


Image size is 128x128, powerWidth/powerHeight is 7, and the input coord is (127/128). The problem is that
result.Y = coord.Y & ((2 << m_powerHeight) - 1);

does not tile. The result still is 128. Any ideas?

Cheers,
data
Advertisement
Hmm, looks like I just got my add off by 1. I think you just need to drop the add 1 in the mirroring code.

Here's an updated version, I've added some comments so it's a bit clearer how it works.

Point2D MirrorPow2(const Point2D& coord){    Point2D result;        result.X = coord.X & ((2 << m_powerWidth)  - 1);   // fast tiling    result.Y = coord.Y & ((2 << m_powerHeight) - 1);   // fast tiling    // Mirror X. bMirrorX will be -1 if it's mirrored, 0 otherwise    const int bMirrorX = (result.X << (31 - m_powerWidth)) >> 31;    // Same as: result.X = (result.X == 0) ? result.X : -(result.X +1);    result.X ^= bMirrorX;    // Same as: if (bMirrorX) result.X += (2 << m_powerWidth);    result.X += (2 << m_powerWidth) & bMirrorX;    // Mirror Y. Same as for X    const int bMirrorY = (result.Y << (31 - m_powerHeight)) >> 31;    result.Y ^= bMirrorY;    result.Y += (2 << m_powerHeight) & bMirrorY;    return result;}
Wow, you're right. And actually it gave me another 15%. That's awesome. Many thanks :-)

This topic is closed to new replies.

Advertisement