diff --git a/assets/shaders/viewport.wgsl b/assets/shaders/viewport.wgsl index 003d167ff..42ce753b7 100644 --- a/assets/shaders/viewport.wgsl +++ b/assets/shaders/viewport.wgsl @@ -347,10 +347,14 @@ fn vs_line(@builtin(vertex_index) vi: u32, @location(0) a: vec3, @location( let t = ts[vi]; let s = ss[vi]; let base = select(ca, cb, t > 0.5); - let off = nrm * s * w * 0.5 + dir * (t * 2.0 - 1.0) * w * 0.5; + // A line thinner than a pixel is drawn a pixel wide, faded by the width it lacks: a narrower + // quad covers no pixel centre when it falls between two rows, so without multisampling thin + // lines (the grid's) came and went as the view zoomed (#2823). + let wd = max(w, 1.0); + let off = nrm * s * wd * 0.5 + dir * (t * 2.0 - 1.0) * wd * 0.5; var o: LOut; o.pos = vec4(base.xy + off / half * base.w, base.z, base.w); - o.c = c; + o.c = vec4(c.rgb, c.a * (w / wd)); o.wp = select(pa, pb, t > 0.5); return o; } diff --git a/crates/gpu/src/capture.rs b/crates/gpu/src/capture.rs index 4ec11a29b..8d93c812d 100644 --- a/crates/gpu/src/capture.rs +++ b/crates/gpu/src/capture.rs @@ -62,8 +62,15 @@ impl Capturer { /// As [`Capturer::new`], drawing with the depth format `depth` (with a stencil): the fallback /// `Depth24PlusStencil8` on a device that has a float one too, to compare the two. pub fn with_depth(adapter: &wgpu::Adapter, device: wgpu::Device, queue: wgpu::Queue, depth: wgpu::TextureFormat) -> Result { + Self::with_samples(adapter, device, queue, depth, SAMPLES) + } + + /// As [`Capturer::with_depth`], with `samples` per pixel (1: no multisampling, as a device or + /// browser without 4× draws); 1 where the device lacks that many. + fn with_samples(adapter: &wgpu::Adapter, device: wgpu::Device, queue: wgpu::Queue, depth: wgpu::TextureFormat, samples: u32) -> Result { let flags = |f: wgpu::TextureFormat| adapter.get_texture_format_features(f).flags; - let samples = if flags(FORMAT).sample_count_supported(SAMPLES) && flags(depth).sample_count_supported(SAMPLES) { SAMPLES } else { 1 }; + let samples = + if samples > 1 && flags(FORMAT).sample_count_supported(samples) && flags(depth).sample_count_supported(samples) { samples } else { 1 }; let scopes = error_scopes(&device); let renderer = Renderer::new(&device, GpuTarget { format: FORMAT, depth: Some(depth), samples, tess: false }, None); let background = background_pipeline(&device, samples, depth); @@ -99,6 +106,16 @@ impl Capturer { Self::with_depth(&adapter, device, queue, depth) } + /// As [`Capturer::headless`], without multisampling: one sample per pixel, as the viewport + /// draws where 4× isn't available (some browsers and devices, or MSAA turned off). + #[cfg(not(target_arch = "wasm32"))] + pub fn headless_single_sample() -> Result { + let (adapter, device, queue) = crate::open_headless()?; + device.on_uncaptured_error(Arc::new(|e: wgpu::Error| log::error!("GPU: {e}"))); + let depth = crate::depth_format(&device, true); + Self::with_samples(&adapter, device, queue, depth, 1) + } + /// The depth format pictures are drawn with; float formats are drawn with reversed depth. pub fn depth_format(&self) -> wgpu::TextureFormat { self.depth diff --git a/crates/gpu/tests/thin_lines.rs b/crates/gpu/tests/thin_lines.rs new file mode 100644 index 000000000..c8cff8493 --- /dev/null +++ b/crates/gpu/tests/thin_lines.rs @@ -0,0 +1,53 @@ +//! Lines thinner than a pixel (the grid's minor lines are 0.7 px) are drawn wherever they fall, +//! without multisampling (#2823). A quad narrower than a pixel covers no pixel centre when it +//! falls between two rows, so such lines came and went as the view zoomed. Skipped, with the +//! reason printed, without a GPU adapter (`SOLIDCRAFT_REQUIRE_GPU=1` makes that a failure). + +use solidcraft_engine::geom::Vec3; +use solidcraft_engine::render::{Camera, Rgb, Scene, SceneLine, StandardView}; +use solidcraft_gpu::GpuError; +use solidcraft_gpu::capture::Capturer; + +/// Pictures drawn with one sample per pixel: with 4× multisampling a thin line covers some +/// samples wherever it falls, which hides the fault. +fn capturer() -> Option<&'static Capturer> { + static SINGLE: std::sync::OnceLock> = std::sync::OnceLock::new(); + match SINGLE.get_or_init(Capturer::headless_single_sample).as_ref() { + Ok(c) => { + assert_eq!(c.samples(), 1); + Some(c) + } + Err(e @ GpuError::NoAdapter(_)) if std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none() => { + eprintln!("skipping: {e}"); + None + } + Err(e) => panic!("{e}"), + } +} + +/// Lines `width` px wide across a front view, each drawn alone at its own height so it falls at +/// its own fraction of a pixel (0.23 mm apart, about 1.15 px at 5 px per mm); returns the ones +/// that left no pixel. +fn missing(c: &Capturer, width: f64) -> Vec { + const N: usize = 40; + let mut cam = Camera { distance: 62.5, fov: 0.0, ..Default::default() }; + cam.set_view(StandardView::Front); + (0..N) + .filter(|&i| { + let z = -4.0 + i as f64 * 0.23; + let line = SceneLine { points: vec![Vec3::new(-8.0, 0.0, z), Vec3::new(8.0, 0.0, z)], color: Rgb(255, 0, 0), width, on_top: true }; + let scene = Scene { lines: vec![line], radius: 30.0, ..Default::default() }; + let img = c.rgba(&scene, &cam, 200, 200).unwrap(); + !img.as_chunks::<4>().0.iter().any(|p| p[0] > 60 && p[0] > p[1].saturating_add(40) && p[0] > p[2].saturating_add(40)) + }) + .collect() +} + +#[test] +fn lines_thinner_than_a_pixel_are_drawn_wherever_they_fall() { + let Some(c) = capturer() else { return }; + for width in [0.4, 0.7, 1.0, 1.5] { + let gone = missing(c, width); + assert!(gone.is_empty(), "{width} px lines: {} of 40 left no pixel ({gone:?})", gone.len()); + } +}