Watch out for reduced precision normalize/length in OpenGL ES

Apr 2015

Watch out for reduced precision normalize/length in OpenGL ES

GLSL language for OpenGL ES introduces concept of precision. You can annotate variable declaration (both float and int/uint) with a precision qualifier: highp, mediump or lowp, like:

mediump float a = 3.0;

You can also specify default precision qualifier by using precision statement. Language specification defines minimum required range and precision for each precision qualifier.

GPU vendors are free to use more precise data types, or even full 32-bit float for all of them. What exact precision is used depends on specific GPU and maybe even operating system or graphics driver version. Using smaller data types can occupy less memory, calculate faster and consume less battery power. But it comes at the price of reduced precision and range of these numbers. Tom Olson wrote interesting articles about this: "Benchmarking floating-point precision in mobile GPUs": Part I, Part II, Part III.

In this post I'd like to warn you against a specific problem related to it - usage of length(), normalize() and distance() functions. Using smaller data types not only limits precision in terms of number of significant digits, but also available range (over which the value will saturate to -INF/+INF). For mediump, this range is defined as +/-2^14, which is only 16384.

This may still look like a lot, but let's remember that calculating vector length involves intermediate value that it sum of squares of this components. This can grow very big before a square root is applied. For example, for 3D vector:

length(a) = sqrt(a.x*a.x + a.y*a.y + a.z*a.z)

If you do this operation on a mediump vector, the term a.x*a.x + a.y*a.y + a.z*a.z can exceed maximum value for vector as small as (74.0, 74.0, 74.0). It can be very dangerous if you do something like this in your fragment shader:

precision mediump float;
uniform vec3 light_pos;
void main()
    vec3 dir_to_light = normalize(pos - light_pos);
    // Calculate your lighting and so on...

You might ask: Why isn't this intermediate value stored in high precision before taking its square root to avoid this overflow problem? Obviously it could be, as precision in any place of the shader is free to be higher than the minimum allowed in that place, so some GPU vendors can do it this way, but you shouldn't rely on this. GLSL specification clearly says that the shader is free to use same, reduced precision for intermediate values.

The precision used to internally evaluate an operation, and the precision qualification subsequently associated with any resulting intermediate values, must be at least as high as the highest precision qualification of the operands consumed by the operation.

Conclusion is: When you write shaders for OpenGL ES, watch out for operations that involve calculating vector length (or dot product) like length(), normalize(), distance(), use highp precision for vectors involved and remember that what works on one GPU due to using precision higher than minimum required, may not work on another GPU and it’s still an application issue.

Comments (4) | Tags: math opengl | Author: Adam Sawicki | Share


2015-04-27 23:42:37
Nice article. Beware of using gl_FragCoord as a texture coordinate too! It's mediump by default.
Ray Ban Outlet
2016-02-29 04:38:35 Nike Factory Outlet Longchamp Outlet Nike Flyknit Oakley Vault Oakley Outlet Ray Ban Outlet Online Ray-Ban Sunglasses Toms Outlet ralph lauren outlet Kate Spade Outlet air max 2015 Adidas Outlet Online cheap ray ban sunglasses Yeezy Boost 350 Nike Factory Outlet Adidas Yeezy 350 Timberland Outlet longchamp outlet kate spade outlet NFL Jerseys Whosale Air Max 2016 Ray Ban Outlet
2016-03-29 09:53:34
2016-03-29 09:58:03
beautiful things to read more, look for a long time, I felt closer to reality very far away, life is short, if put too much time is spent on practical things, really is a waste of life, but some things, but if the raw flavor, leisure goods for gossip, so I love romantic dreamer, but I love real life.Day in a hurry, took all the joy and sadness, a lot of things, not enough time to recall, Chaoyang July, early June already covered confused. Season removed, landscape transformation, the so-called forever always far-fetched, time, not to be squandered, the true meaning of life is to enjoy the good in the flat, I Dimei fireworks, also read the leisure, with a touch of ink, painting a thin clear Huan, look lush green, covered with mottled wall. This July, I kept the book's swoosh; this July, I dream in Lijiang; this July, and has been well, not made sad.If you can do if a light breeze right woman, placed in the text soul, written imprint of life, write thin cool and clear Huan, occasionally write love, but never give love, keep a clear gel Bay, look for a parts of the peace, the joy and sorrow of all his writings, and text only affair has nothing to do with the other.Morning time, with a tranquil and contented Zen cloud water, eyes closed, the heart distant exile, Wind and Smoke curl in the mountains, such as Dai, ups and downs, indeed, like a thread, the ups and downs of life, with long Qingyuan . Flowers followed the road, facing the sun slowly forward, then the mind is calm, quiet as the lake, the water peach, flower with water, just like the fate of this world, do not ask why, regardless of come and go, only flowers need to remember Lu Qing, Yue possessor of the green heart, cool count. Sunlight penetration mist, alluding on the lake, the water green, clear, sparkling, on the beach, there are birds flying over from time to time, the sky blue and empty, with a quiet life after ups and downs, this time the heart is close to nature, without the slightest ripples, content with this quiet and peaceful. An end, wins the snow lotus, world clarity, years of silence, the heart is also safe.Time, very long. I know, I can stay in my mind there are limitations, not the majority, can only rely on these words to memory.Time, is very short. I understand, you can never forget so little, so I was in such a manner so as to leave some more.Youth, is very long. In life, shiny people, in fact, that a figure, how the passage of time, or the whole dazzling Love.Youth, is very short. In life, flickering days

Post comment

Nick *
Your name or nickname
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2016 Adam Sawicki
Copyright © 2004-2016 Adam Sawicki