HLSL random function?

Started by
6 comments, last by smittix 18 years, 4 months ago
Does HLSL have a random number generating function, like rand() in C++? I have looked around at some sites that describe HLSL functions, but I have not seen any functions to produce random numbers. Thanks, -Chris
Advertisement
Under the Intrinsic Functions listing in the DX-HLSL docs, there does not exist a random number generator. You should be able to implement one yourself using a standard template, but the general complexity of a "good" random number generator would probably exceed the amount of allowed instructions in the lower pixel shader versions. The seed could then be supplied by an externally set parameter, or taken from some input data out of the vertex data, texel data, or a combination of both.
Argh, that was me.
I see, well I was actually hoping to randomly offset the texture coordinates in the vertex shader, but as you said, it would more than likely take too many instructions to create my own random number generator.

-Chris
Your best bet is to get the CPU to generate some random numbers and then try and find a "random-ish" way of looking at said numbers [smile]

For example, if you're going to heavily use random numbers create a 1024*1024 texture - you can store 4194304 8-bit random offsets created by the CPU.

For each pass, indicate a particular sub-region that it can examine and then do some screen-space look-up (etc..) to pick a value inside that region. Texture filtering could work nicely here as well.

Sure, it's not as good as a proper random number generator but you can probably get close enough...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

That certainly sounds like an interesting idea, I will have to try and figure that out. Also, how would I get a texture to scroll across a plane? I thought adding a delta time to the texture coordinates would do it, but that just stretches it out.

-Chris
You might want to look into "texture coordinate transformation" - there are a number of articles/threads/resources on it. Basically it's a way of applying a matrix-transform (scaling/rotation/translation etc..) to texture coordinates - might well do what you're after [smile]

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Ok, I'll look into the texture coordinate stuff. Thank you for the help.

-Chris

This topic is closed to new replies.

Advertisement