3

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

我最初的计划如下:为所有同类对象(例如 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 代码时没有进行优化。

但也许我遗漏了一些东西,这段代码可以改进吗?

4

1 回答 1

1

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

'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 倍(我猜这是预期的);

于 2013-05-23T06:18:53.933 回答