Jump to content
  • Advertisement
Sign in to follow this  
smitty1276

Generation of random numbers in fragment shader

This topic is 4559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, does anyone have any thoughts on what might be the best way to generate random numbers from within a fragment shader (GLSL, but that probably doesn't really matter)? I could write a random number generator function, but I was wondering if anyone has used any cool tricks. I'll probably be needing 50-100 of them per fragment for a single quad.

Share this post


Link to post
Share on other sites
Advertisement
I generally use a texture containing only an alpha channel. I fill it with the CPU, then ship it off to the GPU in the form of a sampler handle.

This way the CPU can be used to generate the batch of random numbers only once per frame / app instance, instead of having to calculate individual random numbers per-texel.

It should speed things up nicely. The GPU hardware is geared toward this type of lookup.

Multiple textures can be used if the required batch is larger than your GPU's maximum texture dimensions.

Getting even more creative, one can use randomly generated alpha texture 'A' to randomly generate the lookup indices to randomly generated alpha texture 'B' -- which contains the actual random numbers used by the remainder of the GLSL code.

Mersenne Twister is a good random number generation algorithm, with sources available in several languages.

Share this post


Link to post
Share on other sites
Cool, I may go the texture route. I'm implementing a ray tracer in GPU, and I'm already [planning on] using textures to store vertices, vertex indices, materials, and acceleration structures. I suppose one more texture lookup won't kill me. [grin]

Thanks for the input.

Share this post


Link to post
Share on other sites
That's cool! Maybe we can swap some pointers. The main emphasis of mine at the moment is to implement a realistic camera model (http://www.graphics.stanford.edu/courses/cs348b-05/lectures/lecture7/) in a GPU raytracer. I want to randomly sample across the solid angle subtended by the aperture from each fragment, hence the need for LOTS of random numbers.

After I finish that I'll focus more on rendering complex scenes, but I think know how to go about it... it's just a matter of time. :-(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!