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.
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.
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.
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.
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:
Label placement
Placed once on the whole image's pixels, so each label is in one place and crosses borders whole.
Label visibility
A survey draws each tile once and tests each anchor in the tile that holds it. The tiles then use that answer.
Ranges over the screen
A depth range for weighted transparency is the union the survey found over all tiles, fixed before the first tile.
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.
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
Lines and text keep their pixels: thin on a large image, the most detail.
s = 4: the screen's look
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.
- GPU: the tile's last sample writes its finished pixels to a CPU-readable buffer
- CPU, two frames later: copy the tile's rows into the band
- band full: hand it to the writer thread
- writer: filter, deflate, write the rows; the next band fills meanwhile
| Format | Values | Alpha | Use |
|---|---|---|---|
| PNG, 8 bits | sRGB-encoded display colors | straight | the web, slides |
| PNG, 16 bits | sRGB-encoded, 65 536 steps | straight | print: no banding in soft gradients |
| EXR, half or float | linear; display colors, or the scene's HDR light before tone mapping | premultiplied | compositing, 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.
| This page | RASM |
|---|---|
| tiles, off-center projection | getExportLayout, 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 band | EXPORT_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 sizes | an 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 image | labels 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 scene | the 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 |
| accumulation | ExportDesc::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 scale | ExportDesc::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, alpha | ImageWriter 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
| File | What |
|---|---|
include/rasm/export.h | ExportDesc, ExportFormat, ExportProgress |
src/renderer/export.cpp | the layout, the jitter, the phases (updateExports, finishExportSubmit), the Export pass, the bands (readExports) |
shaders/passes/export.slang | tone map (shaders/lib/agx.slang, shared with the tone-map pass), premultiply, sum, encode |
src/renderer/imageWriter.cpp | streaming PNG (one deflate stream) and EXR (ZIP blocks of 16 lines) on a thread |
src/renderer/gpuScene.cpp | prepareSceneView and placeViewLabels for an export's view |
examples/32_export | exports a scene with every screen-space effect, and checks tiles against one tile; examples/24_map_tiles --export-check does it while streaming |
ExportViewFrame), not the tile.Further reading: the headless readback guide (the copies back to the CPU), bloom, TAA, and the OpenEXR file layout document.