1. Home
  2. Explore
  3. Computer Science
  4. Video Game Graphics Pipeline

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Video Game Graphics Pipeline

How Video Games Turn Ones and Zeroes into 3D Graphics

A modern video game scene may appear to contain a realistic city, battlefield, train station, or magical landscape. Internally, however, the scene is represented by numerical data: geometric points, triangles, colors, textures, camera information, and light sources. A graphics processing unit, or GPU, repeatedly processes this data to produce the two-dimensional image shown on the display.

A complex scene can contain millions of vertices, which are points in three-dimensional space, and millions of triangles, which are formed by connecting groups of three vertices. For example, a detailed locomotive can be built from hundreds of thousands of vertices and hundreds of thousands of flat triangular surfaces. Materials, colors, and textures are assigned to those surfaces, while virtual lights illuminate them.

The GPU performs this work many times per second. At a frame rate of 120120120 frames per second, a new image must be produced approximately every 8.38.38.3 milliseconds:

1 second120 frames≈8.3 milliseconds per frame\frac{1\text{ second}}{120\text{ frames}} \approx 8.3\text{ milliseconds per frame}120 frames1 second​≈8.3 milliseconds per frame

The central process for producing these images is the graphics rendering pipeline. Its three core stages are:

  1. Vertex shading
  2. Rasterization
  3. Fragment shading

Modern games may add other stages, but these three form the foundation of real-time 3D graphics.

The rendering pipeline converts three-dimensional geometric data into a two-dimensional image by transforming vertices, identifying covered pixels, and calculating the appearance of those pixels.

Think About It

A realistic scene is ultimately made from points, triangles, colors, and numerical calculations. Which part of the process must determine where an object appears before its color and lighting can be calculated?

Vertex Shading: Placing Objects on the Screen

The first major stage is vertex shading. Its purpose is to determine where each vertex in the three-dimensional world should appear in the two-dimensional image seen by the camera.

A game world may contain many more objects than the player can currently see. The camera’s field of view determines which portion of the world is visible. Objects outside that region do not need to be rendered for the current frame. This reduces the amount of work the GPU must perform.

For example, a scene might contain 1,1001{,}1001,100 separate models, while the camera’s field of view includes only 600600600 of them. The GPU can focus primarily on those visible objects.

From a model to a triangle mesh

Although a locomotive may have rounded surfaces, it is represented internally by a collection of flat triangles. A model might contain:

  • Hundreds of thousands of vertices
  • Hundreds of thousands of triangles
  • Several materials or colors
  • Textures assigned to different surfaces

The train appears to move as one object, but the GPU processes the vertices individually. Once the vertices are moved into their proper screen positions, the triangles connecting them move into position as well.

To understand the process, consider one vertex. Its position is transformed through several coordinate systems.

The three coordinate transformations

The vertex begins in model space. Model space describes the vertex relative to the object itself. For example, a vertex on a train is stored relative to the train’s own origin, regardless of where the train is located in the game world.

The vertex then passes through three conceptual transformations:

  1. Model space to world space
  2. World space to camera space
  3. Camera space to screen space

1. Model space to world space

The first transformation places the object into the larger game world. The GPU uses the model’s:

  • Position
  • Scale
  • Rotation

A vertex that is located near the center of a train model can therefore be moved, enlarged or reduced, and rotated along with the complete train.

2. World space to camera space

The second transformation expresses the scene relative to the camera. The camera’s position and rotation determine how the world appears from the player’s viewpoint.

A stationary tree may have fixed world coordinates, but its camera-space coordinates change when the player turns or moves. This is why the same object appears in different locations on the screen as the camera changes.

3. Camera space to screen space

The final transformation uses the camera’s perspective field of view to project the three-dimensional scene onto a two-dimensional view screen. The result includes:

  • An XXX coordinate for horizontal screen position
  • A YYY coordinate for vertical screen position
  • A ZZZ value representing depth or distance from the camera

The transformations are performed using transformation matrices. The relevant coordinates, object data, camera data, and field-of-view information are placed into matrices and multiplied together. Matrix multiplication combines the separate transformations into a result that tells the GPU where the vertex belongs in the final image.

The ZZZ value is especially important because two objects may occupy the same screen location while being at different distances. The depth information is used later to determine which object should be visible.

Why GPUs are suited to vertex shading

A detailed scene may require the GPU to transform millions of vertices. Each transformation involves substantial matrix arithmetic, but the same type of calculation can be performed independently for many vertices. This makes the work highly parallel.

