Presentation is loading. Please wait.

Presentation is loading. Please wait.

Language Definitions Chap. 3 of Orange Book.

Similar presentations


Presentation on theme: "Language Definitions Chap. 3 of Orange Book."— Presentation transcript:

1 Language Definitions Chap. 3 of Orange Book

2 Contents Data types Initializers and constructors Type conversions
Qualifiers; interface to a shader Flow control Operators

3 Introduction A program typically contains two shaders: one vertex shader and one fragment shader. More than one shader of each type can be present, but there must be exactly one function main among all the fragment shaders; and exactly one function main among all the vertex shaders.

4 Example (Vertex Shader)

5 Example (Fragment Shader)

6 Data Types Scalar types Vectors Matrices (floating point only)
float, bool, int [bitwise ops are not supported] Vectors vec[2|3|4], bvec[2|3|4], ivec[2|3|4] selecting components: x,y,z,w; s,t,p,q; r,g,b,a GLSL does not distinguish between a color vector and a position vector or other uses of a floating-point vector. Matrices (floating point only) mat[2|3|4] (column-major order) Samplers 1|2|3D textures, cube map, shadow (depth texture) They can only receive them from the application, through a uniform qualified sampler, or pass them on to user or built-in functions. Shaders may not manipulate sampler values. Grass + 1 is not allowed.

7 Bitwise Operators new!

8 Data Types (cont) typedef Structure (no typedef) arrays
No {typecast, pointers, implicit type change} use constructors to perform conversions

9 Note: this is different from C !
Type Conversion Note: this is different from C !

10 Swizzling

11 Component-wise Operation

12 Initializers and Constructors
Constant qualified variables must be initialized. Attribute, uniform, and varying variables cannot be initialized when declared. Attribute,uniform: API sets it Varying: vertex shader sets it Extra components within a single constructor argument are silently ignored. Normally, this is useful for shrinking a value, like eliminating alpha from a color (or w from a position).

13 Initialize Struct To initialize aggregate types, constructors are used. No initializer uses the brace syntax "{. . .}" from C.

14 Example: initializers
(column-major)

15

16 Qualifiers Attribute Uniform Varying Const Absent
Floating point scalar, vectors, and matrices For vertex shaders only Uniform Read-only in shaders Varying Dynamic interface between vertex shaders and fragment shader Const Compile time constant Absent (See next page)

17 Absent Qualifier If no qualifier is specified when a variable (not a function parameter) is declared, the variable can be both read and written by the shader. Nonqualified variables declared at global scope can be shared between shaders of the same type that are linked in the same program. Vertex shaders and fragment shaders each have their own separate global name space for nonqualified globals. (compare uniform) However, nonqualified user-defined variables are not visible outside a program. Unqualified variables have a lifetime limited to a single run of a shader. There is also no concept corresponding to a static variable in a C function that would allow a variable to be set to a value and have its shader retain that value from one execution to the next.

18 goto, switch

19 Flow Control Looping can be done with for, while, and do-while
The keywords break and continue also exist and behave as in C. Selection can be done with if and if-else, just as in C++, with the exception that a variable cannot be declared in the if statement. Selection by means of the selection operator (?:) is also available, with the extra constraint that the second and third operands must have exactly the same type. The type of the expression provided to an if statement or a while statement, or to terminate a for statement, must be a scalar Boolean A special branch, discard, can prevent a fragment from updating the frame buffer

20 Function Calls call by value-return
Parameter qualifiers: in, out, inout; const in qualifier is implied when no qualifiers are specified

21

22 Most GPU driver vendors don't usually bother to implement noiseX in GLSL,
rand() in glsl (url) float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2( ,78.233))) * ); }

23

24


Download ppt "Language Definitions Chap. 3 of Orange Book."

Similar presentations


Ads by Google