OpenGL, Vulkan, D3D12, Metal, WebGPU
Every shader in this course runs on top of a graphics API: the interface between your program and the GPU driver. The playgrounds use WebGL2, a browser version of OpenGL ES 3.0. Game engines sit on top of Direct3D 12, Vulkan or Metal and hide the differences. Graphics programmers work one level lower, inside the engine, and need to know what each API asks of them.
This lesson is the map. The next three lessons zoom in on Vulkan and WebGPU, with code.
The pipeline is the same everywhere
All five APIs drive the same hardware pipeline from the GPU Pipeline lesson:
vertex + index buffers
│
┌──────▼───────┐ per vertex: object → clip space
│ vertex shader │ (optional: tessellation, geometry, or task/mesh shaders)
└──────┬───────┘
│ clip, divide by w (→ NDC), viewport transform
┌──────▼───────┐
│ rasterizer │ triangles → fragments, varyings interpolated
└──────┬───────┘
┌──────▼───────┐
│ fragment / │ per pixel: color (and optionally depth)
│ pixel shader │
└──────┬───────┘
│ depth/stencil test, blending
┌──────▼───────┐
│ render target │ swapchain image or an offscreen texture
└──────────────┘
compute shaders run outside this chain, on buffers and imagesWhat differs is who manages the state around it: who allocates memory, who tracks which resource is being read or written, when the work actually starts, and how shaders find their textures and buffers.
The five APIs
| OpenGL / GLES / WebGL | Vulkan | Direct3D 12 | Metal | WebGPU | |
|---|---|---|---|---|---|
| Style | implicit, global state machine | explicit | explicit | explicit, friendlier | explicit, safe (validated) |
| Platforms | desktop GL on Windows/Linux (deprecated on macOS), GLES on Android, WebGL in browsers | Windows, Linux, Android, Nintendo Switch; macOS/iOS through MoltenVK (on Metal) | Windows 10/11, Xbox | macOS, iOS, iPadOS, visionOS | browsers; also native via Dawn (C++) and wgpu (Rust) |
| Shader language | GLSL / GLSL ES | SPIR-V bytecode (written in GLSL, HLSL, Slang…) | HLSL compiled to DXIL with DXC | Metal Shading Language (C++-based) | WGSL |
| Binding model | texture units, uniform locations, UBO binding points | descriptor sets + pipeline layouts, push constants | root signatures, descriptor heaps, root constants | argument tables (setFragmentTexture:atIndex:), argument buffers |
bind groups + bind group layouts |
| Memory | driver-managed | you allocate (heaps, memory types) | you allocate (heaps, committed/placed resources) | mostly managed, heaps optional | managed |
| Synchronization | implicit | you: barriers, semaphores, fences | you: resource barriers, fences | mostly automatic hazard tracking, fences/events | automatic |
| Multithreaded recording | no (one context per thread, painful) | yes, command buffers on any thread | yes, command lists | yes | single queue; encoding on the main thread or a worker |
A few takeaways:
- OpenGL is still the gentlest way to learn and is fine for tools and small games, but it is no longer evolving: OpenGL 4.6 (2017) is the last version, and Apple's drivers stop at 4.1 and are deprecated. Its driver does a lot of hidden work (tracking state, inserting synchronization), which costs CPU time and makes performance unpredictable.
- Vulkan, D3D12 and Metal are the "explicit" generation. The application does what the GL driver used to guess: memory, synchronization, pipeline state compiled up front. That means far more code and much more predictable performance. Vulkan and D3D12 are very close conceptually; knowing one makes the other mostly vocabulary.
- WebGPU is designed on top of the three modern APIs, so its concepts (pipelines, bind groups, command encoders) look like Vulkan/Metal, but it does memory and synchronization for you and validates everything. It is the best way to learn modern API structure, which is why it gets its own lesson: WebGPU and WGSL.
Same words, different names
| Idea | OpenGL / Vulkan / WebGPU | Direct3D | Metal |
|---|---|---|---|
| per-pixel program | fragment shader | pixel shader | fragment function |
| constant data | uniform buffer | constant buffer (cbuffer) |
buffer argument |
| small constants set per draw | push constants (Vulkan) | root constants | setBytes |
| the image you draw into | framebuffer attachment / color attachment | render target view | render pass color attachment |
| baked state + shaders | pipeline | pipeline state object (PSO) | render pipeline state |
| recorded GPU work | command buffer / command encoder | command list | command buffer + encoder |
Conventions that bite when porting
The APIs do not agree on coordinates. This model takes one clip-space position (the output of a vertex shader) and runs the fixed-function steps after it: divide by w, then the default viewport transform of each API, for an 800 × 600 target.
#include <cstdio>
struct Clip { double x, y, z, w; };
// Default viewport transform for each API; returns false when the point is outside the depth range.
// GL: NDC z in [-1, 1], window origin bottom-left, NDC +y = up.
// Vulkan: NDC z in [0, 1], framebuffer origin top-left, NDC +y = DOWN.
// D3D12 / Metal / WebGPU: NDC z in [0, 1], origin top-left, NDC +y = up.
bool toWindow(const char* api, Clip c, double W, double H) {
double x = c.x / c.w, y = c.y / c.w, z = c.z / c.w; // perspective divide -> NDC
double px = (x + 1) * 0.5 * W;
double rowFromTop, depth;
bool gl = api[0] == 'O', vk = api[0] == 'V';
if (gl) { rowFromTop = H - (y + 1) * 0.5 * H; depth = (z + 1) * 0.5; if (z < -1 || z > 1) return false; }
else if (vk) { rowFromTop = (y + 1) * 0.5 * H; depth = z; if (z < 0 || z > 1) return false; }
else { rowFromTop = (1 - y) * 0.5 * H; depth = z; if (z < 0 || z > 1) return false; }
std::printf(" %-22s pixel (%5.1f, %5.1f from top) depth %.3f\n", api, px, rowFromTop, depth);
return true;
}
int main() {
const char* apis[] = {"OpenGL", "Vulkan", "D3D12/Metal/WebGPU"};
Clip points[] = {{1.0, 1.0, 1.0, 2.0}, {0.0, 0.0, -0.5, 1.0}};
for (Clip c : points) {
std::printf("clip (%.1f, %.1f, %.1f, %.1f):\n", c.x, c.y, c.z, c.w);
for (const char* api : apis)
if (!toWindow(api, c, 800, 600)) std::printf(" %-22s clipped: z/w outside the depth range\n", api);
}
}clip (1.0, 1.0, 1.0, 2.0):
OpenGL pixel (600.0, 150.0 from top) depth 0.750
Vulkan pixel (600.0, 450.0 from top) depth 0.500
D3D12/Metal/WebGPU pixel (600.0, 150.0 from top) depth 0.500
clip (0.0, 0.0, -0.5, 1.0):
OpenGL pixel (400.0, 300.0 from top) depth 0.250
Vulkan clipped: z/w outside the depth range
D3D12/Metal/WebGPU clipped: z/w outside the depth range
Two lessons from this model:
- The Y axis. The same NDC point lands at the top in OpenGL and D3D-style APIs, but at the bottom in Vulkan. Engines fix this with a negative viewport height (allowed since Vulkan 1.1), or by flipping Y in the projection matrix.
- The depth range. An OpenGL projection matrix maps the near plane to −1. Used unchanged in Vulkan, D3D or Metal, everything in the near half of the depth range is clipped away, and depth precision is wasted. Use a projection built for 0..1 (GLM's
GLM_FORCE_DEPTH_ZERO_TO_ONE, see Math Libraries), and consider reversed Z (near = 1, far = 0) for better precision.
Texture coordinates have a similar split: OpenGL's texture origin is the bottom-left, the others use the top-left, which is why images loaded for GL are often flipped on upload.
Which one should you learn?
- To understand graphics: WebGL2 (this course) or OpenGL 3.3+ with learnopengl.com. Fastest to a lit, textured scene.
- To learn modern API structure with less pain: WebGPU.
- For a career in engine or rendering work: Vulkan or D3D12. Studios on Windows and Xbox live in D3D12; Android, Linux, Steam Deck and many cross-platform engines use Vulkan. Metal if you target Apple platforms.
- Engines use all of them. Unity runs on D3D11/12, Vulkan, Metal, OpenGL ES and WebGL/WebGPU; Unreal 5 on D3D12, Vulkan and Metal; Godot 4 on Vulkan, Direct3D 12, Metal and an OpenGL-based Compatibility renderer. Shader cross-compilation (next lessons and the Toolkit) is how one shader source reaches all of them.
Test yourself
Which API consumes shaders as SPIR-V bytecode?
- OpenGL ES 3.0
- Vulkan
- Metal
- WebGPU
A projection matrix written for OpenGL is used unchanged in Direct3D 12. What goes wrong?
- Textures are flipped
- OpenGL maps depth to -1..1, so the part of the scene mapped below 0 is clipped and precision is wasted
- Nothing, NDC is identical
- Colors are in gamma space
What is the Direct3D name for a fragment shader?
- Pixel shader
- Raster shader
- Surface shader
- Hull shader