Drawing

Tiled offscreen export

A poster, a print or a figure for a paper needs a still image much larger than the screen: 8K, 16K, or 40 000 pixels across. One render of that size does not fit. The GPU has a largest image (often 16 384 or 32 768 pixels a side), and the targets of a 16K frame need gigabytes. A tiled export draws the image in pieces with the same camera, and stitches them so well that nobody can see where one tile ends. This page explains how, and the last part shows RASM's version.

In short

  • Each tile is the same camera with an off-center projection: a clip-space scale and shift that maps the tile's rect of the image to the whole target. It works for perspective and orthographic cameras alike.
  • Sizes in pixels need no change: the scale of the projection and the smaller viewport cancel.
  • Anything that reads neighbour pixels (a blur, a hole fill) needs a guard band: each tile is drawn larger than it is kept.
  • Anything decided for the whole screen (label placement, label visibility, a depth range for weights, a streaming cut) is decided once for the whole image, not per tile.
  • Temporal anti-aliasing has no history across tiles. A still instead averages N jittered frames per tile: supersampling.
  • A huge image is never whole in memory: finished rows of tiles stream to the file.

1Why one render does not fit

An image is limited twice: by the largest image the device makes, and by memory. A frame needs more than its final pixels: an HDR color, a depth, a bloom chain, sometimes MSAA samples.

0.5 GB16K RGBA8, final pixels only15 360 × 8 640 × 4 B
1.6 GBits HDR color and depth8 + 4 B a pixel, before the bloom chain
6.4 GBthe same with 4× MSAAsamples multiply the color and depth
32 768a common largest image sidea 40 000 px poster does not fit at all

2Tiles with an off-center projection

Cut the image into a grid. Each tile is drawn into a target of its own size, with the full camera. Only the projection changes: after the camera's projection, a clip-space transform stretches the tile's rect of the image to cover the whole target. Seen from above, each tile is a narrow slice of the camera's frustum, off to one side.

the frustum from above camera tile 1 tile 0 tile 2 tile 3 the image, cut in tiles tile clip'.x = clip.x · W / R + shift · clip.w shift = (W − 2 r) / R − 1, the same for y
The camera, its matrices and its world never change. Each tile's target holds R pixels, from pixel r of the W-pixel image. The scale stretches R pixels to the target; the shift moves pixel r to its left edge. Because the shift is multiplied by w, the same matrix works for a perspective camera (w = depth) and an orthographic one (w = 1).

Drag the point and pick a tile: the point lands on the same pixel of the image whether it is drawn whole or in the tile.

Move the point and change the tile. The pixel it falls on in the tile, plus the tile's offset, is its pixel in the whole image: the tile is the same picture, only cut.

Sizes in pixels need no tile math

Lines, text and points are sized in pixels. A shader turns a size in pixels into a clip-space offset with the viewport size, and a size in metres into pixels with the projection's scale. In a tile both factors change, by the same amount, and cancel:

offset = pixels / (viewport / 2) · w

world size of a pixel = 2 w / (P11 · viewport height)

P11
grows by W / R in a tile
viewport
shrinks from W to R pixels

A 3-pixel line is 3 pixels wide in every tile, and a label is the same size everywhere.

3What reads neighbour pixels: guard bands

A pass that reads pixels around its own sees the tile's edge where the whole image has more pixels. A blur clamps at the edge and darkens or brightens there; a hole fill finds no neighbour. The seam shows on every tile border.

The fix is a guard band: draw each tile larger than you keep it, with at least the effect's reach on every inner side. Keep only the middle. At the image's own edges there is no guard: there the whole image ends too, and its passes clamp at the same place.

kept tile guard band (drawn, dropped) next tile a blur's reach: inside the guard render rect = tile + guard
The neighbour's pixels near the border are drawn twice: once kept in their own tile, once as guard here. A multi-level effect such as a bloom pyramid also needs each tile's origin on its coarsest grid: with 6 halvings, a multiple of 64 pixels, so the tile's level texels sit where the whole image's do.
A row of pixels with three bright spots, blurred. The top curve is the whole row blurred at once; the bottom one is the row cut in tiles, each blurred with its guard and kept in the middle. Make the blur wider than the guard and a seam appears at the tile borders.

