Skip to content

Commit e575fb6

Browse files
committed
refactor
1 parent 7581038 commit e575fb6

14 files changed

Lines changed: 117 additions & 147 deletions

packages/typegpu-noise/src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const BPETER: StatefulGenerator = (() => {
4343
seed.$.x = fract(cos(a) * 136.8168);
4444
seed.$.y = fract(cos(b) * 534.7645);
4545
return seed.$.y;
46-
}),
46+
}).$name('sample'),
4747
};
4848
})();
4949

packages/typegpu-noise/src/random.ts

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,43 @@ import { randomGeneratorSlot } from './generator.ts';
1919
const TWO_PI = Math.PI * 2;
2020
const EPS = 1e-7; // don't ever get any lower than this
2121

22-
export const randSeed: TgpuFn<(seed: d.F32) => d.Void> = (() => {
23-
const seedSlotNotEmpty = tgpu['~unstable'].derived(() => {
24-
if (randomGeneratorSlot.$.seed) {
22+
const seedNotEmpty = tgpu['~unstable'].comptime(
23+
(seedFnName: keyof typeof randomGeneratorSlot.$) => {
24+
if (randomGeneratorSlot.$[seedFnName]) {
2525
return true;
2626
}
27-
console.warn("Called `randf.seed`, but it wasn't provided");
27+
console.warn(`Called \`randf.${seedFnName}\`, but it wasn't provided`);
2828
return false;
29-
});
30-
31-
return tgpu.fn([d.f32])((seed) => {
32-
if (seedSlotNotEmpty.$) {
33-
// @ts-expect-error trust me
34-
randomGeneratorSlot.$.seed(seed);
35-
}
36-
}).$name('randSeed');
37-
})();
38-
39-
export const randSeed2: TgpuFn<(seed: d.Vec2f) => d.Void> = (() => {
40-
const seedSlotNotEmpty = tgpu['~unstable'].derived(() => {
41-
if (randomGeneratorSlot.$.seed2) {
42-
return true;
43-
}
44-
console.warn("Called `randf.seed2`, but it wasn't provided");
45-
return false;
46-
});
47-
48-
return tgpu.fn([d.vec2f])((seed) => {
49-
if (seedSlotNotEmpty.$) {
50-
// @ts-expect-error trust me
51-
randomGeneratorSlot.$.seed2(seed);
52-
}
53-
}).$name('randSeed2');
54-
})();
55-
56-
export const randSeed3: TgpuFn<(seed: d.Vec3f) => d.Void> = (() => {
57-
const seedSlotNotEmpty = tgpu['~unstable'].derived(() => {
58-
if (randomGeneratorSlot.$.seed3) {
59-
return true;
60-
}
61-
console.warn("Called `randf.seed3`, but it wasn't provided");
62-
return false;
63-
});
64-
65-
return tgpu.fn([d.vec3f])((seed) => {
66-
if (seedSlotNotEmpty.$) {
67-
// @ts-expect-error trust me
68-
randomGeneratorSlot.$.seed3(seed);
69-
}
70-
}).$name('randSeed3');
71-
})();
72-
73-
export const randSeed4: TgpuFn<(seed: d.Vec4f) => d.Void> = (() => {
74-
const seedSlotNotEmpty = tgpu['~unstable'].derived(() => {
75-
if (randomGeneratorSlot.$.seed4) {
76-
return true;
77-
}
78-
console.warn("Called `randf.seed4`, but it wasn't provided");
79-
return false;
80-
});
81-
82-
return tgpu.fn([d.vec4f])((seed) => {
83-
if (seedSlotNotEmpty.$) {
84-
// @ts-expect-error trust me
85-
randomGeneratorSlot.$.seed4(seed);
86-
}
87-
}).$name('randSeed4');
88-
})();
29+
},
30+
);
31+
32+
export const randSeed = tgpu.fn([d.f32])((seed) => {
33+
if (seedNotEmpty('seed')) {
34+
// @ts-expect-error trust me
35+
randomGeneratorSlot.$.seed(seed);
36+
}
37+
}).$name('randSeed');
38+
39+
export const randSeed2 = tgpu.fn([d.vec2f])((seed) => {
40+
if (seedNotEmpty('seed2')) {
41+
// @ts-expect-error trust me
42+
randomGeneratorSlot.$.seed2(seed);
43+
}
44+
}).$name('randSeed2');
45+
46+
export const randSeed3 = tgpu.fn([d.vec3f])((seed) => {
47+
if (seedNotEmpty('seed3')) {
48+
// @ts-expect-error trust me
49+
randomGeneratorSlot.$.seed3(seed);
50+
}
51+
}).$name('randSeed3');
52+
53+
export const randSeed4 = tgpu.fn([d.vec4f])((seed) => {
54+
if (seedNotEmpty('seed4')) {
55+
// @ts-expect-error trust me
56+
randomGeneratorSlot.$.seed4(seed);
57+
}
58+
}).$name('randSeed4');
8959

9060
export const randFloat01: TgpuFn<() => d.F32> = tgpu
9161
.fn([], d.f32)(() => randomGeneratorSlot.$.sample());

packages/typegpu/tests/examples/individual/3d-fish.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('3d fish example', () => {
4343
}
4444
}
4545
46-
fn item() -> f32 {
46+
fn sample() -> f32 {
4747
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
4848
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
4949
seed.x = fract((cos(a) * 136.8168f));
@@ -52,7 +52,7 @@ describe('3d fish example', () => {
5252
}
5353
5454
fn randFloat01() -> f32 {
55-
return item();
55+
return sample();
5656
}
5757
5858
struct ModelData {

packages/typegpu/tests/examples/individual/caustics.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('caustics example', () => {
5555
}
5656
}
5757
58-
fn item() -> f32 {
58+
fn sample_1() -> f32 {
5959
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
6060
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
6161
seed.x = fract((cos(a) * 136.8168f));
@@ -64,9 +64,9 @@ describe('caustics example', () => {
6464
}
6565
6666
fn randOnUnitSphere() -> vec3f {
67-
let z = ((2f * item()) - 1f);
67+
let z = ((2f * sample_1()) - 1f);
6868
let oneMinusZSq = sqrt((1f - (z * z)));
69-
let theta = (6.283185307179586f * item());
69+
let theta = (6.283185307179586f * sample_1());
7070
let x = (cos(theta) * oneMinusZSq);
7171
let y = (sin(theta) * oneMinusZSq);
7272
return vec3f(x, y, z);

packages/typegpu/tests/examples/individual/clouds.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('clouds example', () => {
5757
5858
@group(0) @binding(0) var<uniform> resolutionUniform: vec2f;
5959
60-
fn item() -> f32 {
60+
fn sample() -> f32 {
6161
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
6262
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
6363
seed.x = fract((cos(a) * 136.8168f));
@@ -66,7 +66,7 @@ describe('clouds example', () => {
6666
}
6767
6868
fn randFloat01() -> f32 {
69-
return item();
69+
return sample();
7070
}
7171
7272
@group(1) @binding(1) var noiseTexture: texture_2d<f32>;

packages/typegpu/tests/examples/individual/fluid-double-buffering.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('fluid double buffering example', () => {
157157
return true;
158158
}
159159
160-
fn item() -> f32 {
160+
fn sample() -> f32 {
161161
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
162162
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
163163
seed.x = fract((cos(a) * 136.8168f));
@@ -166,7 +166,7 @@ describe('fluid double buffering example', () => {
166166
}
167167
168168
fn randFloat01() -> f32 {
169-
return item();
169+
return sample();
170170
}
171171
172172
fn computeVelocity(x: i32, y: i32) -> vec2f {
@@ -220,13 +220,13 @@ describe('fluid double buffering example', () => {
220220
return 0;
221221
}
222222
223-
struct item_1 {
223+
struct item {
224224
center: vec2f,
225225
radius: f32,
226226
intensity: f32,
227227
}
228228
229-
@group(0) @binding(4) var<uniform> sourceParams: item_1;
229+
@group(0) @binding(4) var<uniform> sourceParams: item;
230230
231231
fn getMinimumInFlow(x: i32, y: i32) -> f32 {
232232
const gridSizeF = 256f;
@@ -335,7 +335,7 @@ describe('fluid double buffering example', () => {
335335
return true;
336336
}
337337
338-
fn item() -> f32 {
338+
fn sample() -> f32 {
339339
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
340340
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
341341
seed.x = fract((cos(a) * 136.8168f));
@@ -344,7 +344,7 @@ describe('fluid double buffering example', () => {
344344
}
345345
346346
fn randFloat01() -> f32 {
347-
return item();
347+
return sample();
348348
}
349349
350350
fn computeVelocity(x: i32, y: i32) -> vec2f {
@@ -398,13 +398,13 @@ describe('fluid double buffering example', () => {
398398
return 0;
399399
}
400400
401-
struct item_1 {
401+
struct item {
402402
center: vec2f,
403403
radius: f32,
404404
intensity: f32,
405405
}
406406
407-
@group(0) @binding(4) var<uniform> sourceParams: item_1;
407+
@group(0) @binding(4) var<uniform> sourceParams: item;
408408
409409
fn getMinimumInFlow(x: i32, y: i32) -> f32 {
410410
const gridSizeF = 256f;

packages/typegpu/tests/examples/individual/jelly-slider.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ describe('jelly-slider example', () => {
167167
168168
@group(0) @binding(6) var bezierTexture: texture_2d<f32>;
169169
170-
fn item() -> f32 {
170+
fn sample() -> f32 {
171171
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
172172
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
173173
seed.x = fract((cos(a) * 136.8168f));
@@ -176,7 +176,7 @@ describe('jelly-slider example', () => {
176176
}
177177
178178
fn randFloat01() -> f32 {
179-
return item();
179+
return sample();
180180
}
181181
182182
fn getNormalFromSdf(position: vec3f, epsilon: f32) -> vec3f {

packages/typegpu/tests/examples/individual/jump-flood-voronoi.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('jump flood (voronoi) example', () => {
3737
}
3838
}
3939
40-
fn item() -> f32 {
40+
fn sample() -> f32 {
4141
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
4242
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
4343
seed.x = fract((cos(a) * 136.8168f));
@@ -46,7 +46,7 @@ describe('jump flood (voronoi) example', () => {
4646
}
4747
4848
fn randFloat01() -> f32 {
49-
return item();
49+
return sample();
5050
}
5151
5252
@group(0) @binding(2) var<uniform> seedThresholdUniform: f32;

packages/typegpu/tests/examples/individual/perlin-noise.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('perlin noise example', () => {
3535
}
3636
}
3737
38-
fn item() -> f32 {
38+
fn sample() -> f32 {
3939
let a = dot(seed, vec2f(23.140779495239258, 232.6168975830078));
4040
let b = dot(seed, vec2f(54.47856521606445, 345.8415222167969));
4141
seed.x = fract((cos(a) * 136.8168f));
@@ -44,9 +44,9 @@ describe('perlin noise example', () => {
4444
}
4545
4646
fn randOnUnitSphere() -> vec3f {
47-
let z = ((2f * item()) - 1f);
47+
let z = ((2f * sample()) - 1f);
4848
let oneMinusZSq = sqrt((1f - (z * z)));
49-
let theta = (6.283185307179586f * item());
49+
let theta = (6.283185307179586f * sample());
5050
let x = (cos(theta) * oneMinusZSq);
5151
let y = (sin(theta) * oneMinusZSq);
5252
return vec3f(x, y, z);

0 commit comments

Comments
 (0)