Wave 297/509: Section cap: only closed bodies take part in the crossing count (and 1 more)

2 commits, squashed; the originals stay reachable from tag archive/merge-main-develop.

- 33acb6dc3 jyviko: Section cap by crossing count: a body touching the cut body no longer shows through
- c33bae870 jyviko: Section cap: only closed bodies take part in the crossing count

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
jyviko
2026-10-28 15:15:13 -04:00
parent 7314e9440c
commit 74cb9ca753
11 changed files with 332 additions and 91 deletions
+32 -2
View File
@@ -213,13 +213,43 @@ fn fs_solid(i: TOut, @builtin(front_facing) front: bool) -> @location(0) vec4<f3
return c;
}
/// A section's crossing count (`Cut::count` in `view.rs`): nothing but the cut itself, so the
/// stencil counts only the faces behind the section plane. The colour is not written.
@fragment
fn fs_clip(i: TOut) -> @location(0) vec4<f32> {
if (clipped(i.wp)) { discard; }
return vec4<f32>(0.0);
}
@fragment
fn fs_clip_flat(i: FOut) -> @location(0) vec4<f32> {
if (clipped(i.rp + u.eye.xyz)) { discard; }
return vec4<f32>(0.0);
}
/// Faces where the section plane is outside every closed body (`Cut::rest`): a back face seen
/// there is the inside of an open body (a surface body, a solid with faces left out), lit as it
/// is without a section rather than hatched, since there is no cut face to show.
@fragment
fn fs_solid_open(i: TOut, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> {
let c = solid(i, true);
return c;
}
@fragment
fn fs_solid_open_flat(i: FOut, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> {
let t = flat_fragment(i, front);
let c = solid(t, true);
return c;
}
struct CutOut {
@location(0) c: vec4<f32>,
@builtin(frag_depth) depth: f32,
};
/// The section cap (the inside seen through the cut, drawn on the far walls' back faces where
/// they are the visible surface) again, at the depth of the section plane rather than of the
/// The section cap (where the section plane lies inside a body, drawn on the far walls' back
/// faces) again, at the depth of the section plane rather than of the
/// far wall, so the cut face hides what lies inside the body instead of showing it like a
/// pane of glass.
@fragment
+17 -4
View File
@@ -73,8 +73,9 @@ pub struct ModelBody {
/// [`kernel::measure`] of `body` (with the body it was measured on), worked out once and
/// shared by clones: inspecting a design again measures only the bodies that changed.
measured: Arc<OnceLock<(Body, kernel::Result<kernel::BodyMeasure>)>>,
/// [`Body::is_surface`] of `body` (with the body it was measured on), worked out once and
/// shared by clones.
/// Whether `body` is open (with the body it was measured on): a B-rep one a surface body
/// ([`Body::is_surface`]), a mesh one with a side only one triangle has
/// ([`Body::is_closed_mesh`]); worked out once and shared by clones.
surface: Arc<OnceLock<(Body, bool)>>,
/// [`ModelBody::iso_lines`] worked out so far, shared by clones.
iso: Arc<std::sync::Mutex<IsoLines>>,
@@ -110,10 +111,22 @@ impl ModelBody {
/// A surface body: some shell does not close ([`Body::is_surface`]), worked out once.
pub fn is_surface(&self) -> bool {
match self.surface.get_or_init(|| (self.body.clone(), self.body.is_surface())) {
!self.body.is_mesh() && self.is_open()
}
/// The body encloses a volume: a solid whose shells all close ([`ModelBody::is_surface`]),
/// or a mesh body every side of which two triangles share ([`Body::is_closed_mesh`]).
/// Worked out once.
pub fn is_closed(&self) -> bool {
!self.is_open()
}
fn is_open(&self) -> bool {
let open = |b: &Body| if b.is_mesh() { !b.is_closed_mesh() } else { b.is_surface() };
match self.surface.get_or_init(|| (self.body.clone(), open(&self.body))) {
(measured, s) if measured.same(&self.body) => *s,
// The body was replaced in this cell's holder since: look again.
_ => self.body.is_surface(),
_ => open(&self.body),
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ fn scene(s: &Session) -> Option<(Scene, Camera)> {
let face_colors =
s.doc.face_colors(body).into_iter().map(|(face, look)| (face as u32, Rgb(look.color[0], look.color[1], look.color[2]))).collect();
let opacity = s.doc.model_body_opacity(body) as f32;
scene.meshes.push(SceneMesh { mesh, color, face_colors, opacity });
scene.meshes.push(SceneMesh { mesh, color, face_colors, opacity, closed: body.is_closed() });
}
if bounds.is_empty() {
return None;
+1 -1
View File
@@ -676,7 +676,7 @@ fn build_scene(
// A see-through appearance is drawn see-through, as the viewport draws it; a selected
// body is drawn solid, so the selection colour reads.
let opacity = if selected { 1.0 } else { s.doc.model_body_opacity(body) as f32 };
sc.meshes.push(SceneMesh { mesh: Arc::clone(&mesh), color, face_colors, opacity });
sc.meshes.push(SceneMesh { mesh: Arc::clone(&mesh), color, face_colors, opacity, closed: body.is_closed() });
if cosmetic_threads
&& thread_budget.available()
&& s.doc.model_body_opacity(body) >= 0.999
+5 -3
View File
@@ -424,8 +424,10 @@ pub fn scene_geometry(scene: &Scene) -> GpuScene {
let n = mesh.normals.get(*k as usize).copied().filter(|n| n.is_finite() && n.len() > 0.5).unwrap_or(flat);
if m.glass() {
sc.glass_tri(p.to_f32(), n.to_f32(), col);
} else {
} else if m.closed {
sc.tri(p.to_f32(), n.to_f32(), col);
} else {
sc.open_tri(p.to_f32(), n.to_f32(), col);
}
}
}
@@ -580,7 +582,7 @@ mod tests {
/// headless picture of a translucent target shows the tool inside it.
#[test]
fn a_see_through_body_is_glass_at_its_own_alpha_and_a_solid_one_is_not() {
let mesh = |opacity: f32| SceneMesh { mesh: sheet(), color: Rgb(200, 30, 30), face_colors: Vec::new(), opacity };
let mesh = |opacity: f32| SceneMesh { mesh: sheet(), color: Rgb(200, 30, 30), face_colors: Vec::new(), opacity, closed: true };
let geometry = |opacity: f32| scene_geometry(&Scene { meshes: vec![mesh(opacity)], ..Default::default() });
let solid = geometry(1.0);
@@ -604,7 +606,7 @@ mod tests {
/// painting one face does not make the body solid again.
#[test]
fn a_painted_face_keeps_its_bodys_opacity() {
let mesh = SceneMesh { mesh: sheet(), color: Rgb(200, 30, 30), face_colors: vec![(0, Rgb(0, 255, 0))], opacity: 0.5 };
let mesh = SceneMesh { mesh: sheet(), color: Rgb(200, 30, 30), face_colors: vec![(0, Rgb(0, 255, 0))], opacity: 0.5, closed: true };
let sc = scene_geometry(&Scene { meshes: vec![mesh], ..Default::default() });
assert_eq!(sc.tris.len(), 0);
assert_eq!(color(&sc, &sc.glass), Some([0, 255, 0, 128]), "the face's own colour at the body's alpha");
+197 -74
View File
@@ -163,6 +163,10 @@ pub struct PlannedBody {
/// How the whole body is drawn: opaque with the model, or faded or see-through blended
/// order-independently (only on a renderer that does that: [`GpuTarget::oit`]).
pub class: TriClass,
/// The body encloses a volume (a solid, a closed mesh). Only closed bodies take part in a
/// section's crossing count ([`Cut`]): an open one has no inside, so it neither gets a cap
/// nor takes one from the body it lies in.
pub closed: bool,
}
/// What the viewport's last frame drew, counted as it was drawn (the performance overlay, #2548).
@@ -403,6 +407,8 @@ pub struct InstancedMesh {
/// in the part's frame): a placement small on screen is drawn from the coarsest whose error
/// stays under [`LOD_PX`] there ([`Batches::cull`]).
pub levels: Vec<(Arc<SharedTris>, f32)>,
/// The triangles enclose a volume (see [`PlannedBody::closed`]).
pub closed: bool,
}
impl InstancedMesh {
@@ -447,6 +453,9 @@ pub struct GpuScene {
pub palette: Arc<Palette>,
/// Opaque triangles. A zero normal draws the colour flat (unlit).
pub tris: Vec<u8>,
/// Opaque triangles of open bodies, drawn as `tris` but left out of a section's crossing
/// count (see [`PlannedBody::closed`]).
pub open_tris: Vec<u8>,
/// Opaque product PBR vertices; linear base color and explicit optical parameters.
pub pbr: Vec<u8>,
/// Translucent triangles (alpha from the colour), drawn after the opaque ones without
@@ -538,6 +547,7 @@ impl GpuScene {
}
}
out.tris.append(&mut self.tris);
out.tris.append(&mut self.open_tris);
out.trans.append(&mut self.trans);
out.lines.append(&mut self.lines);
out.hidden.append(&mut self.hidden);
@@ -603,6 +613,9 @@ pub enum TriClass {
pub struct BlockBytes {
pub vertices: Arc<Vec<u8>>,
pub opaque: Arc<Vec<std::ops::Range<u32>>>,
/// The opaque runs of closed bodies: what a section's crossing count draws (see
/// [`PlannedBody::closed`]).
pub closed: Arc<Vec<std::ops::Range<u32>>>,
pub glass: Arc<Vec<std::ops::Range<u32>>>,
pub faded: Arc<Vec<std::ops::Range<u32>>>,
pub lines: Arc<Vec<u8>>,
@@ -634,6 +647,9 @@ pub fn extend_runs(runs: &mut Vec<std::ops::Range<u32>>, at: u32) {
pub struct BlockBuilder {
vertices: Vec<u8>,
opaque: Vec<std::ops::Range<u32>>,
closed: Vec<std::ops::Range<u32>>,
/// The triangles being added belong to an open body ([`BlockBuilder::open_body`]).
open: bool,
glass: Vec<std::ops::Range<u32>>,
faded: Vec<std::ops::Range<u32>>,
lines: Vec<u8>,
@@ -664,6 +680,16 @@ impl BlockBuilder {
TriClass::Faded => &mut self.faded,
};
extend_runs(runs, at);
if class == TriClass::Opaque && !self.open {
extend_runs(&mut self.closed, at);
}
}
/// Whether the triangles added from now on belong to an open body (a surface body, a mesh
/// that does not close): those are left out of a section's crossing count. Closed until
/// said otherwise.
pub fn open_body(&mut self, open: bool) {
self.open = open;
}
/// The runs of `class` that fall within `vertices` (a body's share of the block).
@@ -705,6 +731,7 @@ impl BlockBuilder {
Arc::new(BlockBytes {
vertices: Arc::new(self.vertices),
opaque: Arc::new(self.opaque),
closed: Arc::new(self.closed),
glass: Arc::new(self.glass),
faded: Arc::new(self.faded),
lines: Arc::new(self.lines),
@@ -727,6 +754,13 @@ impl GpuScene {
}
self.tris.extend_from_slice(&c.0.to_le_bytes());
}
/// A vertex of an open body's opaque triangle (see [`GpuScene::open_tris`]).
pub fn open_tri(&mut self, p: [f32; 3], n: [f32; 3], c: PaletteIndex) {
for v in p.iter().chain(&n) {
self.open_tris.extend_from_slice(&v.to_le_bytes());
}
self.open_tris.extend_from_slice(&c.0.to_le_bytes());
}
/// A vertex of the ground-plane shadow (see [`GpuScene::shadow`]).
pub fn shadow_tri(&mut self, p: [f32; 3], c: PaletteIndex) {
for v in p.iter().chain(&[0.0; 3]) {
@@ -1003,8 +1037,10 @@ struct Batch {
struct PlannedDraw {
mesh: Arc<crate::GpuMesh>,
colors: wgpu::Buffer,
/// Its bodies' class: a chunk holds bodies of one class.
/// Its bodies' class and whether they are closed: a chunk holds bodies of one class, all
/// closed or all open.
class: TriClass,
closed: bool,
/// The address of each plan in the chunk, in `mesh.parts` order: a whole-body highlight
/// finds the evaluated triangles of the body it lights.
plans: Vec<usize>,
@@ -1141,6 +1177,8 @@ struct LevelDraw {
/// while they come back), and the placements in view, each drawn from its level.
struct InstancedDraw {
look: InstanceLook,
/// Its triangles enclose a volume ([`InstancedMesh::closed`]).
closed: bool,
/// The full triangles first, then coarser ones.
levels: Vec<LevelDraw>,
bounds: [[f32; 3]; 2],
@@ -1206,6 +1244,9 @@ struct BlockGpu {
bytes: Arc<BlockBytes>,
vertices: Option<wgpu::Buffer>,
opaque: Option<ClassGpu>,
/// The opaque runs of closed bodies ([`BlockBytes::closed`]): `opaque`'s own when they are
/// all of them.
closed: Option<ClassGpu>,
glass: Option<ClassGpu>,
faded: Option<ClassGpu>,
lines: Option<Batch>,
@@ -1272,6 +1313,12 @@ impl BlockGpu {
class_gpu(device, fits, &bytes.vertices, bytes.runs(class), old_pair, same_vertices, uploaded)
};
let (opaque, glass, faded) = (class(TriClass::Opaque), class(TriClass::Glass), class(TriClass::Faded));
let closed = if bytes.closed == bytes.opaque {
opaque.clone()
} else {
let old_pair = old.as_ref().and_then(|o| o.closed.as_ref().map(|c| (c, &o.bytes.closed)));
class_gpu(device, fits, &bytes.vertices, &bytes.closed, old_pair, same_vertices, uploaded)
};
let keep = |old: Option<&BlockGpu>, pick: fn(&BlockBytes) -> &Arc<Vec<u8>>, old_lines: fn(&BlockGpu) -> Option<&Batch>| {
old.filter(|o| Arc::ptr_eq(pick(&o.bytes), pick(&bytes))).and_then(old_lines).cloned()
};
@@ -1281,7 +1328,7 @@ impl BlockGpu {
};
let lines = keep(old.as_ref(), |b| &b.lines, |b| b.lines.as_ref()).or_else(|| fresh(&bytes.lines, "sc_block_lines"));
let hidden = keep(old.as_ref(), |b| &b.hidden, |b| b.hidden.as_ref()).or_else(|| fresh(&bytes.hidden, "sc_block_hidden"));
BlockGpu { bytes, vertices, opaque, glass, faded, lines, hidden }
BlockGpu { bytes, vertices, opaque, closed, glass, faded, lines, hidden }
}
fn class(&self, class: TriClass) -> Option<&ClassGpu> {
@@ -1294,7 +1341,16 @@ impl BlockGpu {
/// Draw `class`'s triangles with the pipeline already set (`tally`: what is counted).
fn draw(&self, pass: &mut wgpu::RenderPass<'_>, class: TriClass, tally: &Tally) {
match self.class(class) {
self.draw_runs(pass, self.class(class), tally);
}
/// Draw the closed bodies' opaque triangles ([`BlockBytes::closed`]), as [`BlockGpu::draw`].
fn draw_closed(&self, pass: &mut wgpu::RenderPass<'_>, tally: &Tally) {
self.draw_runs(pass, self.closed.as_ref(), tally);
}
fn draw_runs(&self, pass: &mut wgpu::RenderPass<'_>, runs: Option<&ClassGpu>, tally: &Tally) {
match runs {
Some(ClassGpu::Runs(runs)) => {
let Some(vertices) = &self.vertices else { return };
pass.set_vertex_buffer(0, vertices.slice(..));
@@ -1485,6 +1541,8 @@ struct Batches {
block_order: Vec<u64>,
key: Option<u64>,
tris: Option<Batch>,
/// [`GpuScene::open_tris`].
open_tris: Option<Batch>,
pbr: Option<Batch>,
trans: Option<Batch>,
shadow: Option<Batch>,
@@ -1576,6 +1634,7 @@ impl Batches {
upload(device, label, data, stride)
};
self.tris = own("sc_tris", &sc.tris, TRI_SIZE);
self.open_tris = own("sc_open_tris", &sc.open_tris, TRI_SIZE);
self.pbr = own("sc_pbr", &sc.pbr, PBR_SIZE);
self.trans = own("sc_trans", &sc.trans, TRI_SIZE);
self.shadow = own("sc_shadow", &sc.shadow, TRI_SIZE);
@@ -1658,7 +1717,7 @@ impl Batches {
self.instanced_tris.insert(key, gpu);
}
if !levels.is_empty() {
out.push(InstancedDraw { look: m.look, levels, bounds: m.tris.bounds, placements, shown: None });
out.push(InstancedDraw { look: m.look, closed: m.closed, levels, bounds: m.tris.bounds, placements, shown: None });
}
}
out
@@ -1741,7 +1800,9 @@ impl Batches {
for b in bodies {
let n = b.plan.vertex_count();
match chunks.last_mut() {
Some(c) if open && filled + n <= crate::MAX_BATCH && c.first().is_some_and(|f| f.class == b.class) => c.push(b),
Some(c) if open && filled + n <= crate::MAX_BATCH && c.first().is_some_and(|f| (f.class, f.closed) == (b.class, b.closed)) => {
c.push(b)
}
_ => {
chunks.push(vec![b]);
filled = 0;
@@ -1785,26 +1846,42 @@ impl Batches {
}
self.uploads.planned += bytes.len() as u64;
if let Some((colors, _)) = upload(device, "sc_planned_colors", &bytes, 4).and_then(|c| c.parts.into_iter().next()) {
let class = chunk.first().map_or(TriClass::Opaque, |b| b.class);
out.push(PlannedDraw { mesh, colors, class, plans: key(chunk) });
let (class, closed) = chunk.first().map_or((TriClass::Opaque, true), |b| (b.class, b.closed));
out.push(PlannedDraw { mesh, colors, class, closed, plans: key(chunk) });
}
}
out
}
}
/// The section's pipelines (see `Renderer::tri_cut`).
/// Where a section's crossing count starts (`Cut::count`): half the stencil's range, so a count
/// that goes below it (more faces entered than left: an open sheet's front) stays apart from one
/// above it.
const STENCIL_BASE: u32 = 128;
/// The section's pipelines (see `Renderer::tri_cut`), drawn in this order. The cap shows where
/// the section plane lies inside a body: where the faces behind the plane, on the view ray
/// through the pixel, are left by the ray more often than entered. The stencil counts them, so
/// no depth is compared where bodies touch: a cut body's far wall and the face of the body it
/// rests on lie at one depth, and settling that by depth let the other body show through the
/// cap in specks, stripes and triangles. The display meshes are watertight, so the count is
/// exact for closed bodies; an open one (a surface body, a solid with faces left out) gets no
/// cap.
struct Cut {
/// Draws the faces and marks the stencil by their true depth: 1 where a back face (the cap)
/// is the visible surface. Unpushed: `FACE_BIAS`'s slope part would let a front face win a
/// band along its edge with a back face seen through the cut, and the cap would skip it (#6328).
mark: wgpu::RenderPipeline,
/// The faces' depth again, pushed back by `FACE_BIAS` as unsectioned faces are (after
/// `Renderer::section_reset`), so their edges draw over them (#1434). No colour, no stencil.
push: wgpu::RenderPipeline,
/// The cap again, only where marked, at the section plane's depth: depth only, as the mark
/// drew the cap's colour (#6728).
/// Counts the crossings in the stencil from [`STENCIL_BASE`]: a back face behind the plane
/// adds one, a front face takes one away. No colour, no depth. Above the base, the plane is
/// inside a body; at or below it, outside (an open sheet seen from its front counts below).
count: wgpu::RenderPipeline,
/// Where the count is above the base, the nearest back face, by its true depth, in its body's cut
/// colour (the body the plane lies inside: the ray leaves it there).
fill: wgpu::RenderPipeline,
/// The cap again, only there, at the section plane's depth (pushed as faces are), so what
/// lies on the cut face draws over it: depth only, as `fill` drew its colour (#6728).
cap: wgpu::RenderPipeline,
/// Elsewhere, the faces as they are drawn without a section, pushed back by
/// `FACE_BIAS` so their edges draw over them (#1434); a back face there is an open body's
/// inside, lit, not hatched.
rest: wgpu::RenderPipeline,
}
/// The viewport's pipelines and the scenes uploaded to one device, for one kind of target.
@@ -1835,8 +1912,8 @@ pub struct Renderer {
/// The tessellation compute pipeline, where the device runs compute shaders.
tess: Option<crate::TessGpu>,
/// While a section is shown (when the depth buffer has a stencil), in place of `tri`: the
/// faces marking where the cap shows, their depth pushed back, then the cap again at the
/// section plane's depth, so it hides the inside ([`Cut`]).
/// faces behind the plane counted where the cap shows, the cap in its colour and at the
/// section plane's depth, so it hides the inside, then the other faces ([`Cut`]).
tri_cut: Option<Cut>,
/// Highlight triangles over the model (depth test ≤, no depth write).
tri_hl: wgpu::RenderPipeline,
@@ -1924,9 +2001,9 @@ pub struct Renderer {
/// Reversed depth drawn into the window's pass: egui clears its depth to 1, which is the near
/// plane then, so the viewport first sets its rectangle to the far plane (0).
depth_reset: Option<wgpu::RenderPipeline>,
/// The scene target's depth back to the far plane between the section's marking and push
/// passes ([`Cut`]); stencil and colour untouched.
section_reset: Option<wgpu::RenderPipeline>,
/// The scene target's stencil set to [`STENCIL_BASE`] before a section's crossing count
/// ([`Cut`]); depth and colour untouched.
section_stencil: Option<wgpu::RenderPipeline>,
}
/// A triangle over the whole viewport at the far plane's depth (`FAR`), writing no colour:
@@ -2456,36 +2533,40 @@ impl Renderer {
let solid = |label: &str, fs: &str, cull: Option<wgpu::Face>, ds: Option<wgpu::DepthStencilState>, source: TriSource| {
solid_writes(label, fs, cull, ds, source, true)
};
// While a section is shown: the stencil marking and the cap (see `Renderer::tri_cut`).
// While a section is shown: the crossing count and the cap (see `Cut`), drawn with the
// stencil reference `STENCIL_BASE`.
let cut = |source: TriSource| {
t.depth.filter(|f| f.has_stencil_aspect()).map(|format| {
// The visible surface, by its true depth, marks the stencil: 1 where it is a back face (the cap).
use wgpu::StencilOperation::{Keep, Replace, Zero};
let face = |pass_op| wgpu::StencilFaceState { compare: wgpu::CompareFunction::Always, fail_op: Keep, depth_fail_op: Keep, pass_op };
let mark = wgpu::DepthStencilState {
use wgpu::CompareFunction::{Always, GreaterEqual, Less};
use wgpu::StencilOperation::{DecrementWrap, IncrementWrap, Keep};
let face = |compare, pass_op| wgpu::StencilFaceState { compare, fail_op: Keep, depth_fail_op: Keep, pass_op };
let stencil = |front, back, write_mask| wgpu::StencilState { front, back, read_mask: 0xff, write_mask };
let count = wgpu::DepthStencilState {
format,
depth_write_enabled: Some(false),
depth_compare: Some(Always),
stencil: stencil(face(Always, DecrementWrap), face(Always, IncrementWrap), 0xff),
bias: Default::default(),
};
// The reference (`STENCIL_BASE`) below the count: the plane is inside a body.
let inside = stencil(face(Less, Keep), face(Less, Keep), 0);
let fill = wgpu::DepthStencilState {
depth_write_enabled: Some(true),
depth_compare: Some(depth_compare(reversed, wgpu::CompareFunction::Less)),
stencil: wgpu::StencilState { front: face(Zero), back: face(Replace), read_mask: 0xff, write_mask: 0xff },
bias: Default::default(),
};
// Then the cap's back faces again, only there, at the section plane's depth.
let only = wgpu::StencilFaceState { compare: wgpu::CompareFunction::Equal, ..face(Keep) };
let cap = wgpu::DepthStencilState {
format,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Always),
stencil: wgpu::StencilState { front: only, back: only, read_mask: 0xff, write_mask: 0 },
bias: Default::default(),
stencil: inside.clone(),
..count.clone()
};
let cap = wgpu::DepthStencilState { depth_write_enabled: Some(true), stencil: inside, ..count.clone() };
let outside = stencil(face(GreaterEqual, Keep), face(GreaterEqual, Keep), 0);
let rest = pushed(wgpu::CompareFunction::Less, true, FACE_BIAS).map(|d| wgpu::DepthStencilState { stencil: outside, ..d });
Cut {
mark: solid("sc_tris_mark", "fs_solid", None, Some(mark), source),
push: solid_writes("sc_tris_push", "fs_solid", None, pushed(wgpu::CompareFunction::Less, true, FACE_BIAS), source, false),
// Depth only: every back face behind a marked pixel draws here, so writing
count: solid_writes("sc_tris_count", "fs_clip", None, Some(count), source, false),
fill: solid("sc_tris_fill", "fs_solid", Some(wgpu::Face::Front), Some(fill), source),
// Depth only: every back face behind a cap pixel draws here, so writing
// colour let the last one drawn paint the cut, other bodies behind it
// included (their colours and outlines showed through it, #6328). The
// marking pass already drew the nearest back face's colour.
// included (their colours and outlines showed through it, #6328).
cap: solid_writes("sc_tris_cap", "fs_solid_cut", Some(wgpu::Face::Front), Some(cap), source, false),
rest: solid("sc_tris_rest", "fs_solid_open", None, rest, source),
}
})
};
@@ -2557,7 +2638,7 @@ impl Renderer {
Renderer {
ao,
depth_reset,
section_reset: t.depth.filter(|f| f.has_stencil_aspect()).and_then(|_| depth_reset_pipeline(device, t)),
section_stencil: stencil_set_pipeline(device, t),
target: GpuTarget { oit: oit.is_some(), ..t },
window,
max_texture: device.limits().max_texture_dimension_2d,
@@ -2676,25 +2757,51 @@ const GHOST_BIAS: wgpu::DepthBiasState = wgpu::DepthBiasState { constant: 2 * FA
/// leaving colour and stencil as they are. None without a depth buffer.
fn depth_reset_pipeline(device: &wgpu::Device, t: GpuTarget) -> Option<wgpu::RenderPipeline> {
let format = t.depth?;
let reset = wgpu::DepthStencilState {
format,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Always),
stencil: Default::default(),
bias: Default::default(),
};
Some(viewport_pipeline(device, t, "sc_depth_reset", reset))
}
/// A pipeline that sets the stencil of the viewport in a pass of `t`'s kind to the reference,
/// leaving colour and depth as they are: a section's crossing count starts from it ([`Cut`]).
/// None without a stencil.
fn stencil_set_pipeline(device: &wgpu::Device, t: GpuTarget) -> Option<wgpu::RenderPipeline> {
let format = t.depth.filter(|f| f.has_stencil_aspect())?;
let replace = wgpu::StencilFaceState {
compare: wgpu::CompareFunction::Always,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::Replace,
};
let set = wgpu::DepthStencilState {
format,
depth_write_enabled: Some(false),
depth_compare: Some(wgpu::CompareFunction::Always),
stencil: wgpu::StencilState { front: replace, back: replace, read_mask: 0xff, write_mask: 0xff },
bias: Default::default(),
};
Some(viewport_pipeline(device, t, "sc_stencil_set", set))
}
/// A triangle over the whole viewport in a pass of `t`'s kind, writing no colour, with `ds`.
fn viewport_pipeline(device: &wgpu::Device, t: GpuTarget, label: &str, ds: wgpu::DepthStencilState) -> wgpu::RenderPipeline {
let far = if t.reversed_depth() { "0.0" } else { "1.0" };
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("sc_depth_reset"),
label: Some(label),
source: wgpu::ShaderSource::Wgsl(DEPTH_RESET_SHADER.replace("FAR", far).into()),
});
let layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("sc_depth_reset"), bind_group_layouts: &[], immediate_size: 0 });
Some(device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sc_depth_reset"),
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some(label), bind_group_layouts: &[], immediate_size: 0 });
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(label),
layout: Some(&layout),
vertex: wgpu::VertexState { module: &module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() },
primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, ..Default::default() },
depth_stencil: Some(wgpu::DepthStencilState {
format,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Always),
stencil: Default::default(),
bias: Default::default(),
}),
depth_stencil: Some(ds),
multisample: wgpu::MultisampleState { count: t.samples.max(1), mask: !0, alpha_to_coverage_enabled: false },
fragment: Some(wgpu::FragmentState {
module: &module,
@@ -2704,7 +2811,7 @@ fn depth_reset_pipeline(device: &wgpu::Device, t: GpuTarget) -> Option<wgpu::Ren
}),
multiview_mask: None,
cache: None,
}))
})
}
fn upload(device: &wgpu::Device, label: &str, bytes: &[u8], stride: usize) -> Option<Batch> {
@@ -3088,8 +3195,9 @@ impl Renderer {
pass.set_bind_group(1, &res.access_bind, &[]);
pass.set_bind_group(2, &res.ao.bind, &[]);
pass.set_bind_group(0, scene_bind(m, &res.bind), &[]);
let planned = |pass: &mut wgpu::RenderPass<'static>, p: &wgpu::RenderPipeline| {
for d in m.planned.iter().filter(|d| d.class == TriClass::Opaque) {
// `counting`: only closed bodies, for a section's crossing count ([`Cut::count`]).
let planned = |pass: &mut wgpu::RenderPass<'static>, p: &wgpu::RenderPipeline, counting: bool| {
for d in m.planned.iter().filter(|d| d.class == TriClass::Opaque && (d.closed || !counting)) {
pass.set_pipeline(p);
pass.set_vertex_buffer(0, d.mesh.vertices.slice(..));
pass.set_vertex_buffer(1, d.colors.slice(..));
@@ -3099,8 +3207,8 @@ impl Renderer {
}
};
// Instanced meshes, with the pipeline for smooth and for flat triangles.
let instanced = |pass: &mut wgpu::RenderPass<'static>, smooth: &wgpu::RenderPipeline, flat: &wgpu::RenderPipeline| {
for d in m.instanced.iter().filter(|d| d.look == InstanceLook::Opaque) {
let instanced = |pass: &mut wgpu::RenderPass<'static>, smooth: &wgpu::RenderPipeline, flat: &wgpu::RenderPipeline, counting: bool| {
for d in m.instanced.iter().filter(|d| d.look == InstanceLook::Opaque && (d.closed || !counting)) {
d.draw(pass, smooth, flat, tally);
}
};
@@ -3147,36 +3255,51 @@ impl Renderer {
res.tri_cut_planned.as_ref(),
res.tri_cut_instanced.as_ref(),
res.tri_cut_instanced_flat.as_ref(),
res.section_reset.as_ref(),
res.section_stencil.as_ref(),
) {
(Some(cut), Some(cut_planned), Some(cut_instanced), Some(cut_flat), Some(reset)) => {
(Some(cut), Some(cut_planned), Some(cut_instanced), Some(cut_flat), Some(base)) => {
// The model's own triangles, blocks, plans and instances with its palette, then the
// preview's with its own.
let all = |pass: &mut wgpu::RenderPass<'static>, pick: fn(&Cut) -> &wgpu::RenderPipeline| {
// `counting`: the crossing count, which leaves open bodies out.
let all = |pass: &mut wgpu::RenderPass<'static>, pick: fn(&Cut) -> &wgpu::RenderPipeline, counting: bool| {
pass.set_bind_group(0, scene_bind(m, &res.bind), &[]);
tris(pass, pick(cut), &m.tris);
block_class(pass, pick(cut), TriClass::Opaque);
planned(pass, pick(cut_planned));
instanced(pass, pick(cut_instanced), pick(cut_flat));
if counting {
for block in m.block_order.iter().filter_map(|k| m.blocks.get(k)).filter(|b| b.closed.is_some()) {
pass.set_pipeline(pick(cut));
block.draw_closed(pass, tally);
}
} else {
tris(pass, pick(cut), &m.open_tris);
block_class(pass, pick(cut), TriClass::Opaque);
}
planned(pass, pick(cut_planned), counting);
instanced(pass, pick(cut_instanced), pick(cut_flat), counting);
pass.set_bind_group(0, scene_bind(pv, &res.bind), &[]);
tris(pass, pick(cut), &pv.tris);
if !counting {
tris(pass, pick(cut), &pv.open_tris);
}
};
pass.set_stencil_reference(1);
all(pass, |c| &c.mark);
pass.set_pipeline(reset);
pass.set_stencil_reference(STENCIL_BASE);
pass.set_pipeline(base);
pass.draw(0..3, 0..1);
tally.call();
all(pass, |c| &c.push);
all(pass, |c| &c.cap);
all(pass, |c| &c.count, true);
all(pass, |c| &c.fill, false);
all(pass, |c| &c.cap, false);
all(pass, |c| &c.rest, false);
}
_ => {
pass.set_bind_group(0, scene_bind(m, &res.bind), &[]);
tris(pass, &res.tri, &m.tris);
tris(pass, &res.tri, &m.open_tris);
block_class(pass, &res.tri, TriClass::Opaque);
planned(pass, &res.tri_planned);
instanced(pass, &res.tri_instanced, &res.tri_instanced_flat);
planned(pass, &res.tri_planned, false);
instanced(pass, &res.tri_instanced, &res.tri_instanced_flat, false);
pass.set_bind_group(0, scene_bind(pv, &res.bind), &[]);
tris(pass, &res.tri, &pv.tris);
tris(pass, &res.tri, &pv.open_tris);
}
}
pass.set_bind_group(0, scene_bind(m, &res.bind), &[]);
+1 -1
View File
@@ -155,7 +155,7 @@ impl Resources {
for (batch, bind) in batches.into_iter().zip(binds) {
pass.set_bind_group(0, bind, &[]);
pass.set_pipeline(&self.pipeline);
if let Some(tris) = &batch.tris {
for tris in [&batch.tris, &batch.open_tris].into_iter().flatten() {
for (buffer, count) in &tris.parts {
pass.set_vertex_buffer(0, buffer.slice(..));
pass.draw(0..*count, 0..1);
+54
View File
@@ -287,6 +287,60 @@ fn an_edge_on_the_cut_face_draws_over_the_cap() {
}
}
/// A cut body's cap covers the body it touches behind the cut. A block whose far face (y = 30)
/// lies on a larger plate's front face, cut at y = 15: through the cut the block's far wall and
/// the plate's face are at one depth. When the cap went to the nearer of the two by depth, the
/// plate showed through 2721 of its 4350 px here (an engine's sleeves in their bores, pins in
/// their bosses); counted by crossings, the cap is the same with the plate as without it. An
/// open body behind it (a box without its far face, whose near face alone the ray crosses)
/// takes nothing from the cap either: only closed bodies are counted.
#[test]
fn a_cap_covers_the_body_its_cut_body_touches() {
let block = ("solid.box", json!({"length": 40, "width": 30, "height": 20, "body_name": "block"}));
let plate = ("solid.box", json!({"length": 70, "width": 10, "height": 50, "corner": [-15, 30, -15], "operation": "new", "body_name": "plate"}));
let shell = ("solid.box", json!({"length": 70, "width": 10, "height": 50, "corner": [-15, 35, -15], "operation": "new", "body_name": "shell"}));
let far_face = ("face.delete", json!({"faces": [[20, 45, 10]], "heal": false}));
let cut = ("inspect.section", json!({"plane": {"origin": [0, 15, 0], "normal": [0, -1, 0]}}));
let alone = session(&[block.clone(), cut.clone()]);
let touching = session(&[block.clone(), plate, cut.clone()]);
let open = session(&[block, shell, far_face, cut]);
assert!(open.world_state().unwrap().body("shell").unwrap().body.is_surface());
let (w, h) = (360, 280);
for (mode, c) in capturers() {
for (what, other) in [("the touching plate", &touching), ("an open shell behind", &open)] {
for fov in [0.0, 0.6] {
let mut cam = view::home_camera(&touching).unwrap();
(cam.fov, cam.yaw, cam.pitch) = (fov, -0.3, 0.2);
let radius = view::scene(&touching, &cam).unwrap().radius;
let shot = |s: &Session| {
c.rgba(&Scene { grid: Vec::new(), lines: Vec::new(), radius, ..view::scene(s, &cam).unwrap() }, &cam, w, h).unwrap()
};
let (a, b) = (shot(&alone), shot(other));
// The cap on screen: the cut's rectangle (x 0..40, z 0..20 at y = 15), 2 px in.
let corner = |x: f64, z: f64| cam.to_screen(Vec3::new(x, 15.0, z), f64::from(w), f64::from(h), radius).map(|(x, y, _)| (x, y));
let cap: Vec<(f64, f64)> = [(0.0, 0.0), (40.0, 0.0), (40.0, 20.0), (0.0, 20.0)].iter().filter_map(|(x, z)| corner(*x, *z)).collect();
assert_eq!(cap.len(), 4, "fov {fov}: the cut is on screen");
let turn = (0..4).map(|i| cap[i].0 * cap[(i + 1) % 4].1 - cap[(i + 1) % 4].0 * cap[i].1).sum::<f64>().signum();
let inside = |x: f64, y: f64| {
(0..4).all(|i| {
let ((ax, ay), (bx, by)) = (cap[i], cap[(i + 1) % 4]);
((bx - ax) * (y - ay) - (by - ay) * (x - ax)) / (bx - ax).hypot(by - ay) * turn >= 2.0
})
};
let (mut shown, mut all) = (0, 0);
for (i, (p, q)) in pixels(&a).zip(pixels(&b)).enumerate() {
if inside((i % w as usize) as f64 + 0.5, (i / w as usize) as f64 + 0.5) {
all += 1;
shown += usize::from(p.iter().zip(q).any(|(u, v)| u.abs_diff(v) > 24));
}
}
assert!(all > 3000, "{mode}, fov {fov}: the cap ({all} px)");
assert_eq!(shown, 0, "{mode} depth, fov {fov}: {what} shows through {shown} of {all} cap px");
}
}
}
}
/// Pixels of the model's edges lying behind the section plane (recoloured red) that draw over
/// the cap: the cap's pixels are where the picture without lines shows the hatch (warm, with
/// warm pixels 3 px around, so the cut's outline doesn't count).
+1 -1
View File
@@ -424,7 +424,7 @@ mod tests {
/// A front panel over a back one, with a line between them, seen from the front.
fn two_panels(front_opacity: f32) -> (Scene, Camera) {
let glass = SceneMesh { mesh: Arc::new(panel(-5.0)), color: Rgb(255, 0, 0), face_colors: Vec::new(), opacity: front_opacity };
let glass = SceneMesh { mesh: Arc::new(panel(-5.0)), color: Rgb(255, 0, 0), face_colors: Vec::new(), opacity: front_opacity, closed: true };
let scene = Scene {
meshes: vec![SceneMesh::solid(Arc::new(panel(5.0)), Rgb(0, 255, 0)), glass],
lines: vec![SceneLine {
+4 -1
View File
@@ -27,12 +27,15 @@ pub struct SceneMesh {
/// drawn see-through, so what lies inside it shows, as the viewport draws glass. A body
/// without an appearance of its own is solid.
pub opacity: f32,
/// The body encloses a volume (a solid, a closed mesh); an open one (a surface body, a solid
/// with faces left out) has no inside, so a section gives it no cap.
pub closed: bool,
}
impl SceneMesh {
/// A solid body in `color`, with no face of its own look.
pub fn solid(mesh: Arc<Mesh>, color: Rgb) -> SceneMesh {
SceneMesh { mesh, color, face_colors: Vec::new(), opacity: 1.0 }
SceneMesh { mesh, color, face_colors: Vec::new(), opacity: 1.0, closed: true }
}
/// Is the body drawn see-through? (Its opacity is below [`SEE_THROUGH`].)
+19 -3
View File
@@ -920,6 +920,9 @@ struct BodyDraw {
/// The render class of each face (opaque, glass or faded).
face_classes: Vec<crate::gpu::TriClass>,
faded: bool,
/// The body encloses a volume: only closed bodies take part in a section's crossing count
/// ([`crate::gpu::BlockBytes::closed`]).
closed: bool,
/// Its faces are drawn elsewhere, from its plan on the GPU or as an instance of shared
/// triangles (only its edges are built here).
on_gpu: bool,
@@ -936,7 +939,7 @@ impl BodyDraw {
let mut h = std::collections::hash_map::DefaultHasher::new();
(std::sync::Arc::as_ptr(&self.mesh) as usize).hash(&mut h);
self.place.iter().flatten().flatten().for_each(|v| v.to_bits().hash(&mut h));
(self.faded, self.on_gpu, shaded, edges, hidden_edges, self.slot_base, self.edge.0, self.hidden_edge.0).hash(&mut h);
(self.faded, self.closed, self.on_gpu, shaded, edges, hidden_edges, self.slot_base, self.edge.0, self.hidden_edge.0).hash(&mut h);
h.finish()
}
@@ -949,6 +952,7 @@ impl BodyDraw {
}
m.tri_face.get(ti).and_then(|f| self.face_classes.get(*f as usize)).copied().unwrap_or(crate::gpu::TriClass::Opaque)
};
out.open_body(!self.closed);
if shaded && !self.on_gpu {
let corner = |k: u32| {
let i = k as usize;
@@ -1055,6 +1059,7 @@ fn build_block(draws: &[BodyDraw], shaded: bool, build: &impl Fn(&BodyDraw, &mut
fn reindex_block(block: CachedBlock, draws: &[BodyDraw]) -> CachedBlock {
use crate::gpu::TriClass;
let mut runs: [Vec<std::ops::Range<u32>>; 3] = Default::default();
let mut closed = Vec::new();
for (range, d) in block.ranges.iter().zip(draws) {
// The triangles this body wrote (none when its faces are drawn from a plan or as an
// instance), never more than its mesh has.
@@ -1070,6 +1075,9 @@ fn reindex_block(block: CachedBlock, draws: &[BodyDraw]) -> CachedBlock {
if let Some(list) = runs.get_mut(slot) {
crate::gpu::extend_runs(list, at);
}
if class == TriClass::Opaque && d.closed {
crate::gpu::extend_runs(&mut closed, at);
}
}
}
let [opaque, glass, faded] = runs;
@@ -1088,6 +1096,7 @@ fn reindex_block(block: CachedBlock, draws: &[BodyDraw]) -> CachedBlock {
let bytes = std::sync::Arc::new(crate::gpu::BlockBytes {
vertices: old.vertices.clone(),
opaque: std::sync::Arc::new(opaque),
closed: std::sync::Arc::new(closed),
glass: std::sync::Arc::new(glass),
faded: std::sync::Arc::new(faded),
lines: old.lines.clone(),
@@ -1156,7 +1165,10 @@ impl Instances {
self.lods.levels(&mesh).map(|l| l.as_ref().clone()).unwrap_or_default(),
),
};
self.meshes.push((mesh.clone(), crate::gpu::InstancedMesh { key: key as u64, tris, instances: Vec::new(), look, levels }));
self.meshes.push((
mesh.clone(),
crate::gpu::InstancedMesh { key: key as u64, tris, instances: Vec::new(), look, levels, closed: b.is_closed() },
));
self.by_mesh.insert((key, look), self.meshes.len() - 1);
self.meshes.len() - 1
}
@@ -1303,11 +1315,12 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene {
face_colors.push(slot);
face_classes.push(class);
}
let closed = b.is_closed();
let class = plan_class(app, &face_classes);
let planned = class.and_then(|class| drawn_plan(app, b, true).map(|plan| (plan, class)));
let on_gpu = planned.is_some();
if let Some((plan, class)) = planned {
sc.planned.push(crate::gpu::PlannedBody { plan, face_colors: face_colors.clone(), class });
sc.planned.push(crate::gpu::PlannedBody { plan, face_colors: face_colors.clone(), class, closed });
}
// Opaque, faded and see-through bodies alike, in one colour (faces of their own colour
// are built as before).
@@ -1335,6 +1348,7 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene {
body_color: slots.body,
face_classes,
faded,
closed,
on_gpu: true,
slot_base: slots.faces,
edge: slots.edge,
@@ -1348,6 +1362,7 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene {
body_color: slots.body,
face_classes,
faded,
closed,
on_gpu,
slot_base: slots.faces,
edge: slots.edge,
@@ -3127,6 +3142,7 @@ impl HighlightInstances {
instances: Vec::new(),
look: crate::gpu::InstanceLook::Opaque,
levels: Vec::new(),
closed: true,
});
self.by_mesh.insert(key, self.meshes.len() - 1);
self.meshes.len() - 1