在 GPU 上执行 Javascript 函数

问题描述 投票:0回答:1

我想在 GPU 上运行 JavaScript 函数以进行快速计算。
我尝试使用 gpu.js

https://gpu.rocks
https://github.com/gpujs/gpu.js

我的目标是能够执行一些简单的数组函数,例如“for”“if”或“创建变量”来操作GPU上图像的像素以进行快速计算

下面是一个例子:
Simple image form canvas with pixel text value

此代码应将所有数组值 1(黄色)替换为值 9

    gpu = new GPU();
    gpu.mode = "gpu";
    
    f_kernel = gpu.createKernel(function(a){
        //a[2][1]             = value of the pixel array[2][1]
        //a[this.thread.y][0] = all the column 0 ?
        //a[0][this.thread.x] = all the row 0 ?
        //return a[this.thread.y][this.thread.x]; return the full 2d array image
    
        const r = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]];
    
        for (var i=0 ; i<a[0][this.thread.x].length ; i++ ){
            for (var j=0 ; j<a[this.thread.y][0].length ; j++ ){
                if ( a[j][i] == 1 ){
                    r[j][i] = 9;
                    }
                else{
                    r[j][i] = a[j][i];
                    }
                }
            }
    
        return r;
        }
    
    array2d = [[1,1,1,1],[1,2,3,1],[1,4,5,1],[1,1,1,1]];//simple image exemple
    result = f_kernel( array2d );

预期结果:
New image variable result, with new pixel value

但在这种情况下它不起作用,在控制台上出现一些错误,例如:

Uncaught TypeError: Cannot read properties of undefined (reading 'type')

也许这不是最好的方法,我愿意接受任何想法或评论。

javascript image kernel gpu gpu.js
1个回答
0
投票

这里同样的错误。

我已经修复过多次了,但是 我没有具体的答案。

这与你的设置输出有关 与您返回的数组不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.