Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shader-ast] Allow type inference of defn with arbitrary number of arguments #440

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/shader-ast-stdlib/src/math/additive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export const additive = <T extends Prim>(
n,
add(
n,
mul(amp, fn(<any>add(<any>pos, mul(i, <any>shift))))
mul(amp, fn(add(pos, mul(i, shift))))
)
),
assign(amp, mul(amp, decay)),
assign(pos, <any>mul(<any>pos, 2)),
assign(pos, mul(pos, 2)),
]
),
ret(n),
Expand Down
4 changes: 2 additions & 2 deletions packages/shader-ast-stdlib/src/math/magsq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { F } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { dot } from "@thi.ng/shader-ast/builtin/math";

const $ = (n: 2 | 3 | 4) =>
defn(F, `magSq${n}`, [<any>`vec${n}`], (v) => [ret(dot(v, v))]);
const $ = <N extends 2 | 3 | 4>(n: N) =>
defn(F, `magSq${n}`, [`vec${n}`], (v) => [ret(dot(v, v))]);

export const magSq2: TaggedFn1<"vec2", "float"> = $(2);
export const magSq3: TaggedFn1<"vec3", "float"> = $(3);
Expand Down
8 changes: 4 additions & 4 deletions packages/shader-ast-stdlib/src/math/mix-cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const $ = <N extends 1 | 2 | 3 | 4, T extends PrimTypeMap[N]>(n: N, type: T) =>
add(
add(
add(
mul(<any>a, mul(s, s2)),
mul(<any>b, mul(3, mul(s2, t)))
mul(a, mul(s, s2)),
mul(b, mul(3, mul(s2, t)))
),
mul(<any>c, mul(3, mul(t2, s)))
mul(c, mul(3, mul(t2, s)))
),
mul(<any>d, mul(t, t2))
mul(d, mul(t, t2))
)
),
];
Expand Down
6 changes: 3 additions & 3 deletions packages/shader-ast-stdlib/src/math/mix-quadratic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const $ = <N extends 1 | 2 | 3 | 4, T extends PrimTypeMap[N]>(n: N, type: T) =>
ret(
add(
add(
mul(<any>a, mul(s, s)),
mul(<any>b, mul(2, mul(s, t)))
mul(a, mul(s, s)),
mul(b, mul(2, mul(s, t)))
),
mul(<any>c, mul(t, t))
mul(c, mul(t, t))
)
),
];
Expand Down
2 changes: 1 addition & 1 deletion packages/shader-ast-stdlib/src/noise/permute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mod } from "@thi.ng/shader-ast/builtin/math";

const __permute = <T extends Prim>(type: T, suffix = "") =>
defn(type, `permute${suffix}`, [type], (v) => [
ret(mod(mul(<any>v, add(mul(<any>v, float(34)), FLOAT1)), float(289))),
ret(mod(mul(v, add(mul(v, float(34)), FLOAT1)), float(289))),
]);

