GLSL built-in functions

GLSL built-in functions

from http://blog.csdn.net/hgl868/article/details/7876257

 

  The OpenGL ES shading language defines a set of built-in convenience functions for scalar and vector operations. Some built-in functions can be used in multiple types of shaders, and some are specific to fixed hardware, so this part can only be used in a specific shader.

       Built-in functions can basically be divided into three categories:

(1) They use some convenient ways to provide necessary hardware functions, such as material maps. These functions cannot be simulated by shaders alone.

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

(3) They provide access to graphics hardware and, where appropriate, are accelerated. Trigonometric functions are a good example.

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

       It is recommended that applications try to use built-in functions instead of implementing the same computations in shaders, because built-in functions are most optimized (eg, some built-in functions operate directly on the hardware).

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

       The input parameters (and corresponding output parameters) of built-in functions can be float, vec2, vec3, vec4. For any particular application of the function, the actual type must be the same as all parameters and return values. Just like mat, it must be mat2, mat3, mat4.

       Precision modifiers for parameters and return values ​​are hidden. 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 returns type with lowp precision

      Precision modifiers for other built-in function parameters are not associated in any way. A call to the built-in function will return the highest precision of the input arguments.

 

1 Angles and trigonometric functions

         Function arguments identified as angle are assumed to be in radians. Divide by 0 is not possible with these functions, and if the dividend is 0, the result is undefined.

  The radian function converts angles to radians, and the degrees function converts radians to degrees. sin, cos, tan are standard trigonometric functions. asin, acos, atan are inverse trigonometric functions. genType is a bit like object-oriented generics, that is, if genType is float, then

genType pow (genType x, genType y)

becomes:

float pow (float x, float y)

Similarly, if genType is int, then it becomes:

int pow (int x, int y)

2 Exponential function

 

(1)genType pow (genType x, genType y)

          x raised to the y power. If x is less than 0, the result is undefined. Likewise, if x=0 and y<=0, the result is undefined. Pay special attention when using it.

(2)genType exp (genType x)

          e raised to the power of x

(3)genType log (genType x)

          Calculate the value of y such that x equals e to the y power. If the value of x is less than 0, the result is undefined.

(4)genType exp2 (genType x)

         Calculate 2 to the power of x

(5)genType log2 (genType x)

         Calculate the value of y that satisfies x equal to 2 raised to the y power. If the value of x is less than 0, the result is undefined.

(6)genType sqrt (genType x)

         Calculate the square 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 square roots of x. If x is less than or equal to 0, the result is undefined.

3 Common functions

(1)genType abs (genType x)

返回x的绝对值

(2)genType sign (genType x)

如果x>0,返回1.0;如果x=0,返回0,如果x<0,返回-1.0

(3)genType floor (genType x)

返回小于等于x的最大整数值

(4)genType ceil (genType x)

返回大于等于x的最小整数值

(5)genType fract (genType x)

返回x-floor(x),即返回x的小数部分

(6)genType mod (genType x, float y)、genType mod (genType x, genType y)

返回x – y * floor (x/y),即求模计算%

(7)genType min (genType x, genType y),genType min (genType x, float y)

返回x和y的值较小的那个值。

(8)genType max (genType x, genType y),genType max (genType x, float y)

返回x和y的值较大的那个值。

(9)genType clamp (genType x, genType minVal, genType maxVal)、genType clamp (genType x, float minVal, float maxVal)

clamp翻译为夹具,就叫夹具函数吧,这个函数是什么意思呢?看看解释的意思是:获取x和minVal之间较大的那个值,然后再拿较大的那个值和最后那个最大的值进行比较然后获取较小的那个,意思就明白了,clamp实际上是获得三个参数中大小处在中间的那个值。函数有个说明:如果minVal > minMax的话,函数返回的结果是未定的。也就是说x的值大小没有限制,但是minval的值必须比maxVal小。

(10)genType mix (genType x, genType y, genType a)、genType mix (genType x, genType y, float a)

返回线性混合的x和y,如:x⋅(1−a)+y⋅a

(11)genType step (genType edge, genType x),genType step (float edge, genType x)

如果x < edge,返回0.0,否则返回1.0

(12)genType smoothstep (genType edge0,genType edge1,genType x),genType smoothstep (float edge0,float edge1,genType x)

如果x <= edge0,返回0.0 ;如果x >= edge1 返回1.0;如果edge0 < x < edge1,则执行0~1之间的平滑埃尔米特差值。如果edge0 >= edge1,结果是未定义的。

4  几何函数

 

(1)float length (genType x)

返回向量x的长度

(2)float distance (genType p0, genType p1)

计算向量p0,p1之间的距离

(3)float dot (genType x, genType y)

向量x,y之间的点积

(4)vec3 cross (vec3 x, vec3 y)

向量x,y之间的叉积

(5)genType normalize (genType x)

标准化向量,返回一个方向和x相同但长度为1的向量

(6)genType faceforward(genType N, genType I, genType Nref)

如果Nref和I的点积小于0,返回N;否则,返回-N;

(7)genType reflect (genType I, genType N)

返回反射向量

(8)genType refract(genType I, genType N,float eta)

返回折射向量

5  矩阵函数

(1)mat matrixCompMult (mat x, mat y)

矩阵x乘以y,result[i][j]是 x[i][j] 和 y[i][j] 的标量积。注意,要获取线性代数矩阵的乘法,使用乘法操作符*。

6  向量相关函数

       相关或相等的操作符(<, <=, >, >=, ==, !=)被定义(或保留),返回一个标量布尔值。下面, “bvec” 是表示bvec2, bvec3, or bvec4的占位符, “ivec”是ivec2, ivec3, or ivec4的占位符,  “vec” 是vec2, vec3, or vec4的占位符. 在任何情况下,输入和返回值向量的长度必须匹配。

 

(1)lessThan

比较x < y.

(2)lessThanEqual

比较x<=y

(3)greaterThan

比较x>y

(4)greaterThanEqual

比较x>=y

(5)equal

比较x==y

(6)notEqual

比较x!=y

(7)bool any(bvec x)

如果向量x的任何组件为true,则结果返回true。

(8)bool all(bvec x)

            如果向量x的所有组件均为true,则结果返回true。

(9)bvec not(bvec x)

返回向量x的互补矩阵

7  材质查找函数

       纹理(材质)查找函数对于定点着色器和片元着色器都适用。然而,定点着色器的细节级别并不是通过固定功能计算的,所以顶点着色器和片元着色器纹理查找之间还是有一些差别的。一下函数是通过采样器访问纹理,和使用OpenGL ES API是一样的。纹理属性如尺寸,像素格式,维数,过滤方法,纹理映射匹配数,深度比较等等在OpenGL ES API中都有定义。

        在下面的函数中,bias参数对于片元着色器来说是可选的。但在定点着色器中不可使用。对于片元着色器,如果使用了bias这个参数,它被加到优先细节的计算级别中来执行纹理访问操作。如果bias没有使用,那么实现将自动选择一个默认级别。对于非纹理映射的纹理,纹理是直接被使用的。如果是纹理映射的,并且在片元着色器中执行,那么使用LOD来进行纹理查找。如果是纹理映射的,并且在顶点着色器中执行,那么使用的是基本纹理。

        以Lod结尾的内建函数只能用在顶点着色器中,在带有Lod的函数中,lod参数直接用来表示细节级别。

 

8  片元处理函数

 

       片元处理函数只有在片元语言中才有,但在GLSL ES中没有片元处理函数。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444320&siteId=291194637