Skip to content

Commit 054390b

Browse files
authored
refactor: Migrate @typegpu/* libs to use tsover (#2217)
1 parent 54132c0 commit 054390b

14 files changed

Lines changed: 174 additions & 190 deletions

File tree

packages/typegpu-noise/src/generator.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tgpu, { d, type TgpuFnShell, type TgpuSlot } from 'typegpu';
2-
import { add, cos, dot, fract } from 'typegpu/std';
2+
import { cos, dot, fract } from 'typegpu/std';
33

44
export interface StatefulGenerator {
55
seed?: (seed: number) => void;
@@ -28,11 +28,13 @@ export const BPETER: StatefulGenerator = (() => {
2828
}),
2929

3030
seed3: tgpu.fn([d.vec3f])((value) => {
31-
seed.$ = add(value.xy, d.vec2f(value.z));
31+
'use gpu';
32+
seed.$ = value.xy + d.vec2f(value.z);
3233
}),
3334

3435
seed4: tgpu.fn([d.vec4f])((value) => {
35-
seed.$ = add(value.xy, value.zw);
36+
'use gpu';
37+
seed.$ = value.xy + value.zw;
3638
}),
3739

3840
sample: randomGeneratorShell(() => {
Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import tgpu, { d } from 'typegpu';
2-
import { add, dot, floor, fract, mul, sub } from 'typegpu/std';
2+
import { dot, floor, fract } from 'typegpu/std';
33
import { randOnUnitCircle, randSeed2 } from '../random.ts';
4-
import { quinticDerivative2, quinticInterpolation2 } from '../utils.ts';
4+
import { quinticDerivative, quinticInterpolation } from '../utils.ts';
55

66
export const computeJunctionGradient = tgpu.fn(
77
[d.vec2i],
88
d.vec2f,
99
)((pos) => {
10-
randSeed2(mul(0.001, d.vec2f(pos)));
10+
'use gpu';
11+
randSeed2(0.001 * d.vec2f(pos));
1112
return randOnUnitCircle();
1213
});
1314

@@ -20,22 +21,23 @@ export const sample = tgpu.fn(
2021
[d.vec2f],
2122
d.f32,
2223
)((pos) => {
24+
'use gpu';
2325
// Reference: https://iquilezles.org/articles/gradientnoise/
2426

2527
const i = d.vec2i(floor(pos));
2628
const f = fract(pos);
2729

28-
const u = quinticInterpolation2(f);
30+
const u = quinticInterpolation(f);
2931

3032
const ga = getJunctionGradientSlot.$(i);
31-
const gb = getJunctionGradientSlot.$(add(i, d.vec2i(1, 0)));
32-
const gc = getJunctionGradientSlot.$(add(i, d.vec2i(0, 1)));
33-
const gd = getJunctionGradientSlot.$(add(i, d.vec2i(1, 1)));
33+
const gb = getJunctionGradientSlot.$(i + d.vec2i(1, 0));
34+
const gc = getJunctionGradientSlot.$(i + d.vec2i(0, 1));
35+
const gd = getJunctionGradientSlot.$(i + d.vec2i(1, 1));
3436

35-
const va = dot(ga, sub(f, d.vec2f(0, 0)));
36-
const vb = dot(gb, sub(f, d.vec2f(1, 0)));
37-
const vc = dot(gc, sub(f, d.vec2f(0, 1)));
38-
const vd = dot(gd, sub(f, d.vec2f(1, 1)));
37+
const va = dot(ga, f - d.vec2f(0, 0));
38+
const vb = dot(gb, f - d.vec2f(1, 0));
39+
const vc = dot(gc, f - d.vec2f(0, 1));
40+
const vd = dot(gd, f - d.vec2f(1, 1));
3941

4042
const noise = va + u.x * (vb - va) + u.y * (vc - va) + u.x * u.y * (va - vb - vc + vd);
4143

@@ -50,37 +52,33 @@ export const sampleWithGradient = tgpu.fn(
5052
[d.vec2f],
5153
d.vec3f,
5254
)((pos) => {
55+
'use gpu';
5356
// Reference: https://iquilezles.org/articles/gradientnoise/
5457

5558
const i = d.vec2i(floor(pos));
5659
const f = fract(pos);
5760

58-
const u = quinticInterpolation2(f);
59-
const du = quinticDerivative2(f);
61+
const u = quinticInterpolation(f);
62+
const du = quinticDerivative(f);
6063

6164
const ga = getJunctionGradientSlot.$(i);
62-
const gb = getJunctionGradientSlot.$(add(i, d.vec2i(1, 0)));
63-
const gc = getJunctionGradientSlot.$(add(i, d.vec2i(0, 1)));
64-
const gd = getJunctionGradientSlot.$(add(i, d.vec2i(1, 1)));
65+
const gb = getJunctionGradientSlot.$(i + d.vec2i(1, 0));
66+
const gc = getJunctionGradientSlot.$(i + d.vec2i(0, 1));
67+
const gd = getJunctionGradientSlot.$(i + d.vec2i(1, 1));
6568

66-
const va = dot(ga, sub(f, d.vec2f(0, 0)));
67-
const vb = dot(gb, sub(f, d.vec2f(1, 0)));
68-
const vc = dot(gc, sub(f, d.vec2f(0, 1)));
69-
const vd = dot(gd, sub(f, d.vec2f(1, 1)));
69+
const va = dot(ga, f - d.vec2f(0, 0));
70+
const vb = dot(gb, f - d.vec2f(1, 0));
71+
const vc = dot(gc, f - d.vec2f(0, 1));
72+
const vd = dot(gd, f - d.vec2f(1, 1));
7073

7174
const noise = va + u.x * (vb - va) + u.y * (vc - va) + u.x * u.y * (va - vb - vc + vd);
7275

73-
// ga + u.x*(gb-ga) + u.y*(gc-ga) + u.x*u.y*(ga-gb-gc+gd) + du * (u.yx*(va-vb-vc+vd) + vec2(vb,vc) - va))
74-
const grad = add(
75-
ga,
76-
add(
77-
add(
78-
add(mul(u.x, sub(gb, ga)), mul(u.y, sub(gc, ga))),
79-
mul(u.x, mul(u.y, add(sub(sub(ga, gb), gc), gd))),
80-
),
81-
mul(du, sub(add(mul(u.yx, va - vb - vc + vd), d.vec2f(vb, vc)), va)),
82-
),
83-
);
76+
const grad =
77+
ga +
78+
u.x * (gb - ga) +
79+
u.y * (gc - ga) +
80+
u.x * u.y * (ga - gb - gc + gd) +
81+
du * (u.yx * (va - vb - vc + vd) + d.vec2f(vb, vc) - va);
8482

8583
return d.vec3f(noise, grad);
8684
});

packages/typegpu-noise/src/perlin-3d/algorithm.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
import tgpu from 'typegpu';
22
import * as d from 'typegpu/data';
3-
import { add, dot, floor, mix, mul, sub } from 'typegpu/std';
3+
import { dot, floor, mix } from 'typegpu/std';
44
import { randOnUnitSphere, randSeed3 } from '../random.ts';
5-
import { quinticInterpolation3 } from '../utils.ts';
5+
import { quinticInterpolation } from '../utils.ts';
66

77
export const computeJunctionGradient = tgpu.fn(
88
[d.vec3i],
99
d.vec3f,
1010
)((pos) => {
11-
randSeed3(mul(0.001, d.vec3f(pos)));
11+
'use gpu';
12+
randSeed3(0.001 * d.vec3f(pos));
1213
return randOnUnitSphere();
1314
});
1415

1516
export const getJunctionGradientSlot = tgpu.slot(computeJunctionGradient);
1617

17-
const dotProdGrid = tgpu.fn(
18-
[d.vec3f, d.vec3f],
19-
d.f32,
20-
)((pos, junction) => {
21-
const relative = sub(pos, junction);
22-
const gridVector = getJunctionGradientSlot.value(d.vec3i(junction));
18+
const dotProdGrid = (pos: d.v3f, junction: d.v3f): number => {
19+
'use gpu';
20+
const relative = pos - junction;
21+
const gridVector = getJunctionGradientSlot.$(d.vec3i(junction));
2322
return dot(relative, gridVector);
24-
});
23+
};
2524

2625
export const sample = tgpu.fn(
2726
[d.vec3f],
2827
d.f32,
2928
)((pos) => {
29+
'use gpu';
3030
const minJunction = floor(pos);
3131

3232
const xyz = dotProdGrid(pos, minJunction);
33-
const xyZ = dotProdGrid(pos, add(minJunction, d.vec3f(0, 0, 1)));
34-
const xYz = dotProdGrid(pos, add(minJunction, d.vec3f(0, 1, 0)));
35-
const xYZ = dotProdGrid(pos, add(minJunction, d.vec3f(0, 1, 1)));
36-
const Xyz = dotProdGrid(pos, add(minJunction, d.vec3f(1, 0, 0)));
37-
const XyZ = dotProdGrid(pos, add(minJunction, d.vec3f(1, 0, 1)));
38-
const XYz = dotProdGrid(pos, add(minJunction, d.vec3f(1, 1, 0)));
39-
const XYZ = dotProdGrid(pos, add(minJunction, d.vec3f(1, 1, 1)));
40-
41-
const partial = sub(pos, minJunction);
42-
const smoothPartial = quinticInterpolation3(partial);
33+
const xyZ = dotProdGrid(pos, minJunction + d.vec3f(0, 0, 1));
34+
const xYz = dotProdGrid(pos, minJunction + d.vec3f(0, 1, 0));
35+
const xYZ = dotProdGrid(pos, minJunction + d.vec3f(0, 1, 1));
36+
const Xyz = dotProdGrid(pos, minJunction + d.vec3f(1, 0, 0));
37+
const XyZ = dotProdGrid(pos, minJunction + d.vec3f(1, 0, 1));
38+
const XYz = dotProdGrid(pos, minJunction + d.vec3f(1, 1, 0));
39+
const XYZ = dotProdGrid(pos, minJunction + d.vec3f(1, 1, 1));
40+
41+
const partial = pos - minJunction;
42+
const smoothPartial = quinticInterpolation(partial);
4343

4444
// Resolving the z-axis into a xy-slice
4545
const xy = mix(xyz, xyZ, smoothPartial.z);

packages/typegpu-noise/src/random.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import tgpu, { d, type TgpuFn } from 'typegpu';
2-
import {
3-
cos,
4-
dot,
5-
log,
6-
mul,
7-
normalize,
8-
pow,
9-
select,
10-
sign,
11-
sin,
12-
sqrt,
13-
step,
14-
tan,
15-
} from 'typegpu/std';
2+
import { cos, dot, log, normalize, select, sign, sin, sqrt, step, tan } from 'typegpu/std';
163
import { randomGeneratorSlot } from './generator.ts';
174

185
const TWO_PI = Math.PI * 2;
@@ -103,12 +90,13 @@ export const randInUnitSphere: TgpuFn<() => d.Vec3f> = tgpu.fn(
10390
[],
10491
d.vec3f,
10592
)(() => {
93+
'use gpu';
10694
const u = randomGeneratorSlot.$.sample();
10795
const v = d.vec3f(randNormal(0, 1), randNormal(0, 1), randNormal(0, 1));
10896

10997
const vNorm = normalize(v);
11098

111-
return vNorm.mul(pow(u, 0.33));
99+
return vNorm * u ** 0.33;
112100
});
113101

114102
export const randOnUnitSphere: TgpuFn<() => d.Vec3f> = tgpu.fn(
@@ -128,20 +116,22 @@ export const randInUnitHemisphere: TgpuFn<(normal: d.Vec3f) => d.Vec3f> = tgpu.f
128116
[d.vec3f],
129117
d.vec3f,
130118
)((normal) => {
119+
'use gpu';
131120
const value = randInUnitSphere();
132121
const alignment = dot(normal, value);
133122

134-
return mul(sign(alignment), value);
123+
return sign(alignment) * value;
135124
});
136125

137126
export const randOnUnitHemisphere: TgpuFn<(normal: d.Vec3f) => d.Vec3f> = tgpu.fn(
138127
[d.vec3f],
139128
d.vec3f,
140129
)((normal) => {
130+
'use gpu';
141131
const value = randOnUnitSphere();
142132
const alignment = dot(normal, value);
143133

144-
return mul(sign(alignment), value);
134+
return sign(alignment) * value;
145135
});
146136

147137
export const randUniformExclusive: TgpuFn<() => d.F32> = tgpu.fn(
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import tgpu, { d } from 'typegpu';
2-
import { add, mul, sub } from 'typegpu/std';
1+
import type { d } from 'typegpu';
32

43
export type Prettify<T> = {
54
[K in keyof T]: T[K];
@@ -9,38 +8,23 @@ export type PrefixKeys<Prefix extends string, T> = {
98
[K in keyof T as K extends string ? `${Prefix}${K}` : K]: T[K];
109
};
1110

12-
// t * t * t * (t * (6t - (15, 15)) + (10, 10));
13-
const quinticInterpolationImpl = <T extends d.v2f | d.v3f>(t: T): T => {
14-
'use gpu';
15-
return mul(mul(t, mul(t, t)), add(mul(t, sub(mul(t, 6), 15)), 10));
16-
// TODO: Write it using fluent APIs when it becomes available:
17-
// return t.mul(t).mul(t).mul((t.mul(6).sub(15)).add(10));
18-
};
19-
20-
/**
21-
* Works as a replacement for smoothstep, but with a continuous
22-
* second derivative, which in e.g. smooth normals
23-
*/
24-
export const quinticInterpolation2 = tgpu.fn([d.vec2f], d.vec2f)(quinticInterpolationImpl);
25-
2611
/**
2712
* Works as a replacement for smoothstep, but with a continuous
2813
* second derivative, which in e.g. smooth normals
2914
*/
30-
export const quinticInterpolation3 = tgpu.fn([d.vec3f], d.vec3f)(quinticInterpolationImpl);
31-
32-
// 30 * t * t * (t * (t - (2, 2)) + (1, 1))
33-
const quinticDerivativeImpl = <T extends d.v2f | d.v3f>(t: T): T => {
15+
export function quinticInterpolation(t: d.v2f): d.v2f;
16+
export function quinticInterpolation(t: d.v3f): d.v3f;
17+
export function quinticInterpolation(t: d.vecBase): d.vecBase {
3418
'use gpu';
35-
return mul(mul(mul(30, t), t), add(mul(t, sub(t, 2)), 1));
36-
};
19+
return t * t * t * (t * (t * 6 - 15) + 10);
20+
}
3721

3822
/**
39-
* Derivative of {@link quinticInterpolation2}
23+
* Derivative of {@link quinticInterpolation}
4024
*/
41-
export const quinticDerivative2 = tgpu.fn([d.vec2f], d.vec2f)(quinticDerivativeImpl);
42-
43-
/**
44-
* Derivative of {@link quinticInterpolation3}
45-
*/
46-
export const quinticDerivative3 = tgpu.fn([d.vec3f], d.vec3f)(quinticDerivativeImpl);
25+
export function quinticDerivative(t: d.v2f): d.v2f;
26+
export function quinticDerivative(t: d.v3f): d.v3f;
27+
export function quinticDerivative(t: d.vecBase): d.vecBase {
28+
'use gpu';
29+
return 30 * t * t * (t * (t - 2) + 1);
30+
}

0 commit comments

Comments
 (0)