shader-park / shader-park-core

A JavaScript library for creating real-time 2D and 3D shaders. JS -> Shader. https://shaderpark.com/ https://twitter.com/shaderpark

Home Page:https://shaderpark.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposed approach for handling GLSL Builtin Other

torinmb opened this issue · comments

Note: keeping the value of 'mix' as an array allows us to have specific constraints that return a specific dim.
Alternatively, we could write out all the logic and return types with cases, but for readability, this approach could be helpful.

let arg = {
    'mix' : [(a, b, c) => (a.dim === b.dim && (c.dim === 1 || c.dim === a.dim))? a.dim: -1],
};

The goal is to generate this:

function mix(arg_0, arg_1, arg_2) {
    ensureSameDims('mix', arg_0, arg_1);
    if (arg_2.dim !== 1 && arg_2.dim !== arg_0.dim) {
        compileError(`'mix' third argument must be float or match dim of first args`);
    }
    ensureScalar('mix', arg_2);
    arg_0 = tryMakeNum('arg_0');
    arg_1 = tryMakeNum('arg_1');
    arg_2 = tryMakeNum('arg_2');
    return new makeVarWithDims('mix(arg_0, arg_1, arg_2);', arg_0.dim);
}