Skip to content

Commit

Permalink
refactor(shader-ast-stdlib): simplify callsites of addSelf/mulSelf etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 5, 2024
1 parent 41d2882 commit efa1f8d
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 87 deletions.
17 changes: 4 additions & 13 deletions packages/shader-ast-stdlib/src/color/levels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,8 @@ export const levelAdjustGamma = defn(
[F, F, V2, V2],
(x, gamma, input, output) => [
ret(
fit01<FloatTerm>(
pow(
fitClamped<FloatTerm>(
x,
$x(input),
$y(input),
FLOAT0,
FLOAT1
),
gamma
),
fit01(
pow(fitClamped(x, $x(input), $y(input), FLOAT0, FLOAT1), gamma),
$x(output),
$y(output)
)
Expand All @@ -101,9 +92,9 @@ export const levelAdjustGammaRGB = defn(
[V3, V3, M3, M3],
(x, gamma, input, output) => [
ret(
fit01<Vec3Term>(
fit01(
pow(
fitClamped<Vec3Term>(
fitClamped(
x,
indexMat(input, 0),
indexMat(input, 1),
Expand Down
5 changes: 2 additions & 3 deletions packages/shader-ast-stdlib/src/fog/exp2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { F } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { mul, mulSelf, sub } from "@thi.ng/shader-ast/ast/ops";
import { exp2 } from "@thi.ng/shader-ast/builtin/math";
import { clamp01 } from "../math/clamp.js";

Expand All @@ -16,6 +15,6 @@ import { clamp01 } from "../math/clamp.js";
* @param density - float
*/
export const fogExp2 = defn(F, "fogExp2", [F, F], (dist, density) => [
assign(density, mul(density, dist)),
mulSelf(density, dist),
ret(sub(FLOAT1, clamp01(exp2(mul(mul(density, density), -Math.LOG2E))))),
]);
21 changes: 12 additions & 9 deletions packages/shader-ast-stdlib/src/math/additive.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { Fn } from "@thi.ng/api";
import type { FloatSym, FloatTerm, Prim, Term } from "@thi.ng/shader-ast";
import { F } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { FLOAT0, FLOAT05, float } from "@thi.ng/shader-ast/ast/lit";
import { add, inc, lt, mul } from "@thi.ng/shader-ast/ast/ops";
import {
add,
addSelf,
inc,
lt,
mul,
mulSelf,
} from "@thi.ng/shader-ast/ast/ops";
import { sym } from "@thi.ng/shader-ast/ast/sym";

/**
Expand Down Expand Up @@ -45,15 +51,12 @@ export const additive = <T extends Prim>(
(i) => lt(i, float(oct)),
inc,
(i) => [
assign(
addSelf(
n,
add(
n,
mul(amp, fn(<any>add(<any>pos, mul(i, <any>shift))))
)
mul(amp, fn(<any>add(<any>pos, mul(i, <any>shift))))
),
assign(amp, mul(amp, decay)),
assign(pos, <any>mul(<any>pos, 2)),
mulSelf(amp, decay),
mulSelf(<any>pos, 2),
]
),
ret(n),
Expand Down
2 changes: 1 addition & 1 deletion packages/shader-ast-stdlib/src/math/cartesian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { cossin } from "./sincos.js";
*
* @param v -
*/
export const cartesian2 = (v: Vec2Term) => mul(cossin($y(v)), $x(v));
export const cartesian2 = (v: Vec2Term) => cossin($y(v), $x(v));

/**
* Converts 3D polar/spherical vector `v`, i.e. `[r,θ,ϕ]` (angles in
Expand Down
31 changes: 15 additions & 16 deletions packages/shader-ast-stdlib/src/noise/hash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { FloatSym, Vec3Sym, Vec4Sym } from "@thi.ng/shader-ast";
import { F, V2, V3, V4 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { mat2, vec2, vec3, vec4 } from "@thi.ng/shader-ast/ast/lit";
import { add, mul } from "@thi.ng/shader-ast/ast/ops";
import { add, addSelf, mul, mulSelf } from "@thi.ng/shader-ast/ast/ops";
import { $, $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import { dot, fract, sin } from "@thi.ng/shader-ast/builtin/math";
Expand Down Expand Up @@ -53,8 +52,8 @@ export const hash11 = defn(F, "hash11", [F], (p) => {
let x: FloatSym;
return [
(x = sym(fract(mul(p, 0.1031)))),
assign(x, mul(x, add(x, 19.19))),
assign(x, mul(x, add(x, x))),
mulSelf(x, add(x, 19.19)),
mulSelf(x, add(x, x)),
ret(fract(x)),
];
});
Expand All @@ -70,7 +69,7 @@ export const hash12 = defn(F, "hash12", [V2], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), 0.1031)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($x(x), $y(x)), $z(x)))),
];
});
Expand All @@ -86,7 +85,7 @@ export const hash13 = defn(F, "hash13", [V3], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), 0.1031)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($x(x), $y(x)), $z(x)))),
];
});
Expand All @@ -102,7 +101,7 @@ export const hash21 = defn(V2, "hash21", [F], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(vec3(p), H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy")))),
];
});
Expand All @@ -118,7 +117,7 @@ export const hash22 = defn(V2, "hash22", [V2], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy")))),
];
});
Expand All @@ -134,7 +133,7 @@ export const hash23 = defn(V2, "hash23", [V3], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(p, H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy")))),
];
});
Expand All @@ -150,7 +149,7 @@ export const hash31 = defn(V3, "hash31", [F], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(p, H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx")))),
];
});
Expand All @@ -166,7 +165,7 @@ export const hash32 = defn(V3, "hash32", [V2], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx")))),
];
});
Expand All @@ -182,7 +181,7 @@ export const hash33 = defn(V3, "hash33", [V3], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(p, H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
addSelf(x, dot(x, add($(x, "yzx"), 19.19))),
ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx")))),
];
});
Expand All @@ -198,7 +197,7 @@ export const hash41 = defn(V4, "hash41", [F], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul(p, H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
addSelf(x, dot(x, add($(x, "wzxy"), 19.19))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx")))),
];
});
Expand All @@ -214,7 +213,7 @@ export const hash42 = defn(V4, "hash42", [V2], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul($(p, "xyxy"), H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
addSelf(x, dot(x, add($(x, "wzxy"), 19.19))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx")))),
];
});
Expand All @@ -230,7 +229,7 @@ export const hash43 = defn(V4, "hash43", [V3], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul($(p, "xyzx"), H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
addSelf(x, dot(x, add($(x, "wzxy"), 19.19))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx")))),
];
});
Expand All @@ -246,7 +245,7 @@ export const hash44 = defn(V4, "hash44", [V4], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul(p, H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
addSelf(x, dot(x, add($(x, "wzxy"), 19.19))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx")))),
];
});
17 changes: 7 additions & 10 deletions packages/shader-ast-stdlib/src/noise/simplex2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
vec3,
vec4,
} from "@thi.ng/shader-ast/ast/lit";
import { add, gt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { add, gt, mul, mulSelf, sub } from "@thi.ng/shader-ast/ast/ops";
import { $, $x, $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import {
Expand Down Expand Up @@ -76,20 +76,17 @@ export const snoise2 = defn(F, "snoise2", [V2], (v) => {
VEC3_0
)
)),
assign(m, mul(m, m)),
assign(m, mul(m, m)),
mulSelf(m, m),
mulSelf(m, m),
(x = sym(sub(mul(2, fract(mul(p, $(C, "www")))), FLOAT1))),
(h = sym(sub(abs(x), FLOAT05))),
(ox = sym(floor(add(x, FLOAT05)))),
(a0 = sym(sub(x, ox))),
assign(
mulSelf(
m,
mul(
m,
sub(
1.79284291400159,
mul(0.85373472095314, add(mul(a0, a0), mul(h, h)))
)
sub(
1.79284291400159,
mul(0.85373472095314, add(mul(a0, a0), mul(h, h)))
)
),
(g = sym(vec3(add(mul($x(a0), $x(x0)), mul($x(h), $y(x0)))))),
Expand Down
12 changes: 6 additions & 6 deletions packages/shader-ast-stdlib/src/noise/simplex3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
vec3,
vec4,
} from "@thi.ng/shader-ast/ast/lit";
import { add, mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
import { add, mul, mulSelf, neg, sub } from "@thi.ng/shader-ast/ast/ops";
import { $, $w, $x, $xy, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import {
Expand Down Expand Up @@ -121,10 +121,10 @@ export const snoise3 = defn(F, "snoise3", [V3], (v) => {
)
)
)),
assign(p0, mul(p0, $x(norm))),
assign(p1, mul(p1, $y(norm))),
assign(p2, mul(p2, $z(norm))),
assign(p3, mul(p3, $w(norm))),
mulSelf(p0, $x(norm)),
mulSelf(p1, $y(norm)),
mulSelf(p2, $z(norm)),
mulSelf(p3, $w(norm)),
(m = sym(
max(
vec4(),
Expand All @@ -134,7 +134,7 @@ export const snoise3 = defn(F, "snoise3", [V3], (v) => {
)
)
)),
assign(m, mul(m, m)),
mulSelf(m, m),
ret(
mul(
42,
Expand Down
15 changes: 11 additions & 4 deletions packages/shader-ast-stdlib/src/noise/voronoi2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { FloatSym, Vec2Sym, Vec3Sym } from "@thi.ng/shader-ast";
import { F, V2 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import {
Expand All @@ -11,7 +10,15 @@ import {
vec2,
vec3,
} from "@thi.ng/shader-ast/ast/lit";
import { add, div, inc, lte, madd, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import {
add,
addSelf,
div,
inc,
lte,
mul,
sub,
} from "@thi.ng/shader-ast/ast/ops";
import { $xy, $z } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import {
Expand Down Expand Up @@ -82,8 +89,8 @@ export const voronoise2 = defn(F, "voronoise2", [V2, F, F], (x, u, v) => {
k
)
)),
assign(va, madd(w, $z(o), va)),
assign(wt, add(wt, w)),
addSelf(va, mul(w, $z(o))),
addSelf(wt, w),
]
),
]
Expand Down
4 changes: 2 additions & 2 deletions packages/shader-ast-stdlib/src/noise/worley2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export const worley2 = (
(d3 = sym($dist(-1.5))),
(d1a = sym(min(d1, d2))),
assign(d2, min(max(d1, d2), d3)),
assign(d1, min(d1a, d2)),
assign(d2, max(d1a, d2)),
assign(d1, min(d2, d1a)),
assign(d2, max(d2, d1a)),
assign($xy(d1), ternary(lt($x(d1), $y(d1)), $xy(d1), $(d1, "yx"))),
assign(
$(d1, "xz"),
Expand Down
18 changes: 11 additions & 7 deletions packages/shader-ast-stdlib/src/raymarch/ao.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type { FloatSym } from "@thi.ng/shader-ast";
import { F, V3 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { gensym } from "@thi.ng/shader-ast/ast/idgen";
import { FLOAT0, FLOAT05, FLOAT1, float } from "@thi.ng/shader-ast/ast/lit";
import { add, inc, lte, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import {
add,
addSelf,
inc,
lte,
mul,
mulSelf,
sub,
} from "@thi.ng/shader-ast/ast/ops";
import { $x } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import type { RaymarchScene } from "../api.js";
Expand Down Expand Up @@ -39,11 +46,8 @@ export const raymarchAO = (
inc,
(i) => [
(d0 = sym(mul(i, 1 / numSamples))),
assign(
r,
add(r, mul(w, sub(d0, $x(scene(add(p, mul(n, d0)))))))
),
assign(w, mul(w, FLOAT05)),
addSelf(r, mul(w, sub(d0, $x(scene(add(p, mul(n, d0))))))),
mulSelf(w, FLOAT05),
]
),
ret(sub(FLOAT1, clamp01(r))),
Expand Down
Loading

0 comments on commit efa1f8d

Please sign in to comment.