gpujs / gpu.js

GPU Accelerated JavaScript
https://gpu.rocks
MIT License
15.1k stars 652 forks source link

Array(2) vs ArrayTexture(2) issues with addNativeFunction #656

Open blakej11 opened 3 years ago

blakej11 commented 3 years ago

A GIF or MEME to give some spice of the internet

What is wrong?

I'm trying to use addNativeFunction with textures as inputs, and running into typechecking issues in the transpiler process.

Here is my sample code, with some parts commented out:

const { GPU } = require('gpu.js');
const gpu     = new GPU({ mode: 'headlessgl' });

gpu.addNativeFunction('doSample',
  `vec2 doSample(in sampler2D t, in vec2 uv) {
    return (vec2(texture2D(t, uv)));
  }`
  // , { argumentTypes: { t: 'ArrayTexture(2)', uv: 'Array(2)' } }
);

// gpu.addFunction(function doSample(texture, xy) {
//   return (texture[xy[1]][xy[0]]);
// });

const sampleTexture = gpu.createKernel(function (texture, x, y) {
  return doSample(texture, [x, y]);
}).setPipeline(true).setOutput([1]);

const makeTexture = gpu.createKernel(function() {
  return [this.thread.y, this.thread.x];
}).setPipeline(true).setOutput([16, 16]);
const texture = makeTexture();

for (x = 0.0; x <= 1.0; x += 0.1) {
  z = sampleTexture(texture, x, 0.0);
  console.log("sampleTexture(" + x + ", " + 0.0 + ") = " + z.toArray());
}

When I compile this as-is, I get:

Error: Unhandled argument combination of ArrayTexture(2) and Array(2) for argument named "texture" on line 2, position 1:
 doSample(texture, [x, y])

If I uncomment out the argumentTypes line in the call to addNativeFunction, I get:

Error: Error compiling fragment shader: ERROR: 0:508: 'doSample' : no matching overloaded function found
  ERROR: 0:508: 'assign' :  cannot convert from 'const float' to 'lowp 2-component vector of float'

If I use the gpu.addFunction variant of doSample instead of the addNativeFunction variant, it compiles and runs, although that doesn't actually implement the interpolation that GLSL's texture2D does (which is why I need to use addNativeFunction).

Where does it happen?

I'm running this on MacOS 10.13, using node, and gpu.js version 2.10.6.

How do we replicate the issue?

See above.

How important is this (1-5)?

Without being able to do this, I don't believe I'll be able to use gpu.js, so for me personally this is a 5.

Expected behavior (i.e. solution)

It should print something like:

sampleTexture(0, 0) = 0,0
sampleTexture(0.1, 0) = 0,0.1
sampleTexture(0.2, 0) = 0,0.2
sampleTexture(0.3, 0) = 0,0.3
sampleTexture(0.4, 0) = 0,0.4
sampleTexture(0.5, 0) = 0,0.5
sampleTexture(0.6, 0) = 0,0.6
sampleTexture(0.7, 0) = 0,0.7
sampleTexture(0.8, 0) = 0,0.8
sampleTexture(0.9, 0) = 0,0.9
sampleTexture(1.0, 0) = 0,1

while using addNativeFunction.