Graphics Reference
In-Depth Information
Functions
Functions are declared in much the same way as in C. If a function will be
used prior to its definition, then a prototype declaration must be
provided. The most significant difference between functions in the
OpenGL ES Shading Language and C is the way in which parameters are
passed to functions. The OpenGL ES Shading Language provides special
qualifiers to define whether a variable argument can be modified by the
function; these qualifiers are shown in Table 5-3.
Table 5-3
OpenGL ES Shading Language Qualifiers
Qualifier
Description
in
(Default if none specified) This qualifier specifies that the parameter
is passed by value and will not be modified by the function.
inout
This qualifier specifies that the variable is passed by reference into
the function and if its value is modified, it will be changed after
function exit.
out
This qualifier says that the variable's value is not passed into the
function, but it will be modified on return from the function.
An example function declaration is provided here. This example shows
the use of parameter qualifiers.
vec4 myFunc(inout float myFloat, // inout parameter
out vec4 myVec4, // out parameter
mat4 myMat4); // in parameter (default)
An example function definition is given here for a simple function that
computes basic diffuse lighting:
vec4 diffuse(vec3 normal,
vec3 light,
vec4 baseColor)
{
return baseColor * dot(normal, light);
}
One note about functions in the OpenGL ES Shading Language:
functions cannot be recursive. The reason for this limitation is that some
implementations will implement function calls by actually placing the
function code inline in the final generated program for the GPU. The
shading language was purposely structured to allow this sort of inline
implementation to support GPUs that do not have a stack.
 
 
 
Search WWH ::




Custom Search