Skip to content

Commit 618e3e8

Browse files
committed
starting docs update
1 parent 1dc879e commit 618e3e8

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

apps/typegpu-docs/src/content/docs/ecosystem/typegpu-noise.mdx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ const main = tgpu.fragmentFn({
9999
});
100100
```
101101

102+
### Available PRNGs
103+
104+
102105
:::tip
103106
Due to limited float precision, for large seeds the samples tend to repeat quickly. To avoid this, keep the seed in the range `[0, 1]`.
104107
:::
@@ -277,34 +280,33 @@ const f = tgpu.computeFn({ workgroupSize: [1] })(() => {
277280
});
278281
// ---cut---
279282
import {
283+
hash,
284+
randomGeneratorShell,
280285
randomGeneratorSlot,
286+
u32To01F32,
281287
type StatefulGenerator,
282288
} from '@typegpu/noise';
283289

284-
const LCG: StatefulGenerator = (() => {
290+
const LCG32: StatefulGenerator = (() => {
285291
const seed = tgpu.privateVar(d.u32);
286292

287-
const u32To01Float = tgpu.fn([d.u32], d.f32)((value) => {
288-
const mantissa = value >> 9;
289-
const bits = 0x3F800000 | mantissa;
290-
const f = std.bitcastU32toF32(bits);
291-
return f - 1;
292-
});
293-
294-
return {
295-
seed2: (value: d.v2f) => {
296-
'use gpu';
297-
seed.$ = d.u32(value.x * std.pow(32, 3) + value.y * std.pow(32, 2));
298-
},
299-
sample: () => {
300-
'use gpu';
301-
seed.$ = seed.$ * 1664525 + 1013904223; // % 2 ^ 32
302-
return u32To01Float(seed.$);
303-
},
304-
};
293+
const multiplier = d.u32(1664525);
294+
const increment = d.u32(1013904223);
295+
296+
return {
297+
seed: tgpu.fn([d.f32])((value) => {
298+
seed.$ = hash(d.u32(value));
299+
}),
300+
301+
sample: randomGeneratorShell(() => {
302+
'use gpu';
303+
seed.$ = multiplier * seed.$ + increment; // % 2 ^ 32
304+
return u32To01F32(seed.$);
305+
}).$name('sample'),
306+
};
305307
})();
306308

307309
const pipeline = root
308-
.with(randomGeneratorSlot, LCG)
310+
.with(randomGeneratorSlot, LCG32)
309311
.createComputePipeline({ compute: f });
310312
```

packages/typegpu-noise/src/utils.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export function quinticDerivative(t: d.vecBase): d.vecBase {
2828
'use gpu';
2929
return 30 * t * t * (t * (t - 2) + 1);
3030
}
31-
/*
31+
32+
/**
3233
* Left circular shif of x by k positions.
3334
*/
3435
export const rotl = tgpu.fn(
@@ -38,8 +39,8 @@ export const rotl = tgpu.fn(
3839
return (x << k) | (x >> (32 - k));
3940
});
4041

41-
/*
42-
* Converts u32 to f32 value in the range [0.0, 1.0).
42+
/**
43+
* Converts `u32` to `f32` value in the range `[0.0, 1.0)`.
4344
*/
4445
export const u32To01F32 = tgpu.fn(
4546
[d.u32],
@@ -61,8 +62,8 @@ export const u32To01F32 = tgpu.fn(
6162
export const hash = tgpu.fn(
6263
[d.u32],
6364
d.u32,
64-
)((v) => {
65-
let x = v ^ (v >> 17);
65+
)((value) => {
66+
let x = value ^ (value >> 17);
6667
x *= d.u32(0xed5ad4bb);
6768
x ^= x >> 11;
6869
x *= d.u32(0xac4c1b51);
@@ -73,8 +74,8 @@ export const hash = tgpu.fn(
7374
});
7475

7576
/**
76-
* Emulated 64-bit unsigned addition on two vec2u values.
77-
* Each vec2u represents a u64: x = low 32 bits, y = high 32 bits.
77+
* Emulated 64-bit unsigned addition on two `vec2u` values.
78+
* Each `vec2u` represents a `u64`: x = low 32 bits, y = high 32 bits.
7879
*/
7980
export const u64Add = tgpu.fn(
8081
[d.vec2u, d.vec2u],
@@ -87,8 +88,8 @@ export const u64Add = tgpu.fn(
8788
});
8889

8990
/**
90-
* Emulated 64-bit unsigned multiplication on two vec2u values.
91-
* Each vec2u represents a u64: x = low 32 bits, y = high 32 bits.
91+
* Emulated 64-bit unsigned multiplication on two `vec2u` values.
92+
* Each `vec2u` represents a `u64`: x = low 32 bits, y = high 32 bits.
9293
*/
9394
export const u64Mul = tgpu.fn(
9495
[d.vec2u, d.vec2u],

0 commit comments

Comments
 (0)