Introduction

So far, every demo on this site has drawn straight to the screen. But the graphics card is just as happy to draw into a texture, which can then be used, like any other texture, in a later draw. That one idea, called rendering to a texture, is behind an enormous number of effects. This post, the first in the Render Targets & Post-Processing series, covers:

  • What a render target is
  • The simplest use: a live video screen inside a scene
  • Multiple passes, where one pass’s output is the next pass’s input
  • Ping-ponging between two render targets to build up effects over time

What Is a Render Target?

In part 1 of the fundamentals, the graphics card wrote its pixels into a framebuffer, the block of memory that becomes the picture on screen. A render target is simply another block of memory you can tell it to draw into instead. It’s usually a texture, so after you’ve drawn into it, you can sample it in a shader exactly like an image loaded from a file. It can also have its own depth buffer (part 11), so 3D scenes drawn into it work properly.

In WebGL, a program makes a render target by creating an empty texture and attaching it to a framebuffer object. While that framebuffer is selected, everything drawn lands in the texture instead of on screen; selecting the normal framebuffer again goes back to drawing on screen.

A Screen Inside the Scene

The demo below draws each frame in two passes. First, it draws the scene from the red security camera into a small render target. Then it draws the scene again from your viewpoint, to the screen, using that render target as the texture on the TV. Drag to look around.

A live security camera feed, drawn into a render target and shown on a TV

Things to try:

  • Lower the render target width to 16 or so: the TV picture becomes blocky, because the render target has fewer pixels. Its resolution has nothing to do with the screen’s. Games use this freely: reflections, distant views and effects are often drawn at half or quarter resolution to save time.
  • Tick Bilinear filtering: the render target is now sampled like any texture (texture filtering), so the blocks blend smoothly.
  • The security camera’s picture doesn’t include the TV. If it did, drawing the TV into the render target would mean reading the render target while drawing into it. That’s called a feedback loop: it isn’t allowed, and WebGL reports an error. Games that need it (a TV showing itself, or two facing mirrors) use the previous frame’s picture instead.

Passes and Post-Processing

Each “draw everything into one target” step is called a pass. Real games use many passes per frame. A typical modern frame might draw the scene from each light’s point of view to work out shadows (Shadow Mapping), then the scene itself, then the lighting, then see-through objects, then a chain of post-processing passes that each read the previous result and write a changed version: blurs, a glow around bright lights, colour adjustments, smoothing of jagged edges and so on. The final pass writes to the screen. The rest of this series looks at some of those passes.

A post-processing pass usually draws just a single triangle (or rectangle) that covers the whole render target, with a fragment shader that reads the previous pass’s texture. Its fragment shader runs once for every pixel, which is exactly what you want for an effect that changes the whole image.

Ping-Pong: Building Up Effects over Time

What if an effect needs to read last frame’s result, for example to leave trails? A pass can’t read and write the same texture, so the trick is to use two render targets and swap them every frame: read from A and write to B, then read from B and write to A. This is called ping-ponging.

The demo below does exactly that. Each frame reads the previous frame, draws it slightly zoomed, turned and faded, then paints a new blob of colour on top. Move your pointer over it (or tap and drag) to paint.

Ping-pong feedback: each frame is built from the last one

The same idea, used far more subtly, powers modern techniques for smoothing jagged edges and sharpening upscaled pictures, which blend each new frame with carefully lined-up previous ones, and many simulations of water, smoke and fire that run entirely on the graphics card.

Under the hood

Switching render targets isn’t free. Most mobile graphics cards, and many desktop ones to some degree, are tile-based: they split the target into small tiles and keep each tile in a small, very fast memory inside the chip while drawing it, writing it out to main memory only once it’s finished. Every time you switch to a new render target, the finished tiles of the old one have to be written out, and if you later read it back as a texture, it has to come back in from main memory. That memory traffic often costs more than the drawing itself. That’s why graphics APIs let a program say whether a target’s old contents need to be loaded and whether its results need to be kept, so the driver can skip unnecessary copies, and why engines try to combine passes where they can.

Questions You Might Have

Is the screen a render target too?

Yes. The picture that ends up on screen is drawn into the default framebuffer, which the browser or operating system provides. Your own render targets work just like it, except you can read them afterwards as textures.

Summary

  • A render target is a texture (plus optional depth buffer) that the graphics card draws into instead of the screen.
  • It can then be sampled like any texture, in a later pass.
  • Its resolution is independent of the screen’s.
  • A pass can’t read the target it’s writing; ping-ponging between two targets lets effects build up over time.
  • Modern frames are chains of many passes, ending with post-processing.

Next: alpha blending and transparency, and why see-through objects are surprisingly hard.

Further Reading

  • LearnOpenGL, Framebuffers: render targets and simple post-processing in OpenGL.
  • WebGL2 Fundamentals, Rendering to a Texture: the same in WebGL2, step by step.
  • Adrian CourrĆØges, GTA V: Graphics Study: a frame of a real game taken apart pass by pass.
  • Wikipedia, Tiled rendering: how tile-based graphics cards work, and why render target switches matter on them.