What is a shader?

Shader, that is, a shader, is a C-like syntax program that encapsulates hardware-related parts of the code. The difference from ordinary programs is that the shader is executed by the GPU.

The reason you need to write programs for the GPU instead of the CPU is that the GPU is much faster than the CPU at processing graphics-related operations.

Different shader programs are compiled into machine instructions supported by the native hardware when used.

There are two main types of shaders:

  • vertex shader vertex shader
  • Fragment shader fragment shader, also known as pixel shader pixel shader

Of course, not only these two, there are many other shader programs, such as stencil shaders, geometry shaders and so on.

The vertex shader is used to modify (translate, rotate, twist) the position of the vertex: potision and determine the color of the vertex
The fragment shader is used to determine the color of each pixel, use lighting, materials, etc.

Write a shader program

  • Compile vertex shader, to ID
  • Compile fragment shader and get ID
  • Check for compilation errors
  • Link the above two shader programs to get the ID

    • keep this id
    • When coloring triangles, use this ID
    • Different models can have different shader programs

for example

vertex shader vertex shader:

in vec4 s_vPosition;
void main () {
  // 看,我不需要任何矩阵操作
  // s_vPosition 的值需要在 -1.0 和 1.0 之间
  gl_Position = s_vPosition;
}

fragment shader fragment shader:

out vec4 s_vColor;
void main() {
   //不管三七二十一,像素都搞成红的!
   fColor =  vec4 (1.0, 0.0, 0.0, 1.0);
}

Like the C language, the main function is a necessary entry function. Double slashes are notes.

The reason for splitting the program into two parts is mainly for performance reasons: to draw a triangle, the vertex shader is only called 3 times, but the fragment shader (or pixel shader) may be called millions of times, depending on where your triangle is in The size displayed on the screen, including multiple pixels.

Compile the shader

Shader programs can be compiled by calling some OpenGL functions.

Compile a shader in three steps: create id, bind code, compile.

  • <GLuint> glCreateShader (<type>)

    • Create a shader ID, of type GLuint
    • Such as GLuint ID = glCreateShader(GL_VERTEX_SHADER);
  • glShaderSource (<id>, <count>, <src code>, <lengths>)

    • Bind the code to the shader program
    • happens before compilation
  • glCompileShader (<id>)

    • Compile a partial shader program

Create, link, use shaders

The above is just a partial shader, and the complete program needs four more steps:

  • <GLuint> glCreateProgram ()

    • Create a shader program and return an ID, which needs to be maintained throughout the program life cycle
  • glAttachShader (<prog id>, <shader id>)

    • This call requires two separate vertex and fragment shaders
  • glLinkProgram (<prog id>)

    • True linked shader program
  • glUseProgram (<prog id>)

    • Use shader programs when you need to draw triangles

OK, that's all. For a detailed example, see this article .

What is a shader?

Shader, that is, a shader, is a C-like syntax program that encapsulates hardware-related parts of the code. The difference from ordinary programs is that the shader is executed by the GPU.

The reason you need to write programs for the GPU instead of the CPU is that the GPU is much faster than the CPU at processing graphics-related operations.

Different shader programs are compiled into machine instructions supported by the native hardware when used.

There are two main types of shaders:

  • vertex shader vertex shader
  • Fragment shader fragment shader, also known as pixel shader pixel shader

Of course, not only these two, there are many other shader programs, such as stencil shaders, geometry shaders and so on.

The vertex shader is used to modify (translate, rotate, twist) the position of the vertex: potision and determine the color of the vertex
The fragment shader is used to determine the color of each pixel, use lighting, materials, etc.

Write a shader program

  • Compile vertex shader, to ID
  • Compile fragment shader and get ID
  • Check for compilation errors
  • Link the above two shader programs to get the ID

    • keep this id
    • When coloring triangles, use this ID
    • Different models can have different shader programs

for example

vertex shader vertex shader:

in vec4 s_vPosition;
void main () {
  // 看,我不需要任何矩阵操作
  // s_vPosition 的值需要在 -1.0 和 1.0 之间
  gl_Position = s_vPosition;
}

fragment shader fragment shader:

out vec4 s_vColor;
void main() {
   //不管三七二十一,像素都搞成红的!
   fColor =  vec4 (1.0, 0.0, 0.0, 1.0);
}

Like the C language, the main function is a necessary entry function. Double slashes are notes.

The reason for splitting the program into two parts is mainly for performance reasons: to draw a triangle, the vertex shader is only called 3 times, but the fragment shader (or pixel shader) may be called millions of times, depending on where your triangle is in The size displayed on the screen, including multiple pixels.

Compile the shader

Shader programs can be compiled by calling some OpenGL functions.

Compile a shader in three steps: create id, bind code, compile.

  • <GLuint> glCreateShader (<type>)

    • Create a shader ID, of type GLuint
    • Such as GLuint ID = glCreateShader(GL_VERTEX_SHADER);
  • glShaderSource (<id>, <count>, <src code>, <lengths>)

    • Bind the code to the shader program
    • happens before compilation
  • glCompileShader (<id>)

    • Compile a partial shader program

Create, link, use shaders

The above is just a partial shader, and the complete program needs four more steps:

  • <GLuint> glCreateProgram ()

    • Create a shader program and return an ID, which needs to be maintained throughout the program life cycle
  • glAttachShader (<prog id>, <shader id>)

    • This call requires two separate vertex and fragment shaders
  • glLinkProgram (<prog id>)

    • True linked shader program
  • glUseProgram (<prog id>)

    • Use shader programs when you need to draw triangles

OK, that's all. For a detailed example, see this article .


Yujiaao
12.7k 声望4.7k 粉丝

[链接]