bevyengine / bevyengine/bevy

Good deterministic random hash in wgsl: pcg3d/pcg4d

Open
#11,470 6 comments 0 reactions 0 assignees View on GitHub
A-Rendering C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

In shaders, whenever randomness is required a good hash function is good to have.

"Good" can mean:

- Fast, i.e. good runtime performance
- High quality: No apparent visual periodicity, also passes test-benches aimed at hash/rng functions
- Stable: Does not stop being random after some amount of time (happens in e.g. some trigonometry based hash functions)

## What solution would you like?

Availability of `pcg3d` and `pcg4d` in Bevy. Perhaps via `bevy_pbr/src/render/utils.wgsl`, but it might be better to start putting noise/rng/hash functions in a dedicated wgsl file.

[This paper](https://jcgt.org/published/0009/03/02/) shows why `pcg` is a great choice. Section 6.1 also has GLSL code for it, so only porting is needed.

The signatures for `pcg3d` and `pcg4d` is to both take and return vectors of unsigned ints.

[The authors' ShaderToy example](https://www.shadertoy.com/view/XlGcRh) shows that we can get float versions via dividing the unsigned int vectors by float max. So `pcg3df` and `pcg4df` (float versions) would be great to have as well since we often operate on colors.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing bevy_pbr/src/render/utils.wgsl and Section 6.1 of the linked paper, then compare the authors' ShaderToy example for the float variants. Decide whether these functions belong in the existing utility file or a dedicated WGSL file. Done means Bevy provides pcg3d, pcg4d, pcg3df, and pcg4df with unsigned-vector inputs and outputs or float equivalents as described.

Written by the indexing model from the issue text.

Assessment

Domain
computer-graphics
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.