Statements and structs for OpenGL es Shader language (Official document sixth chapter)

The program block for the OpenGL es Shader language is basically composed of the following:

Statements and declarations

**function definition**

**Select (If-else)**

**Iterations (for, while, Do-while)**

**Jump (Discard, return, break, continue)**

6.1 Function definitions

** shaders are made up of a series of global declarations and function definitions ** . The function declaration specification is as follows:

Prototype returntype functionname (type0 arg0, type1 arg1, ..., Typen argn); The function definition specification is as follows: //Definition returntype functionname (type0 arg0, type1 arg1, ..., Typen argn) { //do some com Putation return returnvalue; }

An array can be used as a parameter, but not as a return value. When you use an array as a formal parameter, the number of elements in the array must also be passed in . When an array is passed into a function as an argument, use only the name of the function, not the subscript.

structs can also be parameters, and the return value can also be a struct that does not contain an array .

The main function is the entry function of the shader. Vertex and element shaders must contain a main function. It has no parameters, no return value, and must be declared void.

6.1.1 function calling convention

The function is called by the way the value is returned. This means that when the input parameter is copied into the function at the time of invocation, the output parameter is copied out to the caller before the function exits.

The method of controlling parameters in or out of a function declaration or definition is as follows:

**The keyword in is used to indicate that the parameter is a copy-in parameter and cannot be copied**
**The keyword out is used to indicate that the parameter is a copy parameter and cannot be copied into**
**The keyword inout is used to indicate that the parameter can be either a copy-in parameter or a copy-out parameter**
**function parameters that do not use modifiers default to the specified in**

6.2 Selection

The conditional control flow for a shaded language uses the IF or if-else:

if (bool-expression) true-statement or if (bool-expression) true-statementelse false-statement

6.3 Iterations

The while and do loop specifications are as follows:

for (for-init-statement; condition (opt); expression) Statement-no-new-scopewhile (condition) Statement-no-new-scopedostatementwhile (expression)

The condition statement for the For loop is optional, and the default is true without the statement.

6.4 Jump Statements

**Continue**

Break

Return

return expression;

Discard In the fragment shader language only

There is no goto statement.

Continue can only be used in loops, when the program executes to continue it automatically discards the subsequent operation and enters the next loop.

Break, also can only be used in the loop, the role is directly out of the layer of the loop.

The Discard keyword can only be used in the slice shader . It is used in the **slice shader to discard operations on the current slice** . This keyword causes the slice to be ignored directly and does not update any buffers. The most typical usage is in a conditional statement, such as:

if (Indensity < 0.0)

Discard

**the slice shader may test the alpha value of a slice and ignore the slice based on the test** .

It is important to note that overlay testing (coverage test) occurs after the shader executes, and the override test can change the alpha value.

The return statement directly causes the current function to exit.

OpenGL es Shader language built-in variables (Official document seventh chapter)

Some OpenGL ES operations have specific functionality between the vertex processor and the chip processor, some of which are specific after the chip processor, and the shader communicates with these fixed functions through built-in variables.

In OpenGL ES, the built-in special variables section comes from the output variables of the vertex shader, partly from the input and output variables of the slice shader. Unlike user-defined varying variables, built-in special variables do not have to maintain a strict one-to-one correspondence between vertex languages and chip languages. Instead, two languages each have their own set of variables.

7.1 Vertex shader-specific variables

The **variable gl_position is only available in the vertex language and is used to write to the odd vertex position** . All vertex shaders that conform to a grammar rule should write a value to the variable. You can write a new value to it at any time during the runtime of the shader, or you can read its value at any time. This value can be used to assemble, trim, reject, and otherwise manipulate the elements of the entity after the start of the vertex processing. If it detects that gl_position is not written, or is read before writing, the compiler generates a diagnostic message, but not all cases can be detected. If the vertex shader has been executed, but is not written to gl_position, then the value of gl_position is undefined.

**Gl_pointsize is also used only in vertex languages, where vertex shaders use it to write the dimensions of the point that will be rasterized, in pixels** .

The built-in vertex shader variables that are used to communicate with a particular feature are essentially declared as follows:

HIGHP VEC4 gl_position; should be written tomediump float gl_pointsize; May is written to

