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

flowchart LR CPU[CPU application code] --> Buffers[Vertex/index buffers] CPU --> Uniforms[Uniforms / constants] CPU --> Pipeline[Graphics pipeline state] Buffers --> VS[Vertex shader] Uniforms --> VS VS --> Assembly[Primitive assembly] Assembly --> Rasterizer[Rasterizer] Rasterizer --> FS[Fragment shader] Uniforms --> FS FS --> Tests[Depth / stencil / blend] Tests --> Framebuffer[Framebuffer / swapchain image] Framebuffer --> Screen[Screen]

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

flowchart TD A[Application prepares scene] --> B[Input assembly] B --> C[Vertex shader] C --> D[Primitive assembly] D --> E[Clipping and viewport transform] E --> F[Rasterization] F --> G[Fragment shader] G --> H[Per-fragment tests] H --> I[Color blending] I --> J[Framebuffer]

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.

sequenceDiagram participant App as CPU application participant Driver as Graphics API / driver participant GPU as GPU pipeline participant VS as Vertex shader participant FS as Fragment shader App->>Driver: Create buffers, shaders, pipeline App->>Driver: Upload vertices and uniforms App->>Driver: Submit draw command Driver->>GPU: Schedule GPU work GPU->>VS: Run once per vertex GPU->>GPU: Assemble, clip, rasterize triangles GPU->>FS: Run once per fragment GPU->>GPU: Test, blend, write framebuffer

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_position is the vertex position from the vertex buffer.
  • vec4(a_position, 1.0) converts it into homogeneous coordinates.
  • u_mvp is a model-view-projection matrix supplied by the application.
  • gl_Position is 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.

flowchart LR Position[Vertex position] --> VS[Vertex shader] Color[Vertex color] --> VS Matrix[MVP matrix uniform] --> VS VS --> Clip[gl_Position] VS --> Varying[Output color / varyings]

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_color is an interpolated value received from the vertex shader.
  • vec4(v_color, 1.0) adds an alpha value of 1.0, making the output fully opaque.
  • out_color is 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.

flowchart LR Interpolated[Interpolated inputs] --> FS[Fragment shader] Textures[Textures] --> FS Uniforms[Material / light uniforms] --> FS FS --> Color[Output color] Color --> Tests[Depth, stencil, blending] Tests --> Target[Framebuffer]

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.

flowchart TD V0[Vertex 0: red] --> VS0[Vertex shader] V1[Vertex 1: green] --> VS1[Vertex shader] V2[Vertex 2: blue] --> VS2[Vertex shader] VS0 --> R[Rasterizer interpolates outputs] VS1 --> R VS2 --> R R --> F1[Fragment: mostly red] R --> F2[Fragment: mixed color] R --> F3[Fragment: mostly blue] F1 --> FS[Fragment shader] F2 --> FS F3 --> FS FS --> FB[Framebuffer]

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.