[Transfer] [OpenGL] [OpenGLSE] opengl shader built-in variables and functions

1. uint CreateShader(enum type): Create an empty shader object;

type: VERTEX_SHADER,

2. void ShaderSource(uint shader, sizeicount, const **string, const int *length): Load the shader source code into the shader object; there may be multiple strings

3. void CompileShader(uint shader): compile shader object;

The shader object is stateful and represents the compilation result.

4. void DeleteShader( uint shader ): delete shader object;

5. void ShaderBinary( sizei count, const uint *shaders,
enum binaryformat, const void *binary, sizei length ): Load the precompiled shader binary string;

6. uint CreateProgram(void): Create an empty program object. The programe object organizes multiple shader objects and becomes executable;

7. void AttachShader( uint program, uint shader ): associate shader object and program object;

8. void DetachShader( uint program, uint shader ): Disassociate;

9. void LinkProgram (uint program): The program object is ready for execution, and its associated shader object must be compiled correctly and meet the restrictions;

10. void UseProgram(uint program): execute program object;

11. void ProgramParameteri( uint program, enum pname,
int value ): Set the parameters of the program object;

12. void DeleteProgram( uint program ): delete program object;

13. Qualifier of shader variable:

Default: no modifier, ordinary variable reading and writing, no connection with the outside world;

const: constant const vec3 zAxis = vec3(0.0, 0.0, 1.0);

attribute: declares the variable passed to the vertex shader; read-only; cannot be array or struct; attribute vec4 position;

uniform: indicates that the entire primitive processing value is the same; read-only; uniform vec4 lightPos;

varying: differential value; read and write; varying vec3 normal;

in, out, inout;

14. Precision of shader variables:

highp, mediump, lowp

15. Shader built-in variables:

gl_Position: used by vertex shader to write vertex position; used by fixed operation functions such as primitive collection and cropping;

Its internal declaration is: highp vec4 gl_Position;

gl_PointSize: used in vertex shader to write the point size and number of pixels after rasterization;

Its internal declaration is: mediump float gl_Position;

gl_FragColor: used by Fragment shader to write fragment color; used by subsequent fixed pipeline;

mediump vec4 gl_FragColor;

gl_FragData: used for Fragment shader, is an array, write gl_FragData[n] as data n; used by subsequent fixed pipeline;

mediump vec4 gl_FragData[gl_MaxDrawBuffers];

gl_FragColor and gl_FragData are mutually exclusive and will not be written at the same time;

gl_FragCoord: used for Fragment shader, read-only, Fragment’s coordinate position relative to the window x, y, z, 1/w; This is generated after fixing the pipeline primitive difference; z is the depth value; mediump vec4 gl_FragCoord;

gl_FrontFacing: used to determine whether the fragment belongs to the front-facing primitive; read-only;

bool gl_FrontFacing;

gl_PointCoord: only for point primitive; mediump vec2 gl_PointCoord;

16. Shader built-in constants:

const mediump int gl_MaxVertexAttribs = 8;

const mediump int gl_MaxVertexUniformVectors = 128;

const mediump int gl_MaxVaryingVectors = 8;

const mediump int gl_MaxVertexTextureImageUnits = 0;

const mediump int gl_MaxCombinedTextureImageUnits = 8;

const mediump int gl_MaxTextureImageUnits = 8;

const mediump int gl_MaxFragmentUnitformVectors = 16;

const mediump int gl_MaxDrawBuffers = 1;

17. Shader built-in functions:

Generally, radians are used by default;

radians(degree): angles to radians;

degrees(radian): radians to angles;

sin(angle), cos(angle), tan(angle)

asin(x): arc sine, returns radians [-PI/2, PI/2];

acos(x): arc cosine, returns radians [0, PI];

atan(y, x): arc tangent, returns radians [-PI, PI];

atan(y/x): arc tangent, returns radians [-PI/2, PI/2];

pow(x, y): x raised to the yth power;

exp(x): exponent, log(x):

exp2(x): 2 to the power of x, log2(x):

sqrt(x): the root of x; inversesqrt(x): the reciprocal of the root of x

abs(x): absolute value

sign(x): sign, 1, 0 or -1

floor(x): rounding to the bottom

ceil(x): round to the top

fract(x): take the decimal part

mod(x, y): modulo, x – y*floor(x/y)

min(x, y): take the minimum value

max(x, y): take the maximum value

clamp(x, min, max): min(max(x, min), max);

mix(x, y, a): linear mixing of x, y, x(1-a) + y*a;

step(edge, x): such as x

