A swap chain is a series of framebuffers, or locations in computer memory, that are used to render an image before it is displayed on an actual monitor or other device. There are always at least two framebuffers in a swap chain because the first, or active, buffer is usually identified as the one currently being shown on the display. While one framebuffer is being displayed, the next buffer can be rendered and prepared for display and then swapped to the device as necessary. Using a swap chain can eliminate some of the problems that occur because of conflicts between hardware and software timing, as well as graphical artifacts that can appear because of rendering directly to the display device.
The way an image is shown on a monitor is really just a matter of placing the correct sequence of numbers into a specified area of memory. Whatever is in this area of memory is drawn on the screen several times every second. A screen is drawn in horizontal lines from the top left corner to the bottom right corner. The moment when the screen is starting the next frame, when it moves from the bottom right corner of the screen to the upper left corner, is known as the vertical retrace.
Implementations of a swap buffer only draw to the screen during the vertical retrace. If this timing is off, or if a program attempts to draw to a screen during periods when a frame is being rendered, then a graphical anomaly known as a tear can appear. This occurs when the image in the display device’s memory is suddenly changed in the middle of rendering, causing one side of the display to show the last frame, and the rest of the screen to show the new one.
By rendering a scene ahead of time into a framebuffer, the next frame to be displayed is already prepared and can be quickly pushed into the display memory so it is shown. While one image is displayed, the next is being drawn in the inactive framebuffer, also called a back buffer. This creates smooth animations and visual effects that can be exactly timed to prevent tearing.
The term "swap chain" is more commonly known as double buffering or triple buffering, depending on the number of frames being drawn in advance of the active framebuffer. Using nanosecond or millisecond timing algorithms is an essential part of using a swap chain to ensure that the maximum frame rate of the monitor not exceeded and that programs can pace their own processing and resources on a system. Employing a swap chain, if properly implemented, can result in smooth animations regardless of the properties of a display device or the amount of time it takes to render a single frame of animation.