特别是,我正在尝试实现 vector 数学模块来处理 vector 和矩阵。

我最初的计划是:对所有相同类型的对象(例如4x4矩阵)使用大堆,为内部计算保留一些空间,并通过偏移量访问其余矩阵。这是我的代码示例:

'use strict';

function Mat4(stdlib, foreign, heap) {
    'use asm';

    var H = new stdlib.Float32Array(heap);

    function identity(n) {
        n = n|0;

        var offset = 0;
        offset = ((n|0) << 6)|0;

        H[((offset|0)) >> 2] = 1.0;
        H[((offset|0) + 4) >> 2] = 0.0;
        H[((offset|0) + 8) >> 2] = 0.0;
        H[((offset|0) + 12) >> 2] = 0.0;
        H[((offset|0) + 16) >> 2] = 0.0;
        H[((offset|0) + 20) >> 2] = 1.0;
        H[((offset|0) + 24) >> 2] = 0.0;
        H[((offset|0) + 28) >> 2] = 0.0;
        H[((offset|0) + 32) >> 2] = 0.0;
        H[((offset|0) + 36) >> 2] = 0.0;
        H[((offset|0) + 40) >> 2] = 1.0;
        H[((offset|0) + 44) >> 2] = 0.0;
        H[((offset|0) + 48) >> 2] = 0.0;
        H[((offset|0) + 52) >> 2] = 0.0;
        H[((offset|0) + 56) >> 2] = 0.0;
        H[((offset|0) + 60) >> 2] = 1.0;

        return ((offset|0) >> 2)|0;
    }

    return {
        identity: identity
    };
};

var buffer = new ArrayBuffer(4096);
var fArray = new Float32Array(buffer);
var mod = Mat4(window, {}, buffer);

var dmat4 = {
    counter: 0
};

dmat4.identity = function() {
    var offset = mod.identity(dmat4.counter++);
    return fArray.subarray(offset, offset + 16);
};

这似乎是可行的,但似乎比仅创建Float32Array并用值填充要慢得多。我的猜测是,所有偏移量数学都花费大部分时间,并且在编译此asm.js代码时并未对其进行优化。

但是也许我遗漏了一些东西,并且可以改进此代码?

最佳答案

通过以下方式重新编写代码:

'use strict';

function Mat4(stdlib, foreign, heap) {
    'use asm';

    var H = new stdlib.Float32Array(heap);
    var I = new stdlib.Uint8Array(heap);

    function identity() {
        var offset = 0;
        offset = ((I[0]|0 + 16) << 6)|0;
        I[0] = I[0]|0 + 1;

        H[offset >> 2] = 1.0;
        H[(offset + 4) >> 2] = 0.0;
        H[(offset + 8) >> 2] = 0.0;
        H[(offset + 12) >> 2] = 0.0;
        H[(offset + 16) >> 2] = 0.0;
        H[(offset + 20) >> 2] = 1.0;
        H[(offset + 24) >> 2] = 0.0;
        H[(offset + 28) >> 2] = 0.0;
        H[(offset + 32) >> 2] = 0.0;
        H[(offset + 36) >> 2] = 0.0;
        H[(offset + 40) >> 2] = 1.0;
        H[(offset + 44) >> 2] = 0.0;
        H[(offset + 48) >> 2] = 0.0;
        H[(offset + 52) >> 2] = 0.0;
        H[(offset + 56) >> 2] = 0.0;
        H[(offset + 60) >> 2] = 1.0;

        return (offset >> 2)|0;
    }

    return {
        identity: identity
    };
};

var buffer = new ArrayBuffer(65536);
var array = new Float32Array(buffer);
var mod = Mat4(window, {}, buffer);

var dmat4 = {};

dmat4.identity = function() {
    var offset = mod.identity();
    return array.subarray(offset, offset + 16);
};

dmat4.create = dmat4.identity;

dmat4.identity();

现在,它的工作速度仅比glMarix方法慢2倍(我猜这是预期的);

10-07 13:31