Shader arbitrary precision

Started by
7 comments, last by nullsquared 14 years, 1 month ago
Is there any way of making arbitrary precision calculations in GLSL? I searched around the web but didn't find anything interesting.
Advertisement
You can roll your own algorithms which do that, but don't expect a lot of performance. CPUs are better suited to such things.
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
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.
Yeah I'm rendering a mandelbrot in a shader, and I can't figure out how to gain some precision for the z^2 + p iteration.
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]
Good news!
GLSL 4.00 supports 64-bit double-precision float numbers.
I thought you were crazy, but now I can't really see your nuts : Austin Powers
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by Cathbadh
Good news!
GLSL 4.00 supports 64-bit double-precision float numbers.


Sweet

This topic is closed to new replies.

Advertisement