software-mansion / software-mansion/TypeGPU

feat(@typegpu/noise): Stateless PRNGs

Open
#2,473 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
3.2k
Forks
122
Avg merge
3d 5h
Merged PRs (30d)
34

Description

Right now, our randf.* utilities imported from @typegpu/noise are all stateful. To illustrate this, let's look at randf.sample():

const a = randf.sample();
const b = randf.sample();

a and b are different pseudo-random numbers, even though the call to randf.sample() is the same. There is hidden state, updated on each call to randf.sample(). This state is private, meaning it's shared between calls to these functions on the same thread. The issue mostly arises when using multiple random utilities, like perlin-noise, in conjunction with the more low-level randf.sample().

function main(uv: d.v2f) {
  'use gpu';
  randf.seed(time.$);

  const noise = perlin2d.sample(uv); // perlin2d.sample resets the seed
  const factor = randf.sample(); // no longer seeded based on time
}

Stateless generators

[!NOTE]
The proper API is still yet to be finalized

import { randf } from '@typegpu/noise/stateless';

function main() {
  'use gpu';
  const state = d.ref(randf.createState());
  randf.seed(state, time.$);

  const value = randf.sample(state);
  const direction = randf.randOnUnitSphere(state);
}

The generator slot would be shared between the stateful and stateless implementations.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing randf.* utilities and perlin2d.sample() entry points in @typegpu/noise, then compare them with the proposed @typegpu/noise/stateless API. Done means the stateless generators have a finalized API, explicit state, and compatible generator-slot behavior alongside the stateful implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
computer-graphics
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.