type
Post
status
Published
date
Jan 8, 2025
slug
graph-api-and-shaders
summary
A plain-language explanation of shader compilation: what shaders are, why GPU-specific machine code must be built locally, how graphics APIs changed the workflow, and why the first launch can take several minutes.
tags
Development
Tools
category
Technology
icon
password
paired_with
1751d487-a2a1-80fb-bdff-d7a5648b6115
lang
translation_locked
source_hash
ab3b53509a53d3d386878ad08a54a29f57e4385c86f4216931d241f548ac7ef7
If you play PC games, you have probably seen a progress bar that says something like "compiling shaders." It often appears the first time you launch a new game on Steam, and sometimes it sits there for several minutes. What is it compiling? Why does it need to compile anything? Why was it not already compiled before the game shipped? This post tries to explain it in plain language.
What A Shader Is
Every pixel you see in a game is calculated by a small program running on the GPU. That program is called a shader. Lighting, shadows, water reflections, skin material, particle effects - all of these are handled by shaders.
A shader is a program written for the graphics card. It is not quite like writing C for a CPU. The two most common types are vertex shaders, which decide where each vertex appears on screen, and fragment shaders, which decide what color each pixel should be.
Why It Needs To Be Compiled
Game developers usually write shaders in higher-level languages such as HLSL or GLSL. The GPU cannot execute those directly. They have to be translated into machine code that your specific graphics card and driver understand. That translation step is shader compilation.
The tricky part is that GPU machine code is not one universal standard like x86 on the CPU side. NVIDIA, AMD, and Intel each have different hardware and driver behavior. The same shader source may need to compile into different machine code on different machines.
That is why the developer cannot always compile everything before shipping the game. They do not know exactly which GPU and driver version you will use. The game ships with shader source code or an intermediate representation, and your machine compiles it for your hardware the first time it runs.
A Short History Of Graphics APIs
Shader compilation sits inside the larger history of graphics APIs.
OpenGL started in 1992 from Silicon Graphics and became a cross-platform standard. For a long time it was one of the default choices for PC graphics. Its rough timeline looks like this:
- 1992: OpenGL 1.0
- 2004: OpenGL 2.0 introduced GLSL
- 2010: OpenGL 4.0
- 2017: OpenGL 4.6, with no major version after that
DirectX is Microsoft's stack for Windows. Direct3D 12 gives developers lower-level control over GPU memory and command queues. The performance ceiling is higher, but the developer has more responsibility. Most modern AAA PC games use DX12.
Vulkan is often treated as OpenGL's successor. Khronos released it in 2016. Its design is similar to DX12: explicit control and low overhead. It is cross-platform and open. Games such as Doom Eternal and No Man's Sky use it heavily.
Metal is Apple's graphics API, released in 2014. It is the only modern graphics API on Mac and iPhone, and it is tuned closely for Apple's own GPUs, especially the M-series chips.
Why Compilation Takes So Long
A modern AAA game can contain thousands of shader variants: different light counts, materials, post-processing effects, and graphics settings. Each combination may need its own compiled version on your machine. DX12 and Vulkan also make developers think more explicitly about when compilation happens, so many games choose to compile a large set of shaders up front to avoid stutter during gameplay.
That progress bar is usually doing exactly that. It compiles the shaders the game expects to need, stores the resulting GPU machine code locally, and then reuses the cache later. The second launch is faster because the cache already exists.
The cache can become invalid. If you change GPUs, update your driver, or install a game patch, the cached shader code may no longer match the environment. Then the game has to compile again. That is why a driver update can make the same game show the shader compilation screen again.
Bottom Line
The next time you see "compiling shaders," it does not necessarily mean the game is wasting your time. It is building hardware-specific instructions for your GPU. Doing that work once up front is often better than letting the game stutter every time a new effect appears.
References
- Author:LeoQin
- URL:https://leoqin.com/en/article/graph-api-and-shaders
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!