If these values are not written, their values are undefined. Shaders can also read their values after they have been written to them to regain the value written. If the value has been written more than once, the most recently written value is valid.

These built-in variables have global scope.

7.2 Element Shader specific variables

The output value of the slice shader is processed by the back-end fixed function of the OpenGL ES pipeline. The **slice shader uses Gl_fragcolor and Gl_fragdata built-in variables to output values to the OpenGL ES pipeline unless the keyword discard is executed. **

Writing values to Gl_fragcolor and gl_fragdata is not necessary for the element shader. Many algorithms, such as shadow bodies, contain render channels that are not written to the color values.

These variables may be written multiple times in the element shader. If so, the last assigned value is used in subsequent fixed-function pipelines. These variables can be read at any time after the value is written. If the value is not written before the read, then an undefined value is obtained.

The **Gl_fragcolor is written to indicate the slice color value to be used by the subsequent fixed function pipeline** . If a subsequent fixed-function pipeline uses this slice color value, but the shader does not write it to a value, the slice color value will be an undefined value.

**the Gl_fragdata variable value is an array. Writes data to Gl_fragdata[n] in order to indicate the slice metadata n to be used by subsequent fixed function pipelines. **if the subsequent fixed-function pipeline uses this slice metadata, but the shader does not write it to the value, the slice metadata will be an undefined value.

If the shader statically assigns a value to the Gl_fragcolor, it does not assign a value to any element of Gl_fragdata. If the shader statically assigns a value to any element of gl_fragdata, then it does not assign a value to Gl_fragcolor. In other words, the shader does not assign values to both Gl_fragcolor and gl_fragdata variables.

(If the shader contains a static assignment to the variable x, after preprocessing, the shader contains a statement that writes a value to X, regardless of how the run-time control flow executes the statement.) ）

**If the shader executes the Discard keyword, the elements are thrown away, and the values of Gl_fragcolor and gl_fragdata become irrelevant. **

Gl_fragcoord is a read-only variable in the slice shader that holds the coordinate position of the slice relative to the window : x, Y, Z, 1/w. This value is the result of the vertex processing of the inset element in the fixed function after the slice is created. Component Z is the depth value used to represent the depth of the slice.

Gl_frontfacing is the built-in read-only variable for the slice shader, which is true if the slice belongs to a current entity . one usage of this variable is to simulate two-sided light by selecting one of the two colors computed by the vertex shader.

Gl_pointcoord is the built-in read-only variable for the slice shader, whose value is the two-dimensional sit-mark of the point element where the current slice is located . The range of points is 0.0 to 1.0. If the current entity is not a point, the value read from Gl_pointcoord is undefined.

The built-in variables of the element shader are essentially declared as follows:

Mediump vec4 gl_fragcoord;bool gl_frontfacing;mediump vec4 gl_fragcolor;mediump vec4 gl_fragdata[gl_maxdrawbuffers]; Mediump VEC2 Gl_pointcoord;

They do not behave like variables that do not have a store modifier, and these built-in variables have global scope.

7.3 Vertex shader built-in properties

There are no built-in attribute values in OpenGL es.

7.4 Built-in constants

Vertex and element shaders provide the following built-in constants:

Related constants are implemented. The following example values are the minimum allowable values for these maximum values. const Mediump int gl_maxvertexattribs = 8;const mediump int gl_maxvertexuniformvectors = 128;const Mediump int Gl_maxvar yingvectors = 8;const mediump int gl_maxvertextextureimageunits = 0;const mediump int gl_maxcombinedtextureimageunits = 8; const Mediump int gl_maxtextureimageunits = 8;const mediump int gl_maxfragmentuniformvectors = 16;const Mediump int Gl_max Drawbuffers = 1;

7.5 Built-in uniform status

To facilitate access to OpenGL ES processing status, OpenGL ES defines the following uniform variables. If an implementation of the word language does not support HIGHP, and the listed state is HIGHP, then the meta-language will treat it as mediump.

Window coordinates depth range//struct gl_depthrangeparameters {highp float near;//NHIGHP float far;//FHIGHP float diff;//F-n};uni Form Gl_depthrangeparameters Gl_depthrange;

OpenGL es Shader language built-in function (Official document eighth chapter)