smoothstep(edge0, edge1, x): used when threshod smooth transition. When edge0<=edge0, it is 0.0, when x>=edge1, it is 1.0

length(x): vector length

distance(p0, p1): distance between two points, length(p0-p1);

dot(x, y): dot product, each component is multiplied separately and then added

cross(x, y): difference product, x[1]*y[2]-y[1]*x[2], x[2]*y[0] – y[2]*x[0], x[0]*y[1] – y[0]*x[1]

normalize(x): normalization, length(x)=1;

faceforward(N, I, Nref): If dot(Nref, I)<0, then N, otherwise -N

reflect(I, N): The reflection direction of I, I -2*dot(N, I)*N, N must be normalized first

refract(I, N, eta): refraction, k=1.0-eta*eta*(1.0 – dot(N, I) * dot(N, I)); if k<0.0, then 0.0, otherwise eta*I - ( eta*dot(N, I) + sqrt(k))*N

matrixCompMult(matX, matY): Matrix multiplication, each component is multiplied by itself, that is, r[i][j] = x[i][j]*y[i][j];

For linear multiplication of matrices, use * directly

lessThan(vecX, vecY): vector each component is compared x < y

lessThanEqual(vecX, vecY): vector compares each component x<=y

greaterThan(vecX, vecY): vector each component compares x>y

greaterThanEqual(vecX, vecY): vector compares each component x>=y

equal(vecX, vecY): vector each component compares x==y

notEqual(vecX, vexY): vector each component compares x!=y

any(bvecX): true as long as one component is true

all(bvecX): all components are true, then true

not(bvecX): negate all components

texture2D(sampler2D, coord): texture lookup

texture2D(sampler2D, coord, bias): LOD bias, mip-mapped texture

texture2DProj(sampler2D, coord):

texture2DProj(sampler2D, coord, bias):

texture2DLod(sampler2D, coord, lod):

texture2DProjLod(sampler2D, coord, lod):

textureCube(samplerCube, coord):

textureCube(samplerCube, coord, bias):

textureCubeLod(samplerCube, coord, lod):

================================================== ============

1.uniform variable
Uniform variables are variables passed to (vertex and fragment) shaders by external application programs. Therefore it is the application passed

Function glUniform**() function assignment. Within the (vertex and fragment) shader program, uniform variables are like in C language

A constant (const), which cannot be modified by the shader program. (The shader can only be used, not changed)

A uniform variable can be shared between a vertex and a fragment if it is declared exactly the same in both vertex and fragment.

(Equivalent to a global variable shared by vertex and fragment shaders)

Uniform variables are generally used to represent: transformation matrices, materials, lighting parameters, colors and other information.

Here are examples:

uniform mat4 viewProjMatrix; //Projection + view matrix
uniform mat4 viewMatrix; //view matrix
uniform vec3 lightPosition; //Light source position

2.attribute variable
Attribute variables are variables that can only be used in vertex shaders. (It cannot declare attribute variables in fragment shader,

Also cannot be used in fragment shader)

Attribute variables are generally used to represent some vertex data, such as: vertex coordinates, normals, texture coordinates, vertex colors, etc.

In the application, the function glBindAttribLocation() is generally used to bind the location of each attribute variable, and then the function

glVertexAttribPointer() assigns a value to each attribute variable.

Here are examples:

uniform mat4 u_matViewProjection;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
varying vec2 v_texCoord;
void main(void)
{
    gl_Position = u_matViewProjection * a_position;
    v_texCoord = a_texCoord0;
}

3.varying variables
The varying variable is used for data transfer between vertex and fragment shader. Generally, the vertex shader modifies the value of the varying variable.

The fragment shader then uses the value of that varying variable. Therefore, the sound of varying variables between vertex and fragment shader

Ming must be consistent. Application cannot use this variable.

Here are examples:

// Vertex shader
uniform mat4 u_matViewProjection;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
varying vec2 v_texCoord; // Varying in vertex shader
void main(void)
{
    gl_Position = u_matViewProjection * a_position;
    v_texCoord = a_texCoord0;
}


//Fragment shader
precision mediump float;
varying vec2 v_texCoord; // Varying in fragment shader
uniform sampler2D s_baseMap;
uniform sampler2D s_lightMap;
void main()
{
    vec4 baseColor;
    vec4 lightColor;
    baseColor = texture2D(s_baseMap, v_texCoord);
    lightColor = texture2D(s_lightMap, v_texCoord);
    gl_FragColor = baseColor * (lightColor + 0.25);
}

Original address: https://blog.csdn.net/Taily_Duan/article/details/80731972