dymokomi/luce-gpu
Source of release 0.5.4
0.5.4 / tests / gpu / rays.lucb
import c
from luce_gpu import gpu
import memory
import kernels
from luce_std import math32
## Ray queries against acceleration structures, checked against a CPU
## intersector: closest hits and shadow rays on a small mesh, a refit after its
## vertices move, two instances with transforms, ids and masks, Blases and
## Tlases destroyed while recorded work still uses them, and procedural boxes
## (spheres) beside triangles in one Tlas.
pub func checks(device: gpu.Device) -> !:
let limits = try device.limits()
if not limits.ray_query:
print("skip: no ray query")
return
var scene = try Scene.open(device)
defer scene.close()
try scene.single(device)
try scene.refit(device)
try scene.instances(device)
try lifetimes(device)
try spheres(device, &scene)
## A 4 by 4 grid of quads (32 triangles) over x, y in 0..4, at a height that
## varies with x and y so no two triangles are coplanar.
let grid: u32 = 4
let vertex_count: usize = 25
let triangle_count: usize = 32
func height(x: f32, y: f32, lift: f32) -> f32:
return 1.0 + 0.1 * x + 0.05 * y * y + lift
func mesh_vertices(lift: f32) -> f32[75]:
var positions: f32[75]
for y in 0..<(usize)5:
for x in 0..<(usize)5:
let index = (y * 5 + x) * 3
positions[index] = (f32)x
positions[index + 1] = (f32)y
positions[index + 2] = height((f32)x, (f32)y, lift if x % 2 == 0 else lift * 0.5)
return positions
func mesh_indices() -> u32[96]:
var indices: u32[96]
var at: usize = 0
for y in 0..<grid:
for x in 0..<grid:
let first = y * 5 + x
indices[at] = first
indices[at + 1] = first + 1
indices[at + 2] = first + 6
indices[at + 3] = first
indices[at + 4] = first + 6
indices[at + 5] = first + 5
at += 6
return indices
func bytes_of_floats(values: const f32[]) -> u8[]!:
let bytes = try new u8[values.length * 4] in memory.heap
for index in 0..<values.length:
memory.write[f32]((void*)&bytes[index * 4], values[index])
return bytes
func bytes_of_words(values: const u32[]) -> u8[]!:
let bytes = try new u8[values.length * 4] in memory.heap
for index in 0..<values.length:
memory.write[u32]((void*)&bytes[index * 4], values[index])
return bytes
## A CPU ray-triangle hit: t and barycentrics (weights of the second and third
## vertex), Möller–Trumbore, as every GPU reports them.
struct Hit:
var t: f32 = -1.0
var u: f32
var v: f32
var primitive: u32
func cross(a: f32[3], b: f32[3]) -> f32[3]:
return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
func dot(a: f32[3], b: f32[3]) -> f32:
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
func minus(a: f32[3], b: f32[3]) -> f32[3]:
return [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
func vertex_at(positions: const f32[], index: u32, shift: f32) -> f32[3]:
let at = (usize)index * 3
return [positions[at] + shift, positions[at + 1], positions[at + 2]]
## The closest hit of the ray from `origin` along `direction` up to `tmax`
## with the mesh shifted by `shift` in x.
func closest(positions: const f32[], indices: const u32[], origin: f32[3], direction: f32[3], tmax: f32, shift: f32) -> Hit:
var best = Hit()
for triangle in 0..<triangle_count:
let a = vertex_at(positions, indices[triangle * 3], shift)
let b = vertex_at(positions, indices[triangle * 3 + 1], shift)
let d = vertex_at(positions, indices[triangle * 3 + 2], shift)
let edge1 = minus(b, a)
let edge2 = minus(d, a)
let p = cross(direction, edge2)
let determinant = dot(edge1, p)
if determinant > -0.0000001 and determinant < 0.0000001:
continue
let inverse = 1.0 / determinant
let s = minus(origin, a)
let u = dot(s, p) * inverse
if u < 0.0 or u > 1.0:
continue
let q = cross(s, edge1)
let v = dot(direction, q) * inverse
if v < 0.0 or u + v > 1.0:
continue
let t = dot(edge2, q) * inverse
if t > 0.0 and t <= tmax and (best.t < 0.0 or t < best.t):
best = Hit(t = t, u = u, v = v, primitive = (u32)triangle)
return best
func near(value: f32, wanted: f32) -> bool:
return value >= wanted - 0.0005 and value <= wanted + 0.0005
## The mesh, its Blas and a one-instance Tlas, the ray kernel, and its buffers.
struct Scene:
var program: gpu.Kernel
var vertices: gpu.Buffer
var indices: gpu.Buffer
var blas: gpu.Blas
var tlas: gpu.Tlas
var rays: gpu.Buffer
var hits: gpu.Buffer
var ids: gpu.Buffer
var positions: f32[75]
var corners: u32[96]
static func open(device: gpu.Device) -> Scene!:
var scene = Scene(positions = mesh_vertices(0.0), corners = mesh_indices())
scene.program = try gpu.Kernel.create(device, kernels.rays_comp_words, kernels.rays_comp_msl, kernels.rays_comp_group, kernels.rays_comp_bindings)
let vertex_bytes = try bytes_of_floats(scene.positions)
defer free(vertex_bytes) in memory.heap
let index_bytes = try bytes_of_words(scene.corners)
defer free(index_bytes) in memory.heap
scene.vertices = try gpu.Buffer.create(device, vertex_bytes)
scene.indices = try gpu.Buffer.create(device, index_bytes)
scene.blas = try gpu.Blas.create(device, scene.triangles())
scene.tlas = try gpu.Tlas.create(device, 2)
scene.rays = try gpu.Buffer.allocate(device, 64 * 32)
scene.hits = try gpu.Buffer.allocate(device, 64 * 16)
scene.ids = try gpu.Buffer.allocate(device, 64 * 16)
return scene
func close():
self.program.destroy()
self.vertices.destroy()
self.indices.destroy()
self.blas.destroy()
self.tlas.destroy()
self.rays.destroy()
self.hits.destroy()
self.ids.destroy()
func triangles() -> gpu.Triangles:
return gpu.Triangles(vertices = self.vertices, vertex_count = (u32)vertex_count, indices = self.indices, triangle_count = (u32)triangle_count)
## 64 rays straight up from z = 0 over x in -0.5..4.5 (some miss), each
## starting at `x0`; ray 63 is too short to reach the mesh.
func ray_origin(index: usize, x0: f32) -> f32[3]:
return [x0 + (f32)(index % 8) * 0.71 - 0.5, (f32)(index // 8) * 0.53 + 0.1, 0.0]
func ray_limit(index: usize) -> f32:
return 0.5 if index == 63 else 100.0
func upload_rays(x0: f32) -> !:
var data: f32[512]
for index in 0..<(usize)64:
let origin = self.ray_origin(index, x0)
data[index * 8] = origin[0]
data[index * 8 + 1] = origin[1]
data[index * 8 + 2] = origin[2]
data[index * 8 + 3] = self.ray_limit(index)
data[index * 8 + 6] = 1.0
let bytes = try bytes_of_floats(data)
defer free(bytes) in memory.heap
try self.rays.upload(0, bytes)
## Trace the 64 rays with `mask`, as shadow rays or closest-hit rays.
func trace(device: gpu.Device, shadow: bool, mask: u32) -> !:
var uniforms: u8[12]
memory.write[u32]((void*)&uniforms[0], 64)
memory.write[u32]((void*)&uniforms[4], 1 if shadow else 0)
memory.write[u32]((void*)&uniforms[8], mask)
var pass = try gpu.Compute.begin(device)
defer pass.close()
let bindings: gpu.Binding[4] = [gpu.Binding(accel = self.tlas), gpu.Binding(buffer = self.rays), gpu.Binding(buffer = self.hits), gpu.Binding(buffer = self.ids)]
try pass.dispatch(self.program, bindings, uniforms, 1)
try pass.submit()
try pass.wait()
## Compare the GPU's hits with the CPU's for the mesh shifted by `shift`;
## `id` and `instance` are the instance the hits must report.
func compare(device: gpu.Device, x0: f32, shift: f32, id: u32, instance: u32, shadow: bool) -> usize!:
var hits: u8[1024]
var ids: u8[1024]
try self.hits.read(0, hits)
try self.ids.read(0, ids)
var hit_count: usize = 0
for index in 0..<(usize)64:
let expected = closest(self.positions, self.corners, self.ray_origin(index, x0), [0.0, 0.0, 1.0], self.ray_limit(index), shift)
let t = memory.read[f32]((const void*)&hits[index * 16])
let hit = memory.read[f32]((const void*)&hits[index * 16 + 12])
if expected.t < 0.0:
assert(hit == 0.0)
continue
hit_count += 1
assert(hit == 1.0)
if shadow:
continue
assert(near(t, expected.t))
assert(near(memory.read[f32]((const void*)&hits[index * 16 + 4]), expected.u) and near(memory.read[f32]((const void*)&hits[index * 16 + 8]), expected.v))
assert(memory.read[u32]((const void*)&ids[index * 16]) == expected.primitive)
assert(memory.read[u32]((const void*)&ids[index * 16 + 4]) == id and memory.read[u32]((const void*)&ids[index * 16 + 8]) == instance)
return hit_count
## One instance with id 7: closest hits and shadow rays match the CPU.
func single(device: gpu.Device) -> !:
var pass = try gpu.Compute.begin(device)
defer pass.close()
try pass.build_blas(self.blas, self.triangles())
let instances: gpu.Instance[1] = [gpu.Instance(blas = self.blas, id = 7)]
try pass.build_tlas(self.tlas, instances)
try pass.submit()
try self.upload_rays(0.0)
try self.trace(device, false, 0xff)
let hits = try self.compare(device, 0.0, 0.0, 7, 0, false)
assert(hits > 32 and hits < 64)
try self.trace(device, true, 0xff)
assert((try self.compare(device, 0.0, 0.0, 7, 0, true)) == hits)
## Lift the vertices, refit the Blas and update the Tlas: hits follow.
func refit(device: gpu.Device) -> !:
self.positions = mesh_vertices(0.75)
let bytes = try bytes_of_floats(self.positions)
defer free(bytes) in memory.heap
try self.vertices.upload(0, bytes)
var pass = try gpu.Compute.begin(device)
defer pass.close()
try pass.refit_blas(self.blas, self.triangles())
let instances: gpu.Instance[1] = [gpu.Instance(blas = self.blas, id = 7)]
try pass.update_tlas(self.tlas, instances)
try pass.submit()
try self.trace(device, false, 0xff)
_ = try self.compare(device, 0.0, 0.0, 7, 0, false)
## Two instances of the Blas, the second moved 10 along x with id 2 and
## mask 2: a mask of 2 sees only it, a mask of 1 only the first.
func instances(device: gpu.Device) -> !:
var pass = try gpu.Compute.begin(device)
defer pass.close()
let instances: gpu.Instance[2] = [gpu.Instance(blas = self.blas, id = 1, mask = 1),
gpu.Instance(blas = self.blas, transform = [1.0, 0.0, 0.0, 10.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], id = 2, mask = 2)]
try pass.build_tlas(self.tlas, instances)
try pass.submit()
try self.upload_rays(10.0)
try self.trace(device, false, 2)
assert((try self.compare(device, 10.0, 10.0, 2, 1, false)) > 32)
try self.trace(device, false, 1)
var hits: u8[1024]
try self.hits.read(0, hits)
for index in 0..<(usize)64:
assert(memory.read[f32]((const void*)&hits[index * 16 + 12]) == 0.0)
try self.upload_rays(0.0)
try self.trace(device, false, 3)
assert((try self.compare(device, 0.0, 0.0, 1, 0, false)) > 32)
## Every structure and buffer destroyed after recording and before the pass is
## submitted: the pass keeps them, and the trace still hits.
func lifetimes(device: gpu.Device) -> !:
let positions = mesh_vertices(0.0)
let corners = mesh_indices()
let vertex_bytes = try bytes_of_floats(positions)
defer free(vertex_bytes) in memory.heap
let index_bytes = try bytes_of_words(corners)
defer free(index_bytes) in memory.heap
var program = try gpu.Kernel.create(device, kernels.rays_comp_words, kernels.rays_comp_msl, kernels.rays_comp_group, kernels.rays_comp_bindings)
defer program.destroy()
var vertices = try gpu.Buffer.create(device, vertex_bytes)
var indices = try gpu.Buffer.create(device, index_bytes)
let triangles = gpu.Triangles(vertices = vertices, vertex_count = (u32)vertex_count, indices = indices, triangle_count = (u32)triangle_count)
var blas = try gpu.Blas.create(device, triangles, false)
var tlas = try gpu.Tlas.create(device, 1)
var ray: f32[8] = [2.25, 2.25, 0.0, 100.0, 0.0, 0.0, 1.0, 0.0]
let ray_bytes = try bytes_of_floats(ray)
defer free(ray_bytes) in memory.heap
var rays = try gpu.Buffer.create(device, ray_bytes)
defer rays.destroy()
var hits = try gpu.Buffer.allocate(device, 16)
defer hits.destroy()
var ids = try gpu.Buffer.allocate(device, 16)
defer ids.destroy()
var pass = try gpu.Compute.begin(device)
defer pass.close()
try pass.build_blas(blas, triangles)
let instances: gpu.Instance[1] = [gpu.Instance(blas = blas, id = 3)]
try pass.build_tlas(tlas, instances)
var uniforms: u8[12]
memory.write[u32]((void*)&uniforms[0], 1)
memory.write[u32]((void*)&uniforms[8], 0xff)
let bindings: gpu.Binding[4] = [gpu.Binding(accel = tlas), gpu.Binding(buffer = rays), gpu.Binding(buffer = hits), gpu.Binding(buffer = ids)]
try pass.dispatch(program, bindings, uniforms, 1)
vertices.destroy()
indices.destroy()
blas.destroy()
tlas.destroy()
try pass.submit()
try pass.wait()
var result: u8[16]
try hits.read(0, result)
let expected = closest(positions, corners, [2.25, 2.25, 0.0], [0.0, 0.0, 1.0], 100.0, 0.0)
assert(expected.t > 0.0 and near(memory.read[f32]((const void*)&result[0]), expected.t))
try ids.read(0, result)
assert(memory.read[u32]((const void*)&result[4]) == 3)
# mark: Procedural boxes ======================================================================
let sphere_count: usize = 40
## Sphere `index`'s center and radius: a jittered 8 by 5 grid over the mesh's
## x and y, below it (z from -6 to -2), radii from 0.15 to 0.45.
func sphere(index: usize) -> f32[4]:
let x = (f32)(index % 8) * 0.55 + 0.1 * (f32)(index % 3)
let y = (f32)(index // 8) * 0.85 + 0.07 * (f32)(index % 5)
let z = -6.0 + 0.1 * (f32)((index * 7) % 41)
return [x, y, z, 0.15 + 0.3 * (f32)((index * 13) % 17) / 16.0]
## Ray `index`: from z = -10 straight up, over x in -0.4..4.6 and y in -0.3..4.4.
func sphere_ray(index: usize) -> f32[3]:
return [(f32)(index % 8) * 0.63 - 0.4 + 0.013 * (f32)index, (f32)(index // 8) * 0.59 - 0.3, -10.0]
## The CPU's nearest sphere along +z from `origin` (t < 0: none), and whether
## the ray enters any sphere's box.
func nearest_sphere(origin: f32[3]) -> (f32, i64, bool):
var best: f32 = -1.0
var found: i64 = -1
var boxed = false
for index in 0..<sphere_count:
let s = sphere(index)
let dx = origin[0] - s[0]
let dy = origin[1] - s[1]
if dx >= -s[3] and dx <= s[3] and dy >= -s[3] and dy <= s[3]: boxed = true
let h = s[3] * s[3] - dx * dx - dy * dy
if h < 0.0: continue
let t = (s[2] - origin[2]) - (f32)math32.sqrt(h)
if best < 0.0 or t < best:
best = t
found = (i64)index
return (best, found, boxed)
## A Blas of 40 boxes around spheres, instanced (mask 2) beside the mesh's
## Blas (mask 1, from `scene`): a kernel's sphere tests over the candidates
## commit the nearest sphere; mask 1 sees the mesh through them and reports
## no box; and a refit moves the boxes.
func spheres(device: gpu.Device, scene: Scene*) -> !:
var program = try gpu.Kernel.create(device, kernels.boxes_comp_words, kernels.boxes_comp_msl, kernels.boxes_comp_group, kernels.boxes_comp_bindings)
defer program.destroy()
var centers: f32[160]
var bounds: f32[240]
for index in 0..<sphere_count:
let s = sphere(index)
for k in 0..<(usize)4: centers[index * 4 + k] = s[k]
for k in 0..<(usize)3:
bounds[index * 6 + k] = s[k] - s[3]
bounds[index * 6 + 3 + k] = s[k] + s[3]
let center_bytes = try bytes_of_floats(centers)
defer free(center_bytes) in memory.heap
let bound_bytes = try bytes_of_floats(bounds)
defer free(bound_bytes) in memory.heap
var sphere_buffer = try gpu.Buffer.create(device, center_bytes)
defer sphere_buffer.destroy()
var box_buffer = try gpu.Buffer.create(device, bound_bytes)
defer box_buffer.destroy()
let boxes = gpu.Boxes(buffer = box_buffer, count = (u32)sphere_count)
var blas = try gpu.Blas.create_boxes(device, boxes)
defer blas.destroy()
var tlas = try gpu.Tlas.create(device, 2)
defer tlas.destroy()
# The mesh's Blas at rest again.
scene.positions = mesh_vertices(0.0)
let mesh_bytes = try bytes_of_floats(scene.positions)
defer free(mesh_bytes) in memory.heap
try scene.vertices.upload(0, mesh_bytes)
var build = try gpu.Compute.begin(device)
defer build.close()
try build.build_blas(scene.blas, scene.triangles())
try build.build_boxes(blas, boxes)
let instances: gpu.Instance[2] = [gpu.Instance(blas = blas, id = 5, mask = 2), gpu.Instance(blas = scene.blas, id = 6, mask = 1)]
try build.build_tlas(tlas, instances)
try build.submit()
var rays: f32[512]
for index in 0..<(usize)64:
let origin = sphere_ray(index)
for k in 0..<(usize)3: rays[index * 8 + k] = origin[k]
rays[index * 8 + 3] = 100.0
rays[index * 8 + 6] = 1.0
let ray_bytes = try bytes_of_floats(rays)
defer free(ray_bytes) in memory.heap
var ray_buffer = try gpu.Buffer.create(device, ray_bytes)
defer ray_buffer.destroy()
var hit_buffer = try gpu.Buffer.allocate(device, 64 * 16)
defer hit_buffer.destroy()
let bindings: gpu.Binding[4] = [gpu.Binding(accel = tlas), gpu.Binding(buffer = ray_buffer), gpu.Binding(buffer = sphere_buffer), gpu.Binding(buffer = hit_buffer)]
# Spheres only, then spheres and the mesh, then the mesh only.
let masks: u32[3] = [2, 3, 1]
var sphere_hits: usize = 0
for mask in masks:
var uniforms: u8[8]
memory.write[u32]((void*)&uniforms[0], 64)
memory.write[u32]((void*)&uniforms[4], mask)
var pass = try gpu.Compute.begin(device)
defer pass.close()
try pass.dispatch(program, bindings, uniforms, 1)
try pass.submit()
try pass.wait()
var hits: u8[1024]
try hit_buffer.read(0, hits)
for index in 0..<(usize)64:
let origin = sphere_ray(index)
let (t, found, boxed) = nearest_sphere(origin)
let at = index * 16
let got_t = memory.read[f32]((const void*)&hits[at])
let primitive = memory.read[f32]((const void*)&hits[at + 4])
let candidates = memory.read[f32]((const void*)&hits[at + 8])
let kind = memory.read[f32]((const void*)&hits[at + 12])
let mesh = closest(scene.positions, scene.corners, [origin[0], origin[1], 0.0], [0.0, 0.0, 1.0], 100.0, 0.0)
if mask == 1:
# Boxes are masked out: never candidates; the mesh as from z = 0.
assert(candidates == 0.0)
if mesh.t < 0.0:
assert(kind == 0.0)
else:
assert(kind == 2.0 and near(got_t, mesh.t + 10.0) and primitive == (f32)mesh.primitive)
continue
# Boxes a ray misses may still be reported (NVIDIA's traversal is
# conservative), so only hits are checked for candidates.
_ = boxed
if found >= 0:
assert(candidates >= 1.0)
assert(kind == 1.0 and near(got_t, t) and primitive == (f32)found)
if mask == 2: sphere_hits += 1
elif mask == 2 or mesh.t < 0.0:
assert(kind == 0.0)
else:
assert(kind == 2.0 and near(got_t, mesh.t + 10.0))
assert(sphere_hits > 8 and sphere_hits < 64)
# Refit: every box moves 20 along z, so the same rays from z = -10 meet the
# spheres moved there (their centers in the kernel's buffer too).
for index in 0..<sphere_count:
centers[index * 4 + 2] += 20.0
bounds[index * 6 + 2] += 20.0
bounds[index * 6 + 5] += 20.0
let moved_centers = try bytes_of_floats(centers)
defer free(moved_centers) in memory.heap
let moved_bounds = try bytes_of_floats(bounds)
defer free(moved_bounds) in memory.heap
try sphere_buffer.upload(0, moved_centers)
try box_buffer.upload(0, moved_bounds)
var refit = try gpu.Compute.begin(device)
defer refit.close()
try refit.refit_boxes(blas, boxes)
try refit.update_tlas(tlas, instances)
var uniforms: u8[8]
memory.write[u32]((void*)&uniforms[0], 64)
memory.write[u32]((void*)&uniforms[4], 2)
try refit.dispatch(program, bindings, uniforms, 1)
try refit.submit()
try refit.wait()
var moved: u8[1024]
try hit_buffer.read(0, moved)
for index in 0..<(usize)64:
let (t, found, boxed) = nearest_sphere(sphere_ray(index))
_ = boxed
if found < 0: continue
assert(near(memory.read[f32]((const void*)&moved[index * 16]), t + 20.0))