Post-process
Post-process
The scene is drawn in light: every surface, the sky and every afterLighting feature write linear radiance into a float target, and effects registered on the scene layer see that light. One pass, __display__, then applies the exposure and the tone curve and encodes the result for the screen. The UI lands on that picture, and effects on the all layer grade the picture. postprocess.describe("__display__") reads its curve (0 linear, 1 Reinhard, 2 ACES, 3 Khronos PBR Neutral) and exposure; postprocess.setProperty writes them, and a PostProcess component's display carries them with a scene.
Full-screen effects run after the scene renders — bloom, colour grading, your own. Each is a post-process .shader asset (the shaders guide) that runs once registered as a named pass:
-- No layer: bloom reads light, so the chain runs it on `scene`.
postprocess.add("bloom", asset.resolve("@builtin::shaders.post.bloom", "shader"), opts)
postprocess.setEnabled("bloom", true)
postprocess.setProperty("bloom", "intensity", 0.6)
postprocess.list() -- also setTexture / setSampler / remove
add is what puts the pass into the frame; authoring the shader asset defines the effect without rendering it. Editing a registered shader's body recompiles its pass in place, keeping the pass's enabled state, priority, layer and tuned properties.
A post effect is also an asset of its own, of the postEffect type, which names a post-process shader with its values, its order and its layer; the type's README (guides { path = "types/postEffect" }) describes it.
An effect declares in its properties.yaml whether its fragment() works on light (the default) or on display code values, and the chain converts at both ends, so a vignette grades either layer and a lookup table always sees code values.
An effect may also own several passes, each at its own scale, drawn in order through per-effect scratch targets before the next effect sees the frame — bloom, for instance, is a threshold pass, a pyramid of downsamples and a chain of tent upsamples, each level reading an earlier one at a coarser size. asset.resolve("@builtin::shaders.post.bloom", "shader"):getPasses() reads a shader's own pass list before registering it — for the bloom that is sixteen entries, bright, down1…down7, up6…up0 and composite, each with the entry, scale and inputs it was declared with; the shaders guide (types/shader) has the full passes: shape, the two indexed-read helpers, and what engine.params carries for a pass the chain draws.
opts.layer picks which composited image the pass grades. "scene" runs it before the UI is drawn, over the scene's own light in a float target, so it grades the rendered picture and leaves every widget on screen as authored; "all" runs it after the UI has landed, over the picture __display__ fitted to the screen, so the interface is graded along with the picture.
A registration that names no layer takes the one its space belongs to: a light effect grades scene and a display effect grades all. That is what puts a bloom where highlights still stand above 1.0 and a lookup table where the code values it was built for are. Name "all" for a pass that must reach the interface — on a world several authors share, it grades everyone else's screens too. tools.use("pp", "add", ...) takes the same key and forwards it.
postprocess.status() is the chain as the renderer holds it — per effect: enabled, chain, drivenBy, priority, layer, the shader's compile error, the texture each declared slot is bound to (textures), and the bindings whose texture has not reached the GPU yet (pendingTextures). A texture created in the same step as the setTexture that names it lands there first and moves into textures on the frame the upload completes. Read on the engine thread, the record is the renderer's at the call, so a registration, a setProperty, a setTexture or a setEnabled made earlier in the same call reads back as written.
enabled is whether the renderer runs the effect, and it has three sources. chain is the effect's own switch in the chain, the one opts.enabled and postprocess.setEnabled set. drivenBy lists each render pass of the frame that runs the effect as its shader, with the render feature that queued it (feature, and featureIdentity while that feature runs), its phase, order, inputs and output: a look stack registers its effects with enabled = false and draws them through its passes, so each of them reads chain = false, enabled = true and its passes under drivenBy, and when the look is switched off and its passes leave the frame it reads enabled = false. The renderer's own stages (__display__, __output__, the passthrough copy __blit__) read enabled = true.
drawn is whether the effect drew into the last frame the renderer drew, from the renderer's own record of its draws: the chain drew it, a render feature's pass drew it as its shader, or the renderer drew it as one of its own stages. enabled = true is how the effect is set up; drawn is what the frame did with it. pending is true while the effect has no compiled program for its current source: its first build, or the build of a shader it was given since, is still going. A published effect reads drawn = false while its pipeline is still building, and the frame carries the picture past it unchanged until it lands; it also reads drawn = false for as long as no camera that draws runs post-processing: the editor camera draws without it, so an effect waiting on the editor view never reaches it. A script that needs an effect in the picture waits for drawn. /runtime/fx/<name>/meta.json carries the same error, pending and drawn beside the registration it records.
postprocess.describe(name) is one effect of that chain read in full: the same record, plus properties — the schema the effect declared, entry by entry — and values, what each of those properties holds right now (the value the last setProperty wrote, or the schema default where nothing has written one). setProperty answers whether the uniform took the value; describe answers what the effect holds, which is the read-back a pass driving its properties every frame needs and the one that tells a mistyped property name from a pass that is not grading. Before an effect is registered, the same schema is on the .shader asset it will render: asset.resolve("@builtin::shaders.post.bloom", "shader"):getProperties().
The chain reaches an offscreen capture, at that camera's own matrices
A registered effect runs over the live viewport's frame and over every offscreen
one alike — capture(source = "position"), capture(source = "entity"), a
capture of a named camera, a render-to-texture camera. A render-to-texture
camera's picture reaches the screen through the frame that shows it (a
viewport or image widget, a material), so its own render leaves out the
all layer and that frame grades it there, once, with the rest of the
interface; textureColorSpace = "presented" puts the all layer into the
texture, for a reader that takes it as the finished picture (the Camera
component's README). In each of those the
effect's engine.view_proj / engine.prev_view_proj / engine.inv_view_proj
are the camera THAT render was drawn from and engine.resolution is that
target's own size, so a pass reconstructing world space from
zero_scene_depth(uv) reconstructs against the station and lens the capture
asked for. An offscreen capture is therefore an oracle for an authored grade:
the frame it returns is the look as it stands at a chosen station, and driving
the on-screen camera somewhere else does not move it.
An offscreen render keeps no view history of its own, so engine.prev_view_proj
there holds that same matrix rather than the frame before it, and a pass taking
camera motion from the two reads none. On the viewport the pair is a frame
apart, so camera motion is the one reading a capture answers differently from
the screen.
postProcessing = false is the one control that takes the chain off a capture,
and it also takes off the film a render feature draws: the flare, a
short focus's bokeh, the shutter's smear — so a graded frame and an ungraded one
are different pictures. __display__ still runs on that capture: what
postProcessing = false opts a camera out of is the registered effects, and
the exposure and tone curve that turn the scene's light into the picture a
capture returns stay on it. A diagnostic capture (albedo, normal,
motion_vectors, …) bypasses __display__ itself, since it answers a reading
rather than a graded picture, and the environment-cube capture bypasses it for
the same reason — its faces carry radiance into the cube array, which is what
the PBR indirect term multiplies into its lighting. A
render feature's passes otherwise reach an offscreen render the same way the
chain does, against the same camera; the renderFeature assetType README
carries that half.
The pp toolbox's add tool is the shorter route: tools.use("pp", "add", name, shader, opts) resolves shader — a shipped short name, an identity, a path, a guid, or a scene-authored .shader asset — and applies opts as named properties. The toolbox's presets tool lists every shipped short name.
One dither and one grain
The frame is rounded to the screen in one place: the output stage
(renderer.output), after the UI, writes the presented surface with a
blue-noise triangular dither one step of the surface wide, in the encoding the
surface is written in (sRGB on an 8-bit surface, PQ on a 10-bit one, scRGB or
a browser canvas's extended range on a half-float one at the half-float step of
each value). Everything before it
holds the picture in float, so a smooth sky, a fog falloff or a light's glow
reaches the screen without contour lines and nothing else in the chain needs a
dither of its own.
The dither moves with the count of drawn frames, so two frames drawn in a row
round differently and their mean is the picture in float. Every effect reads
the same engine.time the output stage does: x is the clock surfaces are
drawn at (seconds), y the step since the frame before and z the count of
frames the chain has drawn. A renderer presenting to a display draws a frame
when what it draws changes, so an effect that reads x or y is drawn on
every frame the clock moves (renderer.framePacing().pictureChanges.clock
counts them, naming the effect), and one that reads z alone (a pattern
stepped per drawn frame, as the dither is) changes only on frames something
else drew: a still picture drawn through it is drawn once and then left on
screen. An effect that animates with time reads x.
Film grain is drawn once too: @builtin::systems.lensFx.filmGrain, an effect
of the display stage that runs after the temporal resolve, the debanding, the
edge antialiasing and the sharpening, on the code values a viewer sees, before
the UI. The LensFx component and the lensFx module drive it:
grainIntensity is the grain's peak depth as a fraction of black to SDR white
(0.03 is about 8 code values of an 8-bit display), reached in the midtones;
grainSize is one grain's size in pixels of a frame 1080 rows tall, drawn at
the same fraction of every frame's height. The debanding package rebuilds a
ramp in float and leaves its rounding to the output stage; a debanding grain
above 1 adds the rest of its depth through the same film grain.
HDR output on an HDR display
Whether the game is shown in HDR is the output stage's, through
renderer.output, and the same calls answer on a desktop build and in a
browser. renderer.output.capabilities() states what the presented surface
and the display behind it offer: colorSpaces ("srgb" always; "pq" and
"scrgb" on a native window that presents HDR; "extended-srgb" and
"extended-display-p3" on a browser canvas that does), the display's
peakNits, paperWhiteNits and headroom where it reports them, its
highDynamicRange and gamut where the display or the browser page states
them, and hdrUnavailable, why no HDR colour space is offered, when none is
(the display shows SDR, the browser page hosting the engine has not stated
whether the display shows HDR, or the browser applies no "extended" tone
mapping to its canvas). A browser canvas presents its extended range on any
display, so in a browser HDR is offered only once the page states the display
shows it.
local caps = renderer.output.capabilities()
if caps.hdr then
-- The first HDR space the surface offers: pq or scrgb natively,
-- extended-srgb or extended-display-p3 in a browser.
renderer.output.set({ colorSpace = caps.colorSpaces[2] })
else
print("SDR here:", caps.hdrUnavailable)
end
print(renderer.output.get().colorSpace) -- the next frame presents it
set refuses a colour space the surface does not offer, naming what it
offers and why it offers no HDR. renderer.output.get() reads back the
output the frame presents with, and in a browser canvas, the configuration
the canvas itself reads back (format, colorSpace, toneMapping).
paperWhiteNits and peakNits set the headroom the display transform's
curves reach above SDR white; the API reference
(/zero/docs/api/globals/renderer/output) has every field.
Colour grading with a lookup table
A look is normally delivered as a table rather than as a chain of operations: for an N-entry colour cube, the answer to "what does this colour become" is stored for N³ inputs and everything between them is read by trilinear interpolation. A grade of any complexity then costs the same eight texel reads, and it is what a colourist's .cube export contains.
local lut = require("@builtin::modules.lut")
local ref = lut.install("warm_evening", lut.fromGrade({
exposure = 0.3, temperature = 0.25, saturation = 1.1,
}))
tools.use("pp", "add", "grade", "lut", { texture = ref.guid, amount = 1.0 })
lut.fromCube(text) imports what a grading tool exported, lut.fromImage(bytes) a strip PNG, lut.build(size, fn) bakes an arbitrary transform, and lut.sample(table, r, g, b) reads a table back the way the shader does, so a look can be checked without rendering a frame. amount cross-fades between the ungraded frame and the table's answer.
The cube is stored as a strip: N square tiles in one row, N*N wide by N tall, tile b holding the plane of constant blue. The shader reads N from the image's own height, so one effect serves a 16-, 32- or 64-entry cube; a texture that is not shaped like a strip leaves the frame untouched, which is what an unconfigured lut effect does. The effect works on the display's code values — the usual place for a look exported from a grading tool, since the chain applies it after __display__, the chain's own display transform, has already run.
Verifying a post effect by numbers
Whether a tone curve, a bloom, an antialiasing resolve, a fog or a grade does what it should is read off the pixels a viewer sees and held against a reference by numbers. @builtin::modules.postMetrics is the kit:
local postMetrics = require("@builtin::modules.postMetrics")
local frames = renderer.presentedFrames({ frames = 16 }) -- what the player saw, frame by frame
postMetrics.temporalVariance(frames).mean -- flicker: 0 on a still, resolved view
postMetrics.banding(frames[1], { x = 0, y = 0, w = 256, h = 32 }) -- longestRun: a quantised gradient's bands
postMetrics.expectGolden(frames[1], asset.containing(__FILE__).path .. "/goldens/look.png") -- FLIP against a golden
return postMetrics.picture(postMetrics.scopes.falseColor(frames[1]))
- FLIP and goldens.
postMetrics.flip(test, reference)(andcpu:flip(reference)on any CPU picture) is NVIDIA FLIP: how different two pictures look, per pixel in 0..1, itsmeanthe number the reference evaluator reports.postMetrics.golden(frame, path)holds a picture against a stored PNG and passes at a mean of 0.02 or under, the level of the noise a GPU or a platform puts on a picture, below any change a viewer sees;{ record = true }makes the golden. - Pixel metrics.
banding,haloOvershoot,temporalVariance,ringEnergy(the light a bloom spreads around a point),meanColourandhueDrift(a colour's hue across exposures). - Known light.
postMetrics.chart.show(props)draws@builtin::shaders.post.hdrChartin place of the scene, first in the chain: a ramp, a grey wedge over stops, the ColorChecker, or a saturated colour over its grey.postMetrics.chart.referencebuilds the picture the display should present for it under a curve, frompostMetrics.curves, the CPU twins of what__display__draws. - Scopes.
scopes.histogram(with a metering window),waveform,vectorscope,falseColor(stops from middle grey),zebra(the contours of a quantised gradient) andtoneCurve, each a picturepostMetrics.picturereturns to an agent. - Reference scenes.
@builtin::scenes.postReference.exposureWedge,bloomEnergy,temporalTorture,banding,dofChartandhdrChart: known content with aReferenceCameraeach.
The module's README (@builtin::modules.postMetrics) covers every call, and post_verification.testSuite shows each instrument moving on a known-bad control.