Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,27 @@ in-place tests' world, rows given back; two models in a shared pose posed again
(tests/render/data/arm.glb, a skinned strip of two materials); `geometry/emitter-model-live` (a model's emitting
section that is not its first, the model a unit under software traversal).

**A triangle's texel density is the hit's to compute (2026-11-22).** Every triangle carried a constant for its mip
choice, half the log of its UV area over its area, made when the scene was built; an instance added an offset for
its scale, the skinning pass and the particle passes wrote it again each pose and frame. Now the kernel takes it
from the triangle where it is hit, from its two edges as it stands in the world and its UVs (`mesh_lod`), and
nothing stores or writes it: the attributes' first `x` word and the record's third `info` word are spare.
What changes in a picture: nothing under a uniform scale but rounding (five scenes offline against the build
before: four the same bytes, one with 21 of 921,600 pixels off by 6 steps at most). An instance scaled otherwise
has its triangles' own density now; its offset was the cube root of its volume's scale.
A fault found on the way: a shadow ray through glass on a scaled instance added the instance's offset twice, so a
pane's colour or transmission map was read some levels off (a pane scaled to 1 / 32: five too high).
The cutout test of a candidate hit reads its triangle's edges now too, where its material has an alpha map.
Tests: the kernel's own `mesh_lod`, its text run on the GPU for 512 triangles (a millimetre to a hundred metres,
UVs a hundredth of the texture to a hundred repeats, ones without area) against 64-bit values; a checker on an
instance scaled 32 times, a square's inside reading its colour (a level five too high reads grey); the checker in
a cutout's alpha on one scaled 256 times; a shadow through a pane with a colour map, the pane scaled 1 / 32 and 32
times (the first fails on the build before). Not held by a test: the density a light sample, a material function's input and a
translucent particle read (a wrong level there changes noise, not a mean).
10 mutations: six of the formula fail the unit test, four of the places that use it fail a render test. One did not at
first: a cutout three levels too high still has each square's middle on its side of the test, so that test's
instance is scaled 256 times.

**A posed skinned model is posed in place under software traversal (2026-11-22).** Under software traversal a mesh
that deforms was written out into the still scene's tree, so every pose rebuilt the scene. Now it is a unit as any
mesh. Its tree has the shape of a tree built once over its model's bind pose, one shape for every placement of the
Expand Down
8 changes: 3 additions & 5 deletions renderer/shaders/gpu_particles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn collide(s_in: PState, dt: f32, flags: f32, d: vec4<f32>) -> PState {
}

