关于ios:Swift inout参数性能

Swift inout parameter performance

我的表现很糟糕,速度很快(低fps,只有一些纹理被绘制)。在Android上的爪哇,我正在执行一批30到40倍的代码。我在.mm文件中有一些OpenGL的矩阵数学函数。我通过swift(桥接头)使用matrix.mm文件中的函数。这些矩阵操作的运行速度似乎比OpenGL Draw调用还要慢。完全相反,从我的Android版本。甚至OpenGL绘图调用似乎比Android慢3到4倍。应用程序上是否必须启用某个设置才能使其更快(关闭某些隐藏的调试设置或其他设置)?

matrix.mm函数(示例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
+(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倍)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)" );

}
}


抱歉,伙计们,我知道了。我的应用程序正在调试模式下运行。这立刻修复了FPS。

产品->方案->编辑SCEME->信息选项卡