Swift inout参数性能

Swift inout parameter performance

我在 Swift 上的表现非常糟糕(低 fps,只绘制了一些纹理)。在 android 上的 java 中,我执行下面这批代码的速度提高了 30 到 40 倍。我在 .mm 文件中有一些用于 opengl 的矩阵数学函数。我通过 Swift(桥接头)使用 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)" );

}
}

对不起大家,我明白了。我的应用在调试模式下 运行。这立即修复了 fps。

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