Sign in to follow this  
nullsquared

Shader arbitrary precision

Recommended Posts

If you can write CPU code, then you can almost copy and paste to GLSL. Like the guy above, I don't think you will get much out of it. There would probably be an immense number of if statements to emulate any type of precision and I'm sure you will also need to do bit shifting and OR ing

Share this post


Link to post
Share on other sites
You may not need anything special.
For example, if I were writing a shader to draw fractals on faces, I would need some method to accurately zoom in on a specific area arbitrarily. This sounds like something you need arbitrary precision numbers for, but I think expressing a number as the sum of 2 floats would be enough, simply by taking advantage of how floats are concentrated exponentially around 0. That is, I have a complex number indicating the center of the sample area, and another complex number indicating the offset from that center to sample at, to determine if that point is within the set.

Of course, this would take some mathematical transformation trickery to perform right. Just outright adding a tiny float variable to a much larger float variable is likely to have no effect.

And also, I'm just talking about fractals, as they are the only thing I can think of right off the top of my head that you might want to use arbitrary precision for in a shader.

Share this post


Link to post
Share on other sites
No problem!
I'm suggesting you express p as a sum of 2 points: M, a uniform representing the center of the region you're sampling (i.e. the center of the screen), and D, the offset of this pixel from M. p = M + D. You are going to expect that M is much larger in magnitude than D, especially when you zoom way into the fractal to see smaller details.
Now go through all the calculations with that in mind, but try to avoid any cases where you *add* D and M components together because they are wildly different in magnitude, and you will lose precision. Multiplying is actually a lot better. (Just think of the internal way that floats are multiplied. You only lose at most one digit of precision provided the arguments aren't edge cases) z starts around 0 and should hang around 0, provided it is part of the set.
So instead of z^2 + p, you should try z^2 + D, and the explosion condition is (z + M)^2 > 2^2. Rearrange this to be z^2 > (4 - M^2) - 2*(z_r*M_r + z_i*M_i) or maybe you could put the z_r*M_r crap part on the other side or something. z_r is the real component, and z_i is the imaginary component. Same for M_r and M_i.

Hopefully this should work reasonably.

[Edited by - Cathbadh on March 9, 2010 12:06:50 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this