Video Game Graphics Pipeline


Video Game Graphics Pipeline

Corey
Video games can transport us into detailed cities, battlegrounds, magical worlds, and natural environments. But an apparently realistic scene is really just data being processed inside a graphics card or game console. For example, the train-station scene discussed here contains about 2.1 million vertices assembled into 3.5 million triangles, with 976 colors and textures and a virtual sun illuminating everything.

Maria
So how does a computer turn billions of ones and zeroes into a convincing three-dimensional image? The answer is a graphics rendering pipeline. Although modern games may add extra stages, three core steps form the backbone: vertex shading, rasterization, and fragment shading.

Corey
Let’s begin with vertex shading. The basic job is to take the geometry and meshes of objects in three-dimensional space and calculate where they belong on a two-dimensional image called the view screen. The camera’s field of view determines what the player can see, which reduces the amount of geometry that needs to be rendered.

Maria
In the train-station example, the scene contains 1,100 different models, but the camera’s field of view reduces that to about 600 visible objects. We can use the locomotive as a concrete example because, despite its rounded and complicated appearance, it is built from 762,000 flat triangles, 382,000 vertices, and nine materials or colors.

Corey
The entire train may appear to move as one object, but the computer actually transforms its hundreds of thousands of vertices individually. To move one vertex from the three-dimensional world to the two-dimensional screen, the system performs three transformations: model space to world space, world space to camera space, and finally the camera’s perspective view to the screen.

Maria
Model space describes the vertex’s original X, Y, and Z coordinates within the object. World space accounts for the model’s position, scale, and rotation in the scene. Then camera space uses the camera’s coordinates, rotation, and field of view to determine how that point appears from the player’s viewpoint.

Corey
Those values are placed into transformation matrices and multiplied together. The result gives the vertex’s X and Y position on the view screen, plus a Z value representing depth. That depth becomes important later when the system decides which objects are in front of, or hidden behind, other objects.

Maria
Once three vertices have been transformed, they define one triangle on the screen. The same matrix calculations are then applied to the rest of the train’s vertices and to the 2.1 million vertices belonging to the roughly 600 objects in view, moving all 3.5 million triangles onto the two-dimensional screen.

Corey
That sounds like an enormous amount of computation, and it is. But GPUs are specifically designed to process triangle meshes efficiently. One example GPU has roughly 10,000 cores and can execute up to 35 trillion 32-bit multiplication-and-addition operations per second, distributing vertex coordinates and transformation data across those cores.

Maria
That parallel processing is what allows a GPU to render millions of triangles every few milliseconds and produce 120 or more frames per second. After vertex shading has placed the triangles on a two-dimensional plane, the pipeline moves to rasterization.

Corey
Rasterization determines which specific display pixels are covered by each triangle. A 4K screen has a resolution of 3,840 by 2,160, or about 8.3 million pixels. Using the X and Y coordinates of a triangle’s three vertices, the GPU finds where that triangle falls within this enormous pixel grid.

Maria
The covered pixels are grouped into fragments. A fragment is a group of pixels that comes from the same rasterized triangle and shares that triangle’s texture or color. The GPU repeats this process for every triangle, applying red, green, and blue values to the appropriate pixels and building the image in a frame buffer before sending it to the display.

Corey
But triangles overlap constantly. The train can block the station behind it, and many triangles on the back of the train should not appear at all. Figuring out which surfaces are visible is called the visibility problem, and one important solution is the Z-buffer, also called the depth buffer.

Maria
The Z-buffer stores an additional depth value for every pixel. During vertex transformation, each vertex receives a Z value related to its distance from the camera. When a triangle is rasterized, its depth is compared with the depth already stored for each covered pixel.

Corey
If the new triangle is closer to the camera, it replaces the existing pixel color and updates the Z-buffer with its lower depth value. If it is farther away, its pixels are discarded, leaving the closer triangle visible. Since a triangle’s three vertices can have different depths, the GPU computes the depth separately for each individual pixel, which also lets intersecting triangles render correctly.

Maria
Rasterization can create another problem: jagged edges. If a triangle cuts across a pixel at an angle, the basic process may paint the entire pixel with the triangle’s color, producing a stair-step appearance.

Corey
A technique called Super Sampling Anti-Aliasing, or SSAA, reduces that effect. It distributes 16 sampling points across one pixel. If the triangle covers only some of those points, the pixel receives a corresponding fractional shade, creating a faded edge instead of a sharply jagged one.

Maria
And none of these calculations happen just once. The camera and objects are constantly moving during gameplay, so vertex shading, rasterization, and fragment shading are recalculated for every frame. At 120 frames per second, that means the work is repeated about every 8.3 milliseconds.

