Question

I'm having some trouble with my fragment shader on different devices. On some devices the shader works fine but on other the algorithm produces curious results. I've checked the algorithm in Java to ensure that the algorithm itself does not contain erros.

One part where the algorithm fails is the following. When I write the calculation like this it works on all devices:

float a = sqrt(dot(MD,MD));
float factor = (dot(MD,b)/a)/a;

but if I use

float factor = dot(MD,b)/dot(MD,MD);

which, I believe is the same calculation and thus, should produces the same results, but the algorithm fails on some devices. The other parts where failures can occur are more complex but maybe if I can understand why this is failing I can also solve the other parts.

Any help would be greatly appreciated.

BTW, I'm using OpenGL ES 2.0 with precision mediump float and dot(MD,MD) is always greater than zero

Was it helpful?

Solution

float point works differently on different devices, there is no accuracy guarantees, so you should expect bad results when numbers are too big or too small.

as for your example when dot(MD,MD) is close to 0 sqrt moves it away from it thus stabilising further operation with this number. in the division this is especially critical.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top