Source: ViewEdit

Graphics programming

Developers coming from application or backend code usually meet graphics APIs through a stack of overlapping names — OpenGL, WebGL 2, Vulkan, Direct3D 12, Metal, and WebGPU — without a clear picture of what they actually send to a graphics card. Every one of those APIs exists to solve the same core problem: feed structured geometry, textures, and small GPU programs into a hardware pipeline that produces a frame of pixels, fast enough to keep up with a display. That job is distinct from running general-purpose parallel math (GPU compute) and from hardware video decoding (covered in GPU programming).

What a draw call actually does

At the center of graphics programming is the draw call — gl.drawArrays in WebGL, vkCmdDraw in Vulkan, or pass.draw in WebGPU. A draw call does not draw pixels directly from CPU code. Instead, it triggers a staged hardware pipeline that transforms inputs you already uploaded into GPU memory (allocated through the API, separate from CPU mmap) into colors written to a render target.

A typical draw call moves through four steps:

  1. Vertex shading. The GPU invokes your vertex shader once per vertex, reading vertex attributes (positions, normals, texture coordinates) or generating coordinates from the vertex index. Its main output is each vertex’s final position in clip space.

  2. Primitive assembly and rasterization. Fixed-function hardware groups vertices into triangles (or lines and points), clips anything outside the viewport, and figures out which pixel centers on the screen each triangle covers. Each covered pixel sample becomes a candidate fragment, with per-vertex values interpolated smoothly across the triangle’s face.

  3. Fragment shading. The GPU invokes your fragment shader in parallel across all covered fragments to decide each pixel’s color, sampling textures and evaluating lighting equations.

  4. Depth test, blending, and output. Fixed-function hardware checks whether each fragment is closer to the camera than what was already drawn, blends transparent colors if enabled, and writes the winning pixel into a framebuffer texture.

Putting that framebuffer on screen is a separate concern from the draw call itself. Graphics APIs do not create windows or manage input events. In native code, Vulkan, Direct3D 12, and Metal render into a swapchain — a small ring of images negotiated with the operating system’s window system (Win32, Wayland, X11, or AppKit) so the display compositor can show one completed frame while the GPU draws the next. In a browser, the <canvas> element supplies that swapchain surface on your behalf.

Stateful APIs: OpenGL, OpenGL ES, and WebGL 2

For decades, the standard way to drive that pipeline was OpenGL on desktop operating systems and its streamlined mobile sibling OpenGL ES. Both expose the GPU as a large global state machine. Your CPU code binds a shader program, binds vertex buffers and textures to numbered slots, toggles depth testing or blending flags, and issues a draw call. Because all of that state can mutate right up to the moment you draw, the vendor driver does substantial hidden work behind the scenes: validating combinations of state, tracking hazard dependencies between buffers, recompiling shader variants on the fly, and deciding when to transfer memory.

Browsers brought OpenGL ES to the web as WebGL:

  • WebGL 1.0 exposes OpenGL ES 2.0 inside a browser <canvas>, with shaders written in GLSL ES.

  • WebGL 2.0 exposes OpenGL ES 3.0, adding features such as 3D textures, uniform buffer objects, instanced rendering, and multiple render targets.

WebGL 2 remains widely supported and is still the lowest-friction way to put hardware-accelerated 2D or 3D graphics into almost any browser. Its biggest boundary is compute: WebGL 2 has no compute shader. OpenGL ES did not add compute shaders until OpenGL ES 3.1, and browser vendors chose to put their engineering effort into WebGPU rather than extending WebGL’s stateful model to ES 3.1. Developers who need general parallel math inside a WebGL 2 page have to pack numbers into floating-point textures, draw a full-screen rectangle, and run a fragment shader that writes results into an offscreen framebuffer — a clever workaround that feels indirect because you are bending a pixel pipeline into a number cruncher.

How WebGL reaches your card: ANGLE

