Time-gated ReSTIR (offline)

This tutorial renders the time-gated Cornell box from Time-gated rendering (offline) with TimeGatedReSTIRInline instead of the path tracer, and then compares the two at equal rendering time.

A narrow gate keeps only a small fraction of the paths a path tracer samples, so most pixels see few paths that fit the gate and the image is noisy. TimeGatedReSTIRInline samples paths the same way, but then lets every pixel reuse the paths found by its neighbors: each round of spatial reuse resamples a few nearby pixels’ paths, shifted to this pixel with a path-length-aware shift mapping that keeps their length inside the gate. This tutorial uses a 0.02-wide gate, five times narrower than the path-tracing tutorial.

It uses the same scene file as the other tutorials (see ToF rendering).

Time-gated Cornell box rendered with TG ReSTIR

1. Load the scene

import falcorcomp as falcor

testbed = falcor.Testbed(create_window=False)
testbed.load_scene("cornell-box/scene-v4-nolight.pbrt")
testbed.resize_frame_buffer(512, 512)
testbed.scene.camera.aspectRatio = 1.0
testbed.clock.pause()

2. Build the render graph

The graph is the same as for the path tracer; only the tracer changes. TimeGatedReSTIRInline takes the path tracer’s options for its initial candidates (here 16 per pixel) and the gate, plus the reuse options:

  • spatialReuseIteration, spatialReuseNeighborCount, spatialReuseGatherRadius: 3 rounds of spatial reuse, each resampling 5 neighbors within 10 pixels.

  • useTemporalReuse: off. Temporal reuse also reuses paths from earlier frames, which is meant for online rendering; here every frame is independent and AccumulatePass averages them.

  • shiftMappingMethod, gaugeMode, reconnectionRoughnessThreshold: how a neighbor’s path is shifted to this pixel while keeping its length. The defaults do not shift paths (no), so set them.

graph = testbed.create_render_graph("TimeGatedReSTIR")
graph.create_pass("VBuffer", "VBufferRT", {"samplePattern": "Center", "sampleCount": 1})
graph.create_pass("Laser", "LaserLight", {
    "laserPosition": [0.0, 1.7, 6.8], "laserDirection": [0.0, 0.0, -1.0],
    "laserPower": [170.0, 120.0, 40.0], "laserAngle": 0.0,
})
graph.create_pass("Tracer", "TimeGatedReSTIRInline", {
    # Initial candidates and the gate, as for the path tracer.
    "samplesPerPixel": 16, "maxBounces": 6,
    "timeGateMode": "box", "timeGateWindow": 0.02, "timeCenter": 17.337,
    # Spatial reuse: 3 rounds, each resampling 5 neighbors within 10 pixels.
    "spatialReuseIteration": 3, "spatialReuseNeighborCount": 5,
    "spatialReuseGatherRadius": 10.0, "useTemporalReuse": False,
    # Path-length-aware shift mapping between pixels.
    "shiftMappingMethod": "local_tangent", "gaugeMode": "avg_grad",
    "reconnectionRoughnessThreshold": 0.05,
})
graph.create_pass("Accumulate", "AccumulatePass", {"precisionMode": "SingleCompensated"})
graph.create_pass("ToneMapper", "ToneMapper", {"autoExposure": False})

graph.add_edge("VBuffer.vbuffer", "Tracer.vbuffer")
graph.add_edge("VBuffer.viewW", "Tracer.viewW")
graph.add_edge("Laser", "Tracer")
graph.add_edge("Tracer.color", "Accumulate.input")
graph.add_edge("Accumulate.output", "ToneMapper.src")
graph.mark_output("Accumulate.output")  # output 0: linear radiance
graph.mark_output("ToneMapper.dst")     # output 1: tone mapped
testbed.render_graph = graph

3. Render

A ReSTIR frame costs more than a path-tracing frame with the same number of initial samples, but each of its pixels gathers many more paths that fit the gate.

for _ in range(64):  # 64 frames x 16 initial candidates
    testbed.frame()

4. Save the image

testbed.capture_output("time_gated_restir.exr", 0)
testbed.capture_output("time_gated_restir.png", 1)

The full script: time_gated_restir_offline.py.

Equal-time comparison

To compare the two tracers fairly, give each the same rendering time. The comparison script builds the same render graph around TimeGatedPathTracerInline and TimeGatedReSTIRInline (16 initial samples per pixel each, same gate), renders each for 1 second, and compares both with a reference rendered by the path tracer with 65,536 samples per pixel. The error is the relative mean squared error, \(\mathrm{relMSE} = \overline{(I - I_\mathrm{ref})^2} / \overline{I_\mathrm{ref}^2}\).

The timing waits for the GPU after every frame, so it measures the full cost of each frame, and leaves out a few warm-up frames, in which the shaders are compiled:

def render(graph, seconds=None, frames=None):
    testbed.render_graph = graph
    for _ in range(10):  # warm-up frames compile the shaders; they are not counted
        testbed.frame()
    graph.get_pass("Accumulate").reset()
    testbed.device.wait()
    count, elapsed = 0, 0.0
    while (elapsed < seconds) if seconds is not None else (count < frames):
        start = time.perf_counter()
        testbed.frame()
        testbed.device.wait()  # include the frame's GPU time
        elapsed += time.perf_counter() - start
        count += 1
    image = graph.get_output("Accumulate.output").to_numpy()[..., :3]
    return image, count, elapsed


pt_graph = create_graph("TimeGatedPathTracerInline", PT)
pt_image, pt_frames, pt_time = render(pt_graph, seconds=SECONDS)
restir_graph = create_graph("TimeGatedReSTIRInline", RESTIR)
restir_image, restir_frames, restir_time = render(restir_graph, seconds=SECONDS)

Equal-time comparison of TGPT and TG ReSTIR with a reference

Method (1 second)

Frames

Samples per pixel

relMSE

TimeGatedPathTracerInline

206

3,296

0.0858

TimeGatedReSTIRInline

112

1,792 initial

0.0120

In the same time, ReSTIR renders about half as many frames but reaches a 7 times lower error. These numbers were measured on an NVIDIA GeForce RTX 3090 with Vulkan; frame counts and errors depend on the GPU.

The full comparison script, which also saves the image above: time_gated_equal_time.py. It takes about 40 seconds, most of it for the reference.