How The 3D Rendering Pipeline Works
Modern 3D rendering is a pipeline: the CPU prepares data and commands, then the GPU runs many small programs in parallel to turn vertices into pixels on the screen.
Shaders are those small GPU programs. They are written in a shader language such as GLSL, compiled for the graphics API, and executed by the GPU as part of a configured graphics pipeline.
Key Terms
| Term | Meaning |
|---|---|
| Vertex | One input point of geometry. A vertex usually contains a position and may also contain color, normal, texture coordinates, or other attributes. |
| Vertex attribute | One piece of per-vertex data read from a vertex buffer, such as a_position, color, or texture coordinates. |
| Vertex buffer | GPU memory containing vertex data for one or more shapes. |
| Index buffer | GPU memory containing integer indices that reuse vertices from a vertex buffer, often to build triangles without duplicating shared corners. |
| Uniform | Data supplied by the application that is constant for many shader invocations, such as a transform matrix, camera position, light color, or material value. |
| Shader | A small GPU program used by a pipeline stage. The two most common shaders are vertex shaders and fragment shaders. |
| Vertex shader | A shader that runs once per input vertex and outputs a transformed position, plus optional values to pass forward. |
| Primitive | A basic shape assembled from vertices: usually a triangle, line, or point. |
| Primitive assembly | The pipeline stage that groups processed vertices into primitives, such as every three vertices becoming one triangle. |
| Rasterization | The process of converting a primitive, such as a triangle, into fragments that cover screen samples or pixel-like locations. |
| Fragment | A candidate contribution to a pixel produced by rasterization. A fragment is not guaranteed to become visible because depth, stencil, and blending still happen afterward. |
| Fragment shader | A shader that runs once per fragment and computes color or other per-fragment outputs. |
| Interpolation | The GPU-created blending of vertex shader outputs across a primitive, so each fragment receives values appropriate for its position inside the triangle. |
| Framebuffer | The render target that receives final color, depth, or stencil results. It may be an off-screen image or an image that will later be presented. |
| Swapchain image | A presentable framebuffer image owned by the graphics API and window system, eventually shown on the screen. |
| Pipeline state | The configured GPU behavior for a draw, including shaders, vertex layout, rasterization rules, depth testing, blending, and output formats. |
Big Picture
The CPU does not usually draw every pixel itself. Instead, it uploads geometry, textures, uniforms, and pipeline state to the GPU. Then it submits draw commands. The GPU executes the configured stages for many vertices and fragments in parallel.
Main Pipeline Stages
The exact set of stages depends on the API and pipeline configuration, but the common flow is:
- Input assembly reads vertex data, such as positions, normals, colors, and texture coordinates.
- The vertex shader runs once for each input vertex.
- Primitive assembly groups vertices into triangles, lines, or points.
- Clipping removes geometry outside the visible area.
- Rasterization converts triangles into fragments, which are potential pixels.
- The fragment shader runs once for each generated fragment.
- Depth, stencil, and blending decide whether and how the fragment updates the framebuffer.
What Shaders Are
A shader is a program that runs on the GPU. Unlike normal CPU code, shaders are designed to run thousands or millions of times in parallel.
For example:
- A vertex shader might run 3 times for one triangle, once per vertex.
- A fragment shader might run hundreds, thousands, or millions of times for that same triangle, once per covered screen sample or pixel-like fragment.
Shaders do not run freely whenever they want. They run because a draw command reaches a pipeline stage that uses them.
Vertex Shaders
A vertex shader processes one vertex at a time. Its most important job is to output the final clip-space position of that vertex.
In OpenGL GLSL, that output is usually assigned to gl_Position:
gl_Position = u_mvp * vec4(a_position, 1.0);
That line means:
a_positionis the vertex position from the vertex buffer.vec4(a_position, 1.0)converts it into homogeneous coordinates.u_mvpis a model-view-projection matrix supplied by the application.gl_Positionis the transformed position used by later pipeline stages.
Vertex shaders can also pass extra values to later stages, such as color, normals, or texture coordinates.
Fragment Shaders
A fragment shader decides the color, and sometimes other outputs, for one fragment. A fragment is produced by rasterization when a primitive covers part of the framebuffer.
A simple fragment shader might do only this:
out_color = vec4(v_color, 1.0);
That line means:
v_coloris an interpolated value received from the vertex shader.vec4(v_color, 1.0)adds an alpha value of1.0, making the output fully opaque.out_coloris written to the active color attachment or framebuffer.
Fragment shaders are where lighting, textures, material color, fog, transparency, and many visual effects are usually calculated.
Vertex Shader vs Fragment Shader
| Feature | Vertex Shader | Fragment Shader |
|---|---|---|
| Runs for | Each vertex | Each fragment generated by rasterization |
| Main responsibility | Transform geometry | Compute final surface color |
| Required output | Vertex position, such as gl_Position |
Usually a color output |
| Typical inputs | Vertex attributes and matrices | Interpolated values, textures, material data |
| Common work | Model/view/projection transforms, skeletal animation | Lighting, texture sampling, color effects |
| Execution count | Usually lower | Often much higher |
How They Relate
The vertex shader and fragment shader are connected through interpolated outputs.
If a triangle has three vertex colors, the vertex shader outputs one color for each corner. During rasterization, the GPU interpolates those colors across the triangle. The fragment shader receives the interpolated color for the exact fragment it is shading.
This is why a triangle can smoothly blend from one vertex color to another even when the fragment shader only receives one v_color value per fragment. The GPU creates those per-fragment values between the vertex and fragment stages.
Although OpenGL and Vulkan configure pipelines differently, the conceptual data flow is the same: vertices are transformed, primitives are rasterized, fragments are shaded, and the final image is written to a framebuffer.