SOLVED: GPU.js Error compiling fragment shader: no matching overloaded function found

55 Views Asked by At

I'm new to GPU.js and shaders in general, and I decided to make a raymarcher with the SDFs from here. I can render spheres ok, but when I tried to add a box, I got an error saying:

Error: Error compiling fragment shader: ERROR: 0:467: 'dotS' : no matching overloaded function found
ERROR: 0:520: 'assign' : cannot convert from 'const float' to 'lowp int'
ERROR: 0:522: 'assign' : cannot convert from 'const float' to 'lowp int'

at WebGL2Kernel.build (https://cdn.jsdelivr.net/npm/gpu.js@latest/dist/gpu-browser.min.js:28:299236)
at WebGL2Kernel.t (https://cdn.jsdelivr.net/npm/gpu.js@latest/dist/gpu-browser.min.js:28:379123)
at n (https://cdn.jsdelivr.net/npm/gpu.js@latest/dist/gpu-browser.min.js:28:379450)
at file:///<location>/main.js:108:3

here is my code:

/*
Vectors:
represented with [x,y] or [x,y,z]
in lists they are [x1,y1, x2,y2, x3,y3, ...]
*/

var rays = [];
var size = innerHeight/2;
try {
  const gpu = new GPU.GPU();
  const setupRays = gpu.createKernel(function (size) {
    if(this.thread.x%2==0) {
      return this.thread.x/size - 1 + 1/size;
    } else {
      return this.thread.y/size*2 - 1 + 1/size;
    }
  })
  .setOutput([size*4,size*2]);
  rays = setupRays(size*2);

  var shapes = [0,0,0,0,1,  0,1,0,0,0.5, 1,-1,-1,0,1,1,1];

  const march = gpu.createKernel(function (rays,shapes) {
    // Math
    function dotP(a,b) {return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]}
    function dotS(v)   {return dotP(v,v)}
    function ndot(a,b) {return a[0]*b[0] - a[1]*b[1]}
    function len(v)    {return Math.sqrt(dotS(v))}
    function add(a,b)  {return [a[0]+b[0], a[1]+b[1], a[2]+b[2]]}
    function sub(a,b)  {return [a[0]-b[0], a[1]-b[1], a[2]-b[2]]}
    function mult(v,n) {return [v[0]*n, v[1]*n, v[2]*n]}
    function div(v,n)  {return [v[0]/n, v[1]/n, v[2]/n]}
    function dist(a,b) {return len(sub(b,a))}
    function abs(v)    {return [Math.abs(v[0]),Math.abs(v[1]),Math.abs(v[2])]}
    function maxN(v,n) {return [Math.max(v[0],n),Math.max(v[1],n),Math.max([v[2],n])]}
    function minN(v,n) {return [Math.min(v[0],n),Math.min(v[1],n),Math.min([v[2],n])]}
    function maxV(a,b) {return [Math.max(a[0],b[0]),Math.max(a[1],b[1]),Math.max(a[2],b[2])]}
    function minV(a,b) {return [Math.min(a[0],b[0]),Math.min(a[1],b[1]),Math.min(a[2],b[2])]}
    
    // SDFs
    // 0 pos[] rad[]
    function sphere(p,r) {return len(p)-r}
    // 1 pos[] size[]
    function box(p,b) {
      let q = sub(abs(p),b);
      return len(maxN(q,0)) + Math.min(Math.max(q[0],q[1],q[2]), 0);
    }

    // SDF Mods
    function round(dst,r) {return dst-r;}
    function combine(dst1,dst2) {return Math.min(dst1,dst2)}

    let ray = [rays[this.thread.y] [this.thread.x*2], rays[this.thread.y] [this.thread.x*2+1]];
    let dir = [ray[0],ray[1],2];
    dir = div(dir,len(dir));
    let collision = 0.3;
    let pos = [0,0,-5];
    
    for(let step = 0; step < 20; step++) {
      //calc min dist
      let mDist = 999.0;
      let shape = 0;
      for(let i = 0; i < this.constants.shapesLen; i++) {
        var s = 999.0;
        if(shapes[i] == 0) {
          s = sphere(sub(pos,[shapes[i+1],shapes[i+2],shapes[i+3]]), shapes[i+4]);
          i+=4;
        } else if (shapes[i] == 1) {
          s = box(sub(pos,[shapes[i+1],shapes[i+2],shapes[i+3]]), [shapes[i+4],shapes[i+5],shapes[i+6]]);
          i+=6;
        } else {
          break;
        }
        if(s<mDist) {
          mDist = s;
          shape = i;
        }
      }
      if(mDist <= 0.03 && mDist >= 0) {
        collision = shape/this.constants.shapesLen;
        break;
      }
      pos = add(pos,mult(dir,mDist));
      if(dist(pos,[0,0,-5]) > 100) {
        break;
      }
    }
    this.color(collision,0,0);
  })
  .setOutput([size*2,size*2])
  .setGraphical(true)
  .setConstants({shapesLen:shapes.length});
  march(rays,shapes)
  document.getElementsByTagName("body")[0].appendChild(march.canvas);
} catch(error) {
  console.log("error:");
  console.error(error.stack);
}

I don't know why the dotS function is having trouble, it seems to be in every way just like the others. I've tried looking through other Stack Overflow problems like this, but none of the solutions help for me. I have tried replacing dotS(v) with dotP(v,v), and it gives the same error with dotP. In case it helps, I'm using Eruda as my console since I am on the web on a managed computer. If anyone knows an answer to this problem please tell me.

edit: I changed the dotS use to its definition (v[0]*v[0]+v[1]...) and it still has a similar error, without the no matching overloaded function found.

0

There are 0 best solutions below