[WIP] feat (@typegpu/noise): LCG generator#2215
Conversation
- need to store transition buffer, change only when counts change ;(
…eGPU into docs/generator-slot
|
pkg.pr.new packages benchmark commit |
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:No major changes. 📋 All resultsClick to reveal the results table (348 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
There was a problem hiding this comment.
Pull request overview
Adds a new Linear Congruential Generator (LCG) to @typegpu/noise and wires generator selection into the docs’ probability example, updating resolution/testing snapshots accordingly.
Changes:
- Introduce and export
LCGas aStatefulGeneratorin@typegpu/noise. - Update docs examples to select PRNG generator (BPETER vs LCG) and cache pipelines per (distribution, generator).
- Adjust the probability example’s “Test Resolution” behavior and corresponding snapshot expectations.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/typegpu/tests/examples/individual/probability.test.ts | Updates expected shader-module creation count and inline snapshot to reflect new generator/pipeline resolution behavior. |
| packages/typegpu-noise/src/index.ts | Re-exports LCG from the package entrypoint. |
| packages/typegpu-noise/src/generator.ts | Adds the LCG StatefulGenerator implementation (u32 state + float conversion). |
| apps/typegpu-docs/src/examples/tests/uniformity/prngs.ts | Switches uniformity test PRNG list to import LCG from @typegpu/noise. |
| apps/typegpu-docs/src/examples/tests/uniformity/lcg.ts | Removes now-duplicated local LCG implementation. |
| apps/typegpu-docs/src/examples/algorithms/probability/types.ts | Adds Generator options/type for UI selection. |
| apps/typegpu-docs/src/examples/algorithms/probability/plotter.ts | Adds destroy() to stop the MorphCharts core. |
| apps/typegpu-docs/src/examples/algorithms/probability/index.ts | Adds generator selection control, threads generator into execution, updates “Test Resolution”, and calls plotter.destroy() on cleanup. |
| apps/typegpu-docs/src/examples/algorithms/probability/helpers.ts | Adds generator mapping (getGenerator) and updated imports. |
| apps/typegpu-docs/src/examples/algorithms/probability/executor.ts | Updates pipeline caching to be keyed by (distribution, generator) and binds randomGeneratorSlot per pipeline. |
| apps/typegpu-docs/src/examples/algorithms/probability/constants.ts | Adds initial generator and generator options list. |
Comments suppressed due to low confidence (1)
apps/typegpu-docs/src/examples/algorithms/probability/executor.ts:52
#pipelineCacheis declared as aWeakMap<..., WeakMap<...>>, but it's initialized withnew Map()in the constructor. This is a type mismatch (and changes the GC/retention semantics). Initialize it withnew WeakMap()instead, or change the field type/comment toMapif strong references are intended.
// they can be WeakMaps, because we always have reference to distribution and PRNG
readonly #pipelineCache: WeakMap<
TgpuFn,
WeakMap<StatefulGenerator, TgpuComputePipeline>
>;
constructor(root: TgpuRoot) {
this.#root = root;
this.#bufferCache = new Map();
this.#pipelineCache = new Map();
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
apps/typegpu-docs/src/examples/algorithms/probability/executor.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Yes! You can also scrub the xoroshiro64++ implementation from https://github.com/chaos-matters/chaos-master/blob/main/packages/app/src/shaders/random.ts
It is quite fast!
| const bits = 0x3f800000 | mantissa; | ||
| const f = bitcastU32toF32(bits); | ||
| return f - 1; | ||
| }); |
There was a problem hiding this comment.
This function could be reused for other random generators
| * Naive Linear Congruential Generator (LCG) | ||
| */ | ||
| export const LCG: StatefulGenerator = (() => { | ||
| const seed = tgpu.privateVar(d.u32); |
There was a problem hiding this comment.
I would argue this seed size is not going to be enough for any application with > 1M elements. The reason is simply that your whole internal random state is only ~4B so its going to have a lot of repetition, no matter how good the hashing function is.
There was a problem hiding this comment.
You're right. This was part of earlier research. We ended up sticking with the current PRNG but figured that we'd include LCG in docs section about creating your own PRNG. Ultimately, LCG landed in noise package in this PR without much thinking about its performance 🙈. I'll update it to use larger seed and put xoroshiro64++ next to it 🫡
Uh oh!
There was an error while loading. Please reload this page.