Shaders are also a set of instructions, but the instructions are executed all at once for every single pixel on the screen. That means the code you write has to behave differently depending on the position of the pixel on the screen.
The program will work as a function that receives a position and returns a color, and when it's compiled it will run extraordinarily fast.
GLSL stands for openGL Shading Language, which is the specific standard of shader programs. There are other types of shaders depending on hardware and Operating Systems.
<aside> 💡 Because GLSL (OpenGL Shading Language) shaders compile and run on a variety of platforms, you will be able to apply what you learn here to any environment that uses OpenGL, OpenGL ES or WebGL.
</aside>
Shaders always begin with a version declaration, followed by a list of input and output variables, uniforms and its main function.
Each shader's entry point is at its main function where we process any input variables and output the results in its output variables.
Generally a shader look like this:
#version version_number
in type in_variable_name;
in type in_variable_name;
out type out_variable_name;
uniform type uniform_name;
void main() {
// process input(s) and do some weird graphics stuff ...// output processed stuff to output variable
out_variable_name = weird_stuff_we_processed;
}
GLSL defined the in
and out
keywords to have inputs and outputs on the individual shaders.
The vertex and fragment shaders differ a bit sometimes.
layout (location = 0)
, the location can be 1, 2 if we provide other data such as color or texture co-ordinates as well and specified it's layout in vertex attribute.
vec4
color output variable, since it needs to generate a final output color. If failed to specify an output color in the fragment shader, the color buffer output for those fragments will be undefined (which usually means OpenGL will render them either black or white).