dymokomi/luce-render
Source of release 0.1.2
0.1.2 / src / render / tests.lucb
#==============================================================================================
#
# render/tests - Tests of the host side: scene compilation, the BVH and ray frames
#
#==============================================================================================
## Every triangle in exactly one leaf, every leaf's triangles inside its box, and
## every child's box inside its parent's.
func check_bvh(scene: const CompiledScene*):
let count = scene.triangle_count()
var seen = Growing[u32]()
defer seen.close()
seen.fill(count, 0) catch failure: trap(failure.message)
let p = scene.positions.view()
for node in scene.nodes.view():
if node.count == 0:
if count == 0: continue
for child in [node.first, node.first + 1]:
let inner = scene.nodes.items[(usize)child]
assert(inner.lo.x >= node.lo.x and inner.lo.y >= node.lo.y and inner.lo.z >= node.lo.z)
assert(inner.hi.x <= node.hi.x and inner.hi.y <= node.hi.y and inner.hi.z <= node.hi.z)
continue
for slot in node.first..<node.first + node.count:
seen.items[(usize)slot] += 1
for k in 0..<(u32)3:
let i = (usize)scene.indices.items[(usize)(slot * 3 + k)] * 3
assert(p[i] >= node.lo.x and p[i] <= node.hi.x and p[i + 1] >= node.lo.y and p[i + 1] <= node.hi.y and p[i + 2] >= node.lo.z and p[i + 2] <= node.hi.z)
for at in 0..<count: assert(seen.items[at] == 1)
test "a cube compiles to 12 triangles of 6 faces under one BVH":
let cube = Mesh.cube(2.0) catch failure: trap(failure.message)
defer cube.release()
let set = GeometrySet.of_mesh(cube) catch failure: trap(failure.message)
defer set.release()
var scene = compile_scene(set.value()) catch failure: trap(failure.message)
defer scene.close()
assert(scene.triangle_count() == 12 and scene.point_count() == 8 and scene.skipped == 0)
var faces: u32[6]
for face in scene.faces.view(): faces[(usize)face] += 1
for count in faces: assert(count == 2)
check_bvh(&scene)
test "instances are placed and many triangles split into leaves of at most 16":
let grid = MeshPrimitives.grid(10.0, 40) catch failure: trap(failure.message)
defer grid.release()
let plane = GeometrySet.of_mesh(grid) catch failure: trap(failure.message)
defer plane.release()
# The grid itself, and a copy of it as an instance 5 units up.
let base = GeometrySet.of_mesh(grid) catch failure: trap(failure.message)
defer base.release()
let placed = base.value().with_instance(plane, Vector3(0.0, 5.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0), true) catch failure: trap(failure.message)
defer placed.release()
var scene = compile_scene(placed.value()) catch failure: trap(failure.message)
defer scene.close()
assert(scene.triangle_count() == 2 * 2 * 40 * 40)
var high: f32 = -1.0
for at in 0..<scene.point_count(): high = math32.max(high, scene.positions.items[at * 3 + 1])
assert(high == 5.0)
for node in scene.nodes.view(): assert(node.count <= 16)
assert(scene.nodes.count > 100)
check_bvh(&scene)
test "a camera's ray frame fits its film horizontally and looks down -Z":
let cube = Mesh.cube(1.0) catch failure: trap(failure.message)
defer cube.release()
let base = GeometrySet.of_mesh(cube) catch failure: trap(failure.message)
defer base.release()
let with_camera = base.value().with_camera("/cam", Vector3(0.0, 0.0, 5.0), Vector3(), 0, 18.0, 36.0, 24.0, 0.1, 100.0, 0.0, 5.0) catch failure: trap(failure.message)
defer with_camera.release()
var scene = compile_scene(with_camera.value()) catch failure: trap(failure.message)
defer scene.close()
assert(scene.objects.count() == 1)
let frame = ray_frame(scene.objects.object(0), 200, 100) catch failure: trap(failure.message)
# 36 mm across at 18 mm: 90° horizontally, so the left edge runs at 45°.
let (origin, left) = frame.ray(0.0, 50.0)
assert(origin.z == 5.0 and math.abs(left.x + 1.0) < 1e-9 and math.abs(left.z + 1.0) < 1e-9)
let (_, center) = frame.ray(100.0, 50.0)
assert(math.abs(center.x) < 1e-9 and math.abs(center.y) < 1e-9 and center.z == -1.0)
# Half as tall as wide: the top edge at atan(0.5).
let (_, top) = frame.ray(100.0, 0.0)
assert(math.abs(top.y - 0.5) < 1e-9)
test "a color's spectrum fit returns it through the film, and white is a constant":
let to_acescg = film_matrix()
let white = fit_spectrum(Vector3(1.0, 1.0, 1.0))
assert(white.c0 == 0.0 and white.c1 == 0.0 and white.c2 == 0.0 and math.abs(white.at(500.0) - 1.0) < 1e-12)
# A constant spectrum of 1 is XYZ (1, 1, 1) under E, which is ACEScg white.
let back = to_acescg.apply(Vector3(1.0, 1.0, 1.0))
assert(math.abs(back.x - 1.0) < 1e-6 and math.abs(back.y - 1.0) < 1e-6 and math.abs(back.z - 1.0) < 1e-6)
let integrals = cmf_integrals()
for color in [Vector3(0.8, 0.2, 0.1), Vector3(0.05, 0.5, 0.2), Vector3(0.1, 0.2, 0.9), Vector3(0.9, 0.9, 0.2)]:
let fit = fit_spectrum(color)
var sum = Vector3()
var lambda = lambda_min
while lambda <= lambda_max:
sum = sum.add(cmf(lambda).multiply_scalar(fit.at(lambda)))
lambda += 1.0
let rgb = to_acescg.apply(Vector3(sum.x / integrals.x, sum.y / integrals.y, sum.z / integrals.z))
assert(math.abs(rgb.x - color.x) < 0.01 and math.abs(rgb.y - color.y) < 0.01 and math.abs(rgb.z - color.z) < 0.01)
test "a blackbody near 5455 K (white E's color) is neutral; 3000 K is warm and 9000 K cool":
let to_acescg = film_matrix()
let neutral = blackbody_xyz(5455.0)
let n = to_acescg.apply(neutral.multiply_scalar(1.0 / neutral.y))
# E sits just off the Planckian locus, so its blackbody is a little green.
assert(math.abs(n.x - 1.0) < 0.06 and math.abs(n.y - 1.0) < 0.06 and math.abs(n.z - 1.0) < 0.06)
let warm = blackbody_xyz(3000.0)
let w = to_acescg.apply(warm.multiply_scalar(1.0 / warm.y))
assert(w.x > 1.2 * w.y and w.y > w.z)
let cool = blackbody_xyz(9000.0)
let k = to_acescg.apply(cool.multiply_scalar(1.0 / cool.y))
assert(k.z > k.y and k.y > k.x)
## The kernels' octahedral decode (shaders/openpbr.glsl), for checks.
func decode_octahedral(packed: u32) -> Vector3:
let x = math.max(-1.0, (f64)(i16)(u16)(packed & 0xffff) / 32767.0)
let y = math.max(-1.0, (f64)(i16)(u16)(packed >> 16) / 32767.0)
var n = Vector3(x, y, 1.0 - math.abs(x) - math.abs(y))
if n.z < 0.0:
n = Vector3((1.0 - math.abs(y)) * (1.0 if x >= 0.0 else -1.0), (1.0 - math.abs(x)) * (1.0 if y >= 0.0 else -1.0), n.z)
return n.normalized() catch failure: trap(failure.message)
test "a sphere's shading normals point away from its center (MeshPrimitives centers it at y = radius)":
let sphere = MeshPrimitives.sphere(1.0, 16) catch failure: trap(failure.message)
defer sphere.release()
let set = GeometrySet.of_mesh(sphere) catch failure: trap(failure.message)
defer set.release()
var scene = compile_scene(set.value()) catch failure: trap(failure.message)
defer scene.close()
for direction in [Vector3(0.3, -0.5, 0.8), Vector3(-1.0, 0.0, 0.0), Vector3(0.0, 0.0, -1.0), Vector3(0.6, 0.6, -0.5)]:
let expected = direction.normalized() catch failure: trap(failure.message)
let back = decode_octahedral(octahedral(expected))
assert(back.subtract(expected).length() < 1e-3)
let p = scene.positions.view()
for triangle in 0..<scene.triangle_count():
for k in 0..<(usize)3:
let i = (usize)scene.indices.items[triangle * 3 + k] * 3
let position = Vector3((f64)p[i], (f64)p[i + 1] - 1.0, (f64)p[i + 2])
let normal = decode_octahedral(scene.shading.items[triangle * 4 + k])
if normal.dot(position) < 0.9:
print(f"triangle {triangle} corner {k}: position {position.x} {position.y} {position.z} normal {normal.x} {normal.y} {normal.z}")
assert(false)
test "the light tree reaches every emitter by its trail, and holds their energy at the root":
let grid = MeshPrimitives.grid(4.0, 9) catch failure: trap(failure.message)
defer grid.release()
let set = GeometrySet.of_mesh(grid) catch failure: trap(failure.message)
defer set.release()
var scene = compile_scene(set.value()) catch failure: trap(failure.message)
defer scene.close()
var glowing = OpenPbr.standard()
glowing.emission_luminance = 2.0
let materials: OpenPbr[1] = [glowing]
var no_lights: f32[1] = [0.0]
var tree = build_light_tree(&scene, no_lights[..<0], 0, materials) catch failure: trap(failure.message)
defer tree.close()
let count = tree.emitter_count
assert(count == scene.triangle_count() and count == 162)
var total: f64 = 0.0
for index in 0..<count:
let trail = tree.records.items[index * 4 + 1]
let depth = tree.records.items[index * 4 + 2]
assert(depth <= 8)
var node: usize = 0
for level in 0..<depth:
node = (usize)tree.links.items[node * 2] + (usize)((trail >> level) & 1)
assert(tree.links.items[node * 2 + 1] == leaf_node and (usize)tree.links.items[node * 2] == index)
# The triangle map points back at the emitter.
let code = tree.records.items[index * 4]
assert(code & emitter_triangle != 0 and tree.records.items[count * 4 + (usize)(code & ~emitter_triangle)] == (u32)index)
total += (f64)tree.nodes.items[node * 12 + 3]
assert(math.abs((f64)tree.nodes.items[3] / total - 1.0) < 1e-4)