A GPU contains many computational cores designed to perform large numbers of multiplication and addition operations simultaneously. By distributing vertex coordinates and transformation data among these cores, the GPU can process huge triangle meshes quickly enough for real-time graphics.

After the vertices of a triangle have been transformed, the GPU knows where all three corners of that triangle appear on the view screen. The triangle is now ready for rasterization.

Think About It

Why is it useful to store a vertex in model space instead of storing its final screen position permanently? The answer is that the same model can be translated, rotated, scaled, and viewed from different camera positions without changing its original geometry.

Rasterization: Converting Triangles into Pixels

Once the vertices have been placed on the two-dimensional screen, the next task is to determine which pixels are covered by each triangle. This process is called rasterization.

A display is made from a grid of pixels. A 444K display has a resolution of 3840×21603840 \times 21603840×2160, which produces approximately 8.38.38.3 million pixels:

3840×2160=8,294,4003840 \times 2160 = 8{,}294{,}4003840×2160=8,294,400

The GPU examines the screen-space XXX and YYY coordinates of a triangle’s three vertices. From those coordinates, it determines which pixels lie inside the triangle.

The portions of a triangle associated with particular groups of pixels are called fragments. A fragment can be understood as pixel-related data produced by a rasterized triangle. The fragment will later receive a final color during fragment shading.

The GPU repeats this process for every triangle in the scene. Once all relevant triangles have been rasterized and colored, their results are placed into a frame buffer, which stores the image that will be sent to the display.

The final pixel colors are commonly represented using red, green, and blue values, or RGB values.

The visibility problem

Many triangles overlap from the camera’s point of view. For example:

  • A train may block part of a station.
  • The back side of a train may be geometrically present but hidden.
  • Two objects may occupy similar positions on the screen while having different depths.

The GPU must decide which triangle is visible at each pixel. This is called the visibility problem.

The Z-buffer

The visibility problem is solved using a Z-buffer, also called a depth buffer. In addition to storing the color of each pixel, the Z-buffer stores a depth value for each pixel.

When a triangle is rasterized, the GPU calculates the depth of the triangle at every covered pixel. It compares that depth with the depth already stored in the Z-buffer.

  • If the new triangle is closer to the camera, its color replaces the existing pixel color, and its depth replaces the stored depth.
  • If the new triangle is farther away, it is discarded for that pixel.
  • The result is that the closest visible surface remains in the final image.

If smaller depth values represent objects closer to the camera, the process can be summarized as:

Replace the pixel if Znew<Zstored\text{Replace the pixel if } Z_{\text{new}} < Z_{\text{stored}}Replace the pixel if Znew​<Zstored​

A depth-buffer image can be visualized with black representing nearby surfaces and white representing distant surfaces.

Because the three vertices of a triangle may have different depths, the depth is not necessarily constant across the triangle. The GPU calculates an appropriate depth for each individual pixel. This allows intersecting triangles to be handled correctly, even when their surfaces cross or overlap.

Jagged edges and anti-aliasing

Rasterization works with a square grid of pixels, but geometric edges can run at any angle. If a diagonal triangle edge passes through the center of a pixel, a basic rasterizer may color the entire pixel as though the triangle completely covered it. This creates a jagged, stair-step appearance called aliasing.

A method for reducing this effect is Super Sampling Anti-Aliasing, or SSAA. Instead of examining only one location within a pixel, the GPU examines multiple sampling points. In the example described, 161616 sampling points are distributed across each pixel.

If a triangle covers only some of those sampling points, the GPU applies a proportional color contribution. For example, a triangle covering roughly half the sample points produces an intermediate or partially blended color at that pixel. The edge therefore appears faded rather than sharply jagged.

SSAA improves the appearance of diagonal edges because it estimates partial coverage instead of treating every pixel as entirely covered or entirely uncovered.

Fragment Shading: Making Pixels Look Realistic

Rasterization determines which pixels belong to each triangle, but assigning a simple base color is not enough to create a convincing image. A black metal locomotive cannot be made realistic by coloring every one of its pixels with the exact same black value.

The scene must account for:

  • The direction of the light
  • The strength or intensity of the light
  • The direction the surface faces
  • The camera’s position
  • Reflections and shininess
  • Shadows and illumination from other objects
  • The time of day and surrounding environment

