vec2, vec3, vec4 support
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.
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.
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.
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.
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
I believe this is solved, can anyone confirm?
Fully supported now in v2 rc 13.