execute a Javascript function on GPU

171 Views Asked by At

I want run a JavaScript function on GPU for fast calculation.
I try use gpu.js

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

My goal is to be able to do some simple array function like "for" "if", or "create variable" to manipulate the pixels of the image on GPU for fast calculation

Below an exemple :
Simple image form canvas with pixel text value

this code should replace all the array value 1 (yellow) by value 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 );

Expected result:
New image variable result, with new pixel value

But in this case it don't work got some error on console like:

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

Maybe it's not the best way to do that, I'm open to any idea or comments.

0

There are 0 best solutions below