2025-04-30 18:15:19 +03:30
---
title: The Graphics Pipeline
date: "April 20 - 2025"
---
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
Ever wondered how games put all that gore on your display? All that beauty is brought into life by
a process called **rendering**, and at the heart of it, is the **graphics pipeline**.
In this article we'll dive deep into the intricate details of this beast.
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
Like any pipeline, the **graphics pipeline** is comprised
2025-04-30 18:15:19 +03:30
of several **stages**, each of which can be a pipeline in itself or even parallelized.
2025-05-05 16:54:00 +03:30
Each stage takes some input (data and configuration) to generate some output data for the next stage.
We can coarsely divide the pipeline into **4 stages**:
2025-04-30 18:15:19 +03:30
2025-05-05 16:54:00 +03:30
```math
\texttt{Application} \rightarrow \color{#fabd2f}{\texttt{GeometryProcessing}}\color{none} \rightarrow \texttt{Rasterization} \rightarrow \texttt{PixelProcessing}
```
2025-04-30 18:15:19 +03:30
2025-05-05 16:54:00 +03:30
The pipeline will then serve the output of the **pixel processing** stage, which is a **rendered image**,
2025-04-30 18:15:19 +03:30
to your pretty eyes using your display.
2025-05-05 16:54:00 +03:30
But to avoid drowning you in overviews, let's jump right into the gory details of the **geometry processing**
2025-04-30 18:15:19 +03:30
stage and have a recap afterwards to demystify this 4-stage division.
2025-02-15 17:14:28 +03:30
2025-04-30 18:15:19 +03:30
## Surfaces
2025-05-05 16:54:00 +03:30
Ever been jump-scared by this sight in an FPS? Why are things rendered like that?
In order to display a scene (like a murder scene),
we need to have a way of **representing** the **surface** of the composing objects (like corpses) in computer-memory.
2025-04-30 18:15:19 +03:30
We only care about the **surface** since we won't be seeing the insides anyways---Not that we want to.
At this stage, we only care about the **shape** or the **geometry** of the **surface**.
2025-05-05 16:54:00 +03:30
Texturing, lighting and all the sweet gory details comes at a much later stage once all the **geometry** have been processed.
2025-04-30 18:15:19 +03:30
2025-05-05 16:54:00 +03:30
But how do we represent surfaces in computer-memory?
2025-04-30 18:15:19 +03:30
## Vertices
2025-05-05 16:54:00 +03:30
There are several ways to **represent** the surfaces of 3d objects for a computer to understand.
2025-04-30 18:15:19 +03:30
For instance, **NURB surfaces** are great for representing **curves**
and it's all about the **high-precision** needed to do **CAD**.
We could also do **ray-tracing** using fancy equations for rendering **photo-realistic** images.
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
These are all great--ignoring the fact that they would take an eternity to process...
But what we need is a **performant** approach that can do this for an entire scene with
hundereds of thousands of objects (like a lot of corpses) in under a small fraction of a second. What we need is **polygonal modeling**.
2025-04-30 18:15:19 +03:30
2025-05-05 16:54:00 +03:30
**Polygonal modeling** enables us to do an exciting thing called **real-time rendering**. The idea is that we only need an
2025-04-30 18:15:19 +03:30
**approximation** of a surface to render it **realisticly-enough** for us to have some fun killing time!
We can achieve this approximation using a collection of **triangles**, **lines** and **dots** (primitives),
which themselves are composed of a series of **vertices** (points in space).
2025-02-15 17:14:28 +03:30
2025-04-30 18:15:19 +03:30
A **vertex** is simply a point in space.
Once we get enough of these **points**, we can conncet them to form **primitives** such as **triangles**, **lines** and **dots**.
2025-05-05 16:54:00 +03:30
And once we connect enough of these **primitives** together, they form a **model** or a **mesh** (that we need for our corpse).
With some interesting models put together, we can compose a **scene** (like a murder scene :D).
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
But let's not get ahead of ourselves. The primary type of **primitive** that we care about during **polygonal modeling**
2025-04-30 18:15:19 +03:30
is a **triangle**. But why not squares or polygons with variable number of edges?
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Why Triangles?
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
In **Euclidean geometry**, triangles are always **planar** (they exist only in one plane),
any polygon composed of more than 3 points may break this rule, but why does polygons residing in one plane so important
to us?
Being non-planar makes it rather difficult to determine
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
When a polygon exists only in one plane, we can safely imply that **only one face** of it can be visible
at any one time, this enables us to utilize a huge optimization technique called **back-face culling**.
Which means we avoid wasting a ton of **precious processing time** on the polygons that
we know won't be visible to us. We can safely **cull** the **back-faces** since we won't
be seeing the **back** of a polygon when it's in the context of a closed off model.
We figure this by simply using the **winding-order** of the triangle to determine whether we're looking at the
back of the triangle or the front of it.
Normal surface
Triangles also have very small **memory footprint**, for instance when using the **triangle-strip** topology (more on this very soon), for each additional triangle after the first one, only **one extra vertex** is needed.
The most important attribute however (in my opinion) is the **algorithmic simplicity**.
Any polygon or shape can be composed from a **set of triangle**, for instance a rectangle is
simply **two co-planar triangles**.
It is becoming an increasingly more common practice in computer science to break down
hard problems into simpler, smaller problems. This will be more convincing when we cover the **rasterization** stage :)
Bonus point: present day **hardwares** and **algorithms** have become **extremely efficient** at processing
triangles (sorting, rendering, etc) after eons of evolving around them.
## Primitive Topology
So, we got our set of triangles, but how do we make a model out of them?
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Indices
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Input Assembler
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Coordinate System -- Local Space
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Coordinate System -- World Space
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Coordinate system -- View Space
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Coordinate system -- Clip Space
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Coordinate system -- Screen Space
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Vertex Shader
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Tessellation & Geometry Shaders
2025-02-15 17:14:28 +03:30
2025-05-05 16:54:00 +03:30
## Let's Recap!
2025-04-25 17:13:38 +03:30
## Rasterizer
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Pixel Shader
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Output Merger
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## The Future
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Conclusion
2025-02-15 17:14:28 +03:30
2025-04-25 17:13:38 +03:30
## Sources
2025-02-15 17:14:28 +03:30
[Tomas Akenine Moller - Real-Time Rendering 4th Edition](https://www.realtimerendering.com/intro.html)
2025-04-25 17:13:38 +03:30
<br/>
2025-02-15 17:14:28 +03:30
[LearnOpenGL - Hello Triangle](https://learnopengl.com/Getting-started/Hello-Triangle)
[LearnOpenGL - Face Culling](https://learnopengl.com/Advanced-OpenGL/Face-culling)
[Wikipedia - Polygonal Modeling](https://en.wikipedia.org/wiki/Polygonal_modeling)
[Wikipedia - Non-uniform Rational B-spline Surfaces](https://en.wikipedia.org/wiki/Non-uniform_rational_B-spline)
[Wikipedia - Computer Aided Design (CAD)](https://en.wikipedia.org/wiki/Computer-aided_design)
[Stackoverflow - Why do 3D engines primarily use triangles to draw surfaces?](https://stackoverflow.com/questions/6100528/why-do-3d-engines-primarily-use-triangles-to-draw-surfaces)