dymokomi/luce-3d
Source of release 0.9.6
0.9.6 / src / renderers / fog_scene.lucb
## Every fog drawing of a view ray-marched together (shaders/fog.frag). Each
## FogVolume keeps its own atlas on the GPU; a FogScene keeps a table of the
## drawings' numbers and emission strips with a tile of blue noise, its rows
## sent again only where a look or the drawings change. A pass covers the outline of its
## boxes on screen and marches each pixel's eye ray through them, ending at the
## scene's depth (luce-gpu's scene_depth) a fraction of a step if need be, from
## a blue-noise start: a mesh inside the fog cuts it smoothly at any depth.
## Drawings whose boxes overlap share a pass, up to four (renderers/fog_groups:
## their atlases stacked into one; shaders/fog.frag's group variant), each
## sample summing every drawing holding it, so crossing smokes mix and shadow
## each other; others draw in passes of their own, farther first.
from luce_gpu import gpu
import interop
import memory
from luce_std import math
import errors
from luce_geocore.geocore import Vector3
from cameras.camera import Camera, Projection
from core.material import Color, check_color
from renderers.fog_volume import FogVolume, corner_of
from renderers.fog_groups import FogPass, StackedAtlas, fog_groups, fog_passes, stack_holds, stack_atlases
from luce_geocore.core.growing import Growing
from renderers.fog_ramp import emission_strip, emission_fit
from renderers.fog_noise import blue_noise, noise_side
from renderers.fog_shaders import fog_frag_words, fog_frag_msl, fog_frag_fast_math, fog_group_frag_words, fog_group_frag_msl, fog_group_frag_fast_math
## The table: 256 texels wide; the noise tile's rows, then two rows a drawing.
let table_width: usize = 256
## Texels of a drawing's numbers (shaders/fog.frag's `meet` reads them).
let number_texels: usize = 8
## The most vertices a pass's outline takes: a fan over the hull of four
## boxes' 32 corners.
let outline_vertices: usize = 90
## The most steps a ray takes through all its boxes.
let most_steps: f64 = 4096.0
## A drawing's rows in the table as last sent: which drawing, its look's
## version and where its atlas sat.
struct TableRow:
var serial: u64
var version: u64
var bound: f32[3]
## The fog drawings of one view, ready to draw (main thread, as drawing is).
## What a frame needs (the drawings' order, passes, bound images) lives in
## scratch kept from frame to frame; the overlap groups are found again only
## when the drawings change, and the table's rows are sent only where a
## drawing, its look or its atlas's place changed.
pub struct FogScene:
var table: gpu.Texture?
var table_rows: usize
## The table's contents (rgba32 floats) and what each drawing's rows hold.
var values: Growing[f32]
var rows: Growing[TableRow]
var tables: i64
## The drawings the groups were found for (their serials), and each
## one's group.
var grouped: Growing[u64]
var roots: Growing[usize]
## The groups' stacked atlases, kept while their groups stay the same.
var stacks: Growing[StackedAtlas]
## Passes of the last draw (tests and reports).
var passes_drawn: i64
## Frame scratch.
var drawn: Growing[FogVolume*]
var atlases: Growing[gpu.Texture]
var order: Growing[usize]
var distance: Growing[f64]
var passes: Growing[FogPass]
var open: Growing[usize]
var height: Growing[usize]
var images: Growing[gpu.Texture]
var bounds: Growing[f32]
var shown: Growing[bool]
var outline: Growing[gpu.Vertex]
## Draw `volumes` over what `target` holds, through `camera`: the key light
## colored `key` (intensity folded in) and a sky light of `ambient`. A
## volume's first draw in view sends its atlas; a volume out of view sends
## nothing. A standalone frame (no device) draws nothing.
pub func draw(volumes: const interop.Reference[FogVolume][], camera: interop.Reference[Camera], target: interop.View[gpu.RenderTarget],
key: Color = Color(0.85, 0.85, 0.85), ambient: f64 = 0.35) -> !:
try check_color(key)
if not (ambient >= 0.0 and ambient <= 100.0): error(errors.invalid, "ambient light must be in 0..100")
if volumes.length == 0: return
let native = try target.get()
var device = gpu.device_of(native) catch failure:
if failure.code != gpu.unavailable: error(failure.code, failure.message)
recover gpu.Device()
defer device.destroy()
let size = try native.size()
if not device.is_open() or size.width <= 0.0 or size.height <= 0.0: return
let count = volumes.length
self.drawn.clear()
for at in 0..<count: try self.drawn.push(try volumes[at].get())
let drawn = self.drawn.items[..<count]
try self.regroup(drawn)
let projection = try camera.value().projection(size.width / size.height)
let pixel_format = try native.pixel_format()
# Drawings by distance, farther first, so a nearer pass veils a
# farther one where they do not share a pass.
try self.order.fill(count, 0)
try self.distance.fill(count, 0.0)
for at in 0..<count:
self.order.items[at] = at
self.distance.items[at] = drawn[at].texel_data().box.transform_point(Vector3(0.5, 0.5, 0.5)).subtract(projection.position).length()
for at in 1..<count:
var probe = at
while probe > 0 and self.distance.items[self.order.items[probe - 1]] < self.distance.items[self.order.items[probe]]:
let held = self.order.items[probe]
self.order.items[probe] = self.order.items[probe - 1]
self.order.items[probe - 1] = held
probe -= 1
# The view's place in the frame, in pixels: the shader finds each
# pixel's ray from it.
let placement = try native.placement()
let scale = size.pixel_width / size.width
let view: f64[4] = [placement.x * scale, placement.y * scale, size.pixel_width, size.pixel_height]
# Drawings whose boxes meet march together, up to four a pass, their
# atlases stacked into one; passes draw back to front, each over its
# outline. A pass none of whose boxes reaches the view is skipped
# before its atlases go to the GPU.
self.passes.clear()
try fog_passes(drawn, self.order.view(), self.roots.view(), &self.passes, &self.open, &self.height)
try self.shown.fill(self.passes.count, false)
try self.atlases.fill(count, gpu.Texture())
for at in 0..<self.passes.count:
let pass = &self.passes.items[at]
if not in_view(projection, drawn, pass.members[..<pass.count]): continue
self.shown.items[at] = true
for member in pass.members[..<pass.count]:
let (atlas, _) = try drawn[member].atlas(device)
self.atlases.items[member] = atlas
try self.images.fill(self.passes.count + 1, gpu.Texture())
try self.bounds.fill(count * 3, 0.0)
try self.bind_passes(device, drawn)
try self.fill_table(device, drawn)
let table = self.table else return
try self.outline.fill(outline_vertices, gpu.Vertex(x = 0.0, y = 0.0, z = 0.0, red = 0.0, green = 0.0, blue = 0.0))
let outline = self.outline.items[..<outline_vertices]
var drawn_passes: i64 = 0
for at in 0..<self.passes.count:
if not self.shown.items[at]: continue
let pass = &self.passes.items[at]
let group = pass.members[..<pass.count]
let corners = cover(projection, drawn, group, outline)
if corners == 0: continue
var uniforms = uniforms_of(projection, group, key, ambient, view)
let pipeline = try fog_pipeline(device, pixel_format, pass.count > 1)
try gpu.shade_triangles(native, pipeline, outline[..<corners], u8[]((u8*)&uniforms[0], 128), [self.images.items[at], table], gpu.Filter.linear)
drawn_passes += 1
self.passes_drawn = drawn_passes
## The drawings' overlap groups, found again only when the drawings
## (by serial) changed: a drawing's boxes are fixed, a new placement being
## a new drawing.
func regroup(drawn: FogVolume*[]) -> !:
var same = self.grouped.count == drawn.length
for at in 0..<drawn.length:
if not same: break
same = self.grouped.items[at] == drawn[at].drawing_id()
if same: return
try fog_groups(drawn, &self.roots)
self.grouped.clear()
for volume in drawn: try self.grouped.push(volume.drawing_id())
## The image each shown pass binds into `images`: a lone drawing's own
## atlas, a group's stacked atlases (kept while the group is the same,
## seen from any side, else stacked now; stacks no pass uses are freed);
## and where each drawing's atlas sits in its pass's image into `bounds`
## (3 per drawing). A pass out of view binds nothing; its stack stays.
func bind_passes(device: gpu.Device, drawn: FogVolume*[]) -> !:
for at in 0..<self.stacks.count: self.stacks.items[at].used = false
for at in 0..<self.passes.count:
let pass = self.passes.items[at]
if pass.count == 1:
let index = pass.members[0]
let data = drawn[index].texel_data()
self.images.items[at] = self.atlases.items[index]
self.bounds.items[index * 3] = 0.0
self.bounds.items[index * 3 + 1] = (f32)data.width
self.bounds.items[index * 3 + 2] = (f32)data.height
continue
var found = self.stacks.count
for held in 0..<self.stacks.count:
if not self.stacks.items[held].used and stack_holds(&self.stacks.items[held], &pass, drawn): found = held
if found == self.stacks.count:
if not self.shown.items[at]: continue
try self.stacks.push(try stack_atlases(device, &pass, drawn, self.atlases.view()))
let stack = &self.stacks.items[found]
stack.used = true
self.images.items[at] = stack.texture
for member in 0..<pass.count:
let index = pass.members[member]
self.bounds.items[index * 3] = (f32)stack.starts[stack.slot_of(drawn[index].drawing_id())]
self.bounds.items[index * 3 + 1] = (f32)stack.width
self.bounds.items[index * 3 + 2] = (f32)stack.height
var kept: usize = 0
for at in 0..<self.stacks.count:
if self.stacks.items[at].used:
self.stacks.items[kept] = self.stacks.items[at]
kept += 1
else:
self.stacks.items[at].close()
self.stacks.count = kept
## How many passes the last draw made (one per lone drawing, one per group).
pub func pass_count() -> i64:
return self.passes_drawn
## How many stacked atlases it keeps for its groups.
pub func stack_count() -> i64:
return (i64)self.stacks.count
## How many times the table was made whole (the first draw, other
## drawings); a changed look or place sends only its rows.
pub func table_count() -> i64:
return self.tables
## Bring the table up to date: made whole (the noise tile, then each
## drawing's numbers and emission strip) when the drawings changed; else
## the numbers of a drawing whose look or atlas place changed, and the
## strip of one whose emission changed, are written and sent alone.
func fill_table(device: gpu.Device, drawn: FogVolume*[]) -> !:
let rows = noise_side + 2 * drawn.length
var whole = self.table == none or rows != self.table_rows or self.rows.count != drawn.length
if not whole:
for at in 0..<drawn.length:
if self.rows.items[at].serial != drawn[at].drawing_id(): whole = true
if whole:
try self.values.fill(table_width * rows * 4, 0.0)
var noise: f32[4096]
blue_noise(noise[..<noise_side * noise_side])
for y in 0..<noise_side:
for x in 0..<noise_side:
self.values.items[(y * table_width + x) * 4] = noise[y * noise_side + x]
self.rows.clear()
for at in 0..<drawn.length: try self.rows.push(TableRow(serial = drawn[at].drawing_id(), version = 0, bound = [-1.0, -1.0, -1.0]))
for at in 0..<drawn.length:
let volume = drawn[at]
let row = noise_side + 2 * at
let held = &self.rows.items[at]
let bound = self.bounds.items[at * 3..<at * 3 + 3]
if whole or held.version != volume.look_version() or held.bound[0] != bound[0] or held.bound[1] != bound[1] or held.bound[2] != bound[2]:
write_numbers(self.values.items[row * table_width * 4..<(row * table_width + number_texels) * 4], volume, bound)
held.version = volume.look_version()
held.bound = [bound[0], bound[1], bound[2]]
if not whole: try self.send_rows(row, 1)
if volume.take_strip() or whole:
emission_strip(volume.styled_look(), self.values.items[(row + 1) * table_width * 4..<(row + 2) * table_width * 4])
if not whole: try self.send_rows(row + 1, 1)
if not whole: return
if rows != self.table_rows:
self.release_table()
self.table = try gpu.Texture.create(device, (u32)table_width, (u32)rows, gpu.Format.rgba32_float)
self.table_rows = rows
try self.send_rows(0, rows)
self.tables += 1
## Send `count` table rows from `first`.
func send_rows(first: usize, count: usize) -> !:
let table = self.table else return
let values = self.values.items[first * table_width * 4..<(first + count) * table_width * 4]
try table.upload(u8[]((u8*)values.data, values.length * 4), gpu.Region(0, (u32)first, (u32)table_width, (u32)count))
func release_table():
if let table = self.table:
var owned = table
owned.destroy()
self.table = none
self.table_rows = 0
## Free the table, the stacks and the scratch.
pub func close():
self.release_table()
self.values.close()
self.rows.close()
self.grouped.close()
self.roots.close()
for at in 0..<self.stacks.count: self.stacks.items[at].close()
self.stacks.close()
self.drawn.close()
self.atlases.close()
self.order.close()
self.distance.close()
self.passes.close()
self.open.close()
self.height.close()
self.images.close()
self.bounds.close()
self.shown.close()
self.outline.close()
pub let fog_scene_type: interop.Type[FogScene] = interop.Type[FogScene]("FogScene", FogScene.close, closeable = true, main_thread = true)
## Triangles over the outline of the boxes of `group` on screen (the convex
## hull of their corners; the whole view when one reaches behind the near
## plane), written into `out` (`outline_vertices` at most); how many.
func cover(projection: Projection, drawn: FogVolume*[], group: const usize[], out: gpu.Vertex[]) -> usize:
var xs: f64[32]
var ys: f64[32]
var count: usize = 0
for index in group:
let box = drawn[index].texel_data().box
for corner in 0..<(usize)8:
let point = box.transform_point(corner_of(corner))
if projection.depth(point) <= projection.near:
let whole: f64[8] = [-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0]
return fan(whole[..<8], 4, out)
let at = projection.clip(point)
xs[count] = at[0] / at[3]
ys[count] = at[1] / at[3]
count += 1
# Andrew's monotone chain: sort by x then y, lower then upper hull.
var order: usize[32]
for at in 0..<count: order[at] = at
for at in 1..<count:
var probe = at
while probe > 0 and (xs[order[probe - 1]] > xs[order[probe]] or (xs[order[probe - 1]] == xs[order[probe]] and ys[order[probe - 1]] > ys[order[probe]])):
let held = order[probe]
order[probe] = order[probe - 1]
order[probe - 1] = held
probe -= 1
var hull: usize[65]
var size: usize = 0
for pass in 0..<(usize)2:
let start = size
for step in 0..<count:
let at = order[step] if pass == 0 else order[count - 1 - step]
while size >= start + 2 and turn(xs, ys, hull[size - 2], hull[size - 1], at) <= 0.0: size -= 1
hull[size] = at
size += 1
size -= 1
if size < 3: return 0
var points: f64[130]
for at in 0..<size:
points[at * 2] = xs[hull[at]]
points[at * 2 + 1] = ys[hull[at]]
return fan(points[..<130], size, out)
## Whether any box of `group` may reach the view: false when all their
## corners lie beyond one side of it (or behind the eye).
func in_view(projection: Projection, drawn: FogVolume*[], group: const usize[]) -> bool:
# Bits of the sides each corner lies beyond: left, right, below, above,
# behind the eye; a side every corner lies beyond hides them all.
var all: u32 = 31
for index in group:
let box = drawn[index].texel_data().box
for corner in 0..<(usize)8:
let at = projection.clip(box.transform_point(corner_of(corner)))
var outside: u32 = 0
if at[0] < -at[3]: outside |= 1
if at[0] > at[3]: outside |= 2
if at[1] < -at[3]: outside |= 4
if at[1] > at[3]: outside |= 8
if at[3] <= 0.0: outside |= 16
all &= outside
return all == 0
## The cross product of (b - a) and (c - a): positive when a, b, c turn left.
func turn(xs: const f64[32], ys: const f64[32], a: usize, b: usize, c: usize) -> f64:
return (xs[b] - xs[a]) * (ys[c] - ys[a]) - (ys[b] - ys[a]) * (xs[c] - xs[a])
## The convex polygon of `count` points (x, y pairs in clip space) as a fan of
## triangles into `out`; how many vertices.
func fan(points: const f64[], count: usize, out: gpu.Vertex[]) -> usize:
var written: usize = 0
for at in 1..<count - 1:
for corner in [(usize)0, at, at + 1]:
out[written] = gpu.Vertex(x = (f32)points[corner * 2], y = (f32)points[corner * 2 + 1], z = 0.0, w = 1.0, red = 1.0, green = 1.0, blue = 1.0)
written += 1
return written
## The fragment stage's uniforms (shaders/fog.frag's Params) for a pass
## over `group`, in a view at `view` (x, y, width, height in frame pixels).
func uniforms_of(projection: Projection, group: const usize[], key: Color, ambient: f64, view: f64[4]) -> f32[32]:
let forward = projection.backward.multiply_scalar(-1.0)
# Offsets per unit of x and y: at view depth 1, or across the film.
let reach = projection.ortho if projection.orthographic() else 1.0 / projection.focal
let right = projection.right.multiply_scalar(projection.aspect * reach)
let up = projection.up.multiply_scalar(reach)
let eye = projection.position
var drawings: f32[4] = [0.0, 0.0, 0.0, 0.0]
for at in 0..<group.length: drawings[at] = (f32)group[at]
return [(f32)eye.x, (f32)eye.y, (f32)eye.z, (f32)projection.near,
(f32)forward.x, (f32)forward.y, (f32)forward.z, (f32)projection.far,
(f32)right.x, (f32)right.y, (f32)right.z, (f32)group.length,
(f32)up.x, (f32)up.y, (f32)up.z, (f32)most_steps,
(f32)key.red, (f32)key.green, (f32)key.blue, (f32)ambient,
drawings[0], drawings[1], drawings[2], drawings[3],
(f32)view[0], (f32)view[1], (f32)view[2], (f32)view[3],
1.0 if projection.orthographic() else 0.0, 0.0, 0.0, 0.0]
## Write `volume`'s numbers (shaders/fog.frag's `number`) into `out`, 8 rgba
## texels: the world-to-box rows, its texel counts and atlas columns, its
## atlas's size, second block and world step, its smoke color and shadow
## scale, its density and emission scales and the strip's fit, and where its
## atlas sits in the image its pass binds (`bound`: texels down, the image's
## width and height).
func write_numbers(out: f32[], volume: const FogVolume*, bound: const f32[]):
let data = volume.texel_data()
let look = volume.styled_look()
let inverse = data.box.inverse() catch failure: trap(failure.message)
# Rows of the inverse's linear part with its translation.
let rows: Vector3[3] = [Vector3(inverse.x.x, inverse.y.x, inverse.z.x), Vector3(inverse.x.y, inverse.y.y, inverse.z.y), Vector3(inverse.x.z, inverse.y.z, inverse.z.z)]
let shift: f64[3] = [inverse.translation.x, inverse.translation.y, inverse.translation.z]
for at in 0..<(usize)3:
out[at * 4] = (f32)rows[at].x
out[at * 4 + 1] = (f32)rows[at].y
out[at * 4 + 2] = (f32)rows[at].z
out[at * 4 + 3] = (f32)shift[at]
let (low, fit) = emission_fit(look)
let numbers: f32[20] = [(f32)data.counts[0], (f32)data.counts[1], (f32)data.counts[2], (f32)data.columns,
(f32)data.width, (f32)data.height, (f32)data.block, (f32)(look.step * data.voxel),
(f32)look.smoke.x, (f32)look.smoke.y, (f32)look.smoke.z, (f32)look.shadow,
(f32)look.density, (f32)look.emission, (f32)low, (f32)fit,
bound[0], bound[1], bound[2], 0.0]
for at in 0..<(usize)20: out[12 + at] = numbers[at]
# mark: The shared pipeline and noise
## The fog shaders (one drawing a pass, a group) and their pipelines on one
## device, per target format (slot 0 for presentation surfaces, 1 + the format
## otherwise; groups 9 slots on): made on first use and kept, so a new scene
## compiles nothing. Main thread only, as drawing is.
struct FogPipelines:
var device: gpu.Device
var shader: gpu.Shader
var group_shader: gpu.Shader
var pipelines: (gpu.Pipeline?)[18]
var shared: FogPipelines? = none
## The fog pipeline drawing one drawing a pass, or a `group`, into targets of
## `pixel_format` on `device`: no depth test, the scene's depth read instead
## (where the march ends).
func fog_pipeline(device: gpu.Device, pixel_format: gpu.Format?, group: bool) -> gpu.Pipeline!:
var wanted = device
if let held = shared:
if not held.device.same(&wanted): release_fog_pipelines()
if shared == none:
var shader = try gpu.Shader.create(device, fog_frag_words, fog_frag_msl, fog_frag_fast_math)
errdefer shader.destroy()
var group_shader = try gpu.Shader.create(device, fog_group_frag_words, fog_group_frag_msl, fog_group_frag_fast_math)
errdefer group_shader.destroy()
shared = FogPipelines(device = try device.share(), shader = shader, group_shader = group_shader)
var held = shared else trap("fog pipelines")
var slot: usize = 0
if let format = pixel_format: slot = (usize)(u8)format + 1
if group: slot += 9
if let made = held.pipelines[slot]: return made
let made = try gpu.Pipeline.create(device, held.group_shader if group else held.shader, gpu.Blend.over, pixel_format, scene_depth = true)
held.pipelines[slot] = made
shared = held
return made
## Release the shared fog shader and pipelines (they are made again on the
## next draw): for a clean shutdown, or before closing the device.
pub func release_fog_pipelines():
let held = shared else return
shared = none
for pipeline in held.pipelines:
if let made = pipeline:
var owned = made
owned.destroy()
var shader = held.shader
shader.destroy()
var group_shader = held.group_shader
group_shader.destroy()
var device = held.device
device.destroy()