Swift inout参数性能

时间:2015-02-12 16:08:49

标签: ios objective-c swift opengl-es

我在使用Swift时表现糟糕(低fps只绘制了一些纹理)。在android上的java中,我执行下面的批处理代码的速度提高了30到40倍。我在.mm文件中有一些opengl的矩阵数学函数。我通过Swift(bridging-header)使用Matrix.mm文件中的函数。这些矩阵运算似乎比opengl绘制调用运行得更慢。与我的Android版完全相反。即使是opengl绘图调用似乎比android慢大约3到4倍。是否有必须在应用程序上启用设置以使其更快(关闭一些隐藏的调试设置或其他内容)?

Matrix.mm函数(样本)

+(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;
    }
}

在Swift中调用矩阵函数(比Android慢40倍)

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

}
}

1 个答案:

答案 0 :(得分:0)

对不起家伙我明白了。我的应用程序在调试模式下运行。这马上修复了fps。

产品 - &gt;方案 - &gt;编辑场景 - &gt;信息标签