2

I am having horrendous performance with Swift (low fps with only a few textures being drawn). In java on android I am executing the batch of code below between 30 and 40 times faster. I have some matrix math functions for opengl in a .mm file. I make use of the functions in the Matrix.mm file through Swift (bridging-header). These matrix operations appear to be running even slower than the opengl draw calls. Totally opposite from my android version. Even the opengl drawing calls seem to be around 3 to 4 times slower than android. Is there a setting that has to be enabled on the app to make it faster (turn off some hidden debug settings or something)?

Matrix.mm functions (sample)

+(void) translateM:(mFloat*) m  mOffset:(int) mOffset x:(mFloat) x y:(mFloat) y z:(mFloat) z {
    for (int i=0 ; i<4 ; i++) {
        int mi = mOffset + i;
        m[12 + mi] += m[mi] * x + m[4 + mi] * y + m[8 + mi] * z;
    }
}


#define I(_i, _j) ((_j)+ 4*(_i))
+(void) multiplyMM:(mFloat*) result resultOffset:(int) resultOffset lhs:(const mFloat*) lhs lhsOffset:(int) lhsOffset  rhs:(const mFloat*) rhs  rhsOffset:(int) rhsOffset{
    result += resultOffset;
    lhs += lhsOffset;
    rhs += rhsOffset;

    for (int i=0 ; i<4 ; i++) {
        const mFloat rhs_i0 = rhs[ I(i,0) ];
        mFloat ri0 = lhs[ I(0,0) ] * rhs_i0;
        mFloat ri1 = lhs[ I(0,1) ] * rhs_i0;
        mFloat ri2 = lhs[ I(0,2) ] * rhs_i0;
        mFloat ri3 = lhs[ I(0,3) ] * rhs_i0;
        for (int j=1 ; j<4 ; j++) {
            const mFloat rhs_ij = rhs[ I(i,j) ];
            ri0 += lhs[ I(j,0) ] * rhs_ij;
            ri1 += lhs[ I(j,1) ] * rhs_ij;
            ri2 += lhs[ I(j,2) ] * rhs_ij;
            ri3 += lhs[ I(j,3) ] * rhs_ij;
        }
        result[ I(i,0) ] = ri0;
        result[ I(i,1) ] = ri1;
        result[ I(i,2) ] = ri2;
        result[ I(i,3) ] = ri3;
    }
}

+(void) multiplyMV:(mFloat*) resultVec resultVecOffset:(int) resultVecOffset lhsMat:(const mFloat*) lhsMat lhsMatOffset:(int) lhsMatOffset rhsVec:(const mFloat*) rhsVec rhsVecOffsetint:(int) rhsVecOffset{

    resultVec += resultVecOffset;
    lhsMat += lhsMatOffset;
    rhsVec += rhsVecOffset;

    [Matrix mx4transform:rhsVec[0] y:rhsVec[1] z:rhsVec[2] w:rhsVec[3] pM:lhsMat pDest:resultVec];

}
+(void) setIdentityM:(mFloat*) sm smOffset:(int) smOffset {
    for (int i=0 ; i<16 ; i++) {
        sm[smOffset + i] = 0;
    }
    for(int i = 0; i < 16; i += 5) {
        sm[smOffset + i] = 1.0f;
    }
}

Matrix functions being called in Swift (40 times slower than Android)

