microsoft / hlsl-specs

HLSL Specifications
MIT License
125 stars 34 forks source link

Add a random number generator intrinsic to HLSL #338

Open AdamJMiles opened 1 month ago

AdamJMiles commented 1 month ago

Hi,

Shaders often call for the generation of random numbers. Sometimes that random number generation should form a well distributed 2D pattern (e.g. Blue noise or white noise) but other times it simply calls for generating a 32-bit random number used to make a random choice or as an index to load a random value.

Everyone writes their own function for this already (some better than others) and I believe it's something that would be worthy of being an intrinsic. A GPU may have its own source of random noise it could use to do this "more randomly" or faster than a generically written function that takes a seed. SDKs such as cuRAND exist to generate high quality random numbers of varying types and distributions.

I don't feel it necessary that the PRNGs be deterministic across vendors, but some guarantee of quality would be useful.

A uniform distribution RNG across a user-specified range would be the min-bar for what I'd hope to see supported, but other distributions such as normal would be useful to see too.

Thanks,

Adam

devshgraphicsprogramming commented 1 month ago

For your enjoyment, Apache 2.0 licensed https://github.com/Devsh-Graphics-Programming/Nabla/blob/master/include/nbl/builtin/hlsl/random/xoroshiro.hlsl

Good enough to raytrace on.