What is Shader program:
Program is a set of instructions given to a CPU to perform certain tasks.
Same as in computer graphics, a shader is a set of instructions executed on GPU (Graphics Processing Unit) used to shade an image/buffer before it is drawn to the screen.
What is Shading:
Shading means to calculate the appropriate levels of light, darkness and colour for pixels.
What is a buffer?
Buffer Is a region of memory used to temporarily store data. In graphics shaders usually deal with various buffers. eg.
- Frame Buffer
- Z Buffer / Depth Buffer
- Stencil Buffer
Type of Shader:
Shader type are broadly divided into two category
- Vertex Processing
- Vertex Shader
- Tessellation Shader (Hull Shader)
- Geometry Shader
- Fragment Processing
- Fragment Shader / Pixel Shader
- Post processing.
This tutorial is mainly focusing on the Vertex and Fragment shader.
Below image shows an abstract representation of graphics pipeline stages. The stages shown in blue are programmable stages. Here we consider the triangle geometry and how each stage contributes to final output.
Vertex Shader: Executed for each vertex in VAO (Vertex Array Objects).
Vertex shader usually takes all the vertices of your mesh and runs for each vertex in mesh/3d-object which are built with triangle and vertex. So if we have a mesh built with 9000 vertices, the vertex program executes 9000 times to render this mesh. Each vertex has some data with it like position, colour, normal etc.
These Mesh are usually encoded by artists in certain software like max, maya, blender etc. and then exported as file eg. .FBX, .OBJ.
Each time a shape is rendered, the vertex shader is run for each vertex in the shape. Its job is to transform the input vertex from its original coordinate system (object space coordinate) into the clip space coordinate system, in which each axis has a range from -1.0 to 1.0, regardless of aspect ratio, actual size, or any other factors.
Note:
We will see different types of spaces/coordinate systems in later sections.
Output of shader processing unit then transfer to the rasterizer to interpolate between two points to generate pixels for further processing.
Note: Interpolate meaning: Estimate the value of a function between two known values, e.g. by fitting a smooth function between them.
The output of the rasterization unit then sends to a fragment shader to shade each pixel of the above triangle made up of vertices V1, V2 and V3.
Note:
In the later section we will see what rasterization sampling & multi-sampling technique is? Which is used to smoothen the geometry edges by calculating nearby pixels. [Used for antialiasing technique].
Fragment Shader Or Pixel Shader: Fragment shader is used to shade the pixel and contribute to the final colour of the pixel. It is called once for every pixel on each shape to be drawn.