Corey
Now we reach fragment shading. Simply assigning a single color to every pixel would not make the scene look realistic. The GPU also needs to account for the direction and strength of light, the camera’s position, reflections, and shadows from other objects.

Maria
Imagine coloring the entire locomotive with the same black value. It would look flat and inaccurate. Fragment shading can make the bottom darker, the top lighter, and add specular highlights where light reflects from the metal, producing a much more convincing black train.

Corey
Lighting can also communicate time and environment. As the sun moves, the shading changes across the train. At night, surfaces become darker and may be illuminated by firelight. Even older games such as Super Mario 64 used simpler forms of shading to change surface colors according to lighting and shadows.

Maria
The basic lighting idea is straightforward: a surface facing directly toward a light source is brighter, while a surface facing perpendicular to, or away from, the light is darker. To calculate this, the GPU needs the direction of the light and the direction the triangle’s surface is facing.

Corey
That surface direction is called the surface normal. It is the direction perpendicular to the triangle’s plane, like a flagpole sticking straight out of a flat patch of ground. The GPU compares the light direction with this normal using the cosine of the angle between them.

Maria
The cosine is 1 when the surface faces the light directly and 0 when the surface is perpendicular to the light. The GPU multiplies that cosine value by the light’s intensity and by the material’s color, adjusting the triangle’s red, green, and blue values to produce the appropriate brightness.

Corey
A surface facing away from the light should not become unrealistically negative or completely black just because the cosine is zero or negative. So the calculation uses a minimum of zero and adds ambient light multiplied by the surface color. Ambient intensity can be higher in daytime scenes and closer to zero at night.

Maria
With multiple light sources, the GPU performs the lighting calculation for each source using its own direction and intensity, then adds the contributions together. However, many individual lights are computationally expensive, so games may limit the number of lights or restrict how far each light’s influence reaches.

Corey
The underlying mathematics involves vectors, matrices, trigonometry, and linear algebra. For example, instead of treating the cosine calculation as a simple geometric shortcut, the GPU can use the dot product of the light-direction and surface-normal vectors, divided by the norms of those vectors.

Maria
There is also an important difference between flat shading and smooth shading. With flat shading, every triangle has one normal, so the entire triangle receives one consistent color. That can look unrealistic on a curved surface such as the body of a steam engine.

Corey
Smooth shading solves this by assigning a normal to each vertex. Each vertex normal is calculated from the average of the normals of its adjacent triangles. Then barycentric coordinates create a gradual blend of those three vertex normals across the triangle.

Maria
You can picture barycentric interpolation as mixing three colors across a triangle, except the quantities being mixed are normal directions. For the center of a particular pixel, the GPU uses the triangle’s coordinates and the three vertex normals to calculate that pixel’s blended, or barycentric, normal.

Corey
When many triangles form a curved object, these changing normals create a gradient of angles between the surface and the light. The result is pixel-by-pixel variation in brightness and smooth shading across what would otherwise look like a collection of flat triangular faces.

Maria
That covers the three core stages: vertex shading places geometry on the screen, rasterization determines which pixels each triangle covers, and fragment shading gives those pixels realistic illumination and color. Together, they explain how numerical data becomes the image displayed during gameplay.

Corey
Modern graphics can also add specialized techniques. Ray tracing is mainly associated with highly detailed scenes and accurate lighting and reflections, although movie-quality ray tracing can take dozens of minutes or more for a single frame. In games, the main visibility and shading work still comes from the rendering pipeline we described, while ray tracing may be used for shadows, reflections, and improved lighting.

Maria
DLSS, or deep learning super sampling, is different. It takes a lower-resolution frame and upscales it to a 4K frame using a convolutional neural network. In the sequence described here, DLSS runs after ray tracing and after the graphics pipeline has produced the lower-resolution frame.

Corey
Recent GPUs can have three separate computational architectures: CUDA or shading cores for the graphics rendering pipeline, ray-tracing cores for ray tracing, and Tensor cores for DLSS. When a high-end game uses ray tracing and DLSS together, all three types can operate at the same time, helping produce 4K frames in under 10 milliseconds.

Maria
By comparison, relying only on the CUDA or shading cores could make a single frame take around 50 milliseconds in the example given. Other advanced graphics topics include shadows, reflections, UVs, and normal maps, but those build on the core pipeline we have just followed.

Corey
So the next time a game presents a realistic locomotive, a city, or a magical landscape, remember what is happening underneath: vertices are transformed through several coordinate systems, triangles are converted into pixel fragments, depth testing decides what is visible, and lighting calculations make the surfaces appear real.

Maria
And all of that is repeated frame after frame while the player and camera move. The spectacular image is the visible result of massive parallel computation involving geometry, matrices, pixels, depth values, colors, vectors, and light.

Ask anything
We've read this whole study set, so questions about it get the best answers.