0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, do you know any other methods or references on this topic describing what I could do to avoid such problems when developing for multiple devices? It does not feel right to calculate an unnecessary sqrt and division just to make it work.
@user1581048 in general you just try to keep your calculation in stable range, there is no common recipe how to achieve this. I can suggest you to start searching from keyword Numerical stability.
Thank you. I'm a bit surprised that such problems result from calculations close to 0 because I think that calculations in the range from 1 to 0 are not that uncommon in OpenGL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.