Shader

On this page 7

A shader is a small program that runs on the graphics processor (GPU) to compute part of a rendered image, such as the position of vertices, the color of pixels or the result of a general computation. In game engines, shaders define how every surface looks, from metal reflections and skin to water, fog and post-processing, and they execute in massive parallel for millions of vertices and pixels every frame.

The name comes from the original task of computing shading, meaning light and color, but modern shaders do far more. They are written in shading languages such as HLSL, GLSL or MSL, or built visually in node-based editors, then compiled for the specific GPU and graphics API the game uses.

Where shaders run in the rendering pipeline

The rendering pipeline is the sequence of stages that turns 3D geometry into a 2D image. Several of these stages are programmable, and each programmable stage runs a specific type of shader. The engine supplies data such as meshes, textures, light parameters and camera matrices, and the shaders transform that data into the final picture.

The two essential stages are the vertex shader, which positions each vertex on screen, and the pixel shader, also called a fragment shader, which calculates the color of each covered pixel. Other stages are optional or specialized, as the table shows.

Shader typeRuns once perTypical tasks
Vertex shaderVertexTransform positions, skinning, wind animation
Pixel or fragment shaderPixel sampleLighting, texturing, material appearance
Compute shaderWork item in a thread groupParticles, culling, post-processing, simulation
Mesh and task shadersGroup of primitivesFlexible geometry processing and culling
Ray tracing shadersRay or hitReflections, shadows and global illumination
Tessellation and geometry shadersPatch or primitiveSubdivision and primitive generation
Main shader types

How a shader computes a surface

A pixel shader for a typical material samples several textures, such as base color, normal, roughness and metalness maps, and combines them with light information in a lighting model. Modern engines use physically based rendering (PBR), which describes surfaces with measurable properties so that materials look consistent under any lighting.

Shaders run on the GPU as many small threads executing the same instructions on different data. This architecture rewards uniform work and penalizes divergent branching, where neighboring pixels take different code paths. Shader cost is therefore measured in instructions, texture fetches and register usage, and an expensive shader can limit performance even when the scene has few objects.

FactorWhy it mattersCommon optimization
Texture samplesMemory bandwidth and latencyPack channels into fewer textures
Instruction countArithmetic time per pixelMove work to vertex stage or precompute
Branch divergenceThreads wait for each otherUse shader variants or uniform conditions
Register pressureFewer threads in flightSimplify temporary values
OverdrawSame pixel shaded many timesSort opaque objects, limit transparency
Factors that drive shader cost

Writing shaders in game engines

Most engines let developers create shaders without writing low-level code. Unreal Engine uses a node-based Material Editor, Unity offers Shader Graph, and Godot uses its own shading language similar to GLSL together with a visual editor. These tools generate shader code that the engine compiles into many permutations, one for each combination of features, lights and platforms.

ApproachStrengthLimitation
Visual node editorAccessible to artists, fast iterationCan produce inefficient code
Hand-written HLSL or GLSLFull control and optimizationRequires graphics programming skill
Engine shading languageIntegrated with engine featuresTied to one engine
Precompiled shader libraryConsistent results across a teamLess flexibility per asset
Shader authoring approaches

Shaders are compiled from source into an intermediate representation, such as DXIL for DirectX 12 or SPIR-V for Vulkan, and then into GPU-specific machine code by the graphics driver. On consoles, the hardware is fixed, so shaders can ship fully compiled; on PC, the final step usually happens on the player’s machine.

Shader compilation and stutter

Because the driver must compile shaders for the exact GPU and driver version, a PC game may encounter a new shader the first time an effect appears. If compilation happens in the middle of gameplay, the frame waits, and the player sees a hitch lasting tens or hundreds of milliseconds. This is known as shader compilation stutter.

  • Precompilation at startup compiles known pipelines before play, at the cost of a waiting screen.
  • Shader cache files store compiled results so that later sessions skip compilation.
  • Pipeline state collection during testing records which combinations the game really uses.
  • Asynchronous compilation uses a simpler fallback until the real shader is ready.

Driver updates and game patches can invalidate a shader cache, which is why the first launch after an update often runs worse. Some platforms distribute precompiled caches for common hardware to reduce this effect.

Common pitfalls with shaders

The most frequent problem in production is permutation explosion: every optional feature doubles the number of variants, which inflates build times, memory use and compilation stutter. Teams counter it by limiting toggles and sharing master materials across many assets.

Other pitfalls include precision errors on mobile GPUs using half-precision math, inconsistent results between graphics APIs, and effects that look correct in the editor but break under temporal anti-aliasing or upscaling because they produce noise that the reconstruction cannot resolve. Testing shaders under final rendering settings on target hardware avoids most of these surprises.

A plain-language summary

A shader is a tiny set of instructions that tells the graphics card how to paint each part of a game scene. One shader decides where the corners of a model go on screen, another decides what color each dot should be.

Think of a painter who receives a recipe for every surface: shiny metal, rough stone, wet asphalt. The graphics card follows thousands of these recipes at once, many times per second. The key takeaway: shaders decide how a game looks, and complex ones also decide how fast it runs.

An example from practice

A small team building a rainy city level in a game engine wants streets that look wet. A technical artist creates a material in the engine’s node editor that darkens the base color, lowers roughness and adds animated ripple normals driven by time. A second shader adds puddles using a mask texture, so water collects only in lower areas.

Profiling shows the effect costs too much on mid-range GPUs because every street pixel samples eight textures. The artist packs roughness, puddle mask and ripple height into the channels of one texture and moves a calculation from the pixel shader to the vertex shader. The streets keep their look while the frame time drops by about one millisecond, and the team precompiles the new shader variants so players do not see stutter when it first starts raining.