From 11cbaf364213e1ca7f58ad80264fd77812417489 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Fri, 20 Nov 2026 04:56:14 -0700 Subject: [PATCH] CADCRAFT_STENCIL=0 starts the desktop app without a stencil buffer Some old graphics drivers (Mesa on Intel HD 4000, old Intel drivers on Windows) accept a render pass with a stencil buffer and then present a black window. Setting CADCRAFT_STENCIL to 0, no or false (any case) opens the window without one, so the app is usable on those machines. The window and the canvas take the stencil bits from the same place, so they always agree. Without a stencil buffer, clipped layout viewports draw unclipped; a warning in the log says so. --help lists the variable. Co-authored-by: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> --- apps/cadcraft/src/env.rs | 10 ++++++- apps/cadcraft/src/main.rs | 59 +++++++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/apps/cadcraft/src/env.rs b/apps/cadcraft/src/env.rs index 76df8aa15..21e79ad8a 100644 --- a/apps/cadcraft/src/env.rs +++ b/apps/cadcraft/src/env.rs @@ -11,6 +11,7 @@ use cadcraft_ui_egui::switch::Switch; const CONFIG_DIR: &str = "CADCRAFT_CONFIG_DIR"; const LOCALE: &str = "CADCRAFT_LOCALE"; const VSYNC: &str = "CADCRAFT_VSYNC"; +const STENCIL: &str = "CADCRAFT_STENCIL"; const WAYLAND: &str = "CADCRAFT_WAYLAND"; const NO_NATIVE_MENU: &str = "CADCRAFT_NO_NATIVE_MENU"; const SYSTEM_CURSOR: &str = "CADCRAFT_SYSTEM_CURSOR"; @@ -22,6 +23,7 @@ pub(crate) const VARS: &[(&str, &str)] = &[ (CONFIG_DIR, "the settings folder (logs, autosave, workspace, plotters); empty means no settings folder"), (LOCALE, "the interface language, instead of the system's"), (VSYNC, "1 presents with vsync, 0 without it (default: without on Linux and BSD)"), + (STENCIL, "0 opens the window without a stencil buffer, for drivers that show a black window; clipped layout viewports then draw unclipped"), (WAYLAND, "1 keeps the native Wayland backend instead of XWayland; 0 or unset is off"), (NO_NATIVE_MENU, "macOS: 1 keeps the menus inside the window; 0 or unset is off"), (SYSTEM_CURSOR, "1 uses the system cursor instead of the drawn crosshair; 0 or unset is off"), @@ -42,6 +44,9 @@ pub(crate) struct Env { pub(crate) locale: Option, /// Present with vsync (default: off on Linux and BSD, on elsewhere). pub(crate) vsync: Switch, + /// Off: the window has no stencil buffer (default: on). Some old drivers (Mesa on Intel HD + /// 4000, old Intel drivers on Windows) accept a stencil pass and then present a black window. + pub(crate) stencil: Switch, /// Keep the native Wayland backend instead of XWayland. #[cfg_attr(not(all(unix, not(target_os = "macos"))), allow(dead_code))] pub(crate) wayland: Switch, @@ -73,6 +78,7 @@ impl Env { }, locale: text(LOCALE), vsync: switch(VSYNC), + stencil: switch(STENCIL), wayland: switch(WAYLAND), no_native_menu: switch(NO_NATIVE_MENU), system_cursor: switch(SYSTEM_CURSOR), @@ -129,6 +135,7 @@ mod tests { ("APPDATA", "C:\\a"), (LOCALE, "cs-CZ"), (VSYNC, " On "), + (STENCIL, "No"), (WAYLAND, "0"), (NO_NATIVE_MENU, "yes"), (SYSTEM_CURSOR, "maybe"), @@ -142,6 +149,7 @@ mod tests { config_dir: Some(PathBuf::from("/private/cc")), locale: Some("cs-CZ".into()), vsync: Switch::On, + stencil: Switch::Off, wayland: Switch::Off, no_native_menu: Switch::On, system_cursor: Switch::Default, @@ -153,7 +161,7 @@ mod tests { let read = Env::from_vars(env(&blank)); assert_eq!(read.config_dir, None, "an empty variable means no directory, not the working directory"); assert_eq!((read.locale, read.font_alt, read.font_fallback), (None, None, None)); - assert_eq!([read.vsync, read.wayland, read.no_native_menu, read.system_cursor], [Switch::Default; 4]); + assert_eq!([read.vsync, read.stencil, read.wayland, read.no_native_menu, read.system_cursor], [Switch::Default; 5]); } #[test] diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index 40515014e..c46b4cd90 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -267,8 +267,9 @@ fn without_tiny_window(app_ron: &str) -> Option { const MIN_INNER_SIZE: [f32; 2] = [900.0, 560.0]; /// The main window: its size, title bar, placement and rendering needs. `restoring`: a window state -/// saved by the last session is restored instead of opening centred. -fn window_options(restoring: bool) -> eframe::NativeOptions { +/// saved by the last session is restored instead of opening centred. `stencil`: the stencil bits of +/// its render pass ([`stencil_bits`]). +fn window_options(restoring: bool, stencil: u8) -> eframe::NativeOptions { eframe::NativeOptions { viewport: egui::ViewportBuilder::default() .with_title("CADCraft") @@ -290,7 +291,7 @@ fn window_options(restoring: bool) -> eframe::NativeOptions { // system-font discovery as a hidden fallback. system_font_fallback: false, // A stencil buffer clips the contents of clipped layout viewports on the GPU. - stencil_buffer: STENCIL_BITS, + stencil_buffer: stencil, ..Default::default() } } @@ -303,6 +304,14 @@ const WINDOW_KEY: &str = "window"; /// Stencil bits of the window's render pass (see `NativeOptions::stencil_buffer`). const STENCIL_BITS: u8 = 8; +/// The stencil bits of the window's render pass: none when `CADCRAFT_STENCIL` is off. Some old +/// drivers (Mesa on Intel HD 4000, old Intel drivers on Windows) accept a stencil pass without an +/// error and then present a black window, so only an explicit opt-out drops it. Without a stencil +/// buffer, clipped layout viewports draw unclipped. +fn stencil_bits(stencil: Switch) -> u8 { + if stencil == Switch::Off { 0 } else { STENCIL_BITS } +} + #[cfg(target_os = "macos")] const APP_ICON_PNG: &[u8] = include_bytes!("../../../assets/app-icon/cadcraft-macos-512.png"); #[cfg(not(target_os = "macos"))] @@ -372,11 +381,17 @@ fn main() -> eframe::Result { cadcraft_engine::fonts::ttf::set_fallback_fonts(list); } let surface = cadcraft_ui_egui::gpu::surface_config(vars.vsync, cfg!(all(unix, not(target_os = "macos")))); + // The window's render pass and the canvas's pipelines must agree on it; a mismatch is a wgpu + // validation error. + let stencil = stencil_bits(vars.stencil); + if stencil == 0 { + log::warn!("CADCRAFT_STENCIL is off: the window has no stencil buffer; clipped layout viewports draw unclipped"); + } let mut options = eframe::NativeOptions { // The crosshair is drawn by the app: present without a queue on Linux/BSD, where X11 // swapchains hold two frames back under vsync (`CADCRAFT_VSYNC=1` restores vsync). wgpu_options: eframe::egui_wgpu::WgpuConfiguration::default().with_surface_config(surface), - ..window_options(restores_window()) + ..window_options(restores_window(), stencil) }; if let Some(icon) = app_icon() { options.viewport = options.viewport.with_icon(icon); @@ -466,7 +481,7 @@ fn main() -> eframe::Result { app.frame_cap = Some(cadcraft_ui_egui::gpu::FrameCap::new(240)); } if let Some(rs) = &cc.wgpu_render_state { - app.set_wgpu(rs, eframe::egui_wgpu::depth_format_from_bits(0, STENCIL_BITS)); + app.set_wgpu(rs, eframe::egui_wgpu::depth_format_from_bits(0, stencil)); } if let Some(port) = control_port { let rx = control_server::start(port, cc.egui_ctx.clone()); @@ -512,7 +527,7 @@ fn main() -> eframe::Result { #[cfg(test)] mod tests { - use super::{filters_for_save, follow_desktop, saved_window, window_options, without_tiny_window}; + use super::{STENCIL_BITS, filters_for_save, follow_desktop, saved_window, stencil_bits, window_options, without_tiny_window}; #[test] fn save_dialog_offers_dwg_with_the_drawing_format_first() { @@ -533,7 +548,7 @@ mod tests { /// #1129: the first launch opens centred, not cascaded from the top-left corner. #[test] fn the_first_launch_opens_centred() { - let o = window_options(false); + let o = window_options(false, STENCIL_BITS); assert!(o.centered, "centred on launch"); assert!(o.persist_window, "the window state is saved for the next launch"); } @@ -541,11 +556,39 @@ mod tests { /// #2595: a saved window state (size, position, maximized) is restored, not centred over. #[test] fn a_saved_window_reopens_where_it_was() { - let o = window_options(true); + let o = window_options(true, STENCIL_BITS); assert!(!o.centered, "centring would move a restored window"); assert!(o.persist_window); } + /// `CADCRAFT_STENCIL=0` (also `no`, `false`, any case) opens the window without a stencil + /// buffer; unset, blank, `1` or anything else keeps it. + #[test] + fn the_stencil_buffer_is_dropped_only_when_asked() { + let bits = |value: Option<&str>| { + let vars = crate::env::Env::from_vars(|k| (k == "CADCRAFT_STENCIL").then_some(value).flatten().map(Into::into)); + stencil_bits(vars.stencil) + }; + for off in ["0", "no", "NO", "false", "False", " 0 "] { + assert_eq!(bits(Some(off)), 0, "{off:?}"); + } + for on in [None, Some(""), Some("1"), Some("yes"), Some("8"), Some("maybe")] { + assert_eq!(bits(on), STENCIL_BITS, "{on:?}"); + } + } + + /// The window's render pass carries the chosen stencil bits, and the canvas gets the same. + #[test] + fn the_window_carries_the_chosen_stencil_bits() { + for bits in [0, STENCIL_BITS] { + assert_eq!(window_options(false, bits).stencil_buffer, bits); + assert_eq!(window_options(true, bits).stencil_buffer, bits); + } + let format = |bits| eframe::egui_wgpu::depth_format_from_bits(0, bits); + assert_eq!(format(0), None, "no stencil: the canvas builds no clip pipelines"); + assert!(format(STENCIL_BITS).is_some_and(|f| f.has_stencil_aspect())); + } + /// Only a readable `app.ron` with a window entry counts as a saved window state. #[test] fn a_saved_window_is_read_from_app_ron() {