func draw(){
var s1:Double = Engine.getCurrentTimeMillis();

inPoint[2] = -1;
inPoint[3] = 1;

Matrix.setIdentityM(&mModelMatrix, smOffset: 0);
Matrix.translateM(&mModelMatrix, mOffset: 0, x: positionX, y: positionY, z: positionZ);
Matrix.rotateM(&mModelMatrix, mOffset: 0, a: angle, x: 0.0, y: 0.0, z: 1.0);

inPoint[0] = VERTEX_DATA[0];
inPoint[1] = VERTEX_DATA[1];
Matrix.multiplyMV(&outPoint, resultVecOffset: 0, lhsMat: mModelMatrix, lhsMatOffset: 0, rhsVec: inPoint, rhsVecOffsetint: 0);
rotatedPoints[0] = outPoint[0];
rotatedPoints[1] = outPoint[1];

inPoint[0] = VERTEX_DATA[4];
inPoint[1] = VERTEX_DATA[5];
Matrix.multiplyMV(&outPoint, resultVecOffset: 0, lhsMat: mModelMatrix, lhsMatOffset: 0, rhsVec: inPoint, rhsVecOffsetint: 0);
rotatedPoints[2] = outPoint[0];
rotatedPoints[3] = outPoint[1];

inPoint[0] = VERTEX_DATA[8];
inPoint[1] = VERTEX_DATA[9];
Matrix.multiplyMV(&outPoint, resultVecOffset: 0, lhsMat: mModelMatrix, lhsMatOffset: 0, rhsVec: inPoint, rhsVecOffsetint: 0);
rotatedPoints[4] = outPoint[0];
rotatedPoints[5] = outPoint[1];

inPoint[0] = VERTEX_DATA[12];
inPoint[1] = VERTEX_DATA[13];
Matrix.multiplyMV(&outPoint, resultVecOffset: 0, lhsMat: mModelMatrix, lhsMatOffset: 0, rhsVec: inPoint, rhsVecOffsetint: 0);
rotatedPoints[6] = outPoint[0];
rotatedPoints[7] = outPoint[1];
var s1End:Double = Engine.getCurrentTimeMillis() - s1;

if (isVisible()) {

    test.drawCount++;

    Matrix.multiplyMM(&mMVPMatrix, resultOffset: 0, lhs: mViewMatrix, lhsOffset: 0, rhs: mModelMatrix, rhsOffset: 0);

    Matrix.multiplyMM(&mMVPMatrix, resultOffset: 0,
        lhs: mProjectionMatrix, lhsOffset: 0, rhs: mMVPMatrix, rhsOffset: 0);


    var s2:Double = Engine.getCurrentTimeMillis();
    ResourceManager.props.textureShaderProgram.useProgram();
    bindData(ResourceManager.props.textureShaderProgram);
    ResourceManager.props.textureShaderProgram.setUniforms(mMVPMatrix, textureId: ResourceManager.getTexture(texture));

    glDrawElements(GLenum(GL_TRIANGLES), GLsizei(indices.count), GLenum(GL_UNSIGNED_SHORT), indices);

    var s2End = Engine.getCurrentTimeMillis() - s2;

    println("Matrix:  \(s1End)  GL: \(s2End)" );

}
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
NJGUY
  • 2,045
  • 3
  • 23
  • 43
  • You should try the latest swift 1.2 beta, I heard it's a lot better in terms of performance - maybe it's just that – Antonio Feb 12 '15 at 16:16
  • [This question](http://stackoverflow.com/questions/24101718/swift-performance-sorting-arrays) mentions a few compiler flags you can set to specify the compiler's optimization level. Maybe they'll help you? Things also may have changed since Swift 1.2 came out yesterday. – Morgan Chen Feb 12 '15 at 16:26
  • I am not having compiler issues, I'm having Swift runtime issues. The app is slow a hell. – NJGUY Feb 12 '15 at 16:31
  • Have you tried converting the Objective-C functions to Swift? Perhaps encapsulate the whole concept into a Matrix class instead? I'm thinking that the gyrations around passing Array's in as `float*`'s may be causing an issue. – David Berry Feb 12 '15 at 16:52
  • David, I tried but Swift can't take it. The compiler freezes up. Swift can only do basic short math operations. It is a very immature language. – NJGUY Feb 12 '15 at 17:08
  • Possibly because Swift uses NSArray and the Matrix.mm excepts a Float32*. This is a c array I'm guessing so maybe Swift has to convert NSArray to a c array? – NJGUY Feb 12 '15 at 17:15

1 Answers1

0

Sorry guys I figured it out. My app was running on debug mode. This fixed the fps right away.

Product->Scheme->Edit Sceme-> Info tab

NJGUY
  • 2,045
  • 3
  • 23
  • 43