19. Layers and effects
On this page
- A drawing you can hold: render targets
- Filters
- How new paint meets old: blend modes
- The canvas that keeps everything: noClear
- Brighter than the screen: toneMap
- The canvas that remembers itself: feedback
- Putting it together: comets
- The whole stack in one block: compose
- Converging instead of brightening: the running mean
- When it gets slow
- Which half is slow: the cost row
- Record it once: batches
- More ways to use a finished layer: wipes, copies, and a readback
- Taking a layer in an order: wipes
- Laying a layer out again: copies
- The drawing read back: snapshot and the slit scan
- Where this comes from
- Go deeper
A layer is a second canvas off screen, one you can filter, blend, and keep from frame to frame. You learn to draw into one, run the filter catalog over it, and let a canvas feed its own past back in. The flock of Chapter 12 comes back as the comets above, drawn into a feedback layer that returns bloomed and added as light. After them come the compose block, the running mean, and what to do when a frame gets slow. Last come three more ways to use a finished layer: wipes, copies, and the layer read back by the sketch that drew it.
A drawing you can hold: render targets
Chapter 18 handed you a layer that a shader had filled, and .image put it on the canvas. Here you draw into a layer yourself, with the same calls you have used since Chapter 1. You make one and aim your drawing at it. Nothing appears yet, because the drawing is held, waiting for you to decide what happens to it. Make MySketches/:
import Ollin
final class FirstLayer: Sketch {
override func draw() {
background(.black)
let art = makeRenderTarget()
withTarget(art) {
background(Color(hex: 0x0E1B33))
noStroke()
for i in 0 ..< 14 {
let t = Double(i) / 14
fill(Color(hue: 0.52 + t * 0.38, saturation: 0.7, brightness: 0.95))
drawCircle(width * (0.16 + 0.68 * t),
height * 0.5 + sin(t * .tau * 1.5) * height * 0.2, 64)
}
}
drawImage(art.filtered(.gaussianBlur(radius: 45)).image, 0, 0) // soft, everywhere
drawImage(art.image, in: bounds.inset(by: .all(200))) // sharp, in front
}
}
Three calls carry the idea. makeRenderTarget() makes the layer. withTarget(art) { } sends everything drawn inside the block into it, the way withState { } scopes a transform, and a background(_:) inside clears just the layer. Then art.image hands the finished layer back as an image for Chapter 9's drawImage. So the same drawing can appear twice, once blurred across the whole canvas and once sharp in a card in front of its own blur. One drawing, held and then used, is what most of this chapter builds on.
Two habits follow from that. A makeRenderTarget() is a per-frame handle, so make it fresh inside draw() rather than storing it. The texture behind it is pooled and reused, so making one each frame is cheap. A layer holds one frame's drawing, so one kept in a property is empty again on any frame that draws nothing into it. And a layer that is never drawn back stays invisible, because withTarget only records the drawing, and drawImage is what puts it on screen.
The order of the blocks is yours to choose. A block can draw another layer's image wherever that layer's own block sits in draw(). Each layer is filled after the layers it reads. Two layers that draw each other are the one case with no such order. A note in the console then says which of them went first.
Here is the same idea as a picture, one thumbnail per stage. Two drawings each land in a layer, and each layer goes through a filter. The second filter is the glow the Filters section names. Then the canvas composites the results:
Filters
That .filtered( in the first listing was a filter. A filter reads a layer and hands back a new, transformed layer, with the original untouched. Each one is a pass or a few over the layer's pixels on the GPU. So a filter costs by the pixel and never by the number of marks. Filters also chain:
let moody = art.filtered(.posterize(levels: 5)).filtered(.vignette())Ollin ships dozens of them, in six families. The families are blur and glow, color and tone, stylize, retro, distortion (the warps that bend an image's coordinates), and design. Here is one photograph, a young woman in a lace headdress, through eleven of them:
A sheet like this can be one call. drawSheet takes a list of labeled items and lays them into a near-square grid of tiles. It draws each one through a closure you give it, and sets each label on its tile. So comparing a family of filters is a list and a line. Chapter 20 turns photographs into other kinds of picture with the stylize and design families.
The one to meet properly now is bloom, because the finished sketch rests on it. .bloom( finds the parts of the image brighter than threshold, blurs them, and adds the blur back. A bright mark then bleeds light into its surroundings the way a streetlight bleeds into fog. That is the difference between a white dot and a glowing dot.
Filters are also values, so you can keep several in a [Filter] array and pick one by index from a @Param. And postProcess( applies a filter to the whole finished frame with no layer at all, which is the quick way to make everything glow.
A filter does not care where its layer came from. Chapter 18's generate(_:) ran a shader over a whole layer, your own or one of the design generators Ollin ships. What came back was this same kind of layer. So a generated sky filters and composites like one you drew:
let sky = generate(.meshGradient(colors: [Color(hex: 0xE4572E), Color(hex: 0x2B6C8C),
Color(hex: 0xE8B44A), Color(hex: 0x7C3B5E)],
phase: 5.1))
drawImage(sky.filtered(.paperTexture()).image, 0, 0)
One generator and one filter, and the canvas is a printed poster. The generator makes a mesh gradient of soft color blobs melting into each other. Then .paperTexture() lays it onto a sheet of paper it synthesizes, with its crumples.
How new paint meets old: blend modes
So far every mark, on the canvas or in a layer, has covered what was under it. That is one way for new paint to meet old, and there are others. blendMode(_:) changes the arithmetic of that meeting. It is ordinary drawing state like fill, saved by withState { }, and it applies to shapes and to composited layers alike:
The one to learn first is .add. It sums colors the way light sums, so two faint marks make a brighter one and a thousand make a glow. Ollin blends color as physical amounts of light, called linear light. Appendix B explains it in one picture. Chapter 9's photo mosaic averaged pixels in it. So the sum behaves like lamps overlapping. Against a dark background, additive drawing stops reading as paint and starts reading as light. .multiply does the opposite. It stacks color like layered ink or gels, and it works on light backgrounds. The rest are variations on lighter and darker, and the figure shows each of the seven on the same two discs.
A mode set inside a layer's block only meets what that block has drawn, because a layer starts empty. There the modes that lighten leave the shape itself, and the modes that darken leave nothing, since there is nothing yet to darken. To make a whole layer lighten or darken the canvas, set the mode when you draw the layer back.
One pairing comes back through the chapter. A bloomed layer drawn with blendMode(.add) reads as added light rather than as a sticker laid over the scene. The finished sketch composites its comets that way.
The canvas that keeps everything: noClear
Every layer so far was made for one frame and thrown away with it. From here to the comets, the steps are about what a canvas can keep from one frame to the next. The plainest case is the canvas itself. Chapter 12 gave a first look: noClear() stops the canvas from being wiped between frames, and from then on drawing piles up. Pair it with .add and faint marks become deposits of light, arriving frame after frame, the long-exposure photograph as a drawing style. Make MySketches/:
import Ollin
final class Sandpainting: Sketch {
var grains: [Vector2] = []
override func setup() {
seed(6)
background(Color(hex: 0x05070C))
noClear()
toneMap(.aces, exposure: 1.5)
}
override func draw() {
if grains.isEmpty {
grains = (0 ..< 2600).map { _ in Vector2(random(width), random(height)) }
}
let field = curlField(scale: 0.0021)
grains = field.advected(grains, stepLength: 2.4)
blendMode(.add)
noStroke()
for (i, grain) in grains.enumerated() {
if !bounds.contains(grain) {
grains[i] = Vector2(random(width), random(height))
continue
}
let warm = Double(i % 5) / 5
fill(Color(hue: 0.06 + warm * 0.07, saturation: 0.75, brightness: 1, alpha: 0.045))
drawCircle(center: grain, radius: 1.6)
}
}
}
Each frame draws only 2,600 dots, each at an alpha of 0.045, barely visible alone. Six hundred frames later the canvas holds more than a million deposits. The curl field's eddies emerge as rivers of light, with advected carrying the grains the way it carried Chapter 14's dots. Nothing here is drawn as a line. The lines are where light kept landing.
Swift note.
grains.isEmptyis true while the list has nothing in it, so the scatter is rolled on the first frame only.continueskips the rest of the loop body for this grain and goes on to the next one. Chapter 11'sguardused it the same way.
While accumulating, background(_:) is the reset, so call it on the frame you want to wipe, or never. And a still additive scene only brightens, toward white, for as long as it runs, so keep something moving.
The pile does not run out of digits. A kept canvas holds about seven, so a very long exposure of very faint marks goes on adding up, and a fade painted over it keeps its rate. An ordinary layer holds about three, where a mark under a thousandth of what a pixel already holds adds nothing. noClear( is the kept canvas at those three digits and half the memory, for a very large export of a piece that does not need more.
Brighter than the screen: toneMap
That toneMap( line in the sandpainting needs explaining, because it solves a problem you now have. Additive light does not stop at full brightness. Three overlapping lamps sum to three times what the screen can show. Ollin composites every frame in a high-precision format that keeps those too-bright values. Then toneMap(_:) decides what happens when the frame finally meets the screen. The default clips every too-bright value to white, which is simple and abrupt:
Same lamps, same brightness, one line different. .aces runs the frame through the S-shaped response of film. It rolls highlights off gradually instead of chopping them, and keeps color alive inside the glare. The third picture is .neutral, which keeps each lamp's own hue where the film curve turns a bright saturated color toward the nearest primary (a hot orange reads yellow under .aces): the choice when the colors are the point, with a gentler roll-off. Set it once in setup(), and exposure is the brightness dial applied before the curve, like a camera's. For any glow, accumulation, or additive sketch, set toneMap(.aces) first, because without it the bright cores flatten to white. The details live in the HDR reference. Some displays can show a little light above white, and Chapter 45 keeps it in the files a sketch exports.
The canvas that remembers itself: feedback
Accumulation adds new marks to a picture that otherwise sits still. Feedback hands you the picture itself. Each frame you get the layer's last picture back as an image. You transform it however you like, draw it into the new frame, and add this frame's marks on top. The transformed past becomes the new present, over and over. The transform you choose, a fade, a zoom, a turn, or all three, decides what the effect looks like:
A Feedback layer is made once in setup() and kept, because its identity is what carries the picture from frame to frame. The layer itself starts every frame cleared. The loop below hands you last frame's picture as prev, and if you never draw it back, the past is gone:
var trail: Feedback!
override func setup() { trail = makeFeedback() }The ! is Chapter 12's Boids! promise again, a property filled in setup() before anything reads it. The finished sketch uses Feedback? with a guard let instead.
Then each frame runs the loop. It reads the past, transforms it, draws it back, and adds the new marks. This is the heart of the finished sketch below:
withFeedback(trail) { prev in // prev = last frame, as an image
withState {
translate(width / 2, height / 2) // zoom + turn the past, about the center
scale(1.006)
rotate(0.0025)
translate(-width / 2, -height / 2)
tint(Color(white: 1, alpha: 0.93)) // and fade it a little
drawImage(prev, 0, 0)
noTint()
}
// ...then draw this frame's new marks on top
}
drawImage(trail.image, 0, 0) // composite the resultThe tint alpha is the decay (Chapter 9 used tint to fade a picture the same way). At 0.93 each pass keeps 93% of the past, so marks take dozens of frames to melt away. The layer holds that number: a feedback layer keeps its picture in single precision, where the half float of an ordinary layer would lose a little more every frame and the fade would run fast. The small zoom and rotation mean the past does not only fade, it drifts, and moving things leave wakes that curve. The difference from noClear is where the past goes. Accumulation adds to a fixed canvas, while feedback hands you the past as an image to warp first. The warp is the difference between a long exposure and a hall of mirrors.
Putting it together: comets
Chapter 12 built a flock of triangles trailing fading paint. Here is the same flock rebuilt from four of this chapter's steps. They are the feedback loop, the bloom filter, the .add blend mode, and the ACES tone map. The boids draw as bright dots into a feedback layer, which gives them wakes that drift and curl. The layer comes back bloomed and added as light, and the tone map rolls the hot cores off like film. For contrast, here is the before:
Make MySketches/:
import Ollin
final class Comets: Sketch {
var flock: Boids?
var trail: Feedback?
override func setup() {
toneMap(.aces, exposure: 1.3)
trail = makeFeedback()
}
override func draw() {
background(Color(hex: 0x04060B))
if flock == nil {
flock = Boids(count: 430, in: bounds, seed: 11,
maxSpeed: 3.4 * scale, maxForce: 0.14 * scale,
perceptionRadius: 78 * scale, separationRadius: 24 * scale,
margin: 100 * scale)
}
guard let flock, let trail else { return }
flock.step()
withFeedback(trail) { prev in
withState {
translate(width / 2, height / 2)
scale(1.006)
rotate(0.0025)
translate(-width / 2, -height / 2)
tint(Color(white: 1, alpha: 0.93))
drawImage(prev, 0, 0)
noTint()
}
noStroke()
for i in 0 ..< flock.count {
let heading = flock.heading(i)
fill(Color(hue: (heading + .pi) / .tau + 0.52,
saturation: 0.62, brightness: 1))
drawCircle(center: flock.positions[i], radius: 3.6 * scale)
}
}
blendMode(.add)
drawImage(trail.filtered(.bloom(threshold: 0.25, amount: 1.5, radius: 16)).image, 0, 0)
blendMode(.normal)
}
}Read it in three parts. The flock is Chapter 12's Boids, retuned for comets with fewer birds that see further, and still steering by the same three rules. The middle part is the feedback loop, as the feedback section wrote it. The boids are drawn inside it, so their light lands in the layer that remembers. The last part is one line of compositing. The trail layer comes back bloomed, added as light, and rolled off by the tone map set back in setup(). Every hue still means a heading, and now it also smears into a wake that shows where the heading has been.
Swift note.
guard let flock,unwraps two optionals in onelet trail else { return } guard, the way Chapter 16'sif letbound two at once.flock.heading(i)is the boid's direction in radians, from-.pito.pi, so(turns it into a hue from 0 to 1.heading + .pi) / .tau
Then make it yours:
- Adjust the feedback. An
alpha: 0.85gives short nervous tails, while0.97fills the sky with fog. Flippingscale(1.006)to0.994makes the wakes fall inward instead of spreading outward. - Put a
@Paramon the bloom'samountand the tone map'sexposureand grade the sketch live, like color-timing film. - Swap the flock for anything that moves: Chapter 14's advected dots, Chapter 11's bouncing bodies, or just your mouse.
- Draw a dim
generate(layer where the flat.meshGradient( ...)) backgroundis, and the comets fly over weather.
The comets are motion, so keep them as a few seconds of video:
swift run OllinLive MySketches/Comets.swift --export-video comets.mp4 --seconds 8The whole stack in one block: compose
The comets composited their one layer by hand, bloomed and drawn with .add. A frame with more layers might go like this. Draw a backdrop layer, blur it, draw a lights layer, bloom it, then composite one normally and one additively. You can wire that by hand with the calls above. Or you can declare it as one compose { } block, where each layer { } carries its own filters and blend mode:
import Ollin
final class ComposeStack: Sketch {
override func draw() {
background(Color(hex: 0x080B12))
compose {
layer { // beneath: a soft color field
noStroke()
fill(Color(hex: 0x2C3A8C)); drawCircle(width * 0.35, height * 0.4, 330)
fill(Color(hex: 0x1F7A6B)); drawCircle(width * 0.68, height * 0.62, 300)
fill(Color(hex: 0x6B2C58)); drawCircle(width * 0.45, height * 0.78, 240)
}
.post(.gaussianBlur(radius: 120))
.scaled(0.5)
layer { // on top: lights that glow
noStroke()
for i in 0 ..< 15 {
let a = Double(i) / 15 * .tau
fill(Color(hue: 0.08 + Double(i) * 0.014, saturation: 0.5, brightness: 1))
drawCircle(width / 2 + cos(a) * 300, height / 2 + sin(a) * 300, 10)
}
stroke(Color(hex: 0xFFD98A)); strokeWeight(3); noFill()
drawCircle(width / 2, height / 2, 300)
}
.post(.bloom(threshold: 0.4, amount: 1.8, radius: 26))
.blended(.add)
}
}
}
Layers composite bottom to top in the order written. .post(_:) filters a layer, .blended(_:) sets its mode, and .scaled(0.5) renders it at half resolution, which a layer about to be blurred can afford. The scale changes how sharp the layer is and never how big its effects are: the blur above is 120 canvas pixels wide at any scale. All of it is shorthand, since everything compose does, the calls you already know can do by hand. What the block adds is that it holds the in-between layers for you. Some effects need two layers, a mask or a displacement map. For those the same block takes an aside { }, a helper layer drawn only to feed another one. The effects reference covers it.
Converging instead of brightening: the running mean
The comets let their past fade a little every frame, while the sandpainting kept every grain it drew. That pile has a limit. A noClear canvas holds a sum, and a sum only grows, so a still scene brightens toward white. What a still scene wants is the mean: the sum divided by how many passes went into it. A mean settles at one brightness however long it runs, and only gets smoother. makeAccumulator() keeps that for you. Draw each frame's samples into it with withAccumulator, and read image for the average so far:
var light: Accumulator!
override func setup() {
light = makeAccumulator()
}
override func draw() {
background(Color(hex: 0x05070C))
withAccumulator(light) { // one more pass
blendMode(.add)
noStroke()
for _ in 0 ..< 4000 {
let p = Vector2(random(width), random(height))
let hue = noise(p.x * 0.004, p.y * 0.004)
fill(Color(hue: hue, saturation: 0.7, brightness: 1, alpha: 0.5))
drawCircle(center: p, radius: 1.2)
}
}
drawImage(light.developed(exposure: 3, ground: Color(hex: 0x05070C)).image, 0, 0)
}The first frame is four thousand random dots. After a few hundred, the dots have averaged into the smooth noise field they were sampling, at the brightness one frame had. Nothing saturates, because nothing accumulates: the accumulator holds the sum in single-precision float and the count beside it, and image is their ratio. Each pass is drawn in half float, which stops adding a shape once a pixel holds about two thousand times what the shape brings. A pass that piles thousands of faint shapes on one spot wants makeAccumulator(, at twice the memory. light.reset() starts it over when the scene changes. developed( prints the mean the way a photograph is printed. It applies an exposure, then rolls the highlights off with a curve like the tone map step's, this one Erik Reinhard's. Then it adds the paper's own tone after the curve. The Rendering/ example uses this to turn a million scattered samples a frame into a photograph with a lens. Chapter 35 picks it up with the 3D camera. The Accumulator! makes the same promise as the feedback section's Feedback!.
When it gets slow
The comets add passes to the frame, one for the feedback layer and a few for the bloom, and still fit with room to spare. Sooner or later a sketch will not. A chapter that just added layers, filters, and more passes over every pixel is the place to say what to do then. The useful question is which half of the frame is behind, the drawing your code does or the pixels the card fills. The inspector's cost row answers that, and a batch is the usual fix when the drawing is the slow half.
Which half is slow: the cost row
The cost row is the last row of the inspector, and it measures one frame. Press ⌘/ for the inspector. The cell grid ends with three counts, and two bars sit under it.
The CPU bar is your draw() plus the encoding that turns it into GPU commands. Tessellation lives there. A polygon, a curve, or a stroke is cut into triangles before the GPU sees it. A closed shape like a circle or a rectangle skips that cut. The GPU draws it from one small record, called an instance, so it barely touches this bar. The GPU bar is what the card spent on the frame, taken from its own clock.
Both bars are drawn to the same scale, which is the length of one frame. At 60 frames a second that is 16.7 ms. So the longer bar is your problem, and two short bars mean you have room.
The bars are kept apart on purpose. The CPU is already building the next frame while the GPU draws this one, so the two overlap in time rather than adding up.
The counts say what the frame asked for. Draws is the draw calls. Passes is the render passes, which is two for a plain sketch and one more for every layer and filter. Batches is the runs Ollin recorded from your draw(), each a stretch of marks drawn in one state. A run breaks whenever the blend mode, the texture, the clip, or the kind of drawing changes.
The batch count is the one to watch. Ten thousand circles in a row cost one draw call. Ten circles that each change the blend mode cost ten. If the batch count is close to the shape count, group the shapes that share a state.
The rest of the reading is short:
- CPU bar long? You are making geometry. Hover Draws for the vertex count. Static geometry belongs in a batch, recorded once and replayed from the card, which the batches entry shows.
- GPU bar long? You are filling pixels. Look at the pass count, and give soft layers a smaller
makeRenderTarget(.scale: ) - Both short and still slow? Something outside the drawing is holding the frame, like a file read in the middle of
draw().
When you need to know which pass, hand the frame to Xcode:
MTL_CAPTURE_ENABLED=1 swift run OllinLive MySketches/Comets.swiftThen View ▸ Capture GPU Frame (⌘⇧G), or captureGPUFrame() from your own code. Ollin writes a .gputrace file that opens in Xcode's GPU debugger, and prints the frame's passes in order as it writes the file. Often that printed list answers it.
Record it once: batches
A batch is a recording of drawing calls, kept on the graphics card and replayed on demand. It is for a picture that does not change between frames, which is where a long CPU bar is easiest to fix. Every sketch so far has been immediate-mode drawing, the model Processing, p5.js, and openFrameworks share, where every frame re-issues every mark. Chapter 15's habit was to build the geometry once, hold it, and let draw() only replay it. Even the replaying costs something. draw() still walks your arrays and re-issues every line to the GPU, sixty times a second, for a picture that never changes. A batch is the retained exception to that model:
var drawing: Batch?
override func setup() {
drawing = makeBatch {
// any drawing calls that don't change between frames
}
}
override func draw() {
background(.white)
if let drawing { drawBatch(drawing) }
}makeBatch { } records your drawing once into a Batch you hold, and drawBatch replays it from the GPU's own memory. For static work at scale the difference is large. A hundred and fifty thousand circles cost about 13.5 ms of CPU a frame drawn the ordinary way, and about 0.003 ms replayed. The Rendering/ example has a parameter that switches between the two paths, so you can watch the difference in the inspector. The transform in force when you call drawBatch still applies to the whole recording, so one batch can be stamped at several positions or sizes.
The rule of thumb: if the drawing does not change between frames, it belongs in a batch, and if it does change, leave it alone. A shape whose outline stays put while its color changes needs neither. A Shape kept in a property remembers its triangles and stroke from its second draw on, so drawing it again in a new color costs little more than a copy. A few things are per-frame by nature and cannot be recorded, namely 3D meshes and fields, GPU particles, layer blocks like withTarget, clipping, and background. The recording leaves them out, and Ollin prints a note once, at the call inside makeBatch, saying where to draw them instead.
More ways to use a finished layer: wipes, copies, and a readback
A layer that is finished can still be taken away in an order, laid out again around itself, or handed back to the sketch as pixels it can read. The first two are one pass over the layer, and reach back to older crafts, the film editor's wipe and the printer's step and repeat. The third turns every picture technique in Chapter 9 onto the sketch's own drawing.
Taking a layer in an order: wipes
A wipe replaces one picture with another along a moving front. A film uses one to cut between scenes, an edge sliding across the screen. Film editors have used wipes since the early sound era, and Akira Kurosawa, and George Lucas after him, made them a signature. In a sketch it is the transition between two layers, or a layer arriving over whatever is behind it.
Every wipe is the same call given a different order. A WipeOrder gives each pixel a number from 0, which goes first, to 1, which goes last. A straight edge, a growing circle, a clock hand, a grid of blocks, and a noise dissolve are its five shapes. Modifiers fold it, tile it into blinds, cut it into bands, and shuffle the bands into a seeded order:
let t = (time * 0.25).truncatingRemainder(dividingBy: 1)
let bars = WipeOrder.directional(angle: 0.3).bands(18).shuffled(seed: 4)
drawImage(day.combined(with: night, .wipe(bars, progress: t)).image, 0, 0)
As progress climbs from 0 to 1, each pixel turns over when the progress passes its number. The front is soft, feather wide. At 0 the result is the first layer exactly and at 1 the second, so a finished wipe costs the picture nothing. Filter.wipe is the one-layer form, which brings a layer in over the canvas. Examples/ runs nine orders between two photographs, and the reference lists every shape and modifier.
Laying a layer out again: copies
Copies repeat one layer in a grid, around a ring, or along a line. Each is a step smaller, more turned, fainter, and further around the hue wheel than the last. The print shop calls it step and repeat. Here each step can change the copy a little, so one petal becomes a flower and one mark becomes a trail.
let petal = makeRenderTarget()
withTarget(petal) { fill(.orange); drawEllipse(width / 2, height / 2 - 40, 46, 120) }
drawImage(petal.filtered(.repeated(.ring(radius: 0.18, facesOut: true), count: 9,
scale: 0.94, hueShift: 0.25)).image, 0, 0)Each copy is the whole layer, moved so its middle lands on the copy's place. That is why the petal is drawn in the middle of its layer. Copy i is scaled by scale to the power i and turned i times rotation, and its hue moves i times hueShift. With facesOut, each copy on a ring points away from the center. The first copy is on top, so a line of shrinking copies reads as a trail behind its leader. The same example lays the petal out all three ways.
The drawing read back: snapshot and the slit scan
Everything in Chapter 9 that reads a picture, the glyph mosaic, the stipple, the dither, image[x, y], took a photograph or a picture painted on the CPU, because a layer lives on the GPU. snapshot() brings a layer's pixels back. It hands over the layer as drawn the frame before, as an ordinary Image, and it never waits for the GPU: the copy is made inside the frame's own work and arrives once the frame is done. In the window that is one frame of delay. In an export, where every frame is rendered and read in turn, it is exactly the frame before. The first frame has no frame before and returns nil.
let scene = makeRenderTarget()
withTarget(scene) { drawCircle(mouseX, mouseY, 60) }
if let picture = scene.snapshot() { // last frame's pixels
scan.append(picture) // a SlitScan of what the sketch draws
}The slit scan is the technique that wants this most. Chapter 36 feeds one a camera, and here it reads the sketch: a history of the layer's frames, and every pixel of the result read from a different moment, so slow motion stretches into ribbons and fast motion shears into combs. A full canvas is too much to carry to the CPU every frame, so the scan has a form that stays on the GPU. A LayerHistory, made once in setup() and held like a Feedback, keeps the layer's last frames in a ring there, and Filter.slitScan composes the scan as a filter over the layer:
var history: LayerHistory!
override func setup() { history = makeLayerHistory(capacity: 90) }
override func draw() {
let scene = makeRenderTarget()
withTarget(scene) { drawCircle(mouseX, mouseY, 60) }
drawImage(scene.filtered(.slitScan(history, delay: .across)).image, 0, 0)
}
The delay says where the past is: .across puts now at the left edge and the oldest frame at the right, .down runs it top to bottom, .radial puts now in the middle, and .map(image) makes any picture the lens, black reading now and white the oldest frame. The GPU scan composes what the CPU one composes from the same frames, to a quarter of a level. What it costs is memory, capacity frames of the layer at eight bytes a pixel, so a layer made at a lower scale is how a long reach stays affordable.
The glyph mosaic takes the shorter road: hand drawGlyphMosaic a layer's image and its cells are averaged on the GPU, a frame late, and drawn as text. Examples/ does all three, a scan of its own moving shapes with its own word read back as glyphs beside it, and the reference has the rules a readback follows, including the one that stops a web page at it.
Where this comes from
The shape the layers take here, a filter catalog and explicit compositing in a compose block, follows OPENRNDR's model, on Ollin's own Metal core. The compositing arithmetic descends from Thomas Porter and Tom Duff's 1984 paper "Compositing Digital Images". The everyday blend-mode vocabulary of multiply, screen, lightest, and darkest is the image editors' tradition that grew up after it.
Tone mapping comes from photography by way of Erik Reinhard, Michael Stark, Peter Shirley, and James Ferwerda's 2002 paper "Photographic Tone Reproduction for Digital Images". The film-like curve behind .aces is the Academy Color Encoding System's response, in Krzysztof Narkowicz's fitted approximation. The hue-preserving curve behind .neutral is the Khronos Group's PBR Neutral tone mapper of 2024, written from its published description.
The filter catalog reimplements published image-processing techniques, each written from its method. The menu was cross-read against OPENRNDR's orx-fx, openFrameworks' ofxFX, and AsyncGraphics for which effects exist and how they are approached. The running mean and its print follow Anders Hoff's depth-of-field essays and Domenico Bruzzese's Blurry, read for approach and written independently.
The wipe orders and the copies were read as a map from Shaders, an MIT-licensed library of web effects, and written independently.
Video feedback is the analog ancestor of the Feedback layer. Video artists pointed a camera at its own monitor from the late 1960s on. Steina and Woody Vasulka began that way in New York in 1969, with a tape called Feedback. There the transform is whatever the room does to the signal. Full credits are in the project's attribution notes.
Go deeper
- Layered effects: every filter, generator, combine op, and the full
composegrammar, with wipes and copies,snapshot(), and the layer history behind the GPU slit scan. - Slit scan: the CPU history, its delays and maps, and the GPU form over a layer.
- Layers: one screen on what a layer is, what one costs, and when you do not need one.
- What survives a frame: the list of what carries into the next frame, from the ink state to a checkpoint on disk.
- Accumulation and HDR & tone mapping: the persistent canvas, the
Accumulatorthat keeps a running mean, and the float pipeline underneath both. - Depth of field from light: the
developprint filter, and the lens built on the running mean. - Blend modes: the arithmetic of each mode.
- Profiling: reading the cost row, what to do about each answer, and capturing a frame for a closer look.
- Retained batches: what a
Batchcan and can't record, how transforms apply at replay, and the measured numbers. - Appendix B draws this chapter's math, one picture per idea: Shaping a value, Color and light as numbers.
- Worked examples:
Examples/,Effects/ Layers Examples/,Effects/ Feedback Examples/(Effects/ PigmentMix .paintMixand.mixover the same two layers at once),Examples/,Rendering/ Accumulation Examples/(a running mean of a million samples a frame),Rendering/ DepthOfField Examples/,Rendering/ ToneMapping Examples/(the same field drawn directly or replayed, on a switch), andRendering/ RetainedBatch Examples/(a sketch slit-scanning its own shapes and reading its own word as glyphs).Effects/ ScannedByItself