Skip to content

Commit

Permalink
feat(shader-ast): add reciprocal() syntax sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 14, 2021
1 parent 998cf35 commit c710d81
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/shader-ast/src/ast/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,22 @@ export function modi(l: Term<any> | number, r: Term<any> | number): Op2<any> {
);
}

/**
* Syntax sugar for `-x`.
*
* @param val
*/
export const neg = <T extends Prim | Int | IVec | Mat>(val: Term<T>) =>
op1("-", val);

/**
* Syntax sugar for `1 / x`.
*
* @param val
*/
export const reciprocal = <T extends Prim>(val: Term<T>): Op2<T> =>
op2("/", 1, val);

/**
* Multiply-add: a * b + c. The `b` and `c` terms must be compatible with `a`.
*
Expand Down Expand Up @@ -260,10 +273,10 @@ export const not = (val: BoolTerm) => op1("!", val);
export const or = (a: BoolTerm, b: BoolTerm) => op2("||", a, b);
export const and = (a: BoolTerm, b: BoolTerm) => op2("&&", a, b);

const cmp = (op: ComparisonOperator) => <A extends Comparable, B extends A>(
a: Term<A>,
b: Term<B>
): BoolTerm => op2(op, a, b, "bool");
const cmp =
(op: ComparisonOperator) =>
<A extends Comparable, B extends A>(a: Term<A>, b: Term<B>): BoolTerm =>
op2(op, a, b, "bool");

export const eq = cmp("==");
export const neq = cmp("!=");
Expand Down

0 comments on commit c710d81

Please sign in to comment.