4What the whole screen decides

Some decisions use the whole screen at once. Made per tile, they change from tile to tile, and a label or a color jumps at a border. Each is made once, for the whole image:

Harbour tile border

Label placement

Placed once on the whole image's pixels, so each label is in one place and crosses borders whole.

anchor takes its answer

Label visibility

A survey draws each tile once and tests each anchor in the tile that holds it. The tiles then use that answer.

union

Ranges over the screen

A depth range for weighted transparency is the union the survey found over all tiles, fixed before the first tile.

frozen

What is drawn

Streaming, LOD and animation must not change between tiles. LODs use the whole image's camera; the drawn set of streamed tiles is frozen while the export runs.

5Anti-aliasing a still: accumulate samples

TAA blends each frame with the history of the frames before. In an export the frame before is another tile, so the history is useless. A still has time instead: draw each tile N times, each time moved by a different sub-pixel offset (the jitter), and average. This is supersampling: every pixel is the box-filtered mean of N samples, with no ghosting and no blur from a history.

16 samples a pixel: the centres of a 4 × 4 grid 7 are under the surface: the pixel is 7/16 surface, 9/16 background the same offsets in every tile: the samples form one grid over the image sum in floats, then divide by N; encode at the end
Each sample is a full frame of the tile, so it costs N frames a tile. A square count gives a stratified grid (even coverage); other counts use a low-discrepancy sequence. MSAA can still run inside each sample, but N samples already smooth the edges, so it mostly adds cost.

6Pixel sizes at a higher resolution

An export four times wider than the screen raises a question: should a 2-pixel line stay 2 pixels (a thin line on a poster), or become 8 (the same look as on screen, sharper)? Both are useful, so it is a setting: a pixel scale s multiplies every size given in pixels, and the level of detail is chosen in those larger pixels too.

s = 1: native pixels

Harbour

Lines and text keep their pixels: thin on a large image, the most detail.

s = 4: the screen's look

Harbour

Everything sized in pixels is s times larger: a zoomed screenshot, but sharp.

A blur sized in pixels, such as bloom, is the exception in many engines: its reach comes from the number of pyramid levels, so scaling it means more levels and an s times wider guard band.

7Streaming tiles to the file

Image files are written row by row. The tiles of one row of tiles (a band) fill a buffer one image wide; when the last tile of the band is in, the band goes to a writer thread, which compresses and writes it. The next row of tiles waits while a band is already waiting for the writer, so memory holds at most three bands (one being written, one waiting, one filling) and a few tiles, not the image.

  1. GPU: the tile's last sample writes its finished pixels to a CPU-readable buffer
  2. CPU, two frames later: copy the tile's rows into the band
  3. band full: hand it to the writer thread
  4. writer: filter, deflate, write the rows; the next band fills meanwhile
FormatValuesAlphaUse
PNG, 8 bitssRGB-encoded display colorsstraightthe web, slides
PNG, 16 bitssRGB-encoded, 65 536 stepsstraightprint: no banding in soft gradients
EXR, half or floatlinear; display colors, or the scene's HDR light before tone mappingpremultipliedcompositing, grading

A transparent background needs care at edges. The renderer's colors are premultiplied by coverage, which an EXR keeps. A PNG stores straight color, so the exporter divides by alpha; where alpha is nearly 0 it writes black instead of a color divided by almost nothing, which would show as bright fringes.

8Pitfalls

A bright or dark line along every tile border
A pass reads past the tile: the guard band is smaller than its reach, or the tiles are not aligned to the effect's coarsest grid.
The sky or a grid repeats in every tile
A shader rebuilds view rays from the projection's diagonal only and ignores its off-center terms. Use the full inverse, or add the offset terms.
A label shows in one tile and is missing in the next
Placement or visibility was decided per tile. Decide it once for the image.
Content differs between tiles
The scene changed during the export: an animation ran, a stream loaded new detail, an LOD was chosen per tile. Hold the scene still and choose LODs with the whole image's camera.
Bright fringes around a transparent PNG
Straight color divided by a tiny alpha. Write 0 below a small alpha.
An export that eats all memory
The whole image is kept before it is written. Stream bands.

