Pro OpenGL ES for iOS

(singke) #1

310 CHAPTER 10: OpenGL ES 2, Shaders, and...^


Values are passed to and from shaders in the following types:
„ Uniforms, which are values passed from the calling program. These
might include the matrices for transformations or projection. They are
available in both the vertex and fragment shaders, and they must be
declared as the same type in each place.
„ Varying variables (yes, it is a dumb-sounding name), which are
variables defined in the vertex shader that are passed on to the
fragment shader.
Variables may be defined as the usual numeric primitives or as graphics-oriented types
based on vectors and matrices, as shown in Table 10-1.
Table 10-1. Variable Types Allowed by GLSL

Class Type Description

Primitives float, int, bool You really don’t need me to define these now,
do you?

Vectors int, ivec2, ivec3, ivec4,
float, vec2, vec3, vec4
bool, bvec2, bvec3, bvec4

Float, int, and bools are “one-dimensional
vectors.” Boolean vectors hold only bool values
in their components.

Matrices mat2, mat3, mat4 Nope, no Boolean matrices here.

In addition to these types, you can supply modifiers to define the precision of int- and
float-based types. These can be highp (24 bit), mediump (16 bit), or lowp (10 bit), with
highp being the default. All transformations must be done in highp, while colors need
only mediump. (It beats me why there is no precision qualifier for bools, though.)
Any basic types can be declared as constant variables, such as const float x=1.0.
Structures are also allowed and look just like their C counterparts.

Restrictions


Since shaders reside on the GPU, they naturally have a number of restrictions to them,
limiting their complexity. They may be limited by ‘‘instruction count,’’ number of uniforms
permitted (typically 128), number of temporary variables, and depth of loop nesting.
Unfortunately, on OpenGL ES, there is no real way to fetch these limits from the
hardware, so you can only be aware that they exist and keep your shaders as small as
possible.
There are also differences between the vertex and fragment shaders. For example,
highp support is optional, whereas it is mandatory on the vertex shader. Bah.
Free download pdf