Introduction#
Before we can talk about how a graphics card (also called a GPU, for graphics processing unit) draws a 3D world, we need to know what it’s actually drawing into. Every frame of every game ends up as the same thing: a grid of tiny coloured squares. This post covers:
- What a pixel is, and how a screen makes colours from red, green and blue light
- How colours are stored as numbers, both in memory and in shaders
- The framebuffer: the block of memory where the picture lives while it’s being drawn
The first demo runs on your own graphics card using WebGL, the browser’s built-in way for a web page to use the graphics card, like most demos on this site.
A Picture Is a Grid of Pixels#
Look closely enough at any screen and you’ll find a grid of tiny squares, each showing a single colour. Each is a pixel, short for “picture element”. A Full HD screen has 1920 columns and 1080 rows of them, just over two million; a 4K screen has four times as many.
Everything you see on screen, from text to faces to explosions, is just lots of single-coloured squares close together, which your eye blends into smooth shapes.
In the demo below, the graphics card draws a small landscape at the resolution you choose (the number of pixels across and down), then stretches it to fill the box without smoothing, so you can see every pixel. Drag the slider down to make the pixels huge, then hover over or tap a pixel (or select the image and use the arrow keys) to read its colour straight back from the graphics card.
The same picture at different resolutions
Some things to notice:
- At 8 or 16 pixels across, the shapes are barely recognisable, and the edges of the sun and hills turn into staircases. Those steps are called aliasing, or “jaggies”, and fighting them is a big topic later in the site.
- As the resolution increases, each pixel covers less of the scene and the picture gets sharper. But doubling the width and height needs four times as many pixels.
- Each pixel’s colour is just three numbers. Keep reading to see what they mean.
Mixing Colours from Red, Green and Blue#
Most screens can’t actually show “yellow” or “purple” directly. Instead, each pixel is made of three even smaller lights called sub-pixels: one red, one green and one blue. By setting how brightly each glows, the screen fools your eyes into seeing almost any colour: red and green together look yellow, all three at full brightness look white, and all three off is black. This is additive colour mixing, because the lights add together, unlike paint, which gets darker as you mix it.
Use the sliders to mix a colour. The left side shows what you’d see from a normal distance; the right side shows a few pixels close up, as if under a magnifying glass.
Mixing a colour from red, green and blue light
Real screens arrange their sub-pixels in different patterns (some phone screens share sub-pixels between neighbouring pixels, for example), but the idea is always the same: every colour is a mix of red, green and blue light.
Colours Are Just Numbers#
To a computer, each pixel’s colour is three numbers: how much red, green and blue. Computers store everything as bits, single on-or-off values written as 1 or 0, and the most common colour format uses 8 bits for each of red, green and blue. Eight bits can be set in 2 Ă— 2 Ă— 2 Ă— 2 Ă— 2 Ă— 2 Ă— 2 Ă— 2 = 256 ways, giving 256 levels for each colour, from 0 (off) to 255 (full brightness). A group of 8 bits is a byte, so each channel takes one byte.
That’s also where colour codes like #E8743B come from: the three numbers written in hexadecimal (counting in sixteens, using 0 to 9 and then A to F for ten to fifteen), two digits each for red (E8 = 232), green (74 = 116) and blue (3B = 59). You don’t need to read hexadecimal for this site.
Often there’s a fourth number, alpha, which usually stores how see-through a colour is. Red, green, blue and alpha together make 4 bytes per pixel, which is why the first demo multiplied the number of pixels by 4.
Shaders, the small programs that colour pixels (more on those later in the series), see colours differently. Inside a shader, each channel is a decimal number from 0.0 to 1.0, and a colour is a group of three numbers like vec3(0.91, 0.45, 0.23) (vec3 means “a group, or vector, of three numbers”: here red, green and blue). When the result is stored, the graphics card converts it back: 1.0 becomes 255, 0.5 about 128. Working with 0 to 1 keeps the maths simple: multiplying by 0.5 halves the value, whatever format the screen uses.
A value of 128 doesn’t actually produce half as much light as 255. Most images use a standard called sRGB, which spends more of the 256 levels on dark shades, because our eyes are better at telling dark shades apart. In sRGB, 128 gives only about a fifth of the light of 255. It’s a surprisingly important detail for lighting maths, and it gets its own post in the Textures & Sampling series.
The Framebuffer: Where the Picture Lives#
When a graphics card draws a frame, it doesn’t paint directly onto the glass of your screen. It writes pixel colours into a block of its own memory called a framebuffer: one slot for every pixel, in rows, with 4 bytes per pixel. A 1920 Ă— 1080 framebuffer is 1920 Ă— 1080 Ă— 4 = 8,294,400 bytes, a little over 8 megabytes.
If the screen showed the framebuffer while the graphics card was still drawing into it, you’d see half-finished frames. So games use (at least) two framebuffers:
- The front buffer, which the screen is currently showing
- The back buffer, which the graphics card is drawing the next frame into
When the frame is finished, the two swap roles: double buffering. A screen refreshing 60 times a second gives the graphics card about 16.7 milliseconds per frame, in which, at Full HD, it must colour over two million pixels, and usually many more, because parts of the scene get drawn on top of each other.
A part of the graphics card called the display controller reads the front buffer row by row, from the top of the screen to the bottom, and sends the colours down the cable (HDMI or DisplayPort) to the screen, over and over, every refresh. If the buffers swap while it’s halfway down the image, the top of the screen shows the old frame and the bottom shows the new one, with a visible line between them. That’s called screen tearing. V-sync avoids it by waiting to swap until the display controller has finished sending the current frame.
Questions You Might Have#
Why red, green and blue, and not red, yellow and blue like paint?
Screens add light together. Our eyes have three kinds of colour-sensing cells, and mixing red, green and blue light in different amounts can stimulate them in almost every combination we can see. Paints work the other way: each one absorbs some light, which is why mixing paints follows different rules.
Summary#
- A screen image is a grid of pixels, each a single colour.
- Each pixel’s colour is a mix of red, green and blue light, made by tiny sub-pixels.
- Colours are stored as numbers, usually 0 to 255 per channel in memory and 0.0 to 1.0 in shaders.
- The graphics card draws into a framebuffer in memory, and swaps finished frames to the screen.
Next: how the graphics card draws, through the graphics pipeline, the series of steps that turns triangles into pixels.
Further Reading#
- Wikipedia, RGB color model: how red, green and blue light combine into every colour a screen shows, and how the numbers are stored.
- Wikipedia, Pixel and Subpixel rendering: pixel layouts and the different sub-pixel patterns real screens use.
- Wikipedia, Multiple buffering: double and triple buffering, and why they exist.
- Wikipedia, sRGB: the colour standard most images and screens use.