OpenGL ES coloring language defines a set of built- **in convenience functions** for scalar and vector operations. Some of the built-in functions can be used in multiple types of shaders, some for fixed hardware, so this part can only be used on a particular shader.

The built-in functions can basically be divided into three categories:

(1) They provide the necessary hardware functionality, such as material mapping, in a few easy ways. These functions cannot be simulated by a shader alone.

(2) They show some tedious operations (clamp, mix, etc.) that can be easily written, but these operations are common and provide direct hardware support. It is very difficult for the compiler to map an expression to complex assembly line instructions.

(3) They provide the operation of the graphics hardware and accelerate in due course. Trigonometric functions are a good example.

Some function names are similar to common C library functions, but they support vector input and more traditional scalar input.

It is recommended that the application use the built-in functions as much as possible instead of implementing the same calculations in the shader, because the built-in functions are optimized (for example, some built-in functions operate directly on the hardware).

User-defined code can overload the built-in functions, but it is best not to redefine them.

The input parameters of the built-in function (and the corresponding output parameters) can be float, vec2, VEC3, Vec4. For any particular application of a function, the actual type must be the same as all parameters and return values. Like the mat, it must be MAT2, MAT3, Mat4.

The precision modifiers for parameters and return values are hidden, and for material functions, the precision of the return type must match the sampler type.

Uniform lowp sampler2d sampler;highp vec2 coord;...lowp vec4 col = texture2d (sampler, coord); Texture2d the precision of the return type is LOWP

There is no correlation between the precision decoration of other built-in function parameters. The call to the built-in function returns the highest precision of the input parameter.

8.1 Angle and Trigonometric functions

function parameters that are identified as angle are assumed to be in radians. These functions are not likely to be removed by 0, and if the divisor is 0, the result is undefined.

The **radian function converts an angle to radians, and the degrees function converts radians to angles. Sin, cos, tan are standard trigonometric functions. ASIN, ACOS, Atan is inverse trigonometric function. Gentype is a bit like an object-oriented generic, that is, if Gentype is a float type, then**

Gentype Pow (gentype x, Gentype y)

It becomes:

Float POW (float x, float y)

Similarly, if gentype is of type int, then it becomes:

int pow (int x, int y);

8.2 exponential function

(1) Gentype pow (Gentype x, Gentype y)

The Y-side of X. If x is less than 0, the result is undefined. Similarly, if x=0 and y<=0, the result is undefined. Special care should be taken when using.

(2) Gentype exp (Gentype x)

E's X-square

(3) Gentype log (Gentype x)

Calculates the value of y that satisfies the y of x equal to E. If the value of x is less than 0, the result is undefined.

(4) Gentype EXP2 (Gentype x)

Calculates the X-square of 2

(5) Gentype log2 (Gentype x)

Calculates the value of y that satisfies x equals 2. If the value of x is less than 0, the result is undefined.

(6) Gentype sqrt (Gentype x)

Calculates the root of X. If x is less than 0, the result is undefined.

(7) Gentype inversesqrt (Gentype x)

Calculates the value of one of the root of x, if x is less than or equal to 0, the result is undefined.

8.3 Common functions

(1) Gentype abs (Gentype x)

Returns the absolute value of X

(2) Gentype sign (Gentype x)

If x>0, returns 1.0 if x=0, returns 0 if x<0, returns-1.0

(3) Gentype floor (Gentype x)

Returns the maximum integer value less than or equal to X

(4) Gentype ceil (Gentype x)

Returns the smallest integer value greater than or equal to X

(5) Gentype fract (Gentype x)

Returns X-floor (x), which returns the number of decimal parts of X

(6) Gentype mod (Gentype x, float y), Gentype mod (Gentype x, Gentype y)

Returns computes * floor (x/y), which is the modulus calculation%

(7) Gentype min (Gentype x, Gentype y), Gentype min (gentype x, float y)

Returns the value of the smaller value of x and Y.

(8) Gentype max (Gentype x, Gentype y), Gentype Max (Gentype x, float y)

Returns the value of the larger value of x and Y.

(9) Gentype Clamp (Gentype x, Gentype minval, Gentype maxval), Gentype Clamp (gentype x, float minval, float maxval)

