A code blog
Intro to Shaders & Resource List
What is a Shader?
I find shaders to be a really good introduction to graphics programming. This is for a couple reasons. One is the quick feedback loop. Maybe its just my ADHD talking, but I love being able to make changes and immediately have some visual feedback as to what impact my changes had. This makes shaders a very exploratory space, where you can just do whatever and discover new techniques by accident. Another reason is that they are relatively math heavy, so it will help you get over that fear/mountain quickly if that's a problem for you.
So what is a shader exactly? I think most people that would be interested in this topic probably have some intuition about what a shader is. Generally speaking a shader is just a small program that is compiled and run on the GPU. Shaders are just a type of GPU program that is used specifically to draw things to the screen. I'm not super into building diagrams, but I think this topic is best explained visually, so instead of giving you an exhaustive intro here, I'm just going to link you to existing resources that will do a better job than I could ever dream of. Check the resource list at the end of this for more.
Here's what I will say: our screens are all backed by little LEDs (or groups of LEDs) that essentially
map 1:1 to pixels of images. If you know anything about images, then you know that images which can
be represented as pixels are called
Raster Images. This means that the job of any graphics
program is to essentially tell the screen what color to display at a certain point. And that's exactly
what shaders do. You can think of the GPU as a raster-image-producing-machine. Your job as the engineer
is to provide some code that makes the GPU produce these images in a certain way.
Vertex and Fragment Shaders
Technically there are a bunch of different types of shaders. Geometry, tesselation, mesh, etc. You can look these up indepdently to read about what they do. For now all that really matter up front are the Vertex and Fragment shaders. Put very simply these shaders are just tiny programs that you give the GPU to help it produce a final raster image. These programs run in parallel for a bunch of different data points.
vertex shader is called once for each
Vertex in the objects being rendered. Vertices help
defined the bounds of any geometry we might want to render, whether 2d or 3d. If you've ever messed around
with blender then you've probably directly manipulated vertices, edges, and faces. If you have a 2D plane
defined by four vertices, then your vertex shader will be called once for each of those four vertices! The job
of the vertex shader is to tell the GPU where to render that particular vertex. Once it has all the info
for all of the vertices in your object, it will then
rasterize that object into a 2d image made of
fragments. These fragments are pixels that will be used to draw the object to the screen.
The fragment shader is then called once for each
fragment produced by the GPU's rasterizer. Please note
that there is most definitely NOT a 1:1 relationship between vertices and fragments. You could have a large
triangle defined by only three vertices, the corners of the triangle, that actually takes up thousands of pixels on
screens. 3 vertices, 1000's of pixels/fragments. The vertex shader would be called 3 times, the fragment shader would
be called thousands of times. Where the vertex shader's job is to position a vertex, the fragment shader's job is to
color a pixel. It's really that simple.
These little programs can be written in a couple of different languages, it really depends on what
you are using. All these languages are mostly the same with some quirks. They all look very much like C.
The most common graphics API's are:
Up next I'll show you some examples of simple shaders and do some fun stuff to draw different shapes on a plane. Be ready for some trig! It's super fundamental to writing shaders. Also be aware that realtime rendering is not all there is to graphics! There's a ton of other graphics-related disciplines, such as the rendering work done in movies and animated films, datavis stuff, etc.
Here are some resources I recommend or used during my initial learning phase. I'm still working through some of them actively. Please note that I'm not affiliated in any way with any of these resources.
- Fundamentals of Graphics, 4th Edition This is the course textbooks for Cem Yuksel's intro to graphics course, linked above
- Realtime Rendering This is a much more advanced textbook covering realtime rendering in depth
- Game Engine Architecture
Ask me questions on twitter or email me at firstname.lastname@example.org