When your JavaScript creates a webgl2 context and submits GLSL ES shaders, the operating system’s graphics driver rarely sees OpenGL ES calls at all. Major browsers route WebGL through ANGLE (Almost Native Graphics Layer Engine), a translation layer that validates your shaders for web safety and translates OpenGL ES commands into whichever native graphics API is best supported on the host OS — typically Direct3D 11 on Windows, Metal on Apple devices, and Vulkan or native OpenGL on Linux and Android. That indirection is why a WebGL page runs predictably on Windows laptops whose desktop OpenGL drivers are outdated or buggy, and why browser diagnostics like chrome://gpu frequently name Direct3D 11 or Metal underneath code written against WebGL 2.

WebGL routes through ANGLE, WebGPU through Dawn or wgpu, Vulkan on Apple through MoltenVK, and OpenGL through Zink

The explicit generation: Vulkan, Direct3D 12, and Metal

As GPUs grew more parallel and CPUs grew more cores, OpenGL’s hidden driver magic became a liability. A driver guessing your intent at draw time cannot easily accept commands recorded simultaneously from four CPU threads, and recompiling a shader on the first frame a new material appears causes visible frame stutters.

The modern generation of native APIs — Vulkan, Direct3D 12, and Metal — shifted that responsibility back to the application or engine:

  • Vulkan is the cross-vendor explicit API, running natively on Windows, Linux, and Android (and on Apple platforms through MoltenVK, which translates Vulkan onto Metal). You declare your shader stages, vertex layouts, and blend modes up front in immutable pipeline objects, record rendering work into command buffers, place explicit memory barriers between dependent stages, and submit command buffers to hardware queues. Vulkan does not parse text shaders at runtime; you compile GLSL or HLSL ahead of time into SPIR-V binary modules. To display frames on screen, a Vulkan application must also wire a window-system surface and swapchain.

  • Direct3D 12 occupies the same explicit tier on Windows, consuming shaders written in HLSL.

  • Metal is Apple’s explicit API across macOS and iOS, consuming Metal Shading Language (MSL).

All three can also dispatch compute shaders alongside rendering, which is why modern engines increasingly share one explicit device for both frame rendering and GPU compute (see GPU compute). The cost of that explicitness is upfront verbosity: drawing your first triangle in raw Vulkan takes hundreds of lines of setup compared with a few dozen in WebGL 2, though engines and higher-level wrappers amortize that setup once.

WebGPU’s render path

WebGPU brings the explicit generation to browsers — and to native applications through wgpu and Dawn. It is the successor direction to WebGL, not a minor version bump: it drops the OpenGL ES state machine completely and introduces a new shading language, WGSL.

A WebGPU draw follows the same explicit rhythm as Vulkan, Direct3D 12, and Metal, with safer defaults and far less boilerplate:

  1. Request an adapter and logical device from navigator.gpu (in a browser) or through a native wgpu/Dawn host.

  2. Configure a canvas context (or native swapchain) so the device knows which pixel format to render into.

  3. Compile WGSL vertex and fragment entry points and bake them into a GPURenderPipeline along with the vertex buffer layout and target format.

  4. Bind buffers and textures through explicit bind groups instead of mutating global slots.

  5. Open a GPUCommandEncoder, start a render pass targeting the canvas’s current texture, set the pipeline and bind groups, call pass.draw(…​), end the pass, and submit the finished command buffer to device.queue.

Underneath the browser, Chrome’s WebGPU implementation is Dawn, while Firefox and native Rust projects use wgpu. Both translate your WebGPU commands and WGSL shaders into Direct3D 12 on Windows, Metal on Apple devices, or Vulkan on Linux, Android, and Windows. You author one render pipeline and one WGSL shader, and the implementation handles the native explicit API beneath it.

Because WebGPU supports both render passes and compute passes on the exact same device and buffer model, a browser app no longer has to choose between clean drawing and real GPU math — you can update particle physics or flatten vector paths in a compute pass and draw the resulting buffers in a render pass inside the same command encoder.

Where to go next

  • Put drawing and compute side by side in a single local HTML file with Draw a triangle and run a compute shader.

  • Continue to GPU compute when the job is parallel math on buffers rather than pixels on a canvas.

  • Step back to GPU programming for how graphics, compute, hardware video, and translators fit together, or check GPU API lookup for quick tables.

  • If you are choosing a 2D UI stack rather than writing raw draw calls, see Graphics rendering engines (Skia, Graphite, Impeller, and compute-first renderers like Vello built on wgpu).