GLSL Statements

OPENGL ES SHADING LANGUAGE STATEMENTS

This is a reference for the statements of the OpenGL ES Shading Language that is described in the OpenGL ES Shading Language specification. For more information please visit: www.opengl.org/registry.


LOOPS


For loop

for(;;) {}

The keyword for is used to describe a loop that is controlled by a counter. The parentheses enclose three expressions that initialize, check and update the variable used as counter. The body defined by curly braces encloses the statements that are executed at each pass of the loop.

for(int i = 0; i <= 99; i++) { aFunction(); }

The execution of a single pass or the whole loop can be aborted by using a continue or a break statement respectively.


While loop

while() {}

The keyword while is used to describe a loop that is controlled by a condition. The parentheses enclose the expression that defines the condition. The body defined by curly braces encloses the statements that are executed at each pass of the loop.

while(i <= 99) { aFunction(); }

The execution of a single pass or the whole loop can be aborted by using a continue or a break statement respectively.


Do-while loop

do {} while();

The keyword do is used in combination with while to describe a loop that is controlled by a condition. The body defined by curly braces encloses the statements that are executed at each pass of the loop. The parentheses enclose the expression that defines the condition.

do { aFunction(); } while(i <= 99);

The execution of a single pass or the whole loop can be aborted by using a continue or a break statement respectively.

In contrast to a simple while loop the body is always executed at least one time even if the expression evaluates to false from the beginning.


Abort a single pass of a loop

continue;

The keyword continue is used inside the body of a loop to abort a single pass of the loop. All statements in the body after the continue statement are ignored and the next iteration of the loop is executed immediately.


Abort a loop

break;

The keyword break is used inside the body of a loop to abort the whole loop. All statements in the body after the break statement are ignored and the loop is exited without executing any further iteration.


CONDITIONAL EXECUTION


If statement

if() {}

The keyword if is used to describe the conditional execution of a statement. The parentheses enclose the expression that defines the condition. The curly braces enclose the statements that are executed if the condition evaluates as true.

if(i != 0) { aFunction(); }

In contrast to a loop the statements in curly braces are executed only one time or not at all.


Else statement

if() {} else {}

The keyword else is used in conjunction with the keyword if to describe the alternative execution of a statement. The parentheses enclose the expression that defines the condition. The curly braces after the if statement enclose the statements that are executed if the condition evaluates as true. The curly braces after the else statement enclose the statements that are executed if the condition evaluates as false.

if(i != 0) { aFunction(); } else { bFunction(); }

Depending on the condition either the statements in the first curly braces or the statements in the second curly braces are executed.


EXIT EXECUTION


Return statement

return;

The keyword return is used to define a proper exit for a function. If the function has the return type void no value is passed back to the caller of the function.

return aValue;

If the function has a non-void return type a parameter of the same type has to be included in the statement. The value is passed back to the caller of the function.


Discard statement

discard;

The keyword discard is used to define an exceptionally exit for a fragment shader. It is used exit the fragment shader immediately and to signal the OpenGL ES 2.0 pipeline that the respective fragment should not be drawn.


START EXECUTION


Main function

void main(void) {}

The keyword main is used to define the main function of a shader. This function is the entry point for the execution of every vertex and fragment shader. The main function takes no parameters and does not return a value.