简体   繁体   中英

Swift inout parameter performance

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)" );

}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM