dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / tests / editor / render_mode_tests.luc
## The viewport's Render shading mode (render_mode.luc): luce-render's image
## of the displayed scene through the viewport's camera, drawn offscreen as
## the window would draw it.
## - Switching into the mode renders, switching out stops the render.
## - A camera move restarts the image (at a preview size when the scene is
## slow, settling to full size); a recook (a light's intensity) restarts it too, reloading the
## lights over the geometry already on the GPU.
## - The overlays (the grid, the camera and light helpers, the chrome) still
## draw over the image.
## - Through the scene camera's view, the image matches the Render panel's
## within noise.
## - Until the image suits the viewport (the mode just entered, the viewport
## resized larger or smaller), the viewport shows the shaded scene, never a
## blank nor an image of the old size; then the image, without going back.
from app import Luce3D
from workspace import Workspace
from offscreen_pixels import OffscreenPixels
from render_view import RenderView
from luce_3d.graphics import Vector3
import node_commands
from luce_std import math
import time
## A sphere on a floor, a camera looking down at it, an area light and a
## Render node (16 samples, 4 bounces, RGB); the light's id.
func lit_scene(w: Workspace) -> int!:
node_commands.delete_selected(w)
node_commands.add_node(w, "Sphere", 0.0, 0.0)
node_commands.set_parameter(w, 1, 48.0)
node_commands.set_parameter(w, 2, 24.0)
let ball = w.network.selected
node_commands.add_node(w, "Grid", 200.0, 0.0)
node_commands.set_parameter(w, 0, 12.0)
node_commands.set_parameter(w, 1, 12.0)
let floor = w.network.selected
node_commands.select_node(w, ball)
node_commands.add_node(w, "Merge", 0.0, 100.0)
node_commands.connect(w, floor, w.network.selected, 1)
node_commands.leave_group(w)
let geometry = w.network.selected
node_commands.add_node(w, "Camera", 0.0, 200.0)
let camera = w.network.selected
node_commands.connect(w, geometry, camera, 0)
for (slot, value) in [(1, 0.0), (2, 2.5), (3, 6.0), (4, -18.0), (5, 0.0)]:
node_commands.set_parameter(w, slot, value)
node_commands.add_node(w, "Light", 0.0, 300.0)
node_commands.connect(w, camera, w.network.selected, 0)
for (slot, value) in [(1, 1.0), (2, 4.0), (3, 1.0), (10, 30.0), (14, 3.0), (15, 3.0)]:
node_commands.set_parameter(w, slot, value)
let light = w.network.selected
node_commands.add_node(w, "Render", 0.0, 400.0)
node_commands.connect(w, light, w.network.selected, 0)
for (slot, value) in [(1, 320.0), (2, 200.0), (3, 16.0), (4, 1.0), (5, 4.0)]:
node_commands.set_parameter(w, slot, value)
node_commands.display_selected(w)
return light
## Draw the viewport into `view`.
func draw(w: Luce3D, view: OffscreenPixels) -> unit!:
let target = view.begin()
w.panels.view.draw(target)
view.end()
## Draw until the render shows `samples` at full size (or 60 s pass).
func refine(editor: Luce3D, view: OffscreenPixels, samples: int) -> unit!:
let render = editor.panels.view.render_mode()
let deadline = time.now() + 60000000000
while (render.preparing() or render.samples_done() < samples or render.divisor > 1 or not render.shown) and time.now() < deadline:
draw(editor, view)
assert(not render.preparing() and render.samples_done() >= samples and render.divisor == 1 and render.shown)
## The mean luminance of a block of pixels.
func block(view: OffscreenPixels, x: int, y: int, size: int) -> float:
var sum = 0
for dy in 0..<size:
for dx in 0..<size:
sum += view.luminance(x + dx, y + dy)
return float(sum) / float(size * size)
## Pixels of the rows `y0..<y1` whose luminance differs by more than `by`.
func changed(a: list[int], b: list[int], by: int) -> int:
var count = 0
for at in 0..<a.length:
if math.abs(float(a[at] - b[at])) > float(by):
count += 1
return count
func rows(view: OffscreenPixels, width: int, y0: int, y1: int) -> list[int]:
let values: list[int] = []
for y in y0..<y1:
for x in 0..<width:
values.append(view.luminance(x, y))
return values
## Place the viewport's camera where the scene's camera is, with the field of
## view luce-render gives it at `width` × `height` (its film fits the width).
func look_through_scene_camera(w: Workspace, width: int, height: int) -> unit!:
let data = w.network.evaluate_data(w.network.display)
let objects = data.geometry.scene()
for at in 0..<objects.count():
if not objects.is_camera(at):
continue
let item = objects.object(at)
let tan_half = 0.5 * item.camera.horizontal_aperture / item.camera.focal_length * float(height) / float(width)
let degrees = 2.0 * math.atan2(tan_half, 1.0) * 180.0 / math.pi
w.view.camera.set_projection(degrees, 0.05, 500.0)
let at_position = Vector3(item.matrix.translation.x, item.matrix.translation.y, item.matrix.translation.z)
let forward = Vector3(-item.matrix.z.x, -item.matrix.z.y, -item.matrix.z.z)
w.view.camera.set_position(at_position)
w.view.camera.look_at(at_position.add(forward))
return
trap("the scene has no camera")
pub func render_mode_tests() -> unit!:
let editor = Luce3D()
let w = editor.workspace
let light = lit_scene(w)
w.frame_cube()
let render = editor.panels.view.render_mode()
let view = OffscreenPixels(320, 200)
# Shaded: no render. Render: the image refines, and the floor under the
# light is lit where the shaded view's backdrop was.
draw(editor, view)
assert(not render.active)
w.display.render_samples = 16
w.set_display_mode(5)
refine(editor, view, 16)
let (compiles, uploads, reloads) = render.counts()
assert(compiles == 1 and uploads == 1 and reloads == 0)
assert(not render.running())
# A camera move restarts the image (at a preview size where a sample at
# full size would take longer than a frame; this scene is cheap enough),
# and it refines again.
w.view.orbit(30.0, 0.0)
draw(editor, view)
assert(render.samples_done() < 16 and render.running())
refine(editor, view, 16)
let (_, uploads_moved, _) = render.counts()
assert(uploads_moved == 1)
# A recook with a brighter light restarts the image; the geometry stays
# on the GPU (a reload, no upload).
let before = block(view, 150, 120, 20)
w.network.selected = light
node_commands.set_parameter(w, 10, 60.0)
draw(editor, view)
assert(render.running())
refine(editor, view, 16)
let (_, uploads_after, reloads_after) = render.counts()
assert(uploads_after == 1 and reloads_after == 1)
let brighter = block(view, 150, 120, 20)
assert(brighter > before + 5.0)
# The overlays draw over the image: the grid changes pixels where the
# floor is not, and the chrome's labels stay.
let with_grid = rows(view, 320, 150, 190)
w.view.grid_visible = false
w.revision += 1
draw(editor, view)
let without_grid = rows(view, 320, 150, 190)
assert(changed(with_grid, without_grid, 8) > 100)
w.view.grid_visible = true
# Out of the mode: the render stops.
w.set_display_mode(3)
draw(editor, view)
assert(not render.active and not render.running())
# Through the scene camera, the viewport's image matches the Render
# panel's: both 64 samples of the same scene and settings, compared by
# blocks away from the viewport's chrome (the camera menu and the
# navigation gizmo take the top right).
w.view.grid_visible = false
w.display.hulls_visible = false
w.network.selected = w.network.display
node_commands.set_parameter(w, 3, 64.0)
w.display.render_samples = 64
look_through_scene_camera(w, 320, 200)
w.set_display_mode(5)
refine(editor, view, 64)
let node = w.network.require(w.network.display)
let panel = RenderView(w.display)
panel.start(w.network.evaluate_data(node.id), node.values, w.network.render_camera(node))
let framed = OffscreenPixels(336, 238)
let deadline = time.now() + 60000000000
while panel.running() and time.now() < deadline:
let target = framed.begin()
panel.draw(target)
framed.end()
let target = framed.begin()
panel.draw(target)
framed.end()
var worst = 0.0
var compared = 0
for y in [80, 100, 120, 140]:
for x in [60, 100, 140, 180]:
let ours = block(view, x, y, 16)
let theirs = block(framed, x + 8, y + 30, 16)
if theirs > 12.0:
worst = math.max(worst, math.abs(ours - theirs) / theirs)
compared += 1
if compared < 10 or worst > 0.05:
print(f"render mode against the Render panel: {compared} blocks, worst relative difference {worst}")
assert(false)
panel.stop()
w.set_display_mode(3)
draw(editor, view)
editor.close()
print(f"ok render mode: switches, restarts on camera moves and recooks (reloading lights over the same geometry), keeps overlays, matches the Render panel within {worst * 100.0:.2f}% ({compared} blocks)")
cad_render_mode()
resized_render_mode()
## Analytic CAD renders through the same cached surface as the shaded view,
## locally and across the worker boundary, without inserting Tessellate nodes.
func cad_render_mode() -> unit!:
let editor = Luce3D()
let w = editor.workspace
node_commands.delete_selected(w)
node_commands.leave_group(w)
node_commands.add_node(w, "CAD", 0.0, 0.0)
let cad = w.network.selected
node_commands.enter_group(w, cad)
node_commands.add_node(w, "Solid Box", 0.0, 0.0)
let solid = w.network.selected
node_commands.leave_group(w)
assert(w.network.evaluate_data(solid).has_cad() and w.network.evaluate_data(solid).mesh == none)
w.frame_cube()
w.view.grid_visible = false
w.display.hulls_visible = false
w.display.render_samples = 16
w.set_display_mode(5)
let view = OffscreenPixels(320, 200)
refine(editor, view, 16)
assert(editor.panels.view.render_mode().default_sky)
let local = rows(view, 320, 50, 150)
w.enable_compute()
settle_worker(w)
draw(editor, view)
refine(editor, view, 16)
let worker = rows(view, 320, 50, 150)
assert(changed(local, worker, 8) < 50)
# Quality is display state, but must invalidate the render's scene too.
let before = w.batches.scene_keys.copy()
w.set_cad_quality(32)
settle_worker(w)
assert(w.batches.scene_keys != before)
draw(editor, view)
refine(editor, view, 16)
# Hiding the CAD removes its filled surface, not merely its edge lines.
w.network.require(cad).visible = false
w.rebuild()
settle_worker(w)
draw(editor, view)
refine(editor, view, 16)
let hidden = rows(view, 320, 50, 150)
assert(changed(worker, hidden, 8) > 1000)
editor.close()
print("PASS rendered CAD surfaces: default sky, local/worker image agreement, quality invalidation and visibility")
func settle_worker(w: Workspace) -> unit!:
let session = w.compute else trap("compute")
let deadline = time.now() + 20000000000
while session.busy and time.now() < deadline:
_ = w.poll_compute()
assert(not session.busy and w.notice == "")
## Pixels of `a` and `b` (as the rows `rows` read them) that differ by more
## than a little, between the top and bottom chrome rows.
func differing(a: list[int], b: list[int]) -> int:
return changed(a, b, 6)
## Render mode while the viewport resizes: the shaded scene, exactly as
## Shaded mode draws it (but the corner label), until the first whole image at
## the new size, then the image from that frame on.
func resized_render_mode() -> unit!:
let editor = Luce3D()
let w = editor.workspace
_ = lit_scene(w)
w.frame_cube()
let render = editor.panels.view.render_mode()
let view = OffscreenPixels(320, 200)
# The shaded references, at both sizes.
draw(editor, view)
draw(editor, view)
let shaded_small = rows(view, 320, 40, 160)
view.resize(400, 250)
draw(editor, view)
draw(editor, view)
let shaded_large = rows(view, 400, 40, 210)
view.resize(320, 200)
w.display.render_samples = 16
w.set_display_mode(5)
# Entering the mode: shaded until the first image.
draw(editor, view)
let entered = differing(rows(view, 320, 40, 160), shaded_small)
if render.shown or entered >= 40:
print(f"render mode entered: shown {render.shown}, {entered} pixels differ from Shaded")
assert(false)
refine(editor, view, 16)
let rendered_small = rows(view, 320, 40, 160)
assert(differing(rendered_small, shaded_small) > 2000)
# Larger, then smaller (no new buffers): each time shaded at once, then
# the image of the new size, never back to shaded.
var shaded_frames = 0
for (width, height, shaded) in [(400, 250, shaded_large), (320, 200, shaded_small)]:
view.resize(width, height)
var shown = false
var frames = 0
let deadline = time.now() + 60000000000
while not (shown and render.samples_done() >= 4) and time.now() < deadline:
draw(editor, view)
frames += 1
let now_shown = render.shown
assert(now_shown or not shown)
if frames == 1:
assert(not now_shown)
if not now_shown:
shaded_frames += 1
assert(differing(rows(view, width, 40, height - 40), shaded) < 40)
else:
assert(render.samples_done() >= 1)
assert(differing(rows(view, width, 40, height - 40), shaded) > 2000)
shown = now_shown
assert(shown)
editor.close()
print(f"PASS render mode resized: shaded until the image of the new size ({shaded_frames} shaded frames), never blank or stretched, no flicker")