The stage that calculates the appearance of each rasterized pixel is called fragment shading. A fragment shader determines the final color of the fragments created during rasterization.

For a metal train, for example:

  • Surfaces facing the light become brighter.
  • Surfaces facing away from the light become darker.
  • Highlights appear where light reflects strongly from the surface.
  • Different parts of the train receive different amounts of illumination.
  • Nighttime scenes use weaker ambient illumination and may rely on sources such as fire.

Even older games use simplified versions of this idea. Surface colors can change according to the position of lights and the presence of shadows.

Surface Normals and Light Direction

To calculate how brightly a triangle should be illuminated, the GPU needs two directions:

  1. The direction from the surface toward the light
  2. The direction that the surface itself faces

The direction a triangle faces is called its surface normal. A surface normal is perpendicular to the plane of the triangle. An analogy is a flagpole standing straight out from a flat piece of ground: the direction of the flagpole represents the normal direction of the ground.

The angle between the light direction and the surface normal determines how much light reaches the surface. Let that angle be θ\thetaθ. The cosine of the angle gives the basic directional lighting factor:

  • If the surface faces directly toward the light, cos⁡(θ)=1\cos(\theta)=1cos(θ)=1.
  • If the surface is perpendicular to the light direction, cos⁡(θ)=0\cos(\theta)=0cos(θ)=0.
  • If the surface faces away from the light, the cosine would be negative.

A simplified lighting contribution can therefore be represented as:

Directional light=max⁡(0,cos⁡(θ))×light intensity×surface color\text{Directional light} = \max\bigl(0,\cos(\theta)\bigr) \times \text{light intensity} \times \text{surface color}Directional light=max(0,cos(θ))×light intensity×surface color

The maximum operation prevents a negative lighting value. A surface facing away from a light source should not receive a negative amount of light.

The GPU also adds ambient light, which represents general background illumination. Without ambient light, surfaces facing away from a direct light source could become unnaturally pitch-black. A simplified total color can be expressed as:

Final color=Directional contribution+Ambient intensity×surface color\text{Final color} = \text{Directional contribution} + \text{Ambient intensity} \times \text{surface color}Final color=Directional contribution+Ambient intensity×surface color

Ambient intensity can be stronger in a daytime scene and closer to zero at night.

Multiple light sources

If a scene contains several lights, the GPU performs a similar calculation for each light source. The contributions are then added together:

Total lighting=∑i=1ncontribution from light i\text{Total lighting} = \sum_{i=1}^{n} \text{contribution from light } iTotal lighting=i=1∑n​contribution from light i

This produces illumination from different directions and with different intensities. However, many lights require more computation. To maintain performance, a game may limit the number of active lights or limit each light’s range so that distant triangles ignore it.

Vector and dot-product calculations

In actual graphics mathematics, light directions and surface normals are represented as vectors. The cosine between two vectors can be found using their dot product and magnitudes:

cos⁡(θ)=a⋅b∥a∥∥b∥\cos(\theta) = \frac{\mathbf{a}\cdot\mathbf{b}} {\|\mathbf{a}\|\|\mathbf{b}\|}cos(θ)=∥a∥∥b∥a⋅b​

Here, a\mathbf{a}a and b\mathbf{b}b represent the two directions, and ∥a∥\|\mathbf{a}\|∥a∥ and ∥b∥\|\mathbf{b}\|∥b∥ represent their magnitudes. This calculation gives the directional relationship needed for lighting.

Matrices handle coordinate transformations, while vectors and dot products help determine how surfaces interact with light.

Think About It

A surface facing directly toward a light source has a larger lighting contribution than a surface facing sideways. This occurs because the first surface receives the light more directly, while the second presents less of its area toward the light.

Flat Shading and Smooth Shading

A simple triangle has one surface normal, so every pixel within that triangle receives the same surface direction for lighting calculations. This produces flat shading.

Flat shading works well for intentionally angular objects, but it can look unrealistic on curved surfaces. A curved engine body is represented by many flat triangles. If every triangle has one constant normal, the boundaries between triangles may become visibly distinct.

Vertex normals

To create a smoother appearance, the renderer can assign a normal to each vertex rather than using only one normal for the entire triangle.

A vertex normal is calculated by averaging the normals of the triangles adjacent to that vertex. Since neighboring triangles on a curved surface point in slightly different directions, their averaged normal represents a smoother approximation of the underlying curve.

Barycentric interpolation

The renderer then uses barycentric coordinates to interpolate the three vertex normals across the triangle.

