未捕获的RangeError

未捕获的RangeError

我正在尝试为4x4矩阵乘法运行C++函数。 2天后,它终于可以使用了,但是效果不理想。

通常会将参数反馈给函数,然后返回以下行:

dataHeap2.set( new Uint8Array(data2.buffer) );

产生错误“未捕获的RangeError:源太大”

乍一看,它只是一个具有16个元素的普通Float32Array,但在查看其内部的缓冲区大小后,它似乎有所不同
console.log(data2.buffer.bufferLength);

结果不是期望的64个字节,而是庞大的数字,例如3342345。这是问题吗?我发现了一种通过手动复制值(如下所示)并将其返回的解决方法,然后问题消失了。不幸的是,这使我的代码比直接在缓冲区上运行要慢得多。
// bad solution - which works
    for(var i = 0; i < 16; i++) {
        dataTarget[i] = result[i];
    }

希望今晚能找到一个更好的解决方案,并且希望继续使用此函数,因为它使用的是使用ASM.JS + SIMD的C++编译代码来对多个字符进行动画处理。在纯JavaScript中,它仍然太慢。这是整个功能。我很确定data2正在使用Emscripten的HEAP,并且我想避免它。
matrix4multiply = function(data, data2) {
    // Import function from Emscripten generated file
    var mmul_vec4 = Module.cwrap(
      'mmul_vec4', 'number', ['number', 'number', 'number']
    );

    var dataTarget = new Float32Array(16);

    // Get data byte size, allocate memory on Emscripten heap, and get pointer
    var nDataBytes = dataTarget.length * dataTarget.BYTES_PER_ELEMENT;

    // First matrix copy data to Emscripten heap
    var dataPtr = Module._malloc(nDataBytes);
    var dataPtr2 = Module._malloc(nDataBytes);
    var dataPtr3 = Module._malloc(nDataBytes);

    var dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, nDataBytes);
    dataHeap.set( new Uint8Array(data.buffer) );

    // second matrix allocate and copy to emscripten's heap
    var dataHeap2 = new Uint8Array(Module.HEAPU8.buffer, dataPtr2, nDataBytes);
    dataHeap2.set( new Uint8Array(data2.buffer) );

    // target heap
    var targetHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr3, nDataBytes);
    targetHeap.set( new Uint8Array(dataTarget.buffer) );

    // Call the function by passing a number pointing to the byte location of
    // the array of pointers on the Emscripten heap.  Emscripten knows what to do!
    mmul_vec4(dataHeap.byteOffset, dataHeap2.byteOffset, targetHeap.byteOffset);

    // get result
    var result = new Float32Array(targetHeap.buffer, targetHeap.byteOffset, 16);

    // bad solution - which works
    //for(var i = 0; i < 16; i++) {
    //    dataTarget[i] = result[i];
    //}

    // Free memory
    Module._free(dataHeap.byteOffset);
    Module._free(dataHeap2.byteOffset);
    Module._free(targetHeap.byteOffset);

    return result;
}

编辑:简化版本不必担心malloc等
    new Uint8Array(Module.HEAPU8.buffer, this.dataPtr, 64).set( new Uint8Array(data.buffer, data.byteOffset, 64) );

    // second matrix allocate and copy to emscripten's heap
    new Uint8Array(Module.HEAPU8.buffer, this.dataPtr + 72, 64).set( new Uint8Array(data2.buffer, data2.byteOffset, 64) );

    // multiply first two parameters and return in the last one
    this.mmul_vec4(this.dataPtr, this.dataPtr + 72, this.dataPtr + 144);

    // like that it works, unfotunately copying is being made here
    return new Float32Array(Module.HEAPU8.buffer.slice(this.dataPtr + 144, this.dataPtr + 208));
    // with this version (if uncommented) there's just white screen(but it looks like the game is working.
    //return new Float32Array(Module.HEAPU8.buffer, this.dataPtr + 144, 16);

最佳答案

要使用头64个字节的data2,请指定偏移量和长度。

dataHeap2.set( new Uint8Array(data2.buffer, data2.byteOffset, nDataBytes) );

关于javascript - Emscripten未捕获的RangeError : Source is too large,多个Float32Arrays,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31550198/

10-09 16:06