特别是,我正在尝试实现矢量数学模块来处理向量和矩阵。
我的初步计划如下:对所有相同类型的对象使用大堆(例如,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代码时没有进行优化。
但也许我错过了一些东西,这段代码可以改进吗?
答案 0 :(得分: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倍(这是预期的,我猜);