Clamp translation as a fixture, called the fixture function bar, what does this function mean? Take a look at the explanation: get the larger value between x and Minval, then compare the larger value to the last largest value and then get the smaller one, meaning that the clamp is actually getting the value in the middle of the size of the three parameters. The function has a description: if Minval > Minmax, the result of the function return is undefined. That is, there is no limit to the size of x, but the value of minval must be smaller than maxval.

(Ten) Gentype Mix (Gentype x, Gentype y, Gentype a), Gentype Mix (Gentype x, Gentype y, float a)

Returns a linear blend of x and Y, such as: X⋅ (1−a) +y⋅a

(one) Gentype step (gentype Edge, Gentype x), Gentype step (float edge, Gentype x)

If x < edge, return 0.0, otherwise return 1.0

(Gentype) smoothstep (gentype edge0,gentype edge1,gentype x), Gentype smoothstep (float edge0,float edge1,genType x)

If x <= edge0, returns 0.0, if x >= edge1 returns 1.0, if Edge0 < x < Edge1, the smoothing 0~1 difference between Hermitian is performed. If Edge0 >= edge1, the result is undefined.

8.4 Geometric functions

(1) Float Length (Gentype x)

Returns the length of the vector x

(2) Float distance (Gentype p0, Gentype p1)

Calculates the distance between vector p0,p1

(3) Float dot (gentype x, Gentype y)

dot product between vector x, y

(4) VEC3 Cross (vec3 x, vec3 y)

Cross product between vector x, y

(5) Gentype normalize (Gentype x)

Normalized vector that returns a vector of the same direction as X but with a length of 1

(6) Gentype Faceforward (Gentype N, Gentype I, Gentype nref)

If the dot product of nref and I is less than 0, return n; otherwise, return-N;

(7) Gentype reflect (Gentype I, Gentype N)

Returns the reflection vector

(8) Gentype refract (Gentype I, gentype n,float eta)

Returns the refraction vector

8.5 matrix functions

(1) Mat Matrixcompmult (Mat X, Mat y)

Matrix x multiplied by y,result[i][j] is a scalar product of x[i][j] and y[i][j]. Note that to get the multiplication of a linear algebra matrix, use the multiplication operator *.

8.6 Vector-dependent functions

The associated or equal operator (<, <=,;, >=, = =,! =) is defined (or preserved) and returns a scalar boolean value. Below, "Bvec" is a placeholder representing BVEC2, BVEC3, or BVEC4, "Ivec" is a placeholder for ivec2, IVEC3, or IVEC4, and "VEC" is a placeholder for vec2, VEC3, or VEC4. In any case, the length of the input and return value vectors must match.

(1) LessThan

Compare x < Y.

(2) Lessthanequal

Compare X<=y

(3) GreaterThan

Compare X>y

(4) Greaterthanequal

Compare X>=y

(5) Equal

Compare X==y

(6) NotEqual

Compare X!=y

(7) bool any (Bvec x)

If any component of vector x is true, the result returns TRUE.

(8) bool All (Bvec x)

If all components of vector x are true, the result returns TRUE.

(9) Bvec not (Bvec x)

Returns the complementary matrix of vector x

8.7 Material Lookup function

**Texture (material) lookup functions apply to both fixed-point and slice-shader shaders** . However, the level of detail of the fixed-point shader is not calculated by the fixed function, so there are some differences between the vertex shader and the slice shader texture lookup. The function is to access the texture through the sampler, and is the same as using the OpenGL ES API. Texture attributes such as size, pixel format, dimension, filter method, texture map match number, depth comparison, etc. are all defined in the OpenGL ES API.

In the following function, the bias parameter is optional for the slice shader. However, it is not available in the fixed-point shader. For a slice shader, if you use the bias parameter, it is added to the calculation level of the priority detail to perform the texture access operation. If bias is not used, the implementation will automatically select a default level. Textures are used directly for textures that are not texture-mapped. If the texture is mapped and executed in the slice shader, then the LOD is used for texture lookups. If the texture is mapped and executed in the vertex shader, then the basic texture is used.

Built-in functions ending in Lod can only be used in vertex shaders, where LOD parameters are used directly to represent the level of detail in a function with LOD.

8.8-Element processing function

The slice processing function is only available in the slice language, but there is no element processing function in GLSL es.

OpenGL ES Shader language statements and structures (Official document sixth) built-in variables (Official document, chapter seventh to eighth)