12. Shader built-in variables, constants, functions

Vertex shader built-in variable

Built-in variables role precision
gl_VertexID The index of the vertices currently being processed highp
gl_InstanceID The instance number currently being rendered highp
gl_Position Output vertex position (its value is undefined if gl_Position is not written to vertex shader) highp
gl_PointSize Specifies the diameter of the rasterization point highp
gl_FrontFacing Built-in read-only variable, automatically generated based on vertex position and pixel type, true if it belongs to a current pixel

Vertex shaders are built in Uniform

The depth of the range

struct gl_DepthRangeParameters {
highp float near; //near z 
highp float far; //near far 
highp float diff; //far - near
}
uniform gl_DepthRangeParameters gl_DepthRange;
Copy the code

Vertex shader built-in constants

Const mediump int gl_MaxVertexAttribs = 16; Const mediump int gl_MaxVertexUniformVectors = 256; Const mediump int gl_MaxVertexOutputVectors = 16; / / texture unit quantity biggest const mediump int gl_MaxVertexTextureImageUnits = 16; Available in the / / vertex and fragment shaders texture combined maximum const mediump int gl_MaxCombinedTextureImageUnits = 32;Copy the code

Chip shader built-in variable

Built-in variables role
gl_FragCoord Built-in read-only variable, which holds the coordinate position of the slice relative to the window: x, y, z, 1/w processing vertex index
gl_FrontFacing A built-in read-only variable, true if the current fragment is part of a forward plane, false otherwise
gl_PointCoord A built-in read-only variable whose value is where the current slice is locatedSome primitiveTwo dimensional coordinates of
gl_FragDepth Built – in depth values

Chip shader built-in constants

Const mediump int gl_MaxFragmentInputVectors = 15; Const mediump int gl_MaxTextureImageUnits = 16; / / maximum number of Uniform const mediump int gl_MaxFragmentUniformVectors = 224; Const mediump int gl_MaxDrawBuffers = 4;Copy the code

Multi-texture rendering

Client code: bind each texture object to texture unit 0 and 1, set the number value for the sampler, and bind the collector to the corresponding texture unit

glActiveTexutre(GL_TEXTURE0); 
glBindTeture(GL_TEXTURE_2D ,baseMapTexId); 
glUniformli(baseMapTexId,0);
glActiveTexutre(GL_TEXTURE1); 
glBindTeture(GL_TEXTURE_2D ,secondMapTexId);
glUniformli(secondMapTexId,1);
Copy the code

Fragment shader code:

attribute vec2 v_texCoord;
uniform sampler2D s_baseMap; 
uniform sampler2D s_SecondMap; 
void main()
{
  vec4 baseColor;
  vec4 secondColor;
  baseColor = texture(s_baseMap ,v_texCoord); 
  secondColor = texture(s_SecondMap ,v_texCoord);
  gl_FragColor = baseColor * secondColor; 
}
Copy the code

Built-in functions

Angle and trigonometry

The radians function converts angles to radians, and the degrees function converts radians to degrees. Sines, cosines, and tan are standard trig functions. A sine, a cosine, a sub an are inverse trigonometric functions.Copy the code

Exponential function

GenType pow (genType x, genType y)x to the y. If x is less than 0, it's undefined. Similarly, if x=0 and y<=0, the result is undefined. Special attention should be paid when using. GenType exp (genType x)e to the x genType log (genType x) computes y values such that x is equal to e to the y. If the value of x is less than 0, the result is undefined. GenType exp2 (genType X) computes 2 to the power of x genType log2 (genType X) computes the value of y that satisfies x equals 2 to the power of y. If the value of x is less than 0, the result is undefined. GenType SQRT (genType X) computes the square root of x. If x is less than 0, it's undefined. GenType InversesQRT (genType X) computes the value of one of the squares of x, and if x is less than or equal to 0, the result is undefined.Copy the code

Commonly used functions

GenType ABS (genType x) returns the absolute value of x genType sign (genType X) returns 1.0 if x>0; genType abs (genType x) returns the absolute value of x. If x is 0, return 0, if x is less than 0, GenType Floor (genType X) returns the maximum integer value less than or equal to X genType Ceil (genType X) returns the minimum integer value greater than or equal to x genType Fract (genType GenType mod (genType X, float y) genType mod (genType X, genType Y) genType mod (genType X, genType Y) genType mod (genType X, genType Y) genType mod (genType X, genType Y) genType mod (genType X, genType Y) That is, modular calculation % genType min (genType x, genType y), genType min (genType x, float y) returns the smaller value of x and y. GenType Max (genType x, genType y), genType Max (genType x, float y) returns the larger value of x and y. GenType Clamp (genType X, genType minVal, genType maxVal), genType Clamp (genType X, float minVal, Float maxVal) gets the larger value between x and minVal, and then compares the larger value to the last largest value and gets the smaller value. Clamp actually gets the middle value of the three parameters. The function returns an indeterminate result if minVal > minMax. That is, there is no limit to the size of x, but minval must be smaller than maxVal. GenType Mix (genType X, genType Y, genType A), genType Mix (genType X, genType Y, float A) return a linear mix of X and Y, such as: ⋅(1− A)+y⋅a genType Step (edge, genType X), genType Step (float edge, genType X) Otherwise return 1.0 genType smoothstep (genType EDge0,genType Edge1,genType X), GenType smoothstep (float edge0,float edge1,genType X) If x <= edge0, return 0.0; If x >= edge1 returns 1.0; If edge0 < x < edge1, a smooth Hermite difference between 0 and 1 is performed. If edge0 >= edge1, the result is undefined.Copy the code

Geometric function

Float length (genType x) returns the length of vector x float distance (genType P0, genType p1) computes vector P0, Float dot (genType x, genType Y) vector x, y dot vec3 cross (VEC3 x, vec3 y) cross genType normalize (genType X) standardized vector, GenType faceForward (genType N, genType I, genType Nref) If the dot product of Nref and I is less than 0, return N; genType faceForward (genType N, genType I, genType Nref) Otherwise, -n genType Reflect (genType I, genType N) returns the reflection vector genType refract(genType I, genType N,float ETA) returns the refraction vectorCopy the code

Matrix function

The result[I] is the scalar product of x[I][j] and y[I][j]. Note that to get a multiplication of a linear algebraic matrix, use the multiplication operator (*).Copy the code

Vector correlation function

X < y lessThan equal: compare x<=y greaterThan equal: compare x>=y greaterThan equal: compare x>=y greaterThan equal: compare x>=y greaterThan equal: compare x>=y greaterThan equal: compare x>=y greaterThan equal: compare x>=y greaterThan equal: compare x>=y Bool ALL (bvec x): true if all components of vector X are true. Bool ALL (bvec x): True if all components of vector x are trueCopy the code