The process is similar to blending three colors across a triangular region:

  • Each triangle vertex has its own normal.
  • A pixel near one vertex receives more influence from that vertex’s normal.
  • A pixel near the center receives a proportional mixture of all three vertex normals.
  • The resulting normal changes gradually across the triangle.

The GPU calculates an interpolated normal for each fragment using the fragment’s position and the three vertex normals. Lighting is then calculated using that interpolated normal.

When many triangles are arranged to form a curved object, these smoothly changing normals produce a continuous gradient of brightness. The object appears rounded even though its geometry is still made from flat triangles.

Rendering Every Frame

The entire process is repeated for every frame because the camera, objects, and lights may all be moving.

For each new frame, the GPU must potentially:

  1. Transform the vertices according to object and camera motion.
  2. Rasterize the transformed triangles.
  3. Resolve visibility with depth values.
  4. Calculate lighting and material appearance.
  5. Store the resulting colors in the frame buffer.
  6. Send the completed image to the display.

At 120120120 frames per second, these operations must be completed approximately every 8.38.38.3 milliseconds. This repeated recalculation is what allows a moving game world to appear continuous rather than like a sequence of separate still images.

Ray Tracing and DLSS

The three core pipeline stages provide the main method for rendering real-time game scenes, but modern GPUs may add specialized techniques.

Ray tracing

Ray tracing calculates the paths of rays of light to produce highly detailed lighting, reflections, and shadows. It is widely used for extremely realistic images in film and television, where a single frame may take many minutes or more to render.

In games, the traditional rendering pipeline usually handles the primary visibility and shading calculations. Ray tracing may be added selectively to calculate effects such as:

  • Shadows
  • Reflections
  • More accurate lighting

This allows games to gain some benefits of ray tracing without requiring every part of the frame to be rendered entirely through the slower method.

DLSS

Deep Learning Super Sampling, or DLSS, is an upscaling technique. The game first produces a frame at a lower resolution. A convolutional neural network then processes that image and generates an image at a higher target resolution, such as 444K.

In the described workflow:

  1. The graphics pipeline and ray-tracing calculations produce a lower-resolution frame.
  2. DLSS processes that frame.
  3. The result is upscaled to the desired output resolution.

This can reduce the amount of work required to generate the initial frame while still producing a high-resolution result.

Specialized GPU resources

Recent GPUs may contain separate categories of computational resources:

  • CUDA or shading cores, which execute the main graphics rendering pipeline
  • Ray-tracing cores, which accelerate ray-tracing calculations
  • Tensor cores, which execute neural-network operations such as those used by DLSS

When a game uses the standard pipeline, ray tracing, and DLSS together, these different resources can operate on their specialized tasks. This makes it possible to combine advanced lighting with high-resolution output while keeping frame times low.

Other advanced graphics topics include shadows, reflections, UV coordinates, and normal maps. These techniques build on the same basic framework: geometry is transformed, surfaces are mapped to pixels, and the appearance of those pixels is calculated.

Comprehensive Review

A video game begins with a digital description of a scene. Objects are represented by vertices connected into triangular meshes, while materials, textures, cameras, and lights provide the information needed to make those objects visible and believable.

During vertex shading, each vertex is transformed from model space into world space, then camera space, and finally screen space. Transformation matrices calculate its screen position and depth.

During rasterization, the GPU determines which screen pixels are covered by each transformed triangle. The covered regions become fragments, and the Z-buffer ensures that nearer surfaces replace farther surfaces at each pixel.

During fragment shading, the GPU calculates the appearance of each fragment. Light direction, surface normals, material color, ambient illumination, highlights, shadows, and other effects determine the final RGB values.

Flat shading uses one normal for an entire triangle, while smooth shading interpolates vertex normals across triangles to create gradual lighting changes on curved surfaces. Anti-aliasing reduces jagged edges by estimating how much of a pixel is covered by a triangle.

The complete pipeline must run again for every frame because the camera, objects, and lighting may change. GPUs make this possible by performing enormous numbers of similar calculations in parallel. Ray-tracing cores can add more accurate reflections, shadows, and lighting, while Tensor cores can run DLSS to upscale lower-resolution frames into higher-resolution images.

The realistic scene on the screen is therefore the visible result of repeated geometric transformations, pixel coverage tests, depth comparisons, vector calculations, lighting operations, and specialized hardware working together within milliseconds.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent