Skip to content

Material functions and mirrored placements on instances - #160

Merged
yujiaxu-ops merged 5 commits into
mainfrom
instances-functions-mirrored
Nov 18, 2026
Merged

yujiaxu-ops merged 5 commits into
mainfrom
instances-functions-mirrored

Conversation

@yujiaxu-ops

@yujiaxu-ops yujiaxu-ops commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Two more reasons to flatten a mesh at every placement go: a material function, and a placement that mirrors. Plus what #159 left: an emitting instanced object added in place. Next part of ROADMAP § 6 step 3; what the build still flattens at a placement after this is glass.

What changes

  • Material functions. A function's inputs outside full shading (the alpha test of a candidate hit, an emitter's radiance in light sampling: mat_in_at) read the triangle as world space, so a mesh with a function material was flattened. They now place the triangle by its instance, as full shading does.
  • Mirrored placements. A mesh, a model, an instanced object's copy or a rigid model's node placed with a negative scale was flattened (a rigid node's mesh was built with its winding swapped instead). The instance's record now says that it mirrors (INST_MIRRORED), and the kernel's placement takes the sign for the geometric normal's side and the tangent frame's hand. A copy may become mirrored in place.
  • The flattening paths (software traversal without units, a model's glass part, a rigid node's flattened part) swap the second and third vertex under a mirroring placement, with their attributes and vertex colours (Instance::flat, placed_tri).
  • An emitting instanced object added in place gets its emitters there instead of rebuilding the scene.
  • A kernel feature, F_IFUNC: the placement in mat_in_at is compiled in only where an instanced mesh has a material function (below). An edit that would need it (such a mesh added in place, a material of instances' triangles that gets or loses its function) rebuilds.

One picture changes: authored tangents under a mirrored placement

A model's authored tangent kept its sign when its placement mirrored (flattened, or a rigid node), so the tangent frame had the wrong hand there and a normal map read inverted. The hand is now set by the placement everywhere (the instance's sign; the model loader for what stays in world space). Seen on Zero Day: one object at the clip's start shades otherwise (with the instance's sign alone: 0.24 of 255 mean difference at 1 s, falling to 0.02 at 8 s); the scene's dE against its offline reference reads 3.67 for 3.63, the museum's 7.21 for 7.22 (the references were rendered before this change; not re-rendered here). No test asset has authored tangents: the hand is held by unit tests (the flattening of an instance against a triangle built from its world-space vertices and UVs; the loader's sign), not by a picture.

Cost

None measured. Twelve live scenes against main: GPU time the same, pictures the same but for the two above.

On the way: placing a function's inputs by the instance first cost Kabukicho +3.6 % of its frame's GPU time (7.96 -> 8.25 ms, alternated runs), though nothing there is on an instance: its 77,848 emitters, 64,224 with a function, are all in world space, and the kernel's compiler worked the untaken placement out for every light sample. With F_IFUNC the scene's kernel has no such code: 7.96 against main's 7.96.

Tests

  • Functions on an instance, analytic, far from the origin so that mesh space and world space differ: material/function-emitter-instance (light sampling), material/function-alpha-instance (the alpha test).
  • Mirrored instances, analytic: material/function-mirrored (the tangent frame's hand), material/function-emitter-mirrored (the geometric normal in light sampling), material/vertex-colors-mirrored.
  • geometry/emitter-instanced-added-live, and in place against a build: an emitting instanced object added, changed and removed; a copy mirrored.
  • The flattening of an instance against the build's flattening of the same placement, mirrored too (vertices, normals, UVs, the tangent's hand, vertex colours); a mirrored model's build (the same meshes, instances that say they mirror, the flattened part facing as its normals do); the loader's hand.
  • Mutants, each failing its test on the path meant: mat_in_at without the instance at either caller; the feature never set; the kernel's hand, the CPU's hand, the colours or the vertices not swapped, no flag in the record, mat_in_at's normal without the sign; no emitters or a hidden copy's power kept at the add; a mirrored node's flattened part not swapped, an added mesh's material not recorded, the loader's hand kept.
  • 175 unit tests, 249 render tests natively. Under software traversal the branch and main fail the same 24 tests with the same values.
  • Vulkan (RTX 5090): every new test passes natively; 175 unit tests; under software traversal both builds fail the same 25. Against main only new tests differ beyond single pixels (two listings were one value's largest difference: another noise pattern where main flattens a mirrored panel with its vertices swapped, and the compiler's rounding of a changed kernel on a scene without instances; both builds read the same there).

Not covered

  • surface's front under a mirroring instance has no reader yet (glass and one-sided cards are never instances): right by the same sign, not tested until glass is on instances.
  • An emitting instanced object added in place is sampled by power share where the light tree has no place for it, as a driven mesh added in place is.
  • A rigid node's flattened part with an authored tangent: its hand is set by placed_tri, with no test asset that has one.

Left

Glass (shadow rays pass it): the last thing flattened at a placement.

🤖 Generated with Claude Code

yujiaxu-ops and others added 3 commits November 18, 2026 13:51
A function's inputs outside full shading (the alpha test of a candidate hit,
an emitter's radiance in light sampling) read the triangle as world space, so
a mesh with a function material was flattened at every placement. `mat_in_at`
takes the instance and places the triangle as `surface` does; such a mesh is
an instance like any other, its emitters in the light list.

Tests: a function's emission and a function's alpha on a mesh of its own, far
from the origin (analytic).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A mesh, a model, an instanced object's copy or a rigid model's node placed
with a negative scale was flattened (a rigid node's mesh was built with its
winding swapped instead). The instance's record now says that it mirrors
(`INST_MIRRORED`), and the kernel's placement takes the sign: the geometric
normal's side and the tangent frame's hand. The flattening path (software
traversal without units, a model's glass part) swaps the second and third
vertex of a mirroring instance's triangles, attributes and vertex colours
alike. An instanced object's copy may become mirrored in place.

Tests: the tangent frame's hand and the geometric normal through a function,
vertex colours, a mirrored model's build, a copy mirrored in place; the
flattening of an instance against the build's flattening, mirrored too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
It rebuilt the scene. Each copy's emitters now come into the light list where
its record is made (a run a copy at the list's end; a hidden copy's at no
weight), as an instance's do.

Tests: added, changed and removed in place against a build, with and without
a light tree; an analytic live test.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread renderer/src/scene.rs
let (mat, class, emits) = resolve(t.material.and_then(|i| doc.materials().nth(i)))?;
classed[if emits { 2 } else { class as usize }].push((k, mat));
}
// Triangles flattened per placement (glass, emissive, functions): each model skinned alone.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mirrored rigid rest triangles keep winding

Medium Severity

A rigid node's flattened rest (glass, coincident cutouts) is still transformed by the mirroring matrix without swapping the second and third vertex. Local triangles are no longer pre-flipped, so ng points inward and front is inverted. Static models already go through Instance::flat; pose updates re-transform rest_local the same way.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5d126d4. Configure here.

…used

`mat_in_at`'s placement by the instance cost a scene that has no material
function on an instanced mesh: the kernel's compiler works it out for every
call, taken or not (Kabukicho's 64,224 emitters with a function, none on an
instance: frame GPU +2.7 %). A kernel feature, F_IFUNC, set by the build
when an instanced mesh has a material function; an edit that would need it
(such a mesh added in place, a material of instances' triangles that gets or
loses its function) rebuilds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

Reviewed by Cursor Bugbot for commit d8673aa. Configure here.

Comment thread renderer/src/tables.rs
// whether there is one, `gpu::F_IFUNC`, which the build decides.)
if (gm.tex1[3] != 0) != (self.materials[mi as usize].tex1[3] != 0) && self.fixed_mats.contains(&mi) {
return Ok(None);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instance function edits skip rebuild

Medium Severity

A material function added after an instance is placed in place can skip the rebuild that turns on F_IFUNC. mat_in_at now places the triangle only when that bit is set, and the new guard only rebuilds if the material is already in fixed_mats from the original build. An own-BLAS or instanced mesh added in place with a shared non-function material is never recorded there, so attaching a function later leaves the kernel reading model space for alpha and light sampling.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d8673aa. Configure here.

…terial is known

Review findings, and one more of the same kind:

- A rigid model's moving node no longer has its mesh pre-swapped, so what the
  build flattens of it (glass, a cutout on an emitter) faced inward under a
  mirroring node. Placed by `placed_tri` now, at the build and at a pose: the
  second and third vertex change places, an authored tangent's frame changes
  hand.
- A mesh added in place puts its material among those of instances' triangles
  (`Tables::fixed_mats`): a function it gets later rebuilds (F_IFUNC).
- The model loader's world-space triangles: an authored tangent's frame
  changes hand under a mirrored placement (it kept its sign).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@yujiaxu-ops
yujiaxu-ops merged commit c03fdef into main Nov 18, 2026
9 checks passed
@yujiaxu-ops
yujiaxu-ops deleted the instances-functions-mirrored branch November 18, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant