| |||

Home > Developing OpenGL ES Applications > Recommendations and best practices > Shader arithmetic |

Internally, the geometry processor works on 32-bit floating point values that follow the IEEE 754 standard with the exception that de-normalized, small values are taken as 0.0. The vertex shader represents integers using floating-point values.

You can transfer vertex data, from the geometry processor to the pixel processor, in several different formats. Values are converted by hardware from and to the float forms used in the shaders, at no extra cost.

The OpenGL ES 2.0 standard implies that, by default, data
must be transferred as 32-bit floating point values. However, this
produces a lot of memory traffic, and the full 32-bit precision
is often not necessary. To avoid using 32-bit values, set the precision
for the output varying values from the vertex shader program to
either `mediump`

or `lowp`

.

The fragment shader uses 16-bit floating point format, that consists of:

Sign.

5-bit exponent, with offset 15.

10-bit mantissa, with an implied most significant 1-bit.

This format corresponds to approximately three decimal digits of precision.

The arithmetic for 16-bit floating point values deviates slightly from the IEEE 754 standard. For example:

The fragment shader treats

*Not A Number*(NaN) values and de-normalized values as 0.0 when they are used as input to operations.Converting +INF and -INF to integers yields, respectively, the largest and smallest representable values. This can occur on the output from the two shading units.

In many cases, this treatment of 16-bit floating point values results in reasonable pixel colors rather than NaN values. For example, division by zero or taking a square root of a negative argument always yields NaN. If the fragment shader uses these results as color components, they are dark. When used as texture coordinates, it has the same effect as 0.0.

If you blend the resulting color with other contributions,
the error might not be noticeable. In other words, it might not
be necessary to include `if`

statements in your
shader program to guard against division by zero or bad arguments
to built-in functions. Avoid taking such precautions until you notice
bad pixels on the display.