9In RASM

Renderer::exportView starts an export of a view. It runs in the frames of Renderer::render, after the application's views: each frame draws one sample of one tile, so the application keeps its frame rate and polls getExportProgress.

settle survey: 1 / tile drain: 2 tiles: samples frames / tile flush loads, streams quiet labels, weighted span last bands written scene passes, render rect MSAA resolve, bloom Export pass: AgX, sum output buffer (last sample) one frame of the tiles phase; its scene view takes the tile's clip matrix and jitter the output buffer is read two frames later, after the frame's fence, into the band
An export is a hidden view in the view graph, with a copy of the camera made when it starts and a target of one render rect. Exports run one after another.
This pageRASM
tiles, off-center projectiongetExportLayout, getExportTile, getExportTileClip: tiles of ExportDesc::tileSize (DEFAULT_EXPORT_TILE_SIZE, 2048), the image rounded up to EXPORT_ALIGNMENT (64), one render rect size for every tile, moved back inside the image at its edges
guard bandEXPORT_GUARD (64) without bloom; EXPORT_BLOOM_GUARD (384) with it: the bloom's reach is 189 pixels down its 6 levels and 186 back up
rays through an off-center window, pixel sizesan export's view draws with its own variant of the shaded pipelines (EXPORT_VIEW_DEFINE, RASM_EXPORT_VIEW): shaders/passes/sky.slang and grid.slang add the projection's offset terms (projection[i][2]) to their rays, and sizes in pixels read SceneData::pixelScale through getPixelScale. Every other view's pipelines get 1 there, which the compiler folds away, so they run the same code as before exports; the shared cull and point raster compute passes read it (RASM_PIXEL_SCALE)
placement, visibility, ranges for the whole imagelabels are placed on the whole image (placeViewLabels with the image's size); the survey's probes test each anchor in its tile only, at once (getExportLabelProbe), and the tiles keep the fades; the weighted span is the union of the survey's words (gatherExportWeightedWords)
a still scenethe camera is copied at the start; a TileStreamer of the scene counts the export's view in its screen error, and changes nothing drawn from the survey to the last tile (isExportFrozen); the application holds its animation time still
accumulationExportDesc::samples (16): getExportJitter gives a 4 × 4 grid, the Export pass sums into an RGBA32F buffer; TAA is off and MSAA too unless ExportDesc::msaa
pixel scaleExportDesc::pixelScale → SceneData::pixelScale: widths, text, points, the grid's lines, label cells; the LOD's pixels per unit are divided by it. Bloom keeps its reach in output pixels (D55)
streaming, formats, alphaImageWriter on its own thread, at most EXPORT_MAX_QUEUED_BANDS bands waiting; ExportFormat: PNG8, PNG16, EXR_HALF, EXR_FLOAT; without a path the image stays in memory for takeExport

Code map

FileWhat
include/rasm/export.hExportDesc, ExportFormat, ExportProgress
src/renderer/export.cppthe layout, the jitter, the phases (updateExports, finishExportSubmit), the Export pass, the bands (readExports)
shaders/passes/export.slangtone map (shaders/lib/agx.slang, shared with the tone-map pass), premultiply, sum, encode
src/renderer/imageWriter.cppstreaming PNG (one deflate stream) and EXR (ZIP blocks of 16 lines) on a thread
src/renderer/gpuScene.cppprepareSceneView and placeViewLabels for an export's view
examples/32_exportexports a scene with every screen-space effect, and checks tiles against one tile; examples/24_map_tiles --export-check does it while streaming
Rules to keep A pass that reads pixels around its own must reach less than the guard band, or the guard must grow. A shader that rebuilds a ray from the projection must use its offset terms. Anything decided per view for the whole screen must take the export's whole image (ExportViewFrame), not the tile.

Further reading: the headless readback guide (the copies back to the CPU), bloom, TAA, and the OpenEXR file layout document.