gpu.js icon indicating copy to clipboard operation
gpu.js copied to clipboard

vec2, vec3, vec4 support

Open fuzzie360 opened this issue 10 years ago • 6 comments

Vectors are very useful for graphical computations so it would be very useful to support vec variables and function arguments and function returns.

When it's implemented it could look like:

function kernel() {
    var v = this.vec2( 1, 2 );
    var mag = Math.sqrt(v.x*v.x + v.y*v.y);
}

The "this" context variable can provide the vec2 function in fallback mode just like this.thread.x and etc.

Challenges:

  • Appear to need type inference for variable v which might not be feasible. Either that or commented annotations like: /*vec3*/var v = ... which might look very ugly.

fuzzie360 avatar Feb 19 '16 11:02 fuzzie360

I think type inference for variables for now is a good target to work towards. We can work on type inference for functions if we move on to higher order functions.

sawman avatar Feb 19 '16 15:02 sawman

As of now, the addFunction, allows one to overwrite the return type.

    ///
    /// Function: addFunction
    ///
    /// Adds additional functions, that the kernel may call.
    ///
    /// Parameters: 
    ///     jsFunction      - {JS Function}  JS Function to do conversion   
    ///     paramTypeArray  - {[String,...]} Parameter type array, assumes all parameters are "float" if null
    ///     returnType      - {String}       The return type, assumes "float" if null
    ///
    /// Retuns:
    ///     {GPU} returns itself
    ///
    function addFunction( jsFunction, paramTypeArray, returnType  ) {

This is without the "auto-voodoo" type inference. A similar implementation can be done for the kernel creation function.

Note that internally the kernal also use the FunctionBuilder.addFunction call. But has isRootKernel set to true for some conditional voodoo.

Successful implementation of voodoo can jack into this.

PicoCreator avatar Feb 24 '16 06:02 PicoCreator

For now we go with this.vec2(0,1) to declare vecs bah, so that in jsland we can have a.x, a.y type syntax. I'll go with that and implement the proper objects.

sawman avatar Apr 29 '16 08:04 sawman

Okay let's formalize this.

Let's support the following operations:

this.vec3(1) => (1,1,1)
this.vec3(1,2,3) + this.vec3(1,2,3) => (2,4,6)
this.vec3(1,2,3) - this.vec3(1,2,3) => (0,0,0)
this.vec3(1,2,3) + 1 => (2,3,4)
this.vec3(1,2,3) * 2 => (2,4,6)
this.vec4(1, this.vec3(2,3,4)) => (1,2,3,4)
this.vec3(1,2,3).r => 1
this.vec3(1,2,3).x => 1
this.vec3(1,2,3).s => 1
this.vec3(1,2,3).zyx => (3,2,1)
var x = this.vec3(1,2,3)
x.zyx = x.xyz; => (3,2,1)
x.xxx => (1,1,1)

And of course, bonus would be

var x = [1,2,3] => auto vec3

fuzzie360 avatar Apr 30 '16 08:04 fuzzie360

I believe this is solved, can anyone confirm?

robertleeplummerjr avatar Jun 29 '17 23:06 robertleeplummerjr

Fully supported now in v2 rc 13.

robertleeplummerjr avatar May 22 '19 22:05 robertleeplummerjr