// One triangle of a quad (particles.wgsl `put_tri`, opaque particles only).
fn put_tri(t: u32, dyn_first: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n: vec3<f32>, tg: vec3<f32>, lod_k: f32) {
fn put_tri(t: u32, dyn_first: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n: vec3<f32>, tg: vec3<f32>) {
let flags = verts[t * 3u].w;
let now = array<vec4<f32>, 3>(vec4<f32>(a, flags), vec4<f32>(b - a, 0.0), vec4<f32>(c - a, 0.0));
let first = PPS.env.x > 0.5;
Expand All @@ -160,7 +160,6 @@ fn put_tri(t: u32, dyn_first: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n:
attrs[t].n2.y = n.y;
attrs[t].n2.z = n.z;
attrs[t].t = vec4<f32>(tg, 1.0);
attrs[t].x.x = lod_k;
let corners = array<vec3<f32>, 3>(a, b, c);
for (var k = 0u; k < 3u; k++) {
for (var j = 0u; j < 3u; j++) {
Expand Down Expand Up @@ -238,10 +237,9 @@ fn gpu_particles(@builtin(global_invocation_id) gid: vec3<u32>) {
let area = length(uxv);
let n = select(to_cam, uxv / area, area > 1e-12);
let tg = select(vec3<f32>(1.0, 0.0, 0.0), normalize(u), dot(u, u) > 1e-24);
let lod_k = select(0.0, 0.5 * log2(1.0 / (4.0 * area)), area > 1e-12);
let t = sys.a.x + 2u * k;
put_tri(t, sys.b.y, c00, c10, c11, n, tg, lod_k);
put_tri(t + 1u, sys.b.y, c00, c11, c01, n, tg, lod_k);
put_tri(t, sys.b.y, c00, c10, c11, n, tg);
put_tri(t + 1u, sys.b.y, c00, c11, c01, n, tg);
let oi = (sys.a.z + k) * OBJECT_STRIDE;
objects[oi] = vec4<f32>(s.pos, objects[oi].w);
objects[oi + 1u] = vec4<f32>(vec3<f32>(4.0 * half), objects[oi + 1u].w);
Expand Down
14 changes: 6 additions & 8 deletions renderer/shaders/particles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ fn unit(h: u32) -> f32 { return f32(h >> 8u) * (1.0 / 16777216.0); }
fn particle_function(f: u32, i: PIn, p: array<vec4<f32>, 8>) -> POut { return POut(i.pivot, vec3<f32>(0.0), vec3<f32>(0.0), false, vec4<f32>(0.0)); }
// @particle-functions-end

// One triangle of a quad: positions, last frame's as prev_verts, shading normal / tangent / texel density (`lod_k`, as
// scene.rs tri_attr), BLAS input.
// One triangle of a quad: positions, last frame's as prev_verts, shading normal / tangent (as scene.rs tri_attr),
// BLAS input.
// Translucent triangles (`trans` = first translucent triangle) have no motion vectors (the pass draws them over the
// image each frame) and their own BLAS input.
fn put_tri(t: u32, dyn_first: u32, trans: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n: vec3<f32>, tg: vec3<f32>, lod_k: f32, first: bool) {
fn put_tri(t: u32, dyn_first: u32, trans: u32, a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, n: vec3<f32>, tg: vec3<f32>, first: bool) {
let flags = verts[t * 3u].w;
let now = array<vec4<f32>, 3>(vec4<f32>(a, flags), vec4<f32>(b - a, 0.0), vec4<f32>(c - a, 0.0));
for (var k = 0u; k < 3u; k++) {
Expand All @@ -100,7 +100,6 @@ fn put_tri(t: u32, dyn_first: u32, trans: u32, a: vec3<f32>, b: vec3<f32>, c: ve
attrs[t].n2.y = n.y;
attrs[t].n2.z = n.z;
attrs[t].t = vec4<f32>(tg, 1.0);
attrs[t].x.x = lod_k;
let corners = array<vec3<f32>, 3>(a, b, c);
for (var k = 0u; k < 3u; k++) {
for (var j = 0u; j < 3u; j++) {
Expand Down Expand Up @@ -141,17 +140,16 @@ fn particles(@builtin(global_invocation_id) gid: vec3<u32>) {
let c10 = o.center + o.u - o.v;
let c11 = o.center + o.u + o.v;
let c01 = o.center - o.u + o.v;
// Normal u x v, tangent (dP/du) along u, bitangent (image up) along v; uv area 1 over world area 4 |u x v|.
// Normal u x v, tangent (dP/du) along u, bitangent (image up) along v.
let uxv = cross(o.u, o.v);
let area = length(uxv);
let n = select(vec3<f32>(0.0, 0.0, 1.0), uxv / area, area > 1e-12);
let tg = select(vec3<f32>(1.0, 0.0, 0.0), normalize(o.u), dot(o.u, o.u) > 1e-24);
let lod_k = select(0.0, 0.5 * log2(1.0 / (4.0 * area)), area > 1e-12);
let t = s.a.x + 2u * k;
let trans = select(0xffffffffu, s.b.w, s.b.z == 1u);
let first = PPS.env.z > 0.5 || s.c.w > 1.5;
put_tri(t, s.b.y, trans, c00, c10, c11, n, tg, lod_k, first);
put_tri(t + 1u, s.b.y, trans, c00, c11, c01, n, tg, lod_k, first);
put_tri(t, s.b.y, trans, c00, c10, c11, n, tg, first);
put_tri(t + 1u, s.b.y, trans, c00, c11, c01, n, tg, first);
let oi = (s.a.z + k) * OBJECT_STRIDE;
objects[oi] = vec4<f32>(o.center, objects[oi].w);
objects[oi + 1u] = vec4<f32>(2.0 * (abs(o.u) + abs(o.v)), objects[oi + 1u].w);
Expand Down
52 changes: 33 additions & 19 deletions renderer/shaders/pathtrace.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn env_cdf_at(i: u32) -> f32 { return bitcast<f32>(words[i]); }
@group(0) @binding(11) var<storage, read_write> accum: array<Pixel>;
// The dynamic triangles' vertices (kernel layout, from triangle P.group_first[1].x on) as they were the previous frame.
@group(0) @binding(6) var<storage, read> prev_verts: array<vec4<f32>>;
// Instances (scene::GpuInstance): world matrix rows, normal matrix rows, (first triangle, object, LOD offset bits, its rows' first).
// Instances (scene::GpuInstance): world matrix rows, normal matrix rows, (first triangle, object, a spare word, its rows' first).
struct Instance { m0: vec4<f32>, m1: vec4<f32>, m2: vec4<f32>, n0: vec4<f32>, n1: vec4<f32>, n2: vec4<f32>, info: vec4<u32>, p0: vec4<f32>, p1: vec4<f32>, p2: vec4<f32> };
// Bounded participating media (`VolumeMesh`, gpu.rs MediumGpu; F_MEDIA), P.time.w of them: world -> local affine rows,
// (volume function, shape: 0 box / 1 sphere / 2 none while its node is hidden, homogeneous, 1 = it changes with time: `VolumeFunction` `animated`, a
Expand Down Expand Up @@ -290,10 +290,9 @@ fn inst_sign(r: Instance) -> f32 { return select(1.0, -1.0, (bitcast<u32>(r.n1.w
fn inst_normal(r: Instance, n: vec4<f32>) -> vec4<f32> {
return vec4<f32>(normalize(vec3<f32>(dot(r.n0.xyz, n.xyz), dot(r.n1.xyz, n.xyz), dot(r.n2.xyz, n.xyz))), n.w);
}
// A model-space triangle's `x` (texel density, object, ...) as its instance has it: the instance's texture LOD offset
// and object (the record's `info`).
// A model-space triangle's `x` as its instance has it: the instance's object (the record's `info`).
fn inst_x(info: vec4<u32>, x: vec4<f32>) -> vec4<f32> {
return vec4<f32>(x.x + bitcast<f32>(info.z), bitcast<f32>(info.y), x.z, x.w);
return vec4<f32>(x.x, bitcast<f32>(info.y), x.z, x.w);
}
// An instance's rows (scene.rs `Row`): four words a section of its mesh, from the record's info.w on. The material
// with its class bits, the section's first triangle, its first emitter (NO_EMITTER: it does not emit). A triangle of
Expand Down Expand Up @@ -911,8 +910,7 @@ fn glass_at(h: Hit, d: vec3<f32>, t: f32) -> GlassAt {
if (has(F_VCOLOR) && m.uv.w > 0.0) { g.tint *= vcolor(h.tri, h.u, h.v); }
if (has(F_TEX) && (m.tex0.x >= 0 || m.uv.z > 0.5)) {
let uv = mesh_uv(a, m, h.u, h.v);
var lod = mesh_lod(a, m) + log2(max(t * 2.0 * P.cam_pos.w / lod_height(), 1e-7)) - LOD_BIAS;
if (has(F_INST) && h.inst != NO_INST) { lod += bitcast<f32>(instance_at(h.inst).info.z); }
let lod = mesh_lod(e1, e2, a, m) + log2(max(t * 2.0 * P.cam_pos.w / lod_height(), 1e-7)) - LOD_BIAS;
if (m.tex0.x >= 0) { g.tint *= srgb_to_linear(tex(m.tex0.x, uv, lod).rgb); }
if (m.uv.z > 0.5) { tr *= tex(i32(m.uv.z) - 1, uv, lod).r; }
}
Expand Down Expand Up @@ -1083,9 +1081,18 @@ fn mesh_uv(a: TriAttr, m: Material, u: f32, v: f32) -> vec2<f32> {
return (vec2<f32>(a.n0.w, a.n1.w) * w + vec2<f32>(a.n2.w, a.uv.x) * u + a.uv.yz * v) * m.uv.xy + m.uv2.xy;
}

// log2(texture-UV units per metre) of a triangle's mesh UVs (incl. material repeat).
fn mesh_lod(a: TriAttr, m: Material) -> f32 {
return a.x.x + 0.5 * log2(max(abs(m.uv.x * m.uv.y), 1e-12));
// log2(texture-UV units per metre) of a triangle's mesh UVs (incl. material repeat), from the triangle where it is
// hit: its edges e1, e2 as it stands in the world, its UVs: 0.5 log2(uv area / area), 0 where one of the two has no
// size.
fn mesh_lod(e1: vec3<f32>, e2: vec3<f32>, a: TriAttr, m: Material) -> f32 {
let uv0 = vec2<f32>(a.n0.w, a.n1.w);
let d1 = vec2<f32>(a.n2.w, a.uv.x) - uv0;
let d2 = a.uv.yz - uv0;
let uv_area = 0.5 * abs(d1.x * d2.y - d2.x * d1.y);
let area = 0.5 * length(cross(e1, e2));
var k = 0.0;
if (area > 1e-12 && uv_area > 1e-14) { k = 0.5 * log2(uv_area / area); }
return k + 0.5 * log2(max(abs(m.uv.x * m.uv.y), 1e-12));
}

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1190,7 +1197,7 @@ fn mat_in_at(ti: u32, inst: u32, u: f32, v: f32, view: vec3<f32>, lod: f32) -> M
var n = normalize(a.n0.xyz * w + a.n1.xyz * u + a.n2.xyz * v);
let t = normalize(a.t.xyz - n * dot(n, a.t.xyz) + vec3<f32>(1e-9));
let oi = object_of(a);
return MatIn(p, n, ng, t, cross(n, t) * a.t.w, mesh_uv(a, m, u, v), mesh_lod(a, m), view, object_at(oi * OBJECT_STRIDE).xyz, object_at(oi * OBJECT_STRIDE + 1u).xyz,
return MatIn(p, n, ng, t, cross(n, t) * a.t.w, mesh_uv(a, m, u, v), mesh_lod(e1, e2, a, m), view, object_at(oi * OBJECT_STRIDE).xyz, object_at(oi * OBJECT_STRIDE + 1u).xyz,
P.time.x, P.time.y, P.time.z, lod, cur_depth, object_at(oi * OBJECT_STRIDE).w, cur_depth == 0u || mirror_path,
object_at(oi * OBJECT_STRIDE + 2u));
}
Expand Down Expand Up @@ -1225,14 +1232,20 @@ var<private> nee_alone: bool = false;
var<private> lights_only: bool = false;

// Alpha-cutout test for candidate hits on masked triangles (mesh UVs, footprint of a camera ray at distance t). `inst`:
// the model instance a pool triangle belongs to (its scale shifts the texel density, as in `inst_attr`), or NO_INST.
// the model instance a pool triangle belongs to (it places the triangle's edges: its texel density), or NO_INST.
fn alpha_pass(ti: u32, inst: u32, u: f32, v: f32, t: f32) -> bool {
let a = attrs[ti];
let m = material_at(material_of(a, inst));
var alpha = m.base_color.a;
var lod = mesh_lod(a, m) + log2(max(t * 2.0 * P.cam_pos.w / lod_height(), 1e-7)) - LOD_BIAS;
if (has(F_INST) && inst != NO_INST) { lod += bitcast<f32>(instance_at(inst).info.z); }
if (has(F_TEX) && m.tex0.x >= 0) {
var e1 = tri_verts[ti * 3u + 1u].xyz;
var e2 = tri_verts[ti * 3u + 2u].xyz;
if (has(F_INST) && inst != NO_INST) {
let r = instance_at(inst);
e1 = inst_vec(r, e1);
e2 = inst_vec(r, e2);
}
let lod = mesh_lod(e1, e2, a, m) + log2(max(t * 2.0 * P.cam_pos.w / lod_height(), 1e-7)) - LOD_BIAS;
alpha *= tex(m.tex0.x, mesh_uv(a, m, u, v), lod).a;
}
if (has(F_FUNC) && m.tex1.w > 0) {
Expand Down Expand Up @@ -1278,7 +1291,8 @@ fn surface(o: vec3<f32>, d: vec3<f32>, h: Hit, width: f32) -> Surface {
let p = o + d * h.t;

let uv = mesh_uv(a, m, h.u, h.v);
let lod = mesh_lod(a, m) + log2(max(width, 1e-7)) - 0.5 * log2(max(abs(dot(ng, d)), 1e-3)) - LOD_BIAS;
let mesh_k = mesh_lod(e1, e2, a, m);
let lod = mesh_k + log2(max(width, 1e-7)) - 0.5 * log2(max(abs(dot(ng, d)), 1e-3)) - LOD_BIAS;

var s: Surface;
s.p = p;
Expand Down Expand Up @@ -1321,7 +1335,7 @@ fn surface(o: vec3<f32>, d: vec3<f32>, h: Hit, width: f32) -> Surface {
let oi = object_of(a);
var t = normalize(a.t.xyz - n * dot(n, a.t.xyz) + vec3<f32>(1e-9));
let nv = normalize(a.n0.xyz * w + a.n1.xyz * h.u + a.n2.xyz * h.v) * select(-1.0, 1.0, front || dot(a.n0.xyz, ng) > 0.0);
let i = MatIn(p, select(-nv, nv, dot(nv, ng) >= 0.0), ng, t, cross(n, t) * a.t.w, mesh_uv(a, m, h.u, h.v), mesh_lod(a, m), -d,
let i = MatIn(p, select(-nv, nv, dot(nv, ng) >= 0.0), ng, t, cross(n, t) * a.t.w, mesh_uv(a, m, h.u, h.v), mesh_k, -d,
object_at(oi * OBJECT_STRIDE).xyz, object_at(oi * OBJECT_STRIDE + 1u).xyz, P.time.x, P.time.y, P.time.z,
log2(max(width, 1e-7)) - 0.5 * log2(max(abs(dot(ng, d)), 1e-3)), cur_depth, object_at(oi * OBJECT_STRIDE).w, near,
object_at(oi * OBJECT_STRIDE + 2u));
Expand Down Expand Up @@ -1869,9 +1883,9 @@ fn sun_disks(d: vec3<f32>, pdf: f32) -> vec3<f32> {
return c;
}

// What light sampling reads of an emitter's attributes (UVs, material, object, texel density), as shading has them
// at a hit of it: its triangle's, with its instance's object and texture LOD offset if it has one. (Not the normals
// and the tangent, which stay the mesh's: a light sample reads none, and placing them is work a sample.)
// What light sampling reads of an emitter's attributes (UVs, material, object), as shading has them at a hit of it:
// its triangle's, with its instance's object if it has one. (Not the normals and the tangent, which stay the mesh's:
// a light sample reads none, and placing them is work a sample. Its texel density is from the list's own edges.)
fn emitter_attrs(em: EmitterTri) -> TriAttr {
var a = attrs[em.tri];
if (has(F_INST) && em.inst != NO_INST) { a.x = inst_x(instance_at(em.inst).info, a.x); }
Expand Down Expand Up @@ -2146,7 +2160,7 @@ fn emitter_radiance(em: EmitterTri, b1: f32, b2: f32, l: vec3<f32>) -> vec3<f32>
let m = material_at(material_of(a, em.inst));
// 5 cm footprint: a blurred texel is plenty for lighting and keeps NEE cheap.
let pw = obj_power(a);
var le = emission(m, mesh_uv(a, m, b1, b2), mesh_lod(a, m) + log2(0.05)) * pw;
var le = emission(m, mesh_uv(a, m, b1, b2), mesh_lod(em.e1, em.e2, a, m) + log2(0.05)) * pw;
if (has(F_FUNC) && m.tex1.w > 0) {
var mb = mat_base(m);
mb.emissive *= pw;
Expand Down
4 changes: 2 additions & 2 deletions renderer/shaders/records.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// `live_source`, build.rs).

// A triangle's shading attributes (scene.rs GpuTriAttr): normals (w: uv parts), uv parts (w: material index, bits),
// tangent (w: bitangent sign), x: (texel density, object (bits), a particle's area, its emitter entry + 1 (bits; 0: it
// is not light-sampled)). A triangle of a mesh with sections: uv.w its section, x.w its index in it (its instance's
// tangent (w: bitangent sign), x: (a spare word, object (bits), a particle's area, its emitter entry + 1 (bits; 0: it
// is not light-sampled)). (Its texel density is computed where it is hit: pathtrace.wgsl `mesh_lod`.) A triangle of a mesh with sections: uv.w its section, x.w its index in it (its instance's
// row has the material and where the section's emitters start: pathtrace.wgsl `material_of`, `row_word`).
struct TriAttr { n0: vec4<f32>, n1: vec4<f32>, n2: vec4<f32>, uv: vec4<f32>, t: vec4<f32>, x: vec4<f32> };

Expand Down
8 changes: 0 additions & 8 deletions renderer/shaders/skin.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,10 @@ fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_index) l
}
}
}
// Texel density for the mip choice: 0.5 log2(uv area / area).
let area = 0.5 * length(area2);
let uv_area = 0.5 * abs(det);
var lod = 0.0;
if area > 1e-12 && uv_area > 1e-14 {
lod = 0.5 * log2(uv_area / area);
}
at.n0 = vec4<f32>(n0, at.n0.w);
at.n1 = vec4<f32>(n1, at.n1.w);
at.n2 = vec4<f32>(n2, at.n2.w);
at.t = vec4<f32>(t, hand);
at.x.x = lod;
attrs[k] = at;
// (The flag word: the kernel takes a deforming mesh's motion from the previous frame's vertices.)
verts[k * 3u] = vec4<f32>(c0.p, bitcast<f32>(TRI_DYNAMIC));
Expand Down
2 changes: 1 addition & 1 deletion renderer/shaders/translucent.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
// (A particle's triangle is in the world: no instance.)
let m = material_at(material_of(a, NO_INST));
let uv = mesh_uv(a, m, b.x, b.y);
let tl = mesh_lod(a, m) + lod - LOD_BIAS;
let tl = mesh_lod(tri_verts[ti * 3u + 1u].xyz, tri_verts[ti * 3u + 2u].xyz, a, m) + lod - LOD_BIAS;
var r = mat_base(m);
if (has(F_TEX) && m.tex0.x >= 0) {
let c = tex(m.tex0.x, uv, tl);
Expand Down
Loading