From ef2f485e15343729ea1fa404ee55b74bbca70299 Mon Sep 17 00:00:00 2001 From: takuto-NA Date: Wed, 4 Nov 2026 19:42:01 +0000 Subject: [PATCH] Extend viewport AO to the existing virtual ground (#3413) (#11979) * claim: #3413 * feat(gpu): extend viewport AO to the virtual ground * test(gpu): retain measured AO render attachments * perf: initialize ground AO pipeline with concurrent jobs * fix(gpu): size ground AO receiver for the current frame radius * fix(viewport): retain orbit caches when ground AO visibility changes --- assets/shaders/viewport.wgsl | 7 + crates/gpu/src/capture.rs | 1 + crates/gpu/src/view.rs | 59 ++++- crates/gpu/src/view/ao.rs | 112 +++++++- crates/gpu/src/view/msaa_check.rs | 1 + crates/gpu/tests/ground_ao_measurement.rs | 240 ++++++++++++++++++ crates/ui-egui/src/gpu.rs | 31 +++ crates/ui-egui/src/render_workspace.rs | 1 + crates/ui-egui/src/scale_tests.rs | 1 + crates/ui-egui/src/viewport.rs | 28 +- crates/ui-egui/src/viewport_ground_tests.rs | 129 ++++++++++ .../src/viewport_navigation_cache_tests.rs | 6 +- docs/ambient-occlusion.md | 16 +- 13 files changed, 621 insertions(+), 11 deletions(-) create mode 100644 crates/gpu/tests/ground_ao_measurement.rs create mode 100644 crates/ui-egui/src/viewport_ground_tests.rs diff --git a/assets/shaders/viewport.wgsl b/assets/shaders/viewport.wgsl index fd7ca921a..6f52b8de3 100644 --- a/assets/shaders/viewport.wgsl +++ b/assets/shaders/viewport.wgsl @@ -366,6 +366,13 @@ fn fs_ao_depth(i: TOut, @builtin(front_facing) front: bool) -> AoDepth { return opaque_depth(i, front); } +// The virtual ground preserves the viewport background; only contact occlusion is blended. +@fragment +fn fs_ground_ao(i: TOut) -> @location(0) vec4 { + if (clipped(i.wp)) { discard; } + return vec4(0.0, 0.0, 0.0, 1.0 - occlusion(i)); +} + /// The AO prepass's depth of an opaque surface's fragment. fn opaque_depth(i: TOut, front: bool) -> AoDepth { if (clipped(i.wp) || length(i.n) < 0.5) { discard; } diff --git a/crates/gpu/src/capture.rs b/crates/gpu/src/capture.rs index 14ba8ce62..df80e1817 100644 --- a/crates/gpu/src/capture.rs +++ b/crates/gpu/src/capture.rs @@ -397,6 +397,7 @@ fn frame(scene: &Scene, cam: &Camera, [width, height]: [u32; 2], key: u64, acces pv_key: 0, pv_slot: empty(), pv_hide: false, + preview_bounds: None, view_proj: cam.view_proj(f64::from(width) / f64::from(height.max(1)), scene.radius.max(1.0)).to_f32(), back: cam.back().to_f32(), size_px: [width as f32, height as f32], diff --git a/crates/gpu/src/view.rs b/crates/gpu/src/view.rs index 32649e025..be747134c 100644 --- a/crates/gpu/src/view.rs +++ b/crates/gpu/src/view.rs @@ -18,6 +18,7 @@ use crate::pbr; use wgpu::util::DeviceExt; mod ao; +pub use ao::GroundAo; mod clouds; pub use clouds::{CloudFilter, CloudStyle, ColorMode, DOT_SIZE, Dot, FILTERS, GpuCloud, cloud_dot}; #[cfg(not(target_arch = "wasm32"))] @@ -494,6 +495,8 @@ impl InstancedMesh { pub struct GpuScene { /// The colours every vertex, line and instance index into. pub palette: Arc, + /// Non-selectable virtual ground receiving viewport AO, separate from model triangles. + pub ground_ao: Option, /// Opaque triangles. A zero normal draws the colour flat (unlit). pub tris: Vec, /// Opaque triangles of open bodies, drawn as `tris` but left out of a section's crossing @@ -1017,6 +1020,9 @@ pub struct Frame { pub pv_slot: SceneSlot, /// Preview reselect: skip drawing the preview while Primary rolls it back. pub pv_hide: bool, + /// World bounds of the active preview. Ground effects update their small receiver without + /// changing the model scene's key or re-uploading model geometry/colours. + pub preview_bounds: Option, /// Column-major view-projection. pub view_proj: [[f32; 4]; 4], /// Direction toward the eye (for lighting). @@ -1700,6 +1706,8 @@ struct Batches { tris: Option, /// [`GpuScene::open_tris`]. open_tris: Option, + ground_ao: Option, + ground_input: Option, pbr: Option, trans: Option, shadow: Option, @@ -1792,6 +1800,7 @@ impl Batches { geometry += data.len() as u64; upload(device, label, data, stride) }; + self.ground_input = sc.ground_ao; 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); @@ -2077,6 +2086,8 @@ pub struct Renderer { ss_stats: DrawStats, /// The AO depth prepass resources (a 1 × 1 stand-in while ambient occlusion is off). ao: ao::Resources, + ground_ao: wgpu::RenderPipeline, + ground_ao_key: Option<(u64, u64, bool, u32, bool)>, tri: wgpu::RenderPipeline, /// `tri` and `tri_cut` for planned bodies (positions and normals from the GPU evaluation, /// colours per vertex, indexed). @@ -2737,6 +2748,29 @@ impl Renderer { bind_group_layouts: &[Some(&bgl), Some(&access_bgl), Some(&ao_layout)], immediate_size: 0, }); + let ground_ao = || { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("sc_ground_ao"), + layout: Some(&solid_layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some("vs_tri"), + buffers: &[Some(tri_layout())], + compilation_options: Default::default(), + }, + primitive: wgpu::PrimitiveState { cull_mode: Some(wgpu::Face::Back), ..Default::default() }, + depth_stencil: depth(wgpu::CompareFunction::LessEqual, false), + multisample: wgpu::MultisampleState { count: t.samples.max(1), ..Default::default() }, + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("fs_ground_ao"), + targets: &[Some(wgpu::ColorTargetState { format: t.format, blend: alpha, write_mask: wgpu::ColorWrites::ALL })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }) + }; let built = [Some(tri_layout())]; let evaluated = [Some(planned_layout()), Some(color_layout())]; let placed = [Some(shared_layout()), Some(instance_layout())]; @@ -2982,6 +3016,7 @@ impl Renderer { let cloud_pipes = jobs.spawn(cloud_pipes); let image = jobs.spawn(image); let ao = jobs.spawn(ao); + let ground_ao = jobs.spawn(ground_ao); let oit = jobs.spawn(oit); let blits = jobs.spawn(blits); let resets = jobs.spawn(resets); @@ -3000,6 +3035,8 @@ impl Renderer { let oit = oit.join(); Renderer { ao: ao.join(), + ground_ao: ground_ao.join(), + ground_ao_key: None, depth_reset, section_stencil, #[cfg(test)] @@ -3484,6 +3521,17 @@ impl Renderer { res.model.sort_glass(queue, cb.back); } res.model.cull(queue, &cb.view_proj, cb.size_px); + let ground_visible = res.model.ground_input.is_some_and(|ground| ground.visible_from(cb.back)); + let ground_key = (cb.key, cb.pv_key, cb.pv_hide, cb.ao_radius.to_bits(), ground_visible); + if res.ground_ao_key != Some(ground_key) { + let preview = if cb.pv_hide { None } else { cb.preview_bounds }; + res.model.ground_ao = res + .model + .ground_input + .filter(|_| ground_visible) + .and_then(|ground| upload(device, "sc_ground_ao", &ground.vertices_with_radius(preview, cb.ao_radius), TRI_SIZE)); + res.ground_ao_key = Some(ground_key); + } queue.write_buffer(&res.uniform, 0, &uniform_bytes(cb, cb.size_px[0], cb.size_px[1], res.linear_out, res.target.reversed_depth())); if let Some(a) = &cb.access && a.version != res.access_version @@ -3491,8 +3539,9 @@ impl Renderer { res.access_bind = access_texture(device, Some(queue), &res.access_bgl, a.size, &a.depth); res.access_version = a.version; } - let ao_binds = [scene_bind(&res.model, &res.bind), scene_bind(&res.preview, &res.bind)]; - res.ao.prepare(device, encoder, ao_binds, cb.ao_radius, cb.size_px, res.target.reversed_depth(), [&res.model, &res.preview]); + let preview = if cb.pv_hide { &res.empty } else { &res.preview }; + let ao_binds = [scene_bind(&res.model, &res.bind), scene_bind(preview, &res.bind)]; + res.ao.prepare(device, encoder, ao_binds, cb.ao_radius, cb.size_px, res.target.reversed_depth(), [&res.model, preview]); // Images: upload new or changed textures, drop removed ones, rebuild the quads. res.textures.retain(|id, _| cb.images.iter().any(|i| i.id == *id)); res.quads.clear(); @@ -3838,6 +3887,9 @@ impl Renderer { pass.set_bind_group(0, scene_bind(pv, &res.bind), &[]); lines(pass, &res.line, &pv.lines); pass.set_bind_group(0, scene_bind(m, &res.bind), &[]); + if !cb.product && cb.ao_radius > 0.0 { + tris(pass, &res.ground_ao, &m.ground_ao); + } for (id, q) in res.quads.iter().filter(|_| !cb.product) { if let Some((_, bind)) = res.textures.get(id) { pass.set_pipeline(&res.image); @@ -4514,6 +4566,7 @@ mod tests { pv_key: 4, pv_slot: slot(), pv_hide: false, + preview_bounds: None, view_proj: [[0.0; 4]; 4], back: [0.0; 3], size_px: [800.0, 600.0], @@ -4973,6 +5026,7 @@ mod glass_tests { pv_key: 0, pv_slot: none(), pv_hide: false, + preview_bounds: None, view_proj: [[0.1, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0], [0.0, 0.0, -0.01, 0.0], [0.0, 0.0, 0.5, 1.0]], back: [0.0, 0.0, 1.0], size_px: [SIZE as f32; 2], @@ -5150,6 +5204,7 @@ mod glass_tests { pv_key: 0, pv_slot: none(), pv_hide: false, + preview_bounds: None, view_proj: [[0.1, 0.0, 0.0, 0.0], [0.0, 0.1, 0.0, 0.0], [0.0, 0.0, -0.01, 0.0], [0.0, 0.0, 0.5, 1.0]], back: [0.0, 0.0, 1.0], size_px: [SIZE as f32; 2], diff --git a/crates/gpu/src/view/ao.rs b/crates/gpu/src/view/ao.rs index 2449e6e58..0c6392b19 100644 --- a/crates/gpu/src/view/ao.rs +++ b/crates/gpu/src/view/ao.rs @@ -1,4 +1,4 @@ -//! The optional AO passes. Only opaque model and preview triangles participate: their depth is +//! The optional AO passes. Opaque model, preview and virtual ground triangles participate: their depth is //! drawn once into a single-sample depth map, and the occlusion term of the surface that depth //! holds is drawn into a texture of the same size and blurred by depth (#9327), which the lit //! fragment reads. The maps are independent of the window's MSAA, and the depth runs the same way @@ -30,6 +30,78 @@ pub(super) struct Maps { smooth: wgpu::TextureView, } +/// A virtual receiver in the camera's up-axis frame. Geometry is built independently of the +/// model batches, so moving a preview never re-uploads unchanged model colours or vertices. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct GroundAo { + bounds: solidcraft_geom::Aabb3, + height: f64, + up: solidcraft_render::UpAxis, + radius: f32, +} + +impl GroundAo { + pub(super) fn visible_from(&self, back: [f32; 3]) -> bool { + if !back.iter().all(|v| v.is_finite()) { + return false; + } + let cam = solidcraft_render::Camera { up: self.up, ..Default::default() }; + cam.to_frame(solidcore_record::Vec3::new(f64::from(back[0]), f64::from(back[1]), f64::from(back[2]))).z > 0.0 + } + + pub fn new(bounds: solidcraft_geom::Aabb3, height: f64, up: solidcraft_render::UpAxis, radius: f32) -> Option { + if bounds.is_empty() || !bounds.min.is_finite() || !bounds.max.is_finite() || !height.is_finite() || !radius.is_finite() || radius <= 0.0 { + return None; + } + Some(Self { bounds, height, up, radius }) + } + + /// Two triangles (168 bytes), extended to cover an optional preview without moving the + /// existing ground height. Invalid preview bounds are ignored. + pub fn vertices(&self, preview: Option) -> Vec { + self.vertices_with_radius(preview, self.radius) + } + + /// Cover the preview plus the current frame's sampling margin. Invalid radii draw nothing. + pub fn vertices_with_radius(&self, preview: Option, radius: f32) -> Vec { + use solidcore_record::Vec3; + if !radius.is_finite() || radius <= 0.0 { + return Vec::new(); + } + let cam = solidcraft_render::Camera { up: self.up, ..Default::default() }; + let mut coverage = self.bounds; + if let Some(bb) = preview.filter(|b| !b.is_empty() && b.min.is_finite() && b.max.is_finite()) { + for i in 0..8 { + coverage.add(cam.to_frame(Vec3::new( + if i & 1 == 0 { bb.min.x } else { bb.max.x }, + if i & 2 == 0 { bb.min.y } else { bb.max.y }, + if i & 4 == 0 { bb.min.z } else { bb.max.z }, + ))); + } + } + let margin = f64::from(radius) * 2.0; + let points = [ + Vec3::new(coverage.min.x - margin, coverage.min.y - margin, self.height), + Vec3::new(coverage.max.x + margin, coverage.min.y - margin, self.height), + Vec3::new(coverage.max.x + margin, coverage.max.y + margin, self.height), + Vec3::new(coverage.min.x - margin, coverage.max.y + margin, self.height), + ] + .map(|p| cam.from_frame(p).to_f32()); + if !points.iter().flatten().all(|v| v.is_finite()) { + return Vec::new(); + } + let normal = cam.from_frame(Vec3::Z).to_f32(); + let mut bytes = Vec::with_capacity(6 * super::TRI_SIZE); + for i in [0, 1, 2, 0, 2, 3] { + for value in points[i].iter().chain(&normal) { + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes.extend_from_slice(&0u32.to_le_bytes()); + } + bytes + } +} + pub(super) struct Resources { pub layout: wgpu::BindGroupLayout, pub maps: Maps, @@ -87,7 +159,7 @@ fn draw_opaque(pass: &mut wgpu::RenderPass<'_>, pipelines: &OpaquePipelines, bin for (batch, bind) in batches.into_iter().zip(binds) { pass.set_bind_group(0, bind, &[]); pass.set_pipeline(&pipelines.built); - for tris in [&batch.tris, &batch.open_tris].into_iter().flatten() { + for tris in [&batch.tris, &batch.open_tris, &batch.ground_ao].into_iter().flatten() { for (buffer, count) in &tris.parts { pass.set_vertex_buffer(0, buffer.slice(..)); pass.draw(0..*count, 0..1); @@ -299,7 +371,41 @@ fn target_size(enabled: bool, pixels: [f32; 2], device_limit: u32) -> [u32; 2] { #[cfg(test)] mod tests { - use super::target_size; + use super::{GroundAo, target_size}; + + #[test] + fn receiver_visibility_follows_the_camera_side_in_both_up_axes() { + use solidcore_record::Vec3; + let bounds = solidcraft_geom::Aabb3 { min: Vec3::ZERO, max: Vec3::new(3.0, 4.0, 12.0) }; + for up in [solidcraft_render::UpAxis::Y, solidcraft_render::UpAxis::Z] { + let cam = solidcraft_render::Camera { up, ..Default::default() }; + let ground = GroundAo::new(bounds, 0.0, up, 1.0).unwrap(); + assert!(ground.visible_from(cam.from_frame(Vec3::Z).to_f32())); + assert!(!ground.visible_from(cam.from_frame(-Vec3::Z).to_f32())); + assert!(!ground.visible_from(cam.from_frame(Vec3::X).to_f32())); + assert!(!ground.visible_from([f32::NAN; 3])); + } + } + + #[test] + fn a_growing_preview_keeps_the_current_sampling_margin_at_the_fixed_ground_height() { + use solidcore_record::Vec3; + use solidcraft_geom::Aabb3; + let bounds = Aabb3 { min: Vec3::ZERO, max: Vec3::new(3.0, 4.0, 12.0) }; + let ground = GroundAo::new(bounds, -0.014, solidcraft_render::UpAxis::Z, 1.0).unwrap(); + let preview = Aabb3 { min: Vec3::new(100.0, 200.0, -40.0), max: Vec3::new(103.0, 204.0, -28.0) }; + let mut drawn = Aabb3::EMPTY; + for vertex in ground.vertices_with_radius(Some(preview), 10.0).as_chunks::<{ crate::view::TRI_SIZE }>().0 { + let read = |i| f64::from(f32::from_le_bytes(vertex[i..i + 4].try_into().unwrap())); + drawn.add(Vec3::new(read(0), read(4), read(8))); + } + assert_eq!((drawn.min.x, drawn.min.y, drawn.max.x, drawn.max.y), (-20.0, -20.0, 123.0, 224.0)); + assert!((drawn.min.z + 0.014).abs() < 1e-6 && (drawn.max.z + 0.014).abs() < 1e-6); + assert_eq!(ground.vertices(Some(preview)), ground.vertices_with_radius(Some(preview), 1.0)); + for radius in [f32::NAN, f32::INFINITY, -1.0, 0.0] { + assert!(ground.vertices_with_radius(Some(preview), radius).is_empty()); + } + } #[test] fn depth_map_is_bounded_and_released_when_disabled() { assert_eq!(target_size(true, [6000.0, 3000.0], 8192), [2048, 1024]); diff --git a/crates/gpu/src/view/msaa_check.rs b/crates/gpu/src/view/msaa_check.rs index 755541b98..8a846959b 100644 --- a/crates/gpu/src/view/msaa_check.rs +++ b/crates/gpu/src/view/msaa_check.rs @@ -107,6 +107,7 @@ impl Renderer { pv_key: 4, pv_slot: slot(None), pv_hide: false, + preview_bounds: None, view_proj: [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.5, 1.0]], back: [0.0, 0.0, 1.0], size_px: [SIZE as f32; 2], diff --git a/crates/gpu/tests/ground_ao_measurement.rs b/crates/gpu/tests/ground_ao_measurement.rs new file mode 100644 index 000000000..a8104dd44 --- /dev/null +++ b/crates/gpu/tests/ground_ao_measurement.rs @@ -0,0 +1,240 @@ +//! Manual timestamp measurement: model-only AO versus the virtual-ground receiver. +//! Run explicitly with --ignored, WGPU_BACKEND and SOLIDCRAFT_GPU=hardware|software. +//! SOLIDCRAFT_BENCH_OUTPUT_DIR is the folder for JSON evidence. Not a CI speed threshold. +#![cfg(not(target_arch = "wasm32"))] +use serde_json::json; +use solidcore_record::Vec3; +use solidcraft_engine::{geom::Aabb3, render::Camera}; +use solidcraft_gpu as gpu; +use solidcraft_gpu::view::{Frame, GpuScene, GpuTarget, Renderer}; +use std::{ + sync::Arc, + time::{Duration, Instant}, +}; + +#[test] +#[ignore = "manual CPU/GPU measurement; requires a timestamp-capable adapter"] +fn measure_ground_ao_renderer_cost() -> Result<(), Box> { + ao_comparison(0)?; + ao_comparison(1) +} +fn ao_comparison(ground: u32) -> Result<(), Box> { + let (adapter, _) = gpu::select::pick(gpu::select::Policy::from_env().map_err(std::io::Error::other)?).ok_or("no matching adapter")?; + let info = adapter.get_info(); + let features = wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS; + if !adapter.features().contains(features) { + return Err("adapter has no encoder timestamps".into()); + } + let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { + label: Some("existing AO comparison"), + required_features: features, + required_limits: wgpu::Limits::downlevel_defaults().using_resolution(adapter.limits()), + ..Default::default() + }))?; + let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let target = GpuTarget { + format: wgpu::TextureFormat::Rgba8Unorm, + depth: Some(wgpu::TextureFormat::Depth24PlusStencil8), + samples: 4, + tess: false, + oit: false, + }; + let mut session = solidcraft_engine::Session::default(); + for corner in [[-65, -10, 0], [45, -10, 0]] { + session.execute("solid.box", &json!({"length":20,"width":20,"height":20,"corner":corner,"operation":"new"}))?; + } + session.execute("solid.torus", &json!({"major":14,"minor":4,"center":[0,0,9],"operation":"new"}))?; + let meshes: Vec<_> = session.world_state()?.bodies.iter().map(|b| b.mesh()).collect(); + let bounds = meshes.iter().fold(Aabb3::EMPTY, |bounds, mesh| bounds.union(&mesh.bounds())); + let radius = (bounds.diagonal() * 0.04).clamp(1e-4, 1e6) as f32; + let cam = Camera { + target: Vec3::new(8.379000844961002, 2.6776676413414724, 7.806862777456871), + distance: 131.41653316382525, + yaw: -0.5, + pitch: 0.65, + ..Default::default() + }; + let query = + device.create_query_set(&wgpu::QuerySetDescriptor { label: Some("existing AO timestamps"), ty: wgpu::QueryType::Timestamp, count: 4 }); + let buffer = |usage| device.create_buffer(&wgpu::BufferDescriptor { label: None, size: 32, usage, mapped_at_creation: false }); + let resolve = buffer(wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC); + let read = buffer(wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ); + let software = info.device_type == wgpu::DeviceType::Cpu; + let samples = if software { 12 } else { 30 }; + let warm = 5; + let mut rows = Vec::new(); + for (width, height) in [(1670u32, 838u32), (3340, 1676)] { + if software && width > 1670 { + continue; + } + let mut scene = GpuScene::default(); + let color = scene.color([190, 195, 205, 255]); + for mesh in &meshes { + for tri in &mesh.triangles { + for index in tri { + let p = mesh.positions.get(*index as usize).ok_or("bad index")?; + let n = mesh.normals.get(*index as usize).copied().unwrap_or(Vec3::Z); + scene.tri(p.to_f32(), n.to_f32(), color); + } + } + } + if ground > 0 { + receiver(&mut scene, bounds_in_frame(&[bounds], &cam), &cam, f64::from(radius)); + } + let mut frame = comparison_frame(scene, &cam, width, height, 1); + let mut renderer = Renderer::new(&device, target, None); + let attachment = |format| { + device + .create_texture(&wgpu::TextureDescriptor { + label: Some("AO comparison target"), + size: wgpu::Extent3d { width, height, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 4, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }) + .create_view(&Default::default()) + }; + let color = attachment(target.format); + let depth = attachment(wgpu::TextureFormat::Depth24PlusStencil8); + // Keep each state stable after warm-up: disabling releases the depth image, so alternating + // every frame would benchmark repeated allocation rather than normal steady AO use. + for enabled in [false, true, false, true] { + frame.ao_radius = if enabled { radius } else { 0.0 }; + let mut cpu = Vec::new(); + let mut prepass = Vec::new(); + let mut shade = Vec::new(); + let mut total = Vec::new(); + for sample in 0..samples + warm { + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.write_timestamp(&query, 0); + let started = Instant::now(); + renderer.prepare(&device, &queue, &mut encoder, &frame); + let cpu_ms = started.elapsed().as_secs_f64() * 1000.0; + encoder.write_timestamp(&query, 1); + encoder.write_timestamp(&query, 2); + { + let mut pass = encoder + .begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("existing AO shaded model"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &color, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &depth, + depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), store: wgpu::StoreOp::Store }), + stencil_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0), store: wgpu::StoreOp::Store }), + }), + ..Default::default() + }) + .forget_lifetime(); + renderer.paint(&frame, &mut pass, [0.0, 0.0, width as f32, height as f32]); + } + encoder.write_timestamp(&query, 3); + encoder.resolve_query_set(&query, 0..4, &resolve, 0); + encoder.copy_buffer_to_buffer(&resolve, 0, &read, 0, 32); + let submitted = queue.submit([encoder.finish()]); + read.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + device.poll(wgpu::PollType::Wait { submission_index: Some(submitted), timeout: Some(Duration::from_secs(30)) })?; + let mapped = read.slice(..).get_mapped_range()?; + let times: Vec<_> = + mapped.as_chunks::<8>().0.iter().map(|b| u64::from_le_bytes(*b) as f64 * f64::from(queue.get_timestamp_period()) / 1e6).collect(); + if sample >= warm { + let at = |i| times.get(i).copied().ok_or("missing timestamp"); + cpu.push(cpu_ms); + prepass.push((at(1)? - at(0)?).max(0.0)); + shade.push((at(3)? - at(2)?).max(0.0)); + total.push((at(3)? - at(0)?).max(0.0)); + } + drop(mapped); + read.unmap(); + } + rows.push(json!({"enabled":enabled,"width":width,"height":height,"samples":samples, + "cpu_cached_prepare_ms":summary(&mut cpu),"gpu_prepare_ms":summary(&mut prepass), + "gpu_model_pass_ms":summary(&mut shade),"gpu_total_ms":summary(&mut total)})); + } + } + if let Some(error) = pollster::block_on(scope.pop()) { + return Err(error.to_string().into()); + } + let result = json!({"adapter":info.name,"backend":format!("{:?}",info.backend),"radius":radius,"ground":ground, + "triangles":meshes.iter().map(|m|m.triangles.len()).sum::(),"rows":rows, + "notes":"Actual Renderer, same cached opaque model, MSAA4, two off/on pairs after warm-up. ground=0: surface AO; 1: virtual floor; Floor composites require AO on. Projection is not part of this measurement. Stored MSAA4 attachments prevent unused-output elimination; presentation, MSAA resolve and final downsample are excluded. 3340x1676 is SSAA2 working resolution. GPU total includes prepare plus model/clear, not the application frame."}); + let label = match ground { + 1 => "ao-ground", + 2 => "ao-ground-projection", + _ => "ao-baseline", + }; + let output = + std::env::var_os("SOLIDCRAFT_BENCH_OUTPUT_DIR").map(std::path::PathBuf::from).ok_or("set SOLIDCRAFT_BENCH_OUTPUT_DIR to an output folder")?; + std::fs::create_dir_all(&output)?; + std::fs::write(output.join(format!("{label}-comparison.json")), serde_json::to_vec_pretty(&result)?)?; + println!("{label} comparison completed: {} {:?}", info.name, info.backend); + Ok(()) +} + +fn summary(values: &mut [f64]) -> serde_json::Value { + values.sort_by(f64::total_cmp); + json!({"median":values.get(values.len()/2),"p95":values.get(values.len().saturating_mul(95)/100),"samples":values.len()}) +} + +fn bounds_in_frame(bounds: &[Aabb3], cam: &Camera) -> Aabb3 { + let mut combined = Aabb3::EMPTY; + for b in bounds.iter().filter(|b| !b.is_empty()) { + for i in 0..8 { + combined.add(cam.to_frame(Vec3::new( + if i & 1 == 0 { b.min.x } else { b.max.x }, + if i & 2 == 0 { b.min.y } else { b.max.y }, + if i & 4 == 0 { b.min.z } else { b.max.z }, + ))); + } + } + combined +} + +fn comparison_frame(scene: GpuScene, cam: &Camera, width: u32, height: u32, key: u64) -> Frame { + let empty = || Arc::new(std::sync::Mutex::new(None)); + Frame { + product_lights: None, + upload_error: None, + product: false, + key, + slot: Arc::new(std::sync::Mutex::new(Some(scene))), + sketch_key: 0, + sketch_slot: empty(), + hl_key: 0, + hl_slot: empty(), + pv_key: 0, + pv_slot: empty(), + pv_hide: false, + preview_bounds: None, + view_proj: cam.view_proj(f64::from(width) / f64::from(height), 200.0).to_f32(), + back: cam.back().to_f32(), + size_px: [width as f32, height as f32], + clip: [0.0; 4], + cap: [0.0; 4], + images: Vec::new(), + clouds: Vec::new(), + analysis: [0.0; 8], + analysis_colors: [0.0; 36], + access: None, + outline: false, + eye: [0.0; 4], + ao_radius: 0.0, + ao_samples: gpu::view::AO_SAMPLES, + hover_tint: [0.0; 3], + selection_tint: [0.0; 3], + hatch: Default::default(), + sky: [0.0; 4], + ssaa: 1.0, + } +} + +fn receiver(sc: &mut GpuScene, b: Aabb3, cam: &Camera, radius: f64) { + sc.ground_ao = solidcraft_gpu::view::GroundAo::new(b, b.min.z - 0.001 * (1.0 + b.diagonal()), cam.up, radius as f32); +} diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index 3be5b0520..399cec50a 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -375,6 +375,36 @@ struct Offscreen<'a> { ao_samples: u32, } +#[cfg(all(test, not(target_arch = "wasm32")))] +pub(crate) fn render_ao_offscreen( + scene: GpuScene, + view_proj: [[f32; 4]; 4], + back: [f32; 3], + eye: [f32; 4], + size: u32, + tess: Option<&str>, + ao_radius: f32, +) -> Option> { + offscreen( + scene, + None, + view_proj, + back, + eye, + size, + Offscreen { + tess, + product: false, + srgb: false, + analysis: [0.0; 8], + analysis_colors: [0.0; 36], + ssaa: 1.0, + ao_radius, + ao_samples: solidcraft_gpu::view::AO_SAMPLES, + }, + ) +} + /// [`render_offscreen`] of a scene supersampled `ssaa` times (1: off). #[cfg(all(test, not(target_arch = "wasm32")))] pub(crate) fn render_offscreen_ssaa( @@ -461,6 +491,7 @@ fn offscreen( pv_key: 0, pv_slot: none(), pv_hide: false, + preview_bounds: None, view_proj, back, size_px: [size as f32; 2], diff --git a/crates/ui-egui/src/render_workspace.rs b/crates/ui-egui/src/render_workspace.rs index 2fba6821b..a334fec2f 100644 --- a/crates/ui-egui/src/render_workspace.rs +++ b/crates/ui-egui/src/render_workspace.rs @@ -2241,6 +2241,7 @@ fn paint_product(app: &mut SolveApp, ui: &mut egui::Ui, rect: egui::Rect, camera pv_key: 0, pv_slot: slot, pv_hide: false, + preview_bounds: None, view_proj: product_projection(camera, radius, rect, image).to_f32(), back: camera.back().to_f32(), size_px: [rect.width() * ppp, rect.height() * ppp], diff --git a/crates/ui-egui/src/scale_tests.rs b/crates/ui-egui/src/scale_tests.rs index 0fc76b197..29f0f1f34 100644 --- a/crates/ui-egui/src/scale_tests.rs +++ b/crates/ui-egui/src/scale_tests.rs @@ -277,6 +277,7 @@ pub(crate) fn view_frame( pv_key: 0, pv_slot: none(), pv_hide: false, + preview_bounds: None, view_proj: proj.vp.to_f32(), back: proj.cam.back().to_f32(), size_px: [size as f32; 2], diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 844b4dc26..f26f84cd9 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -39,6 +39,10 @@ mod removed_later_tests; #[path = "viewport_plane_target_tests.rs"] mod plane_target_tests; +#[cfg(test)] +#[path = "viewport_ground_tests.rs"] +mod ground_tests; + #[cfg(test)] #[path = "viewport_refine_wait_tests.rs"] mod refine_wait_tests; @@ -1716,6 +1720,11 @@ pub(crate) fn build_scene(app: &SolveApp) -> GpuScene { *b = made; } sc.instanced = instances.finish(app); + if app.ui.visual_style != 2 { + let bounds = ground_bounds(app, &st); + let radius = (bounds.diagonal() * 0.04).clamp(1e-4, 1e6) as f32; + sc.ground_ao = solidcraft_gpu::view::GroundAo::new(bounds, ground_height(bounds), app.cam.up, radius); + } // Section: the outline of each body where the plane cuts it, in the view's text colour (#4146). if let Some((o, n)) = section_plane(app) { let col = sc.color(c4(Tokens::view().text.gamma_multiply(0.9))); @@ -1986,7 +1995,7 @@ fn shadow_geometry(app: &SolveApp, st: &std::sync::Arc) { +fn ground_bounds(app: &SolveApp, st: &std::sync::Arc) -> solidcraft_engine::geom::Aabb3 { // The bodies' box in the camera's Z-up frame (the ground is its XY plane). let mut b = solidcraft_engine::geom::Aabb3::EMPTY; let boxes = body_bounds(app, st); @@ -2012,12 +2021,21 @@ fn ground_shadow(sc: &mut GpuScene, app: &SolveApp, st: &std::sync::Arc f64 { + bounds.min.z - 1e-3 * (1.0 + bounds.diagonal()) +} + +fn ground_shadow(sc: &mut GpuScene, app: &SolveApp, b: solidcraft_engine::geom::Aabb3) { if b.is_empty() { return; } let size = b.size(); let c = b.center(); - let z = b.min.z - 1e-3 * (1.0 + b.diagonal()); + let z = ground_height(b); let (rx, ry) = (size.x * 0.62 + size.z * 0.15, size.y * 0.62 + size.z * 0.15); if !(rx > 0.0 && ry > 0.0) { return; @@ -4721,6 +4739,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { pv_key: app.preview.key, pv_slot: app.preview.slot.clone(), pv_hide: app.preview.rolled_back, + preview_bounds: (app.preview.active && !app.preview.rolled_back).then_some(app.preview.bounds), clip: section_clip(app), cap: { let [r, g, b, _] = t.section_cap.to_normalized_gamma_f32(); @@ -7154,7 +7173,7 @@ mod tests { session.execute("solid.box", &json!({"length": 30, "width": 30, "height": 10}))?; let app = SolveApp::new(session, crate::Services::default()); let mut scene = GpuScene::default(); - ground_shadow(&mut scene, &app, &crate::open_meshing::session_world(&app.session)); + ground_shadow(&mut scene, &app, ground_bounds(&app, &crate::open_meshing::session_world(&app.session))); assert!(scene.trans.is_empty(), "the ground shadow must not trigger order-independent transparency"); assert_eq!(scene.shadow.len(), 48 * 5 * 3 * crate::gpu::TRI_SIZE, "all shadow rings are retained"); let shadow_alpha = |v: &[u8]| { @@ -8710,6 +8729,7 @@ mod tests { pv_key: 0, pv_slot: none(), pv_hide: false, + preview_bounds: None, view_proj: proj.vp.to_f32(), back: proj.cam.back().to_f32(), size_px: [w as f32, h as f32], diff --git a/crates/ui-egui/src/viewport_ground_tests.rs b/crates/ui-egui/src/viewport_ground_tests.rs new file mode 100644 index 000000000..88b89c74d --- /dev/null +++ b/crates/ui-egui/src/viewport_ground_tests.rs @@ -0,0 +1,129 @@ +use super::*; +use solidcraft_engine::{Session, geom::Aabb3, render::UpAxis}; + +#[test] +fn ground_ao_follows_the_existing_plane_in_both_up_axes_without_origin_clamping() { + for up in [UpAxis::Z, UpAxis::Y] { + let cam = Camera { up, ..Default::default() }; + for bottom in [-20.0, 20.0] { + // A 3 x 4 x 12 box has diagonal 13: the original ground offset is .014 mm. + let bounds = Aabb3 { min: Vec3::new(-1.5, -2.0, bottom), max: Vec3::new(1.5, 2.0, bottom + 12.0) }; + let ground = solidcraft_gpu::view::GroundAo::new(bounds, ground_height(bounds), cam.up, 2.0).unwrap(); + let bytes = ground.vertices(None); + assert_eq!(bytes.len(), 6 * crate::gpu::TRI_SIZE); + for vertex in bytes.as_chunks::<{ crate::gpu::TRI_SIZE }>().0 { + let read = |i| f64::from(f32::from_le_bytes(vertex[i..i + 4].try_into().unwrap())); + let point = cam.to_frame(Vec3::new(read(0), read(4), read(8))); + assert!((point.z - (bottom - 0.014)).abs() < 1e-5); + assert!(point.x >= -5.5 && point.x <= 5.5 && point.y >= -6.0 && point.y <= 6.0); + assert_eq!(cam.to_frame(Vec3::new(read(12), read(16), read(20))), Vec3::Z); + } + } + } + for (bounds, radius) in [(Aabb3::EMPTY, 2.0), (Aabb3 { min: Vec3::ZERO, max: Vec3::X }, f32::NAN)] { + assert!(solidcraft_gpu::view::GroundAo::new(bounds, ground_height(bounds), Camera::default().up, radius).is_none()); + } +} + +#[test] +fn ground_ao_is_independent_of_the_ellipse_and_respects_visibility_and_view_style() { + let mut session = Session::default(); + session.execute("solid.box", &json!({"length":3,"width":4,"height":12,"corner":[0,0,-20],"body_name":"Foot"})).unwrap(); + let mut app = SolveApp::new(session, crate::Services::default()); + app.ui.ground_shadow = false; + app.cam.pitch = 0.6; + let key = scene_key(&app); + let ground = build_scene(&app).ground_ao; + assert!(ground.is_some(), "cache receiver data independently of its frame visibility"); + assert_eq!(ao_radius(&app), 0.0); + let revision = app.session.revision; + app.run("view.ambient_occlusion", json!({"enabled":true})).unwrap(); + assert_eq!(scene_key(&app), key, "AO only changes frame visibility, not model geometry"); + assert_eq!(app.session.revision, revision, "AO is a preference, not a design edit"); + assert_eq!(build_scene(&app).ground_ao, ground); + assert_eq!(ground.unwrap().vertices(None).len(), 6 * crate::gpu::TRI_SIZE); + app.ui.ground_shadow = true; + assert_eq!(build_scene(&app).ground_ao, ground); + app.cam.yaw += 0.5; + app.cam.pitch += 0.1; + assert_eq!(build_scene(&app).ground_ao, ground, "camera motion does not rebuild ground geometry"); + for style in [0, 1, 3] { + app.ui.visual_style = style; + assert!(build_scene(&app).ground_ao.is_some()); + } + app.ui.visual_style = 2; + assert!(build_scene(&app).ground_ao.is_none()); + app.ui.visual_style = 0; + app.cam.pitch = -0.5; + assert_eq!(build_scene(&app).ground_ao, ground, "the renderer hides the receiver below the ground"); + app.cam.pitch = 0.5; + app.session.visibility.hidden_bodies.push("Foot".into()); + assert!(build_scene(&app).ground_ao.is_none()); +} + +#[test] +fn ground_ao_covers_a_live_preview_without_moving_the_existing_plane() { + let mut session = Session::default(); + session.execute("solid.box", &json!({"length":3,"width":4,"height":12,"corner":[0,0,-20]})).unwrap(); + let mut app = SolveApp::new(session, crate::Services::default()); + app.preferences.ambient_occlusion = true; + app.cam.pitch = 0.5; + let before = scene_key(&app); + app.preview.active = true; + app.preview.key += 1; + app.preview.bounds = Aabb3 { min: Vec3::new(100.0, 0.0, -40.0), max: Vec3::new(103.0, 4.0, -28.0) }; + assert_eq!(scene_key(&app), before, "a preview update must not re-upload unchanged model batches"); + let scene = build_scene(&app); + let bytes = scene.ground_ao.unwrap().vertices(Some(app.preview.bounds)); + let points: Vec = bytes + .as_chunks::<{ crate::gpu::TRI_SIZE }>() + .0 + .iter() + .map(|v| { + let read = |i| f64::from(f32::from_le_bytes(v[i..i + 4].try_into().unwrap())); + Vec3::new(read(0), read(4), read(8)) + }) + .collect(); + assert!(points.iter().all(|p| (p.z + 20.014).abs() < 1e-5)); + assert!(points.iter().any(|p| p.x > 103.0)); + app.preview.rolled_back = true; + assert!(build_scene(&app).ground_ao.unwrap().vertices(None).len() == 6 * crate::gpu::TRI_SIZE); +} + +/// Exercise both native geometry paths on a real adapter. White background pixels at the +/// foot gain contact shading; a stale receiver must draw nothing when AO is disabled. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn ground_ao_draws_contact_pixels_without_an_ellipse_and_disappears_when_disabled() { + let mut session = Session::default(); + session.execute("solid.box", &json!({"length":10,"width":10,"height":20,"corner":[-5,-5,0]})).unwrap(); + let mut app = SolveApp::new(session, crate::Services::default()); + app.preferences.ambient_occlusion = true; + app.ui.ground_shadow = false; + app.cam = Camera { target: Vec3::new(0.0, 0.0, 5.0), distance: 70.0, pitch: 0.6, yaw: 0.5, fov: 0.0, ..Default::default() }; + let projection = projection(&app, Rect::from_min_size(Pos2::ZERO, vec2(256.0, 256.0))); + let eye = app.cam.eye().to_f32(); + for tess in [false, true] { + app.viewport.gpu = Some(GpuTarget { format: egui_wgpu::wgpu::TextureFormat::Rgba8Unorm, depth: None, samples: 1, tess, oit: false }); + let scene = build_scene(&app); + let mut without_ground = scene.clone(); + without_ground.ground_ao = None; + let render = |scene, radius| { + crate::gpu::render_ao_offscreen( + scene, + projection.vp.to_f32(), + app.cam.back().to_f32(), + [eye[0], eye[1], eye[2], 0.0], + 256, + tess.then_some(solidcraft_gpu::SHADER), + radius, + ) + }; + let Some(baseline) = render(without_ground.clone(), 0.0) else { return }; + assert_eq!(render(scene.clone(), 0.0).unwrap(), baseline, "AO off skips even a cached receiver"); + let shaded = render(scene, ao_radius(&app)).unwrap(); + let contacts = baseline.as_chunks::<4>().0.iter().zip(shaded.as_chunks::<4>().0).filter(|(a, b)| a[..3] == [255; 3] && b[0] < 253).count(); + assert!(contacts > 5, "no ground contacts on tess={tess}: {contacts} pixels"); + assert_eq!(&shaded[..4], &[255; 4], "a distant background pixel stays unchanged"); + } +} diff --git a/crates/ui-egui/src/viewport_navigation_cache_tests.rs b/crates/ui-egui/src/viewport_navigation_cache_tests.rs index 5f7d4eeaa..6b8752efc 100644 --- a/crates/ui-egui/src/viewport_navigation_cache_tests.rs +++ b/crates/ui-egui/src/viewport_navigation_cache_tests.rs @@ -60,10 +60,11 @@ fn face_hover_and_clicks_follow_device_rotation_on_each_axis() { /// Crossing the ground used to rebuild every body even with the shadow switched off. #[test] fn flips_reuse_the_model_and_only_switch_shadow_visibility() { - for shadow in [false, true] { + for (shadow, ao) in [false, true].into_iter().flat_map(|shadow| [false, true].map(|ao| (shadow, ao))) { let ctx = crate::fonts::test_context(); let mut app = app(); app.ui.ground_shadow = shadow; + app.preferences.ambient_occlusion = ao; frame(&mut app, &ctx); let model_key = app.viewport.key; let above = app.viewport.sketch_slot.lock().unwrap().take().unwrap(); @@ -96,6 +97,9 @@ fn flips_reuse_the_model_and_only_switch_shadow_visibility() { assert!(app.viewport.slot.lock().unwrap().is_none(), "toggling a shadow must not rebuild the model"); let toggled = app.viewport.sketch_slot.lock().unwrap().take().unwrap(); assert_eq!(!toggled.shadow.is_empty(), !shadow); + app.preferences.ambient_occlusion = !ao; + frame(&mut app, &ctx); + assert!(app.viewport.slot.lock().unwrap().is_none(), "toggling AO must not rebuild model geometry"); } } diff --git a/docs/ambient-occlusion.md b/docs/ambient-occlusion.md index 6664d3c37..3437693de 100644 --- a/docs/ambient-occlusion.md +++ b/docs/ambient-occlusion.md @@ -3,7 +3,9 @@ Enable **Ambient occlusion** in the viewport's **View settings** menu or in **Preferences → Graphics**. It adds soft contact shading at corners, recesses and nearby opaque surfaces. It is off by default, applies immediately, and is saved -between runs. The shared **Graphics Preset** selector in both settings surfaces +between runs. It also shades contacts with the viewport's virtual ground, independently +of the Ground shadow setting. Turning Ground shadow off leaves AO contacts enabled. +The shared **Graphics Preset** selector in both settings surfaces combines AO with the display-quality and supersampling controls from #1319 and #1321: | Preset | Ambient occlusion | Display quality | Supersampling | @@ -43,6 +45,18 @@ It supports orthographic and perspective cameras and respects section clipping. Wireframe, transparent bodies, overlays and surface-analysis colours are unaffected. Disabling it skips the depth prepass and sampling and releases the full-size depth map. +The ground follows the existing ellipse's visible-body bounds and up-axis convention, +slightly below their lowest point; it is not the origin grid or a saved floor object. +It appears only in shaded views from above. The receiver adds two triangles to the +existing depth prepass and draws contact opacity over the background, without another +texture or body-mesh copy. Its fragment sampling still adds GPU work, especially on +software adapters and with supersampling. The existing structured SSAO bands remain +a separate quality issue (#3407). + +Live-preview bounds update the small receiver through frame metadata, without changing +the model scene's cache key or re-uploading unchanged model colours. Only its extent +grows to cover the preview; the existing ground height remains unchanged. + This is screen-space shading: off-screen and hidden surfaces cannot contribute. The software fallback and CPU model snapshots do not apply this GPU effect. Capture the actual window with `ui.screenshot` when reviewing it.