export const permute = __permute(F);
Expand Down
9 changes: 5 additions & 4 deletions packages/shader-ast-stdlib/src/sdf/line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PrimTypeMap, Sym } from "@thi.ng/shader-ast";
import type { PrimTypeMap } from "@thi.ng/shader-ast";
import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
Expand All @@ -15,10 +15,11 @@ import { clamp01 } from "../math/clamp.js";
*/
const $ = <N extends 2 | 3, T extends PrimTypeMap[N]>(n: N, type: T) =>
defn(F, `sdLine${n}`, [type, type, type], (p, a, b) => {
let pa: Sym<T>, ba: Sym<T>;
const pa = sym(sub(p, a));
const ba = sym(sub(b, a));
return [
(pa = sym(sub(p, a))),
(ba = sym(sub(b, a))),
pa,
ba,
ret(
length(sub(pa, mul(ba, clamp01(div(dot(pa, ba), dot(ba, ba))))))
),
Expand Down
173 changes: 79 additions & 94 deletions packages/shader-ast/src/api/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Fn, Fn0, Fn2, Fn3, Fn4, Fn5, Fn6, Fn7, Fn8 } from "@thi.ng/api";
import type { FnCall, Sym, Term } from "./nodes.js";
import type { SymOpts } from "./syms.js";
import type { Type } from "./types.js";
Expand All @@ -7,40 +6,39 @@ export type ScopeBody = (Term<any> | null | undefined)[];

export type Arg<A extends Type> = A | [A, string?, SymOpts?];

export type Arg1<A extends Type> = [Arg<A>];

export type Arg2<A extends Type, B extends Type> = [Arg<A>, Arg<B>];

export type Arg3<A extends Type, B extends Type, C extends Type> = [
Arg<A>,
Arg<B>,
Arg<C>
];

/** @deprecated */
export type Arg1<A extends Type> = VariadicArgs<[A]>;
/** @deprecated */
export type Arg2<A extends Type, B extends Type> = VariadicArgs<[A, B]>;
/** @deprecated */
export type Arg3<A extends Type, B extends Type, C extends Type> = VariadicArgs<
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

[A, B, C]
>;
/** @deprecated */
export type Arg4<
A extends Type,
B extends Type,
C extends Type,
D extends Type
> = [Arg<A>, Arg<B>, Arg<C>, Arg<D>];

> = VariadicArgs<[A, B, C, D]>;
/** @deprecated */
export type Arg5<
A extends Type,
B extends Type,
C extends Type,
D extends Type,
E extends Type
> = [Arg<A>, Arg<B>, Arg<C>, Arg<D>, Arg<E>];

> = VariadicArgs<[A, B, C, D, E]>;
/** @deprecated */
export type Arg6<
A extends Type,
B extends Type,
C extends Type,
D extends Type,
E extends Type,
F extends Type
> = [Arg<A>, Arg<B>, Arg<C>, Arg<D>, Arg<E>, Arg<F>];

> = VariadicArgs<[A, B, C, D, E, F]>;
/** @deprecated */
export type Arg7<
A extends Type,
B extends Type,
Expand All @@ -49,8 +47,8 @@ export type Arg7<
E extends Type,
F extends Type,
G extends Type
> = [Arg<A>, Arg<B>, Arg<C>, Arg<D>, Arg<E>, Arg<F>, Arg<G>];

> = VariadicArgs<[A, B, C, D, E, F, G]>;
/** @deprecated */
export type Arg8<
A extends Type,
B extends Type,
Expand All @@ -60,49 +58,43 @@ export type Arg8<
F extends Type,
G extends Type,
H extends Type
> = [Arg<A>, Arg<B>, Arg<C>, Arg<D>, Arg<E>, Arg<F>, Arg<G>, Arg<H>];

export type FnBody0 = Fn0<ScopeBody>;

export type FnBody1<A extends Type> = Fn<Sym<A>, ScopeBody>;

export type FnBody2<A extends Type, B extends Type> = Fn2<
Sym<A>,
Sym<B>,
ScopeBody
>;

export type FnBody3<A extends Type, B extends Type, C extends Type> = Fn3<
Sym<A>,
Sym<B>,
Sym<C>,
ScopeBody
>;
> = VariadicArgs<[A, B, C, D, E, F, G, H]>;

export type FnBody0 = VariadicFnBody<[]>;
export type FnBody1<A extends Type> = VariadicFnBody<[A]>;
/** @deprecated */
export type FnBody2<A extends Type, B extends Type> = VariadicFnBody<[A, B]>;
/** @deprecated */
export type FnBody3<
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

A extends Type,
B extends Type,
C extends Type
> = VariadicFnBody<[A, B, C]>;
/** @deprecated */
export type FnBody4<
A extends Type,
B extends Type,
C extends Type,
D extends Type
> = Fn4<Sym<A>, Sym<B>, Sym<C>, Sym<D>, ScopeBody>;

> = VariadicFnBody<[A, B, C, D]>;
/** @deprecated */
export type FnBody5<
A extends Type,
B extends Type,
C extends Type,
D extends Type,
E extends Type
> = Fn5<Sym<A>, Sym<B>, Sym<C>, Sym<D>, Sym<E>, ScopeBody>;

> = VariadicFnBody<[A, B, C, D, E]>;
/** @deprecated */
export type FnBody6<
A extends Type,
B extends Type,
C extends Type,
D extends Type,
E extends Type,
F extends Type
> = Fn6<Sym<A>, Sym<B>, Sym<C>, Sym<D>, Sym<E>, Sym<F>, ScopeBody>;

> = VariadicFnBody<[A, B, C, D, E, F]>;
/** @deprecated */
export type FnBody7<
A extends Type,
B extends Type,
Expand All @@ -111,8 +103,8 @@ export type FnBody7<
E extends Type,
F extends Type,
G extends Type
> = Fn7<Sym<A>, Sym<B>, Sym<C>, Sym<D>, Sym<E>, Sym<F>, Sym<G>, ScopeBody>;

> = VariadicFnBody<[A, B, C, D, E, F, G]>;
/** @deprecated */
export type FnBody8<
A extends Type,
B extends Type,
Expand All @@ -122,52 +114,43 @@ export type FnBody8<
F extends Type,
G extends Type,
H extends Type
> = Fn8<
Sym<A>,
Sym<B>,
Sym<C>,
Sym<D>,
Sym<E>,
Sym<F>,
Sym<G>,
Sym<H>,
ScopeBody
>;

export type Func0<T extends Type> = Fn0<FnCall<T>>;

export type Func1<A extends Type, T extends Type> = Fn<Term<A>, FnCall<T>>;

export type Func2<A extends Type, B extends Type, T extends Type> = Fn2<
Term<A>,
Term<B>,
FnCall<T>
>;
> = VariadicFnBody<[A, B, C, D, E, F, G, H]>;

/** @deprecated */
export type Func0<T extends Type> = VariadicFunc<[], T>;
/** @deprecated */
export type Func1<A extends Type, T extends Type> = VariadicFunc<[A], T>;
/** @deprecated */
export type Func2<
A extends Type,
B extends Type,
T extends Type
> = VariadicFunc<[A, B], T>;
/** @deprecated */
export type Func3<
A extends Type,
B extends Type,
C extends Type,
T extends Type
> = Fn3<Term<A>, Term<B>, Term<C>, FnCall<T>>;

> = VariadicFunc<[A, B, C], T>;
/** @deprecated */
export type Func4<
A extends Type,
B extends Type,
C extends Type,
D extends Type,
T extends Type
> = Fn4<Term<A>, Term<B>, Term<C>, Term<D>, FnCall<T>>;

> = VariadicFunc<[A, B, C, D], T>;
/** @deprecated */
export type Func5<
A extends Type,
B extends Type,
C extends Type,
D extends Type,
E extends Type,
T extends Type
> = Fn5<Term<A>, Term<B>, Term<C>, Term<D>, Term<E>, FnCall<T>>;

> = VariadicFunc<[A, B, C, D, E], T>;
/** @deprecated */
export type Func6<
A extends Type,
B extends Type,
Expand All @@ -176,8 +159,8 @@ export type Func6<
E extends Type,
F extends Type,
T extends Type
> = Fn6<Term<A>, Term<B>, Term<C>, Term<D>, Term<E>, Term<F>, FnCall<T>>;

> = VariadicFunc<[A, B, C, D, E, F], T>;
/** @deprecated */
export type Func7<
A extends Type,
B extends Type,
Expand All @@ -187,17 +170,8 @@ export type Func7<
F extends Type,
G extends Type,
T extends Type
> = Fn7<
Term<A>,
Term<B>,
Term<C>,
Term<D>,
Term<E>,
Term<F>,
Term<G>,
FnCall<T>
>;

> = VariadicFunc<[A, B, C, D, E, F, G], T>;
/** @deprecated */
export type Func8<
A extends Type,
B extends Type,
Expand All @@ -208,14 +182,25 @@ export type Func8<
G extends Type,
H extends Type,
T extends Type
> = Fn8<
Term<A>,
Term<B>,
Term<C>,
Term<D>,
Term<E>,
Term<F>,
Term<G>,
Term<H>,
> = VariadicFunc<[A, B, C, D, E, F, G, H], T>;

export type VariadicFunc<Xs extends Type[], T extends Type> = VariadicFn<
VariadicTerms<Xs>,
FnCall<T>
>;
export type VariadicFnBody<Xs extends Type[]> = VariadicFn<
VariadicSyms<Xs>,
ScopeBody
>;
type VariadicFn<Xs extends unknown[], Y> = (...xs: Xs) => Y;
export type VariadicArgs<Xs extends Type[]> = {
[i in keyof Xs]: Arg<Xs[i]>;
};
export type VariadicSyms<Xs extends Type[]> = {
[i in keyof Xs]: Sym<Xs[i]>;
};
export type VariadicTerms<Xs extends Type[]> = {
[i in keyof Xs]: Term<Xs[i]>;
};
type TypeOfArg<X> = X extends Arg<infer A> ? A : never;
export type VariadicTypeOfArg<Xs extends unknown[]> = { [i in keyof Xs]: TypeOfArg<Xs[i]> };