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

Source: Internet
Author: User
Tags pow scalar nref

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.

Original link Http://

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.