Function declaration
Function declarations in Shadron have exactly the same syntax as in GLSL. That is, the return type comes first, followed by the function's name, its argument types and names in parentheses, and finally the function's body enclosed in curly braces. The argument types may be prefixed by in, out, or inout qualifiers to designate the argument as input, output, or both.
The function's body is actual GLSL code, extended by Shadron-specific built-in constants, as well as the sizeof() operator, which returns the current dimensions of the object supplied as argument.
Example:
float sqr(float x) {
return x*x;
}
Depending on the function's usage, some limitations may apply:
- For functions invoked outside shader context, such as in an expression outside a function's body, called by a generator, or used as event handler, certain OpenGL-specific mechanisms cannot be used. These include:
- Texture sampling (and other texture operations)
- Accessing built-in OpenGL variables (starting with gl_)
- Using fragment derivative functions (dFdx, dFdy, fwidth, etc.)
- The discard statement
- For functions invoked by a specific shader stage (vertex shader, geometry shader, ...), certain OpenGL-specific mechanisms and built-in variables may not be available depending on the stage. Please refer to OpenGL documentation for more information.
- State variables may only be modified by functions invoked by an event handler.
As with GLSL functions, any recursion is forbidden, and to enforce this, it is only possible to call functions that have been defined prior to the call. Forward declarations are not allowed.
It is also possible to declare functions as templates.