Graphics Reference
In-Depth Information
Of course, that efficiency savings comes at the cost of precision, which
can result in artifacts if precision qualifiers are not used properly. Note
that nothing in the OpenGL ES specification says that multiple precisions
must be supported in the underlying hardware, so it is perfectly valid
for an implementation of OpenGL ES to perform all calculations at the
highest precision and simply ignore the qualifiers. However, on some
implementations, using a lower precision might offer an advantage.
Precision qualifiers can be used to specify the precision of any floating-
point or integer-based variable. The keywords for specifying the precision
are
lowp
,
mediump
, and
highp
. Some examples of declarations with
precision qualifiers are shown here:
highp vec4 position;
varying lowp vec4 color;
mediump float specularExp;
In addition to precision qualifiers, the notion of default precision is
available. That is, if a variable is declared without having a precision
qualifier, it will have the default precision for that type. The default
precision qualifier is specified at the top of a vertex or fragment shader
using the following syntax:
precision highp float;
precision mediump int;
The precision specified for
float
will be used as the default precision
for all variables based on a floating-point value. Likewise, the precision
specified for
int
will be used as the default precision for all integer-based
variables.
In the vertex shader, if no default precision is specified, then the default
precision for both
int
and
float
is
highp
. That is, all variables declared
without a precision qualifier in a vertex shader will have the highest
precision. The rules for the fragment shader are different. In the fragment
shader, there is no default precision given for floating-point values: Every
shader must declare a default
float
precision or specify the precision for
every
float
variable.
One final note is that the precision specified by a precision qualifier has an
implementation-dependent range and precision. There is an associated API
call for determining the range and precision for a given implementation,
which is covered in Chapter 15, “State Queries.” As an example, on the
PowerVR SGX GPU, a
lowp
float
variable is represented in a 10-bit fixed
point format, a
mediump
float
variable is a 16-bit floating-point value,
and a
highp
float
is a 32-bit floating-point value.