Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop-gpui): Cap Glass renderer, model and real-app probe

Richie McIlroya25aafd5bd12
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 70bb14815..4527fba58 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -421,6 +421,7 @@ jobs:
cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} --bin cap-gpui
fi
cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-gpui-kernels
+ cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-glass
cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-i18n-build
# Cap only builds gpui's crates as dependencies, so their own tests never run
diff --git a/.github/workflows/glass-probe.yml b/.github/workflows/glass-probe.yml
new file mode 100644
index 000000000..0ccced411
--- /dev/null
+++ b/.github/workflows/glass-probe.yml
@@ -0,0 +1,147 @@
+name: Glass probe (real app)
+# Draws Cap Glass in the real GPUI app on macOS (Metal), Windows (Direct3D 11
+# on WARP) and Linux (wgpu on lavapipe), captures the window and scores every
+# scene and preset against the CPU model in apps/desktop-gpui/glass, which is
+# fitted to Apple's own glass.
+
+on:
+ pull_request:
+ types: [opened, synchronize, reopened, ready_for_review]
+ paths:
+ - "apps/desktop-gpui/glass/**"
+ - "apps/desktop-gpui/patches/zed-glass.patch"
+ - "apps/desktop-gpui/src/ui/glass.rs"
+ - "apps/desktop-gpui/src/glass_probe.rs"
+ - "scripts/glass-probe/**"
+ - ".github/workflows/glass-probe.yml"
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: glass-probe-${{ github.event.pull_request.number || github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ probe:
+ name: Glass probe (${{ matrix.settings.name }})
+ timeout-minutes: 75
+ strategy:
+ fail-fast: false
+ matrix:
+ settings:
+ - name: macOS
+ slug: macos
+ target: aarch64-apple-darwin
+ runner: blacksmith-6vcpu-macos-15
+ - name: Windows
+ slug: windows
+ target: x86_64-pc-windows-msvc
+ runner: windows-2022
+ - name: Linux X11
+ slug: linux-x11
+ target: x86_64-unknown-linux-gnu
+ runner: blacksmith-8vcpu-ubuntu-2404
+ runs-on: ${{ matrix.settings.runner }}
+ env:
+ CARGO_PROFILE_DEV_DEBUG: "0"
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Install Linux desktop dependencies
+ if: ${{ runner.os == 'Linux' }}
+ uses: ./.github/actions/install-desktop-deps
+
+ - name: Install a window manager and software Vulkan
+ if: ${{ runner.os == 'Linux' }}
+ run: sudo apt-get install -y openbox mesa-vulkan-drivers libvulkan1
+
+ - name: Rust setup
+ uses: dtolnay/[email protected]
+ with:
+ targets: ${{ matrix.settings.target }}
+
+ - uses: ./.github/actions/setup-rust-cache
+ with:
+ target: ${{ matrix.settings.target }}
+ prefix-key: v1-rust-gpui
+ save-cache: "false"
+ workspaces: apps/desktop-gpui -> target
+
+ - name: Run setup
+ shell: bash
+ run: node scripts/setup.js
+ env:
+ RUST_TARGET_TRIPLE: ${{ matrix.settings.target }}
+
+ - name: Prepare GPUI dependency
+ shell: bash
+ run: |
+ node scripts/sync-desktop-versions.mjs
+ ./scripts/prepare-gpui-dependency.sh
+
+ - name: Test the glass model
+ shell: bash
+ run: cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked -p cap-glass
+
+ - name: Build the app
+ shell: bash
+ run: |
+ cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked \
+ --target ${{ matrix.settings.target }} --bin cap-gpui
+ if [[ "$RUNNER_OS" == "macOS" ]]; then
+ for frameworks in "$GITHUB_WORKSPACE/apps/desktop-gpui/target/${{ matrix.settings.target }}/debug/Frameworks" \
+ "$GITHUB_WORKSPACE/apps/desktop-gpui/target/${{ matrix.settings.target }}/Frameworks"; do
+ mkdir -p "$frameworks"
+ ln -sfn "$GITHUB_WORKSPACE/target/Frameworks/Spacedrive.framework" "$frameworks/Spacedrive.framework"
+ done
+ fi
+
+ - name: Screen capture permission
+ if: ${{ runner.os == 'macOS' }}
+ run: |
+ set -euo pipefail
+ sudo pmset -a displaysleep 0 sleep 0 || true
+ caffeinate -u -t 2
+ app="$GITHUB_WORKSPACE/apps/desktop-gpui/target/${{ matrix.settings.target }}/debug/cap-gpui"
+ db="/Library/Application Support/com.apple.TCC/TCC.db"
+ now=$(date +%s)
+ for client in "$app" /bin/bash /Users/runner/bin/Runner.Worker /Users/runner/bin/Runner.Listener; do
+ sudo sqlite3 "$db" "INSERT OR REPLACE INTO access (service,client,client_type,auth_value,auth_reason,auth_version,indirect_object_identifier,flags,last_modified) VALUES ('kTCCServiceScreenCapture','$client',1,2,4,1,'UNUSED',0,$now);"
+ done
+ sudo killall tccd || true
+ sleep 2
+
+ - name: Glass probe
+ shell: bash
+ run: |
+ app="$GITHUB_WORKSPACE/apps/desktop-gpui/target/${{ matrix.settings.target }}/debug/cap-gpui"
+ out="$RUNNER_TEMP/glass-probe"
+ case "$RUNNER_OS" in
+ macOS)
+ scripts/glass-probe/run.sh "$app" "$out"
+ ;;
+ Windows)
+ export PATH="$(cygpath "$GITHUB_WORKSPACE/target/debug"):$PATH"
+ scripts/glass-probe/run.sh "$app.exe" "$out"
+ ;;
+ Linux)
+ export LD_LIBRARY_PATH="$GITHUB_WORKSPACE/target/native-deps/lib:$GITHUB_WORKSPACE/target/debug${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+ Xvfb :99 -screen 0 1920x1080x24 -noreset &
+ export DISPLAY=:99
+ sleep 1
+ openbox &
+ sleep 1
+ scripts/glass-probe/run.sh "$app" "$out"
+ ;;
+ esac
+
+ - name: Upload captures
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: glass-probe-${{ matrix.settings.slug }}
+ path: ${{ runner.temp }}/glass-probe
+ retention-days: 7
diff --git a/.github/workflows/glass-reference.yml b/.github/workflows/glass-reference.yml
deleted file mode 100644
index aff28f02c..000000000
--- a/.github/workflows/glass-reference.yml
+++ /dev/null
@@ -1,64 +0,0 @@
-name: Glass reference (Apple)
-# Captures Apple's NSGlassEffectView over the fixed backdrops in
-# scripts/glass-reference so Cap's glass renderer can be compared against it.
-
-on:
- pull_request:
- paths:
- - "scripts/glass-reference/**"
- - ".github/workflows/glass-reference.yml"
- workflow_dispatch:
-
-permissions:
- contents: read
-
-concurrency:
- group: glass-reference-${{ github.event.pull_request.number || github.ref }}
- cancel-in-progress: true
-
-jobs:
- capture:
- name: Apple glass reference (${{ matrix.runner }})
- strategy:
- fail-fast: false
- matrix:
- runner: [macos-26, blacksmith-6vcpu-macos-26]
- runs-on: ${{ matrix.runner }}
- timeout-minutes: 30
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
-
- - name: Screen capture permission
- run: |
- set -euo pipefail
- sudo pmset -a displaysleep 0 sleep 0 || true
- caffeinate -u -t 2
- swiftc -O scripts/glass-reference/reference.swift -o "$RUNNER_TEMP/reference"
- db="/Library/Application Support/com.apple.TCC/TCC.db"
- now=$(date +%s)
- for client in "$RUNNER_TEMP/reference" /usr/sbin/screencapture /bin/bash /Users/runner/bin/Runner.Worker /Users/runner/bin/Runner.Listener; do
- sudo sqlite3 "$db" "INSERT OR REPLACE INTO access (service,client,client_type,auth_value,auth_reason,auth_version,indirect_object_identifier,flags,last_modified) VALUES ('kTCCServiceScreenCapture','$client',1,2,4,1,'UNUSED',0,$now);" || true
- done
- sudo killall tccd || true
- defaults read com.apple.universalaccess 2>/dev/null || true
- for key in reduceTransparency increaseContrast reduceMotion; do
- defaults write com.apple.universalaccess "$key" -bool false || true
- sudo defaults write com.apple.universalaccess "$key" -bool false || true
- done
- killall cfprefsd || true
- sleep 2
- sw_vers
- system_profiler SPDisplaysDataType | grep -E "Resolution|Chipset|Metal" || true
-
- - name: Capture
- run: |
- "$RUNNER_TEMP/reference" "$RUNNER_TEMP/glass-reference"
- ls "$RUNNER_TEMP/glass-reference/apple" | head
-
- - name: Upload
- uses: actions/upload-artifact@v4
- with:
- name: glass-reference-${{ matrix.runner }}
- path: ${{ runner.temp }}/glass-reference
- retention-days: 30
diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock
index 900791a5c..2516cffb2 100644
--- a/apps/desktop-gpui/Cargo.lock
+++ b/apps/desktop-gpui/Cargo.lock
@@ -1551,6 +1551,7 @@ dependencies = [
"cap-editor",
"cap-enc-ffmpeg",
"cap-export",
+ "cap-glass",
"cap-gpui-kernels",
"cap-i18n-build",
"cap-media-info",
@@ -1773,6 +1774,15 @@ dependencies = [
"workspace-hack",
]
+[[package]]
+name = "cap-glass"
+version = "0.1.0"
+dependencies = [
+ "image 0.25.10",
+ "serde",
+ "serde_json",
+]
+
[[package]]
name = "cap-gpui-kernels"
version = "0.1.0"
diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml
index 8b9c003a1..4bcf0a2bc 100644
--- a/apps/desktop-gpui/Cargo.toml
+++ b/apps/desktop-gpui/Cargo.toml
@@ -5,7 +5,7 @@
# wgpu-hal, tao) that only exist for the Tauri app.
[workspace]
resolver = "2"
-members = [".", "kernels", "i18n-build"]
+members = [".", "kernels", "i18n-build", "glass"]
[package]
name = "cap-desktop-gpui"
@@ -27,6 +27,7 @@ path = "../../crates/recording/examples/studio-finalization-benchmark.rs"
cap-audio = { path = "../../crates/audio" }
cap-cli-install = { path = "../../crates/cli-install" }
cap-gpui-kernels = { path = "kernels" }
+cap-glass = { path = "glass" }
# gpui — the wingleeio/zed fork Comet ships. The pinned rev carries f596cde
# ("destination alpha on transparent windows: Porter-Duff OVER, not additive"),
# which our transparent rounded main-window shell depends on.
diff --git a/apps/desktop-gpui/glass/Cargo.toml b/apps/desktop-gpui/glass/Cargo.toml
new file mode 100644
index 000000000..2eeea4765
--- /dev/null
+++ b/apps/desktop-gpui/glass/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "cap-glass"
+version = "0.1.0"
+edition = "2024"
+license = "AGPL-3.0"
+publish = false
+
+[dependencies]
+image = { version = "0.25.1", default-features = false, features = ["png"] }
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
diff --git a/apps/desktop-gpui/glass/presets.json b/apps/desktop-gpui/glass/presets.json
new file mode 100644
index 000000000..9953d4dfe
--- /dev/null
+++ b/apps/desktop-gpui/glass/presets.json
@@ -0,0 +1,214 @@
+{
+ "regular-light": {
+ "blur_k": 0.19,
+ "blur_max": 21,
+ "rim_k": 0.24,
+ "rim_max": 24,
+ "bend": -2,
+ "lens_curve": 0.4,
+ "poly": [
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0
+ ],
+ "opacity": 0.6,
+ "line": 0,
+ "line_lit": 0,
+ "line_width": 1,
+ "light_angle": -2.3,
+ "glow": 0,
+ "glow_width": 4,
+ "hairline": [
+ 0.86,
+ -0.12,
+ 0.92,
+ -0.08
+ ]
+ },
+ "regular-dark": {
+ "blur_k": 0.19,
+ "blur_max": 21,
+ "rim_k": 0.24,
+ "rim_max": 24,
+ "bend": -2,
+ "lens_curve": 0.4,
+ "poly": [
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0
+ ],
+ "opacity": 0.6,
+ "line": 0,
+ "line_lit": 0,
+ "line_width": 1,
+ "light_angle": -2.3,
+ "glow": 0,
+ "glow_width": 4,
+ "hairline": [
+ 0.86,
+ -0.12,
+ 0.92,
+ -0.08
+ ]
+ },
+ "clear-light": {
+ "blur_k": 0.19,
+ "blur_max": 21,
+ "rim_k": 0.24,
+ "rim_max": 24,
+ "bend": -2,
+ "lens_curve": 0.4,
+ "poly": [
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0
+ ],
+ "opacity": 0.6,
+ "line": 0,
+ "line_lit": 0,
+ "line_width": 1,
+ "light_angle": -2.3,
+ "glow": 0,
+ "glow_width": 4,
+ "hairline": [
+ 0.86,
+ -0.12,
+ 0.92,
+ -0.08
+ ]
+ },
+ "clear-dark": {
+ "blur_k": 0.19,
+ "blur_max": 21,
+ "rim_k": 0.24,
+ "rim_max": 24,
+ "bend": -2,
+ "lens_curve": 0.4,
+ "poly": [
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 1,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0,
+ 0.0
+ ],
+ "opacity": 0.6,
+ "line": 0,
+ "line_lit": 0,
+ "line_width": 1,
+ "light_angle": -2.3,
+ "glow": 0,
+ "glow_width": 4,
+ "hairline": [
+ 0.86,
+ -0.12,
+ 0.92,
+ -0.08
+ ]
+ }
+}
\ No newline at end of file
diff --git a/apps/desktop-gpui/glass/src/bin/glass-fit.rs b/apps/desktop-gpui/glass/src/bin/glass-fit.rs
new file mode 100644
index 000000000..e9ca249f1
--- /dev/null
+++ b/apps/desktop-gpui/glass/src/bin/glass-fit.rs
@@ -0,0 +1,520 @@
+//! Fits a `GlassMaterial` to Apple's reference captures.
+//!
+//! glass-fit <reference dir> <out dir> [style-appearance ...]
+//!
+//! Reads `<reference dir>/manifest.json`, `scenes/*.png` and `apple/*.png`
+//! (from scripts/glass-reference) and writes `<out dir>/params.json`,
+//! `<out dir>/report.json` and side-by-side renders. The colour transform is
+//! linear in its coefficients, so every evaluation solves it exactly by
+//! least squares; Nelder-Mead only searches blur, lensing and lighting.
+//! `GLASS_PARAMS=<params.json>` skips the search and renders those.
+
+use std::path::{Path, PathBuf};
+
+use cap_glass::{
+ Rgba, Shape, metrics,
+ model::{self, GlassMaterial, POLY_TERMS},
+};
+
+#[derive(serde::Deserialize)]
+struct Manifest {
+ shapes: Vec<ManifestShape>,
+ cases: Vec<Case>,
+}
+
+#[derive(serde::Deserialize)]
+struct ManifestShape {
+ rect: [f32; 4],
+ radius: f32,
+}
+
+#[derive(serde::Deserialize, Clone)]
+struct Case {
+ name: String,
+ scene: String,
+ style: String,
+ appearance: String,
+ tint: String,
+}
+
+struct Sample {
+ name: String,
+ backdrop: Rgba,
+ apple: Rgba,
+ /// One blurred crop per shape: (origin x, origin y, crop).
+ crops: Vec<(usize, usize, Rgba)>,
+}
+
+const PAD: f32 = 110.;
+
+fn main() {
+ let args: Vec<String> = std::env::args().collect();
+ let reference = PathBuf::from(&args[1]);
+ let out = PathBuf::from(&args[2]);
+ std::fs::create_dir_all(out.join("renders")).unwrap();
+ let manifest: Manifest =
+ serde_json::from_slice(&std::fs::read(reference.join("manifest.json")).unwrap()).unwrap();
+ let shapes: Vec<Shape> = manifest
+ .shapes
+ .iter()
+ .map(|shape| Shape {
+ x: shape.rect[0],
+ y: shape.rect[1],
+ width: shape.rect[2],
+ height: shape.rect[3],
+ radius: shape.radius,
+ })
+ .collect();
+ let presets: Vec<String> = if args.len() > 3 {
+ args[3..].to_vec()
+ } else {
+ ["regular-light", "regular-dark", "clear-light", "clear-dark"]
+ .map(String::from)
+ .to_vec()
+ };
+ let fixed: Option<serde_json::Value> = std::env::var("GLASS_PARAMS")
+ .ok()
+ .and_then(|path| serde_json::from_slice(&std::fs::read(path).ok()?).ok());
+
+ let mut fitted = serde_json::Map::new();
+ let mut report = serde_json::Map::new();
+ for preset in presets {
+ let mut samples: Vec<Sample> = manifest
+ .cases
+ .iter()
+ .filter(|case| {
+ format!("{}-{}", case.style, case.appearance) == preset
+ && case.tint == "none"
+ && case.scene != "uvfine"
+ })
+ .map(|case| Sample {
+ name: case.name.clone(),
+ backdrop: Rgba::load(&reference.join(format!("scenes/{}.png", case.scene))).unwrap(),
+ apple: Rgba::load(&reference.join(format!("apple/{}.png", case.name))).unwrap(),
+ crops: Vec::new(),
+ })
+ .collect();
+
+ let material = match fixed
+ .as_ref()
+ .and_then(|all| serde_json::from_value::<GlassMaterial>(all.get(&preset)?.clone()).ok())
+ {
+ Some(material) => material,
+ None => fit(&preset, &mut samples, &shapes),
+ };
+ eprintln!("{preset}:\n{material:#?}");
+
+ prepare(&mut samples, &shapes, &material);
+ let mask = region_mask(&shapes, &samples[0].backdrop);
+ let mut scores = serde_json::Map::new();
+ for sample in &samples {
+ let rendered = render_sample(&material, &shapes, sample);
+ let ssim = metrics::ssim(&rendered, &sample.apple, &mask);
+ let (mean, p95) = metrics::delta_e(&rendered, &sample.apple, &mask);
+ scores.insert(
+ sample.name.clone(),
+ serde_json::json!({ "ssim": ssim, "deltaE": mean, "deltaE95": p95 }),
+ );
+ side_by_side(&rendered, &sample.apple, &out.join(format!("renders/{}.png", sample.name)));
+ }
+ fitted.insert(preset.clone(), serde_json::to_value(material).unwrap());
+ report.insert(preset, serde_json::Value::Object(scores));
+ std::fs::write(out.join("params.json"), serde_json::to_string_pretty(&fitted).unwrap()).unwrap();
+ std::fs::write(out.join("report.json"), serde_json::to_string_pretty(&report).unwrap()).unwrap();
+ }
+}
+
+fn fit(preset: &str, samples: &mut [Sample], shapes: &[Shape]) -> GlassMaterial {
+ let start = GlassMaterial {
+ blur_k: 0.19,
+ blur_max: 21.,
+ rim_k: 0.24,
+ rim_max: 24.,
+ bend: -2.,
+ lens_curve: 0.4,
+ poly: [0.; 3 * POLY_TERMS],
+ opacity: 0.6,
+ line: 0.,
+ line_lit: 0.,
+ line_width: 1.,
+ light_angle: -2.3,
+ glow: 0.,
+ glow_width: 4.,
+ hairline: [0.85, -0.12, 0.9, -0.08],
+ };
+ let mut best: Option<(f64, GlassMaterial)> = None;
+ let mut try_law = |blur_k: f32, blur_max: f32, best: &mut Option<(f64, GlassMaterial)>| {
+ let seed = GlassMaterial {
+ blur_k,
+ blur_max,
+ ..best.map_or(start, |(_, material)| material)
+ };
+ let (loss, material) = fit_at_law(samples, shapes, seed);
+ eprintln!("{preset}: blur {blur_k:.3} x side, max {blur_max:.1} -> loss {loss:.2}");
+ if best.is_none_or(|(current, _)| loss < current) {
+ *best = Some((loss, material));
+ }
+ };
+ for blur_k in [0.06, 0.1, 0.14, 0.19, 0.25] {
+ try_law(blur_k, 60., &mut best);
+ }
+ let k = best.unwrap().1.blur_k;
+ for blur_max in [8., 12., 16., 20., 26., 34.] {
+ try_law(k, blur_max, &mut best);
+ }
+ for _ in 0..2 {
+ let law = best.unwrap().1;
+ for (dk, dm) in [(-0.02, 0.), (0.02, 0.), (0., -2.), (0., 2.)] {
+ try_law((law.blur_k + dk).max(0.01), (law.blur_max + dm).max(1.), &mut best);
+ }
+ }
+ best.unwrap().1
+}
+
+fn crop(image: &Rgba, x0: usize, y0: usize, x1: usize, y1: usize) -> Rgba {
+ let mut out = Rgba::solid(x1 - x0, y1 - y0, [0.; 4]);
+ for y in y0..y1 {
+ for x in x0..x1 {
+ out.set(x - x0, y - y0, image.get(x, y));
+ }
+ }
+ out
+}
+
+fn prepare(samples: &mut [Sample], shapes: &[Shape], material: &GlassMaterial) {
+ std::thread::scope(|scope| {
+ for sample in samples.iter_mut() {
+ scope.spawn(move || {
+ sample.crops = shapes
+ .iter()
+ .map(|shape| {
+ let blur = material.resolve(shape.width, shape.height).blur;
+ let pad = PAD + blur * 3.;
+ let x0 = (shape.x - pad).max(0.) as usize;
+ let y0 = (shape.y - pad).max(0.) as usize;
+ let x1 = ((shape.x + shape.width + pad) as usize).min(sample.backdrop.width);
+ let y1 = ((shape.y + shape.height + pad) as usize).min(sample.backdrop.height);
+ (x0, y0, crop(&sample.backdrop, x0, y0, x1, y1).blurred(blur))
+ })
+ .collect();
+ });
+ }
+ });
+}
+
+/// Visit glass pixels with their global position and weight. With `sparse`,
+/// pixels deeper than 12 px inside are visited on every other row and
+/// column at four times the weight: they carry far less than the rim does.
+fn for_each_glass_pixel(
+ material: &GlassMaterial,
+ shapes: &[Shape],
+ sample: &Sample,
+ sparse: bool,
+ mut visit: impl FnMut(&model::GlassSample, &model::PaneStyle, usize, usize, f64),
+) {
+ for (shape, (ox, oy, blurred)) in shapes.iter().zip(&sample.crops) {
+ let style = material.resolve(shape.width, shape.height);
+ let local = Shape {
+ x: shape.x - *ox as f32,
+ y: shape.y - *oy as f32,
+ ..*shape
+ };
+ let x0 = (local.x - 3.).floor().max(0.) as usize;
+ let y0 = (local.y - 3.).floor().max(0.) as usize;
+ let x1 = ((local.x + local.width + 3.).ceil() as usize).min(blurred.width);
+ let y1 = ((local.y + local.height + 3.).ceil() as usize).min(blurred.height);
+ for y in y0..y1 {
+ for x in x0..x1 {
+ let (px, py) = (x as f32 + 0.5, y as f32 + 0.5);
+ let weight = if sparse && local.sdf(px, py) < -12. {
+ if x % 2 != 0 || y % 2 != 0 {
+ continue;
+ }
+ 4.
+ } else {
+ 1.
+ };
+ let glass = model::sample(&style, &local, blurred, px, py);
+ if glass.coverage > 0. || glass.ring > 0. {
+ visit(&glass, &style, x + ox, y + oy, weight);
+ }
+ }
+ }
+ }
+}
+
+fn render_sample(material: &GlassMaterial, shapes: &[Shape], sample: &Sample) -> Rgba {
+ let mut out = sample.backdrop.clone();
+ for_each_glass_pixel(material, shapes, sample, false, |glass, style, x, y, _| {
+ let under = sample.backdrop.get(x, y);
+ let under =
+ model::under_with_hairline(glass, [under[0] / 255., under[1] / 255., under[2] / 255.]);
+ let color = if glass.coverage > 0. { model::finish(style, glass) } else { under };
+ let mix = |i: usize| (under[i] + (color[i].clamp(0., 1.) - under[i]) * glass.coverage) * 255.;
+ out.set(x, y, [mix(0), mix(1), mix(2), 255.]);
+ });
+ out
+}
+
+/// Normal equations of the colour transform over every covered pixel:
+/// `coverage * scale * features . poly = target - (1 - coverage) * under -
+/// coverage * glow`, per channel, plus what the residual needs.
+struct Normal {
+ ata: [[f64; POLY_TERMS]; POLY_TERMS],
+ atb: [[f64; 3]; POLY_TERMS],
+ btb: [f64; 3],
+ count: usize,
+ /// Squared error of hairline-only pixels, which the transform cannot
+ /// change.
+ fixed_sse: f64,
+ fixed_count: usize,
+}
+
+impl Normal {
+ fn new() -> Self {
+ Self {
+ ata: [[0.; POLY_TERMS]; POLY_TERMS],
+ atb: [[0.; 3]; POLY_TERMS],
+ btb: [0.; 3],
+ count: 0,
+ fixed_sse: 0.,
+ fixed_count: 0,
+ }
+ }
+
+ fn add(&mut self, other: &Self) {
+ for i in 0..POLY_TERMS {
+ for j in 0..POLY_TERMS {
+ self.ata[i][j] += other.ata[i][j];
+ }
+ for c in 0..3 {
+ self.atb[i][c] += other.atb[i][c];
+ }
+ }
+ for c in 0..3 {
+ self.btb[c] += other.btb[c];
+ }
+ self.count += other.count;
+ self.fixed_sse += other.fixed_sse;
+ self.fixed_count += other.fixed_count;
+ }
+
+ /// Least-squares coefficients and the mean squared error per channel
+ /// value on the 0-255 scale.
+ fn solve(&self) -> ([f32; 3 * POLY_TERMS], f64) {
+ let mut a = self.ata;
+ for (i, row) in a.iter_mut().enumerate() {
+ row[i] += 1e-6;
+ }
+ let mut poly = [0f32; 3 * POLY_TERMS];
+ let mut sse = 0.;
+ for c in 0..3 {
+ let b: [f64; POLY_TERMS] = std::array::from_fn(|i| self.atb[i][c]);
+ let x = solve_linear(a, b);
+ let mut xax = 0.;
+ let mut xb = 0.;
+ for i in 0..POLY_TERMS {
+ poly[c * POLY_TERMS + i] = x[i] as f32;
+ xb += x[i] * b[i];
+ for j in 0..POLY_TERMS {
+ xax += x[i] * self.ata[i][j] * x[j];
+ }
+ }
+ sse += self.btb[c] - 2. * xb + xax;
+ }
+ let total = sse.max(0.) + self.fixed_sse;
+ let values = 3 * (self.count + self.fixed_count).max(1);
+ (poly, total / values as f64 * 255. * 255.)
+ }
+}
+
+fn solve_linear(
+ mut m: [[f64; POLY_TERMS]; POLY_TERMS],
+ mut v: [f64; POLY_TERMS],
+) -> [f64; POLY_TERMS] {
+ for col in 0..POLY_TERMS {
+ let pivot = (col..POLY_TERMS)
+ .max_by(|a, b| m[*a][col].abs().total_cmp(&m[*b][col].abs()))
+ .unwrap();
+ m.swap(col, pivot);
+ v.swap(col, pivot);
+ for row in 0..POLY_TERMS {
+ if row != col && m[col][col] != 0. {
+ let factor = m[row][col] / m[col][col];
+ for k in 0..POLY_TERMS {
+ m[row][k] -= factor * m[col][k];
+ }
+ v[row] -= factor * v[col];
+ }
+ }
+ }
+ std::array::from_fn(|i| if m[i][i] == 0. { 0. } else { v[i] / m[i][i] })
+}
+
+fn normal_equations(material: &GlassMaterial, shapes: &[Shape], samples: &[Sample]) -> Normal {
+ std::thread::scope(|scope| {
+ let handles: Vec<_> = samples
+ .iter()
+ .map(|sample| {
+ scope.spawn(move || {
+ let mut normal = Normal::new();
+ for_each_glass_pixel(material, shapes, sample, true, |glass, _, x, y, weight| {
+ let under = sample.backdrop.get(x, y);
+ let under = model::under_with_hairline(
+ glass,
+ [under[0] / 255., under[1] / 255., under[2] / 255.],
+ );
+ let target = sample.apple.get(x, y);
+ if glass.coverage <= 0. {
+ for c in 0..3 {
+ let error = under[c] as f64 - target[c] as f64 / 255.;
+ normal.fixed_sse += weight * error * error;
+ }
+ normal.fixed_count += weight as usize;
+ return;
+ }
+ let features = model::poly_features(glass.source);
+ let weight = (glass.coverage * glass.scale) as f64;
+ let row: [f64; POLY_TERMS] =
+ std::array::from_fn(|i| features[i] as f64 * weight);
+ for i in 0..POLY_TERMS {
+ for j in i..POLY_TERMS {
+ normal.ata[i][j] += weight * row[i] * row[j];
+ }
+ }
+ let coverage = glass.coverage as f64;
+ for c in 0..3 {
+ let b = target[c] as f64 / 255.
+ - (1. - coverage) * under[c] as f64
+ - coverage * glass.glow as f64;
+ for i in 0..POLY_TERMS {
+ normal.atb[i][c] += weight * row[i] * b;
+ }
+ normal.btb[c] += weight * b * b;
+ }
+ normal.count += weight as usize;
+ });
+ for i in 0..POLY_TERMS {
+ for j in 0..i {
+ normal.ata[i][j] = normal.ata[j][i];
+ }
+ }
+ normal
+ })
+ })
+ .collect();
+ let mut total = Normal::new();
+ for handle in handles {
+ total.add(&handle.join().unwrap());
+ }
+ total
+ })
+}
+
+fn evaluate(material: &GlassMaterial, shapes: &[Shape], samples: &[Sample]) -> (f64, GlassMaterial) {
+ let (poly, loss) = normal_equations(material, shapes, samples).solve();
+ (loss, GlassMaterial { poly, ..*material })
+}
+
+/// Fit everything but the blur law, which stays fixed so each backdrop is
+/// blurred once.
+fn fit_at_law(
+ samples: &mut [Sample],
+ shapes: &[Shape],
+ start: GlassMaterial,
+) -> (f64, GlassMaterial) {
+ prepare(samples, shapes, &start);
+ let samples: &[Sample] = samples;
+ let objective = |values: &[f32]| evaluate(&start.with_shape(values), shapes, samples).0;
+ let scales = [
+ 0., 0., 0.05, 6., 0.6, 0.2, 0.04, 0.04, 0.5, 0.8, 0.04, 3., 0.08, 0.05, 0.08, 0.05,
+ ];
+ let mut point = start.shape_vec().to_vec();
+ let mut value = objective(&point);
+ for _ in 0..4 {
+ let (next_value, next) = nelder_mead(&objective, &point, &scales, 160);
+ if next_value >= value - 1e-3 {
+ break;
+ }
+ value = next_value;
+ point = next;
+ }
+ evaluate(&start.with_shape(&point), shapes, samples)
+}
+
+fn region_mask(shapes: &[Shape], image: &Rgba) -> Vec<bool> {
+ (0..image.width * image.height)
+ .map(|index| {
+ let (x, y) = ((index % image.width) as f32 + 0.5, (index / image.width) as f32 + 0.5);
+ shapes.iter().any(|shape| shape.sdf(x, y) < 1.5)
+ })
+ .collect()
+}
+
+fn nelder_mead(
+ objective: &impl Fn(&[f32]) -> f64,
+ start: &[f32],
+ scales: &[f32],
+ iterations: usize,
+) -> (f64, Vec<f32>) {
+ let dims: Vec<usize> = (0..start.len()).filter(|i| scales[*i] != 0.).collect();
+ let mut simplex: Vec<(f64, Vec<f32>)> = vec![(objective(start), start.to_vec())];
+ for &dim in &dims {
+ let mut vertex = start.to_vec();
+ vertex[dim] += scales[dim];
+ simplex.push((objective(&vertex), vertex));
+ }
+ for _ in 0..iterations {
+ simplex.sort_by(|a, b| a.0.total_cmp(&b.0));
+ let worst = simplex.len() - 1;
+ let centroid: Vec<f32> = (0..start.len())
+ .map(|i| simplex[..worst].iter().map(|(_, v)| v[i]).sum::<f32>() / worst as f32)
+ .collect();
+ let along = |t: f32| -> Vec<f32> {
+ centroid
+ .iter()
+ .zip(&simplex[worst].1)
+ .map(|(c, w)| c + t * (c - w))
+ .collect()
+ };
+ let reflected = along(1.);
+ let reflected_value = objective(&reflected);
+ if reflected_value < simplex[0].0 {
+ let expanded = along(2.);
+ let expanded_value = objective(&expanded);
+ simplex[worst] = if expanded_value < reflected_value {
+ (expanded_value, expanded)
+ } else {
+ (reflected_value, reflected)
+ };
+ } else if reflected_value < simplex[worst - 1].0 {
+ simplex[worst] = (reflected_value, reflected);
+ } else {
+ let contracted = along(-0.5);
+ let contracted_value = objective(&contracted);
+ if contracted_value < simplex[worst].0 {
+ simplex[worst] = (contracted_value, contracted);
+ } else {
+ let best = simplex[0].1.clone();
+ for vertex in simplex.iter_mut().skip(1) {
+ let shrunk: Vec<f32> =
+ best.iter().zip(&vertex.1).map(|(b, v)| b + 0.5 * (v - b)).collect();
+ *vertex = (objective(&shrunk), shrunk);
+ }
+ }
+ }
+ }
+ simplex.sort_by(|a, b| a.0.total_cmp(&b.0));
+ simplex.swap_remove(0)
+}
+
+fn side_by_side(ours: &Rgba, apple: &Rgba, path: &Path) {
+ let mut sheet = Rgba::solid(ours.width * 2, ours.height, [255.; 4]);
+ for y in 0..ours.height {
+ for x in 0..ours.width {
+ sheet.set(x, y, apple.get(x, y));
+ sheet.set(x + ours.width, y, ours.get(x, y));
+ }
+ }
+ sheet.save(path).unwrap();
+}
diff --git a/apps/desktop-gpui/glass/src/lib.rs b/apps/desktop-gpui/glass/src/lib.rs
new file mode 100644
index 000000000..b8e5edd8e
--- /dev/null
+++ b/apps/desktop-gpui/glass/src/lib.rs
@@ -0,0 +1,147 @@
+//! Cap Glass: the glass material's math, shared by the GPU shaders (Metal,
+//! HLSL, WGSL in the gpui patch) and the CPU reference renderer that the
+//! comparison harness fits against Apple's own glass.
+
+pub mod metrics;
+pub mod model;
+pub mod scenes;
+
+use std::path::Path;
+
+/// The fitted materials, keyed `regular-light`, `regular-dark`,
+/// `clear-light` and `clear-dark` (see `glass-fit`).
+pub const PRESETS_JSON: &str = include_str!("../presets.json");
+
+/// An RGBA image with 0-255 float channels, non-premultiplied.
+#[derive(Clone)]
+pub struct Rgba {
+ pub width: usize,
+ pub height: usize,
+ pub pixels: Vec<f32>,
+}
+
+impl Rgba {
+ pub fn solid(width: usize, height: usize, color: [f32; 4]) -> Self {
+ Self {
+ width,
+ height,
+ pixels: color.repeat(width * height),
+ }
+ }
+
+ pub fn load(path: &Path) -> image::ImageResult<Self> {
+ let image = image::open(path)?.to_rgba8();
+ Ok(Self {
+ width: image.width() as usize,
+ height: image.height() as usize,
+ pixels: image.as_raw().iter().map(|value| *value as f32).collect(),
+ })
+ }
+
+ pub fn save(&self, path: &Path) -> image::ImageResult<()> {
+ let bytes: Vec<u8> = self
+ .pixels
+ .iter()
+ .map(|value| value.round().clamp(0., 255.) as u8)
+ .collect();
+ image::RgbaImage::from_raw(self.width as u32, self.height as u32, bytes)
+ .expect("pixel buffer matches its size")
+ .save(path)
+ }
+
+ pub fn get(&self, x: usize, y: usize) -> [f32; 4] {
+ let index = (y * self.width + x) * 4;
+ [
+ self.pixels[index],
+ self.pixels[index + 1],
+ self.pixels[index + 2],
+ self.pixels[index + 3],
+ ]
+ }
+
+ pub fn set(&mut self, x: usize, y: usize, color: [f32; 4]) {
+ let index = (y * self.width + x) * 4;
+ self.pixels[index..index + 4].copy_from_slice(&color);
+ }
+
+ /// Bilinear sample with clamped edges, in pixel coordinates (pixel
+ /// centres at +0.5).
+ pub fn sample(&self, x: f32, y: f32) -> [f32; 4] {
+ let fx = (x - 0.5).clamp(0., (self.width - 1) as f32);
+ let fy = (y - 0.5).clamp(0., (self.height - 1) as f32);
+ let (x0, y0) = (fx.floor() as usize, fy.floor() as usize);
+ let (x1, y1) = ((x0 + 1).min(self.width - 1), (y0 + 1).min(self.height - 1));
+ let (tx, ty) = (fx - x0 as f32, fy - y0 as f32);
+ let (a, b, c, d) = (self.get(x0, y0), self.get(x1, y0), self.get(x0, y1), self.get(x1, y1));
+ std::array::from_fn(|i| {
+ let top = a[i] + (b[i] - a[i]) * tx;
+ let bottom = c[i] + (d[i] - c[i]) * tx;
+ top + (bottom - top) * ty
+ })
+ }
+
+ /// Gaussian blur of every channel with clamped edges.
+ pub fn blurred(&self, sigma: f32) -> Self {
+ if sigma < 0.5 {
+ return self.clone();
+ }
+ let radius = (sigma * 3.).ceil() as usize;
+ let kernel = metrics::gaussian_kernel(sigma as f64, radius);
+ let mut out = self.clone();
+ for channel in 0..4 {
+ let values: Vec<f64> = self
+ .pixels
+ .iter()
+ .skip(channel)
+ .step_by(4)
+ .map(|value| *value as f64)
+ .collect();
+ let blurred = metrics::separable_blur(&values, self.width, self.height, &kernel);
+ for (index, value) in blurred.into_iter().enumerate() {
+ out.pixels[index * 4 + channel] = value as f32;
+ }
+ }
+ out
+ }
+}
+
+/// A rounded rectangle in pixels, top-left origin.
+#[derive(Clone, Copy, Debug, serde::Deserialize, serde::Serialize)]
+pub struct Shape {
+ pub x: f32,
+ pub y: f32,
+ pub width: f32,
+ pub height: f32,
+ pub radius: f32,
+}
+
+impl Shape {
+ /// Signed distance to the rounded rectangle's edge, negative inside.
+ pub fn sdf(&self, px: f32, py: f32) -> f32 {
+ let radius = self.radius.min(self.width / 2.).min(self.height / 2.);
+ let cx = px - (self.x + self.width / 2.);
+ let cy = py - (self.y + self.height / 2.);
+ let qx = cx.abs() - (self.width / 2. - radius);
+ let qy = cy.abs() - (self.height / 2. - radius);
+ let outside = (qx.max(0.).powi(2) + qy.max(0.).powi(2)).sqrt();
+ outside + qx.max(qy).min(0.) - radius
+ }
+
+ /// Unit outward normal of the distance field.
+ pub fn normal(&self, px: f32, py: f32) -> (f32, f32) {
+ let e = 0.5;
+ let dx = self.sdf(px + e, py) - self.sdf(px - e, py);
+ let dy = self.sdf(px, py + e) - self.sdf(px, py - e);
+ let length = (dx * dx + dy * dy).sqrt().max(1e-6);
+ (dx / length, dy / length)
+ }
+
+ pub fn mask(&self, width: usize, height: usize, inset: f32) -> Vec<bool> {
+ (0..width * height)
+ .map(|index| {
+ let (x, y) = ((index % width) as f32 + 0.5, (index / width) as f32 + 0.5);
+ self.sdf(x, y) < -inset
+ })
+ .collect()
+ }
+}
diff --git a/apps/desktop-gpui/glass/src/metrics.rs b/apps/desktop-gpui/glass/src/metrics.rs
new file mode 100644
index 000000000..45cb04b4d
--- /dev/null
+++ b/apps/desktop-gpui/glass/src/metrics.rs
@@ -0,0 +1,192 @@
+use crate::Rgba;
+
+/// Mean SSIM over the masked pixels, computed on Rec. 709 luma with the usual
+/// 11x11, sigma 1.5 gaussian window.
+pub fn ssim(a: &Rgba, b: &Rgba, mask: &[bool]) -> f64 {
+ assert_eq!((a.width, a.height), (b.width, b.height));
+ let (width, height) = (a.width, a.height);
+ let luma = |image: &Rgba| -> Vec<f64> {
+ image
+ .pixels
+ .chunks_exact(4)
+ .map(|px| 0.2126 * px[0] as f64 + 0.7152 * px[1] as f64 + 0.0722 * px[2] as f64)
+ .collect()
+ };
+ let (x, y) = (luma(a), luma(b));
+ let kernel = gaussian_kernel(1.5, 5);
+ let blur = |values: &[f64]| separable_blur(values, width, height, &kernel);
+ let xy: Vec<f64> = x.iter().zip(&y).map(|(x, y)| x * y).collect();
+ let xx: Vec<f64> = x.iter().map(|x| x * x).collect();
+ let yy: Vec<f64> = y.iter().map(|y| y * y).collect();
+ let (mu_x, mu_y) = (blur(&x), blur(&y));
+ let (e_xx, e_yy, e_xy) = (blur(&xx), blur(&yy), blur(&xy));
+ let (c1, c2) = ((0.01f64 * 255.).powi(2), (0.03f64 * 255.).powi(2));
+ let mut total = 0.;
+ let mut count = 0usize;
+ for index in 0..width * height {
+ if !mask[index] {
+ continue;
+ }
+ let (mx, my) = (mu_x[index], mu_y[index]);
+ let vx = e_xx[index] - mx * mx;
+ let vy = e_yy[index] - my * my;
+ let cov = e_xy[index] - mx * my;
+ total += ((2. * mx * my + c1) * (2. * cov + c2)) / ((mx * mx + my * my + c1) * (vx + vy + c2));
+ count += 1;
+ }
+ if count == 0 { 1. } else { total / count as f64 }
+}
+
+/// CIEDE2000 colour difference per masked pixel, returned as (mean, p95).
+pub fn delta_e(a: &Rgba, b: &Rgba, mask: &[bool]) -> (f64, f64) {
+ let mut values: Vec<f64> = a
+ .pixels
+ .chunks_exact(4)
+ .zip(b.pixels.chunks_exact(4))
+ .zip(mask)
+ .filter(|(_, keep)| **keep)
+ .map(|((p, q), _)| ciede2000(srgb_to_lab(p), srgb_to_lab(q)))
+ .collect();
+ if values.is_empty() {
+ return (0., 0.);
+ }
+ values.sort_by(f64::total_cmp);
+ let mean = values.iter().sum::<f64>() / values.len() as f64;
+ let p95 = values[((values.len() - 1) as f64 * 0.95).round() as usize];
+ (mean, p95)
+}
+
+pub fn gaussian_kernel(sigma: f64, radius: usize) -> Vec<f64> {
+ let mut kernel: Vec<f64> = (0..=2 * radius)
+ .map(|i| {
+ let x = i as f64 - radius as f64;
+ (-(x * x) / (2. * sigma * sigma)).exp()
+ })
+ .collect();
+ let sum: f64 = kernel.iter().sum();
+ kernel.iter_mut().for_each(|value| *value /= sum);
+ kernel
+}
+
+pub fn separable_blur(values: &[f64], width: usize, height: usize, kernel: &[f64]) -> Vec<f64> {
+ let radius = kernel.len() / 2;
+ let mut horizontal = vec![0.; values.len()];
+ for row in 0..height {
+ for col in 0..width {
+ let mut sum = 0.;
+ for (k, weight) in kernel.iter().enumerate() {
+ let x = (col + k).saturating_sub(radius).min(width - 1);
+ sum += weight * values[row * width + x];
+ }
+ horizontal[row * width + col] = sum;
+ }
+ }
+ let mut out = vec![0.; values.len()];
+ for row in 0..height {
+ for col in 0..width {
+ let mut sum = 0.;
+ for (k, weight) in kernel.iter().enumerate() {
+ let y = (row + k).saturating_sub(radius).min(height - 1);
+ sum += weight * horizontal[y * width + col];
+ }
+ out[row * width + col] = sum;
+ }
+ }
+ out
+}
+
+fn srgb_to_lab(px: &[f32]) -> [f64; 3] {
+ let linear = |c: f32| {
+ let c = (c as f64 / 255.).clamp(0., 1.);
+ if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) }
+ };
+ let (r, g, b) = (linear(px[0]), linear(px[1]), linear(px[2]));
+ let x = (0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / 0.95047;
+ let y = 0.2126729 * r + 0.7151522 * g + 0.0721750 * b;
+ let z = (0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / 1.08883;
+ let f = |t: f64| {
+ if t > 216. / 24389. { t.cbrt() } else { (24389. / 27. * t + 16.) / 116. }
+ };
+ let (fx, fy, fz) = (f(x), f(y), f(z));
+ [116. * fy - 16., 500. * (fx - fy), 200. * (fy - fz)]
+}
+
+fn ciede2000(lab1: [f64; 3], lab2: [f64; 3]) -> f64 {
+ let [l1, a1, b1] = lab1;
+ let [l2, a2, b2] = lab2;
+ let c1 = (a1 * a1 + b1 * b1).sqrt();
+ let c2 = (a2 * a2 + b2 * b2).sqrt();
+ let c_bar = (c1 + c2) / 2.;
+ let g = 0.5 * (1. - (c_bar.powi(7) / (c_bar.powi(7) + 25f64.powi(7))).sqrt());
+ let (a1p, a2p) = ((1. + g) * a1, (1. + g) * a2);
+ let (c1p, c2p) = ((a1p * a1p + b1 * b1).sqrt(), (a2p * a2p + b2 * b2).sqrt());
+ let hue = |b: f64, a: f64| {
+ if a == 0. && b == 0. {
+ 0.
+ } else {
+ let h = b.atan2(a).to_degrees();
+ if h < 0. { h + 360. } else { h }
+ }
+ };
+ let (h1p, h2p) = (hue(b1, a1p), hue(b2, a2p));
+ let dl = l2 - l1;
+ let dc = c2p - c1p;
+ let dh_angle = if c1p * c2p == 0. {
+ 0.
+ } else if (h2p - h1p).abs() <= 180. {
+ h2p - h1p
+ } else if h2p - h1p > 180. {
+ h2p - h1p - 360.
+ } else {
+ h2p - h1p + 360.
+ };
+ let dh = 2. * (c1p * c2p).sqrt() * (dh_angle.to_radians() / 2.).sin();
+ let l_bar = (l1 + l2) / 2.;
+ let cp_bar = (c1p + c2p) / 2.;
+ let hp_bar = if c1p * c2p == 0. {
+ h1p + h2p
+ } else if (h1p - h2p).abs() <= 180. {
+ (h1p + h2p) / 2.
+ } else if h1p + h2p < 360. {
+ (h1p + h2p + 360.) / 2.
+ } else {
+ (h1p + h2p - 360.) / 2.
+ };
+ let t = 1. - 0.17 * (hp_bar - 30.).to_radians().cos()
+ + 0.24 * (2. * hp_bar).to_radians().cos()
+ + 0.32 * (3. * hp_bar + 6.).to_radians().cos()
+ - 0.20 * (4. * hp_bar - 63.).to_radians().cos();
+ let d_theta = 30. * (-((hp_bar - 275.) / 25.).powi(2)).exp();
+ let rc = 2. * (cp_bar.powi(7) / (cp_bar.powi(7) + 25f64.powi(7))).sqrt();
+ let sl = 1. + (0.015 * (l_bar - 50.).powi(2)) / (20. + (l_bar - 50.).powi(2)).sqrt();
+ let sc = 1. + 0.045 * cp_bar;
+ let sh = 1. + 0.015 * cp_bar * t;
+ let rt = -(2. * d_theta).to_radians().sin() * rc;
+ ((dl / sl).powi(2) + (dc / sc).powi(2) + (dh / sh).powi(2) + rt * (dc / sc) * (dh / sh)).sqrt()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn ciede2000_matches_published_pairs() {
+ let pairs = [
+ ([50.0, 2.6772, -79.7751], [50.0, 0.0, -82.7485], 2.0425),
+ ([50.0, 3.1571, -77.2803], [50.0, 0.0, -82.7485], 2.8615),
+ ([50.0, 2.5, 0.0], [73.0, 25.0, -18.0], 27.1492),
+ ([60.2574, -34.0099, 36.2677], [60.4626, -34.1751, 39.4387], 1.2644),
+ ];
+ for (a, b, expected) in pairs {
+ assert!((ciede2000(a, b) - expected).abs() < 1e-3, "{a:?} {b:?}");
+ }
+ }
+
+ #[test]
+ fn identical_images_score_perfectly() {
+ let image = Rgba::solid(16, 16, [120., 40., 200., 255.]);
+ let mask = vec![true; 256];
+ assert!((ssim(&image, &image, &mask) - 1.).abs() < 1e-9);
+ assert_eq!(delta_e(&image, &image, &mask), (0., 0.));
+ }
+}
diff --git a/apps/desktop-gpui/glass/src/model.rs b/apps/desktop-gpui/glass/src/model.rs
new file mode 100644
index 000000000..9f1f7fabc
--- /dev/null
+++ b/apps/desktop-gpui/glass/src/model.rs
@@ -0,0 +1,265 @@
+use crate::{Rgba, Shape};
+
+/// Terms of the colour transform, per output channel:
+/// `r, g, b, r², g², b², rg, gb, rb, 1` of the blurred backdrop (0-1 sRGB).
+pub const POLY_TERMS: usize = 10;
+
+pub fn poly_features(c: [f32; 3]) -> [f32; POLY_TERMS] {
+ let [r, g, b] = c;
+ [r, g, b, r * r, g * g, b * b, r * g, g * b, r * b, 1.]
+}
+
+/// A glass material, fitted against Apple's glass. Blur and lensing scale
+/// with the pane's shorter side, the way Apple's do; [`GlassMaterial::resolve`]
+/// turns them into the absolute [`PaneStyle`] the renderers draw with.
+#[derive(Clone, Copy, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
+pub struct GlassMaterial {
+ /// Blur sigma: `min(blur_max, blur_k * shorter side)`.
+ pub blur_k: f32,
+ pub blur_max: f32,
+ /// Width of the lensing rim: `min(rim_max, rim_k * shorter side)`.
+ pub rim_k: f32,
+ pub rim_max: f32,
+ /// How far the rim bends light along the normal, in rim widths
+ /// (negative pulls from inside the pane).
+ pub bend: f32,
+ /// Exponent of the bend's falloff across the rim.
+ pub lens_curve: f32,
+ /// Second-order colour transform of the blurred backdrop: for each output
+ /// channel, coefficients of [`poly_features`]. It carries the frost, the
+ /// saturation, the hue shift and the brightness compression Apple's
+ /// materials apply.
+ #[serde(with = "serde_poly")]
+ pub poly: [f32; 3 * POLY_TERMS],
+ /// How opaque the glass is over a transparent backdrop.
+ pub opacity: f32,
+ /// Edge line: scales the colour by `1 + line + line_lit * facing` at the
+ /// edge, decaying inwards over `line_width`; `facing` is how much the
+ /// edge faces `light_angle` (radians, y down).
+ pub line: f32,
+ pub line_lit: f32,
+ pub line_width: f32,
+ pub light_angle: f32,
+ /// A soft inner glow from the edge.
+ pub glow: f32,
+ pub glow_width: f32,
+ /// The hairline Apple draws in the pixel just outside the edge:
+ /// `clamp(min(under, a * under + b), 0, 1)`, with `a` and `b` blended
+ /// between vertical edges (`[a, b]` first) and horizontal ones by the
+ /// edge normal.
+ pub hairline: [f32; 4],
+}
+
+mod serde_poly {
+ use super::POLY_TERMS;
+ use serde::{Deserialize, Deserializer, Serializer};
+
+ pub fn serialize<S: Serializer>(poly: &[f32; 3 * POLY_TERMS], s: S) -> Result<S::Ok, S::Error> {
+ s.collect_seq(poly.iter())
+ }
+
+ pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<[f32; 3 * POLY_TERMS], D::Error> {
+ let values = Vec::<f32>::deserialize(d)?;
+ values
+ .try_into()
+ .map_err(|_| serde::de::Error::custom("poly needs 30 coefficients"))
+ }
+}
+
+/// One pane's resolved style, in logical pixels. Mirrored field for field by
+/// `GlassStyle` in the gpui patch's shaders (which take device pixels).
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct PaneStyle {
+ pub blur: f32,
+ pub lens_rim: f32,
+ pub lens_curve: f32,
+ pub rim_bend: f32,
+ pub poly: [f32; 3 * POLY_TERMS],
+ pub opacity: f32,
+ pub line: f32,
+ pub line_lit: f32,
+ pub line_width: f32,
+ pub light: [f32; 2],
+ pub glow: f32,
+ pub glow_width: f32,
+ pub hairline: [f32; 4],
+}
+
+impl PaneStyle {
+ /// The same style for a window drawing `scale` device pixels per
+ /// logical pixel.
+ pub fn scaled(&self, scale: f32) -> Self {
+ Self {
+ blur: self.blur * scale,
+ lens_rim: self.lens_rim * scale,
+ rim_bend: self.rim_bend * scale,
+ line_width: self.line_width * scale,
+ glow_width: self.glow_width * scale,
+ ..*self
+ }
+ }
+}
+
+impl GlassMaterial {
+ /// Parameters the optimiser searches; the colour transform is solved.
+ pub const SHAPE_FIELDS: usize = 16;
+
+ pub fn resolve(&self, width: f32, height: f32) -> PaneStyle {
+ let shorter = width.min(height).max(1.);
+ let lens_rim = (self.rim_k * shorter).min(self.rim_max).max(0.5);
+ PaneStyle {
+ blur: (self.blur_k * shorter).min(self.blur_max).max(0.),
+ lens_rim,
+ lens_curve: self.lens_curve,
+ rim_bend: self.bend * lens_rim,
+ poly: self.poly,
+ opacity: self.opacity,
+ line: self.line,
+ line_lit: self.line_lit,
+ line_width: self.line_width,
+ light: [self.light_angle.cos(), self.light_angle.sin()],
+ glow: self.glow,
+ glow_width: self.glow_width,
+ hairline: self.hairline,
+ }
+ }
+
+ pub fn shape_vec(&self) -> [f32; Self::SHAPE_FIELDS] {
+ [
+ self.blur_k,
+ self.blur_max,
+ self.rim_k,
+ self.rim_max,
+ self.bend,
+ self.lens_curve,
+ self.line,
+ self.line_lit,
+ self.line_width,
+ self.light_angle,
+ self.glow,
+ self.glow_width,
+ self.hairline[0],
+ self.hairline[1],
+ self.hairline[2],
+ self.hairline[3],
+ ]
+ }
+
+ pub fn with_shape(&self, values: &[f32]) -> Self {
+ Self {
+ blur_k: values[0].max(0.),
+ blur_max: values[1].max(0.),
+ rim_k: values[2].max(0.),
+ rim_max: values[3].max(0.5),
+ bend: values[4],
+ lens_curve: values[5].clamp(0.2, 6.),
+ line: values[6],
+ line_lit: values[7],
+ line_width: values[8].max(0.3),
+ light_angle: values[9],
+ glow: values[10],
+ glow_width: values[11].max(0.5),
+ hairline: [values[12], values[13], values[14], values[15]],
+ ..*self
+ }
+ }
+}
+
+/// The blurred backdrop colour a glass pixel samples (0-1), before the
+/// colour transform, plus everything applied around it.
+pub struct GlassSample {
+ pub source: [f32; 3],
+ /// How much of the pixel the glass covers.
+ pub coverage: f32,
+ /// How much of the pixel the hairline ring outside the edge covers.
+ pub ring: f32,
+ /// The hairline's `a` and `b` at this point of the edge.
+ pub hairline: [f32; 2],
+ /// `1 + line term`.
+ pub scale: f32,
+ pub glow: f32,
+}
+
+pub fn sample(style: &PaneStyle, shape: &Shape, blurred: &Rgba, px: f32, py: f32) -> GlassSample {
+ let d = shape.sdf(px, py);
+ let coverage = (0.5 - d).clamp(0., 1.);
+ let ring = (1.5 - d).clamp(0., 1.) - coverage;
+ let (nx, ny) = shape.normal(px, py);
+ let vertical = nx * nx;
+ let hairline = [
+ style.hairline[0] * vertical + style.hairline[2] * (1. - vertical),
+ style.hairline[1] * vertical + style.hairline[3] * (1. - vertical),
+ ];
+ if coverage <= 0. {
+ return GlassSample {
+ source: [0.; 3],
+ coverage: 0.,
+ ring,
+ hairline,
+ scale: 1.,
+ glow: 0.,
+ };
+ }
+ let edge = (-d).max(0.);
+ let rim = 1. - (edge / style.lens_rim).clamp(0., 1.).powf(style.lens_curve);
+ let value = blurred.sample(px + nx * style.rim_bend * rim, py + ny * style.rim_bend * rim);
+ let facing = nx * style.light[0] + ny * style.light[1];
+ GlassSample {
+ source: [value[0] / 255., value[1] / 255., value[2] / 255.],
+ coverage,
+ ring,
+ hairline,
+ scale: 1. + (-edge / style.line_width).exp() * (style.line + style.line_lit * facing),
+ glow: (-edge / style.glow_width).exp() * style.glow,
+ }
+}
+
+/// The backdrop under a glass pixel once the hairline is drawn on it (0-1).
+pub fn under_with_hairline(sample: &GlassSample, under: [f32; 3]) -> [f32; 3] {
+ std::array::from_fn(|i| {
+ let line = under[i].min(sample.hairline[0] * under[i] + sample.hairline[1]).clamp(0., 1.);
+ under[i] + (line - under[i]) * sample.ring
+ })
+}
+
+/// Apply the colour transform and lighting: 0-1 sRGB out, unclamped.
+pub fn finish(style: &PaneStyle, sample: &GlassSample) -> [f32; 3] {
+ let features = poly_features(sample.source);
+ std::array::from_fn(|channel| {
+ let coefficients = &style.poly[channel * POLY_TERMS..(channel + 1) * POLY_TERMS];
+ let value: f32 = coefficients.iter().zip(features).map(|(c, f)| c * f).sum();
+ value * sample.scale + sample.glow
+ })
+}
+
+/// Composite one pane over `out`, sampling `blurred` (the backdrop blurred
+/// by the pane's own blur).
+pub fn composite(style: &PaneStyle, shape: &Shape, blurred: &Rgba, out: &mut Rgba) {
+ let x0 = (shape.x - 3.).floor().max(0.) as usize;
+ let y0 = (shape.y - 3.).floor().max(0.) as usize;
+ let x1 = ((shape.x + shape.width + 3.).ceil() as usize).min(out.width);
+ let y1 = ((shape.y + shape.height + 3.).ceil() as usize).min(out.height);
+ for y in y0..y1 {
+ for x in x0..x1 {
+ let glass = sample(style, shape, blurred, x as f32 + 0.5, y as f32 + 0.5);
+ if glass.coverage <= 0. && glass.ring <= 0. {
+ continue;
+ }
+ let under = out.get(x, y);
+ let under = under_with_hairline(&glass, [under[0] / 255., under[1] / 255., under[2] / 255.]);
+ let color = if glass.coverage > 0. { finish(style, &glass) } else { under };
+ let mix = |i: usize| (under[i] + (color[i].clamp(0., 1.) - under[i]) * glass.coverage) * 255.;
+ out.set(x, y, [mix(0), mix(1), mix(2), 255.]);
+ }
+ }
+}
+
+/// Composite every pane of `material` over `backdrop`.
+pub fn render(material: &GlassMaterial, shapes: &[Shape], backdrop: &Rgba) -> Rgba {
+ let mut out = backdrop.clone();
+ for shape in shapes {
+ let style = material.resolve(shape.width, shape.height);
+ composite(&style, shape, &backdrop.blurred(style.blur), &mut out);
+ }
+ out
+}
diff --git a/apps/desktop-gpui/glass/src/scenes.rs b/apps/desktop-gpui/glass/src/scenes.rs
new file mode 100644
index 000000000..d88a2ef72
--- /dev/null
+++ b/apps/desktop-gpui/glass/src/scenes.rs
@@ -0,0 +1,117 @@
+use crate::{Rgba, Shape};
+
+/// Logical size of every scene; the probe window and the reference capture
+/// share it.
+pub const CANVAS: (f32, f32) = (960., 600.);
+
+/// The panes the probe and the reference capture lay over each scene, in
+/// logical pixels with a top-left origin.
+pub const SHAPES: [Shape; 5] = [
+ Shape {
+ x: 60.,
+ y: 60.,
+ width: 320.,
+ height: 120.,
+ radius: 60.,
+ },
+ Shape {
+ x: 460.,
+ y: 60.,
+ width: 420.,
+ height: 240.,
+ radius: 24.,
+ },
+ Shape {
+ x: 60.,
+ y: 260.,
+ width: 300.,
+ height: 48.,
+ radius: 24.,
+ },
+ Shape {
+ x: 120.,
+ y: 380.,
+ width: 96.,
+ height: 96.,
+ radius: 48.,
+ },
+ Shape {
+ x: 460.,
+ y: 380.,
+ width: 360.,
+ height: 160.,
+ radius: 16.,
+ },
+];
+
+/// Backdrops that render identically everywhere (no text, so no fonts).
+pub const NAMES: [&str; 9] = [
+ "white", "black", "gray", "stripes", "grid", "checker", "hues", "blobs", "split",
+];
+
+/// Render `name` at `scale` device pixels per logical pixel.
+pub fn render(name: &str, scale: f32) -> Option<Rgba> {
+ let (width, height) = ((CANVAS.0 * scale).round() as usize, (CANVAS.1 * scale).round() as usize);
+ let mut image = Rgba::solid(width, height, [255.; 4]);
+ for y in 0..height {
+ for x in 0..width {
+ let (lx, ly) = ((x as f32 + 0.5) / scale, (y as f32 + 0.5) / scale);
+ image.set(x, y, pixel(name, lx, ly)?);
+ }
+ }
+ Some(image)
+}
+
+fn pixel(name: &str, x: f32, y: f32) -> Option<[f32; 4]> {
+ let gray = |value: f32| [value, value, value, 255.];
+ Some(match name {
+ "white" => gray(255.),
+ "black" => gray(0.),
+ "gray" => gray(128.),
+ "stripes" => gray(if (x / 8.).floor() as i64 % 2 == 0 { 0. } else { 255. }),
+ "grid" => {
+ let on_line = |v: f32| (v + 1.).rem_euclid(24.) < 2.;
+ if on_line(x) || on_line(y) {
+ [26., 77., 230., 255.]
+ } else {
+ [245., 245., 247., 255.]
+ }
+ }
+ "checker" => {
+ if ((x / 32.).floor() as i64 + (y / 32.).floor() as i64) % 2 == 0 {
+ [242., 82., 51., 255.]
+ } else {
+ [31., 140., 242., 255.]
+ }
+ }
+ "hues" => {
+ let hue = (x / CANVAS.0).clamp(0., 1.) * 6.;
+ let f = |n: f32| {
+ let k = (n + hue) % 6.;
+ 242. * (1. - 0.85 * (k.min(4. - k).clamp(0., 1.)))
+ };
+ [f(5.), f(3.), f(1.), 255.]
+ }
+ "blobs" => {
+ let mut color = [250., 240., 224.];
+ for (fx, fy, radius, blob) in [
+ (0.2, 0.7, 260., [255., 89., 115.]),
+ (0.7, 0.75, 300., [64., 140., 255.]),
+ (0.5, 0.2, 280., [51., 217., 140.]),
+ (0.9, 0.2, 220., [255., 204., 51.]),
+ ] {
+ let d = ((x - fx * CANVAS.0).powi(2) + (y - fy * CANVAS.1).powi(2)).sqrt();
+ let t = (1. - d / radius).clamp(0., 1.);
+ for i in 0..3 {
+ color[i] += (blob[i] - color[i]) * t;
+ }
+ }
+ [color[0], color[1], color[2], 255.]
+ }
+ "split" => {
+ let white = y < CANVAS.1 / 2. || x < 170. || x >= 670.;
+ gray(if white { 255. } else { 0. })
+ }
+ _ => return None,
+ })
+}
diff --git a/apps/desktop-gpui/patches/zed-glass.patch b/apps/desktop-gpui/patches/zed-glass.patch
new file mode 100644
index 000000000..12d721a63
--- /dev/null
+++ b/apps/desktop-gpui/patches/zed-glass.patch
@@ -0,0 +1,2020 @@
+diff --git a/crates/gpui/src/scene.rs b/crates/gpui/src/scene.rs
+index 6cc3562..6ea0576 100644
+--- a/crates/gpui/src/scene.rs
++++ b/crates/gpui/src/scene.rs
+@@ -52,7 +52,7 @@ pub struct Scene {
+ pub surfaces: Vec<PaintSurface>,
+ /// Backdrop-blur regions — deliberately OUTSIDE the primitive batch
+ /// stream: the renderer breaks its render pass at each blur's order to
+- /// snapshot the framebuffer (macOS Metal; other renderers ignore them).
++ /// snapshot the framebuffer.
+ pub backdrop_blurs: Vec<BackdropBlur>,
+ }
+
+@@ -613,8 +613,8 @@ impl From<Underline> for Primitive {
+
+ /// A within-window backdrop blur region: the renderer snapshots everything
+ /// painted below this order and paints it back gaussian-blurred inside the
+-/// rounded bounds (frosted-glass popovers). macOS Metal only — see
+-/// [`crate::Window::paint_backdrop_blur`].
++/// rounded bounds, optionally shaded as glass. See
++/// [`crate::Window::paint_backdrop_blur`] and [`crate::Window::paint_glass`].
+ #[derive(Debug, Copy, Clone)]
+ #[repr(C)]
+ #[expect(missing_docs)]
+@@ -624,6 +624,53 @@ pub struct BackdropBlur {
+ pub bounds: Bounds<ScaledPixels>,
+ pub content_mask: ContentMask<ScaledPixels>,
+ pub corner_radii: Corners<ScaledPixels>,
++ pub glass: GlassStyle,
++}
++
++/// The glass material a backdrop blur is shaded with, resolved for one
++/// pane. Lengths are device pixels; colours are 0-1 in the framebuffer's
++/// (gamma-encoded) space. All zeros (`enabled == 0`) is a plain blur.
++///
++/// Per pixel: the blurred backdrop is sampled along the edge normal by
++/// `rim_bend` within the `lens_rim`-wide rim (falling off with
++/// `lens_curve`) and transformed by a second-order polynomial: each output
++/// channel `c` is `poly[c * 10..]` dotted with `r, g, b, r², g², b², rg, gb,
++/// rb, 1`. An edge line then scales the colour by `1 + line + line_lit`
++/// times how much the edge faces `light_x`/`light_y` (decaying over
++/// `line_width`), and an inner glow adds `glow` (decaying over
++/// `glow_width`). Over a transparent backdrop the glass is `opacity` opaque.
++/// The pixel ring just outside the edge gets a hairline,
++/// `clamp(min(under, a * under + b), 0, 1)`, with `a` and `b` blended from
++/// `hairline[0..2]` on vertical edges to `hairline[2..4]` on horizontal ones.
++#[derive(Debug, Default, Copy, Clone, PartialEq)]
++#[repr(C)]
++#[expect(missing_docs)]
++pub struct GlassStyle {
++ pub enabled: u32,
++ pub lens_rim: f32,
++ pub lens_curve: f32,
++ pub rim_bend: f32,
++ pub poly: [f32; 30],
++ pub opacity: f32,
++ pub line: f32,
++ pub line_lit: f32,
++ pub line_width: f32,
++ pub light_x: f32,
++ pub light_y: f32,
++ pub glow: f32,
++ pub glow_width: f32,
++ pub hairline: [f32; 4],
++}
++
++impl GlassStyle {
++ /// How far the shader may sample outside the pane's bounds.
++ pub fn reach(&self) -> f32 {
++ if self.enabled == 0 {
++ 0.
++ } else {
++ self.rim_bend.abs() + 2.
++ }
++ }
+ }
+
+ #[derive(Debug, Copy, Clone)]
+diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
+index 4299e15..91bee9f 100644
+--- a/crates/gpui/src/window.rs
++++ b/crates/gpui/src/window.rs
+@@ -13,7 +13,7 @@ use crate::{
+ PolychromeSprite,
+ Priority, PromptButton, PromptLevel, Quad, Render, RenderGlyphParams, RenderImage,
+ RenderImageParams, RenderSvgParams, Replay, ResizeEdge, SMOOTH_SVG_SCALE_FACTOR,
+- SUBPIXEL_VARIANTS_X, SUBPIXEL_VARIANTS_Y, BackdropBlur, ScaledPixels, Scene, Shadow, SharedString, Size,
++ SUBPIXEL_VARIANTS_X, SUBPIXEL_VARIANTS_Y, BackdropBlur, GlassStyle, ScaledPixels, Scene, Shadow, SharedString, Size,
+ StrikethroughStyle, Style, SubpixelSprite, SubscriberSet, Subscription, SystemWindowTab,
+ SystemWindowTabController, TabStopMap, TaffyLayoutEngine, Task, TextRenderingMode, TextStyle,
+ TextStyleRefinement, ThermalState, TransformationMatrix, Underline, UnderlineStyle,
+@@ -4186,6 +4186,19 @@ impl Window {
+ bounds: Bounds<Pixels>,
+ corner_radii: Corners<Pixels>,
+ blur_radius: Pixels,
++ ) {
++ self.paint_glass(bounds, corner_radii, blur_radius, GlassStyle::default());
++ }
++
++ /// Paint a glass pane: [`Self::paint_backdrop_blur`] shaded with
++ /// `glass`, whose lengths are logical pixels here and scaled to device
++ /// pixels for the renderer.
++ pub fn paint_glass(
++ &mut self,
++ bounds: Bounds<Pixels>,
++ corner_radii: Corners<Pixels>,
++ blur_radius: Pixels,
++ glass: GlassStyle,
+ ) {
+ self.invalidator.debug_assert_paint();
+ let scale_factor = self.scale_factor();
+@@ -4210,6 +4223,13 @@ impl Window {
+ bounds: bounds.scale(scale_factor),
+ content_mask,
+ corner_radii: corner_radii.scale(scale_factor),
++ glass: GlassStyle {
++ lens_rim: glass.lens_rim * scale_factor,
++ rim_bend: glass.rim_bend * scale_factor,
++ line_width: glass.line_width * scale_factor,
++ glow_width: glass.glow_width * scale_factor,
++ ..glass
++ },
+ });
+ }
+
+diff --git a/crates/gpui_macos/build.rs b/crates/gpui_macos/build.rs
+index 70e9a0b..79415df 100644
+--- a/crates/gpui_macos/build.rs
++++ b/crates/gpui_macos/build.rs
+@@ -52,6 +52,7 @@ mod macos_build {
+ "Shadow".into(),
+ "BackdropBlurInputIndex".into(),
+ "BackdropBlur".into(),
++ "GlassStyle".into(),
+ "QuadInputIndex".into(),
+ "Underline".into(),
+ "UnderlineInputIndex".into(),
+diff --git a/crates/gpui_macos/src/metal_renderer.rs b/crates/gpui_macos/src/metal_renderer.rs
+index 4be15aa..f4c608d 100644
+--- a/crates/gpui_macos/src/metal_renderer.rs
++++ b/crates/gpui_macos/src/metal_renderer.rs
+@@ -979,7 +979,7 @@ impl MetalRenderer {
+ // so snapshot just that padded region instead of the whole
+ // drawable (popovers are small; this is a 10-20x reduction).
+ let sigma = blur.blur_radius.0.max(1.0);
+- let padding = (sigma * 3.0).ceil() + 2.0;
++ let padding = (sigma * 3.0 + blur.glass.reach()).ceil() + 2.0;
+ let visible = blur.bounds.intersect(&blur.content_mask.bounds);
+ let drawable_width = texture.width() as i64;
+ let drawable_height = texture.height() as i64;
+@@ -1064,6 +1064,7 @@ impl MetalRenderer {
+ &mut instance_offset,
+ viewport_size,
+ &blurred,
++ &scratch,
+ command_encoder,
+ ) {
+ command_encoder.end_encoding();
+@@ -1425,6 +1426,7 @@ impl MetalRenderer {
+ instance_offset: &mut usize,
+ viewport_size: Size<DevicePixels>,
+ source_texture: &metal::TextureRef,
++ sharp_texture: &metal::TextureRef,
+ command_encoder: &metal::RenderCommandEncoderRef,
+ ) -> bool {
+ if blurs.is_empty() {
+@@ -1462,6 +1464,10 @@ impl MetalRenderer {
+ BackdropBlurInputIndex::SourceTexture as u64,
+ Some(source_texture),
+ );
++ command_encoder.set_fragment_texture(
++ BackdropBlurInputIndex::SharpTexture as u64,
++ Some(sharp_texture),
++ );
+
+ let blur_bytes_len = mem::size_of_val(blurs);
+ let buffer_contents =
+@@ -2397,6 +2403,8 @@ enum BackdropBlurInputIndex {
+ /// Drawable rect the snapshot covers (x, y, w, h in device px) — the
+ /// fragment shader maps frag position → snapshot UV through it.
+ SourceRect = 4,
++ /// The unblurred snapshot glass antialiases its edge against.
++ SharpTexture = 5,
+ }
+
+ #[repr(C)]
+@@ -2568,7 +2576,7 @@ mod renderer_hardening_tests {
+ #[cfg(test)]
+ mod backdrop_blur_tests {
+ use super::*;
+- use gpui::{Corners, Edges, Hsla, solid_background, transparent_black};
++ use gpui::{Corners, Edges, GlassStyle, Hsla, solid_background, transparent_black};
+ use image::RgbaImage;
+
+ const VIEW_W: i32 = 640;
+@@ -2645,6 +2653,7 @@ mod backdrop_blur_tests {
+ bounds: blur_bounds,
+ content_mask: ContentMask { bounds: viewport() },
+ corner_radii: Corners::default(),
++ glass: Default::default(),
+ });
+ scene.pop_layer();
+ }
+@@ -2741,6 +2750,197 @@ mod backdrop_blur_tests {
+ );
+ }
+
++ fn push_stripes(scene: &mut Scene) {
++ let mut x = 0.;
++ let mut dark = true;
++ while x < VIEW_W as f32 {
++ push_quad(scene, bounds(x, 0., 8., VIEW_H as f32), if dark { 0.05 } else { 0.95 });
++ x += 8.;
++ dark = !dark;
++ }
++ }
++
++ fn test_glass() -> GlassStyle {
++ let mut poly = [0.; 30];
++ poly[..10].copy_from_slice(&[0.6, 0.15, 0.05, -0.2, 0.1, 0.0, 0.05, 0.0, 0.02, 0.22]);
++ poly[10..20].copy_from_slice(&[0.05, 0.5, 0.1, 0.0, -0.15, 0.05, 0.0, 0.04, 0.0, 0.2]);
++ poly[20..].copy_from_slice(&[0.12, 0.05, 0.55, 0.0, 0.0, -0.1, 0.02, 0.0, 0.03, 0.18]);
++ GlassStyle {
++ enabled: 1,
++ lens_rim: 28.,
++ lens_curve: 0.4,
++ rim_bend: -40.,
++ poly,
++ opacity: 0.6,
++ line: -0.1,
++ line_lit: 0.05,
++ line_width: 1.,
++ light_x: -0.7,
++ light_y: -0.7,
++ glow: 0.04,
++ glow_width: 6.,
++ hairline: [0.86, -0.12, 0.92, -0.08],
++ }
++ }
++
++ fn gaussian_blur(image: &RgbaImage, sigma: f32) -> Vec<[f32; 4]> {
++ let (w, h) = (image.width() as i32, image.height() as i32);
++ let radius = (sigma * 3.).ceil() as i32;
++ let weights: Vec<f32> = (-radius..=radius)
++ .map(|i| (-(i * i) as f32 / (2. * sigma * sigma)).exp())
++ .collect();
++ let total: f32 = weights.iter().sum();
++ let pass = |src: &dyn Fn(i32, i32) -> [f32; 4], horizontal: bool| -> Vec<[f32; 4]> {
++ let mut out = vec![[0.; 4]; (w * h) as usize];
++ for y in 0..h {
++ for x in 0..w {
++ let mut acc = [0.; 4];
++ for (k, weight) in weights.iter().enumerate() {
++ let offset = k as i32 - radius;
++ let (sx, sy) = if horizontal {
++ ((x + offset).clamp(0, w - 1), y)
++ } else {
++ (x, (y + offset).clamp(0, h - 1))
++ };
++ let px = src(sx, sy);
++ for c in 0..4 {
++ acc[c] += px[c] * weight / total;
++ }
++ }
++ out[(y * w + x) as usize] = acc;
++ }
++ }
++ out
++ };
++ let first = pass(
++ &|x, y| {
++ let p = image.get_pixel(x as u32, y as u32);
++ [p[0] as f32, p[1] as f32, p[2] as f32, p[3] as f32]
++ },
++ true,
++ );
++ pass(&|x, y| first[(y * w + x) as usize], false)
++ }
++
++ /// The glass shader against the same math on the CPU: the snapshot is
++ /// gaussian-blurred, sampled along the edge normal in the rim, then
++ /// colour-transformed, compressed and lit.
++ #[test]
++ fn glass_matches_the_cpu_model() {
++ if metal::Device::system_default().is_none() {
++ return;
++ }
++ let pane = bounds(200., 140., 240., 160.);
++ let radius = 30.;
++ let sigma = 12.;
++ let glass = test_glass();
++
++ let mut base = Scene::default();
++ push_stripes(&mut base);
++ let backdrop = render(&mut base);
++
++ let mut scene = Scene::default();
++ push_stripes(&mut scene);
++ scene.push_layer(pane);
++ scene.insert_primitive(Shadow {
++ order: 0,
++ blur_radius: ScaledPixels(0.),
++ bounds: pane,
++ corner_radii: Corners::all(ScaledPixels(radius)),
++ content_mask: ContentMask { bounds: viewport() },
++ color: transparent_black(),
++ element_bounds: pane,
++ element_corner_radii: Corners::all(ScaledPixels(radius)),
++ inset: 0,
++ pad: 0,
++ });
++ scene.insert_backdrop_blur(BackdropBlur {
++ order: 0,
++ blur_radius: ScaledPixels(sigma),
++ bounds: pane,
++ content_mask: ContentMask { bounds: viewport() },
++ corner_radii: Corners::all(ScaledPixels(radius)),
++ glass,
++ });
++ scene.pop_layer();
++ let rendered = render(&mut scene);
++
++ let blurred = gaussian_blur(&backdrop, sigma);
++ let width = VIEW_W;
++ let sample = |x: f32, y: f32| -> [f32; 4] {
++ let fx = (x - 0.5).clamp(0., (VIEW_W - 1) as f32);
++ let fy = (y - 0.5).clamp(0., (VIEW_H - 1) as f32);
++ let (x0, y0) = (fx.floor() as i32, fy.floor() as i32);
++ let (x1, y1) = ((x0 + 1).min(VIEW_W - 1), (y0 + 1).min(VIEW_H - 1));
++ let (tx, ty) = (fx - x0 as f32, fy - y0 as f32);
++ let at = |x: i32, y: i32| blurred[(y * width + x) as usize];
++ let (a, b, c, d) = (at(x0, y0), at(x1, y0), at(x0, y1), at(x1, y1));
++ std::array::from_fn(|i| {
++ let top = a[i] + (b[i] - a[i]) * tx;
++ top + (c[i] + (d[i] - c[i]) * tx - top) * ty
++ })
++ };
++ let sdf = |x: f32, y: f32| -> f32 {
++ let half = (pane.size.width.0 / 2., pane.size.height.0 / 2.);
++ let qx = (x - (pane.origin.x.0 + half.0)).abs() - (half.0 - radius);
++ let qy = (y - (pane.origin.y.0 + half.1)).abs() - (half.1 - radius);
++ (qx.max(0.).powi(2) + qy.max(0.).powi(2)).sqrt() + qx.max(qy).min(0.) - radius
++ };
++
++ let mut errors = Vec::new();
++ for y in (pane.origin.y.0 as i32 - 2)..(pane.origin.y.0 + pane.size.height.0) as i32 + 2 {
++ for x in (pane.origin.x.0 as i32 - 2)..(pane.origin.x.0 + pane.size.width.0) as i32 + 2 {
++ let (px, py) = (x as f32 + 0.5, y as f32 + 0.5);
++ let d = sdf(px, py);
++ let coverage = (0.5 - d).clamp(0., 1.);
++ let ring = (1.5 - d).clamp(0., 1.) - coverage;
++ let (nx, ny) = {
++ let dx = sdf(px + 0.5, py) - sdf(px - 0.5, py);
++ let dy = sdf(px, py + 0.5) - sdf(px, py - 0.5);
++ let len = (dx * dx + dy * dy).sqrt().max(1e-6);
++ (dx / len, dy / len)
++ };
++ let vertical = nx * nx;
++ let hair_a = glass.hairline[2] + (glass.hairline[0] - glass.hairline[2]) * vertical;
++ let hair_b = glass.hairline[3] + (glass.hairline[1] - glass.hairline[3]) * vertical;
++ let raw = backdrop.get_pixel(x as u32, y as u32);
++ let under: [f32; 3] = std::array::from_fn(|i| {
++ let value = raw[i] as f32 / 255.;
++ let line = value.min(hair_a * value + hair_b).clamp(0., 1.);
++ (value + (line - value) * ring) * 255.
++ });
++ let expected: [f32; 3] = if coverage <= 0. {
++ under
++ } else {
++ let edge = (-d).max(0.);
++ let rim = 1. - (edge / glass.lens_rim).clamp(0., 1.).powf(glass.lens_curve);
++ let s = sample(px + nx * glass.rim_bend * rim, py + ny * glass.rim_bend * rim);
++ let c = [s[0] / 255., s[1] / 255., s[2] / 255.];
++ let features = [
++ c[0], c[1], c[2], c[0] * c[0], c[1] * c[1], c[2] * c[2], c[0] * c[1],
++ c[1] * c[2], c[0] * c[2], 1.,
++ ];
++ let facing = nx * glass.light_x + ny * glass.light_y;
++ let line = (-edge / glass.line_width).exp() * (glass.line + glass.line_lit * facing);
++ let glow = (-edge / glass.glow_width).exp() * glass.glow;
++ std::array::from_fn(|i| {
++ let value: f32 = (0..10).map(|t| glass.poly[i * 10 + t] * features[t]).sum();
++ let value = (value * (1. + line) + glow).clamp(0., 1.) * 255.;
++ under[i] + (value - under[i]) * coverage
++ })
++ };
++ let got = rendered.get_pixel(x as u32, y as u32);
++ for c in 0..3 {
++ errors.push((got[c] as f32 - expected[c]).abs());
++ }
++ }
++ }
++ errors.sort_by(f32::total_cmp);
++ let mean = errors.iter().sum::<f32>() / errors.len() as f32;
++ let p99 = errors[errors.len() * 99 / 100];
++ assert!(mean < 1.5 && p99 < 8., "glass vs CPU model: mean {mean:.2}, p99 {p99:.2}");
++ }
++
+ #[test]
+ fn two_blurs_of_different_sizes_in_one_frame() {
+ if metal::Device::system_default().is_none() {
+diff --git a/crates/gpui_macos/src/shaders.metal b/crates/gpui_macos/src/shaders.metal
+index e087c0d..40c50bd 100644
+--- a/crates/gpui_macos/src/shaders.metal
++++ b/crates/gpui_macos/src/shaders.metal
+@@ -1354,9 +1354,17 @@ vertex BackdropBlurVertexOutput backdrop_blur_vertex(
+ [[buffer(BackdropBlurInputIndex_ViewportSize)]]) {
+ float2 unit_vertex = unit_vertices[unit_vertex_id];
+ BackdropBlur blur = blurs[blur_id];
++ // Glass draws a hairline in the pixel ring just outside its edge.
++ Bounds_ScaledPixels bounds = blur.bounds;
++ if (blur.glass.enabled != 0) {
++ bounds.origin.x -= 2.;
++ bounds.origin.y -= 2.;
++ bounds.size.width += 4.;
++ bounds.size.height += 4.;
++ }
+ float4 device_position =
+- to_device_position(unit_vertex, blur.bounds, viewport_size);
+- float4 clip_distance = distance_from_clip_rect(unit_vertex, blur.bounds,
++ to_device_position(unit_vertex, bounds, viewport_size);
++ float4 clip_distance = distance_from_clip_rect(unit_vertex, bounds,
+ blur.content_mask.bounds);
+ return BackdropBlurVertexOutput{
+ device_position,
+@@ -1364,26 +1372,77 @@ vertex BackdropBlurVertexOutput backdrop_blur_vertex(
+ {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
+ }
+
++float3 glass_shade(GlassStyle glass, float3 c, float edge, float2 normal) {
++ float features[10] = {c.r, c.g, c.b, c.r * c.r, c.g * c.g, c.b * c.b,
++ c.r * c.g, c.g * c.b, c.r * c.b, 1.};
++ float3 color = float3(0.);
++ for (int term = 0; term < 10; term++) {
++ color += features[term] *
++ float3(glass.poly[term], glass.poly[10 + term], glass.poly[20 + term]);
++ }
++ float facing = dot(normal, float2(glass.light_x, glass.light_y));
++ float line = exp(-edge / glass.line_width) * (glass.line + glass.line_lit * facing);
++ float glow = exp(-edge / glass.glow_width) * glass.glow;
++ return color * (1. + line) + glow;
++}
++
+ fragment float4 backdrop_blur_fragment(
+ BackdropBlurFragmentInput input [[stage_in]],
+ constant BackdropBlur *blurs [[buffer(BackdropBlurInputIndex_Blurs)]],
+ constant float4 &source_rect [[buffer(BackdropBlurInputIndex_SourceRect)]],
+ texture2d<float> source_texture
+- [[texture(BackdropBlurInputIndex_SourceTexture)]]) {
++ [[texture(BackdropBlurInputIndex_SourceTexture)]],
++ texture2d<float> sharp_texture
++ [[texture(BackdropBlurInputIndex_SharpTexture)]]) {
+ constexpr sampler source_sampler(coord::normalized, address::clamp_to_edge,
+ filter::linear);
+ BackdropBlur blur = blurs[input.blur_id];
++ float2 position = input.position.xy;
++ float distance = quad_sdf(position, blur.bounds, blur.corner_radii);
+
+- // Rounded-rect clip: blending is disabled on this pipeline (the blur
+- // REPLACES the region), so fragments outside must discard, not return 0.
+- float distance = quad_sdf(input.position.xy, blur.bounds, blur.corner_radii);
+- if (distance > 0.) {
+- discard_fragment();
++ // The snapshot was gaussian-blurred on the GPU (MPSImageGaussianBlur)
++ // before this pass. It covers only source_rect (x, y, w, h) of the
++ // drawable, not the whole viewport.
++ if (blur.glass.enabled == 0) {
++ // Blending is disabled on this pipeline (the blur REPLACES the region),
++ // so fragments outside must discard, not return 0.
++ if (distance > 0.) {
++ discard_fragment();
++ }
++ float2 uv = (position - source_rect.xy) / source_rect.zw;
++ return source_texture.sample(source_sampler, uv);
+ }
+
+- // The snapshot was gaussian-blurred on the GPU (MPSImageGaussianBlur)
+- // before this pass — one clean sample. The snapshot covers only
+- // source_rect (x, y, w, h) of the drawable, not the whole viewport.
+- float2 uv = (input.position.xy - source_rect.xy) / source_rect.zw;
+- return source_texture.sample(source_sampler, uv);
++ // Glass antialiases its own edge against the unblurred snapshot, and
++ // draws its hairline in the ring just outside the edge.
++ float coverage = saturate(0.5 - distance);
++ float ring = saturate(1.5 - distance) - coverage;
++ if (coverage <= 0. && ring <= 0.) {
++ discard_fragment();
++ }
++ float2 normal = float2(
++ quad_sdf(position + float2(0.5, 0.), blur.bounds, blur.corner_radii) -
++ quad_sdf(position - float2(0.5, 0.), blur.bounds, blur.corner_radii),
++ quad_sdf(position + float2(0., 0.5), blur.bounds, blur.corner_radii) -
++ quad_sdf(position - float2(0., 0.5), blur.bounds, blur.corner_radii));
++ normal /= max(length(normal), 1e-6);
++ float vertical = normal.x * normal.x;
++ float hair_a = mix(blur.glass.hairline[2], blur.glass.hairline[0], vertical);
++ float hair_b = mix(blur.glass.hairline[3], blur.glass.hairline[1], vertical);
++ float4 under = sharp_texture.sample(
++ source_sampler, (position - source_rect.xy) / source_rect.zw);
++ float3 hairline = saturate(min(under.rgb, hair_a * under.rgb + hair_b));
++ under.rgb = mix(under.rgb, hairline, ring);
++ if (coverage <= 0.) {
++ return under;
++ }
++ float edge = max(-distance, 0.);
++ float rim = 1. - pow(saturate(edge / blur.glass.lens_rim), blur.glass.lens_curve);
++ float2 sample_at = position + normal * blur.glass.rim_bend * rim;
++ float4 backdrop = source_texture.sample(
++ source_sampler, (sample_at - source_rect.xy) / source_rect.zw);
++ float3 straight = backdrop.a > 0. ? backdrop.rgb / backdrop.a : float3(0.);
++ float alpha = mix(backdrop.a, 1., blur.glass.opacity);
++ float3 glass = saturate(glass_shade(blur.glass, straight, edge, normal));
++ return mix(under, float4(glass * alpha, alpha), coverage);
+ }
+diff --git a/crates/gpui_wgpu/src/shaders.wgsl b/crates/gpui_wgpu/src/shaders.wgsl
+index 011be71..47a16cd 100644
+--- a/crates/gpui_wgpu/src/shaders.wgsl
++++ b/crates/gpui_wgpu/src/shaders.wgsl
+@@ -1399,3 +1399,194 @@ fn fs_surface(input: SurfaceVarying) -> @location(0) vec4<f32> {
+
+ return ycbcr_to_RGB * y_cb_cr;
+ }
++
++// --- backdrop blur and glass --- //
++
++struct GlassStyle {
++ enabled: u32,
++ lens_rim: f32,
++ lens_curve: f32,
++ rim_bend: f32,
++ poly: array<f32, 30>,
++ opacity: f32,
++ line_strength: f32,
++ line_lit: f32,
++ line_width: f32,
++ light_x: f32,
++ light_y: f32,
++ glow: f32,
++ glow_width: f32,
++ hairline: array<f32, 4>,
++}
++
++struct BackdropBlur {
++ order: u32,
++ blur_radius: f32,
++ bounds: Bounds,
++ content_mask: Bounds,
++ corner_radii: Corners,
++ glass: GlassStyle,
++}
++
++// For the downsample and blur passes `source_rect` is the texel rect of the
++// source texture being read and `params` is (direction x, direction y,
++// sigma in source texels or the downsample factor, unused). For the glass
++// pass `source_rect` is the drawable rect that was blurred, `params.x` the
++// factor it was blurred at (the blurred texture holds it from texel 0,
++// scaled down by that factor), and `sharp_texture_size` the size of the
++// drawable-sized unblurred copy.
++struct BackdropParams {
++ source_rect: vec4<f32>,
++ params: vec4<f32>,
++ source_texture_size: vec2<f32>,
++ sharp_texture_size: vec2<f32>,
++}
++
++@group(1) @binding(0) var<storage, read> backdrop_params: BackdropParams;
++@group(1) @binding(1) var t_backdrop: texture_2d<f32>;
++@group(1) @binding(2) var s_backdrop: sampler;
++@group(1) @binding(3) var<storage, read> b_backdrop_blurs: array<BackdropBlur>;
++@group(1) @binding(4) var t_backdrop_sharp: texture_2d<f32>;
++
++struct BackdropPassVarying {
++ @builtin(position) position: vec4<f32>,
++ @location(0) uv: vec2<f32>,
++}
++
++@vertex
++fn vs_backdrop_pass(@builtin(vertex_index) vertex_id: u32) -> BackdropPassVarying {
++ let uv = vec2<f32>(f32((vertex_id << 1u) & 2u), f32(vertex_id & 2u));
++ var out = BackdropPassVarying();
++ out.position = vec4<f32>(uv * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0), 0.0, 1.0);
++ out.uv = uv;
++ return out;
++}
++
++fn backdrop_source_uv(uv: vec2<f32>) -> vec2<f32> {
++ let rect = backdrop_params.source_rect;
++ return (rect.xy + uv * rect.zw) / backdrop_params.source_texture_size;
++}
++
++@fragment
++fn fs_backdrop_downsample(input: BackdropPassVarying) -> @location(0) vec4<f32> {
++ let rect = backdrop_params.source_rect;
++ let texel = 1.0 / backdrop_params.source_texture_size;
++ let center = backdrop_source_uv(input.uv);
++ let quarter = texel * backdrop_params.params.z * 0.25;
++ let lo = (rect.xy + 0.5) * texel;
++ let hi = (rect.xy + rect.zw - 0.5) * texel;
++ return 0.25 * (
++ textureSampleLevel(t_backdrop, s_backdrop, clamp(center + vec2<f32>(-quarter.x, -quarter.y), lo, hi), 0.0) +
++ textureSampleLevel(t_backdrop, s_backdrop, clamp(center + vec2<f32>(quarter.x, -quarter.y), lo, hi), 0.0) +
++ textureSampleLevel(t_backdrop, s_backdrop, clamp(center + vec2<f32>(-quarter.x, quarter.y), lo, hi), 0.0) +
++ textureSampleLevel(t_backdrop, s_backdrop, clamp(center + vec2<f32>(quarter.x, quarter.y), lo, hi), 0.0));
++}
++
++@fragment
++fn fs_backdrop_blur_pass(input: BackdropPassVarying) -> @location(0) vec4<f32> {
++ let rect = backdrop_params.source_rect;
++ let sigma = max(backdrop_params.params.z, 0.5);
++ let radius = i32(ceil(sigma * 3.0));
++ let step = backdrop_params.params.xy / backdrop_params.source_texture_size;
++ let uv = backdrop_source_uv(input.uv);
++ let lo = (rect.xy + 0.5) / backdrop_params.source_texture_size;
++ let hi = (rect.xy + rect.zw - 0.5) / backdrop_params.source_texture_size;
++ var sum = vec4<f32>(0.0);
++ var total = 0.0;
++ for (var i = -radius; i <= radius; i++) {
++ let weight = exp(-f32(i * i) / (2.0 * sigma * sigma));
++ sum += weight * textureSampleLevel(t_backdrop, s_backdrop, clamp(uv + step * f32(i), lo, hi), 0.0);
++ total += weight;
++ }
++ return sum / total;
++}
++
++struct BackdropGlassVarying {
++ @builtin(position) position: vec4<f32>,
++ @location(0) @interpolate(flat) blur_id: u32,
++ @location(1) clip_distances: vec4<f32>,
++}
++
++@vertex
++fn vs_backdrop_glass(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> BackdropGlassVarying {
++ let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
++ let blur = b_backdrop_blurs[instance_id];
++ // Glass draws a hairline in the pixel ring just outside its edge.
++ var bounds = blur.bounds;
++ if (blur.glass.enabled != 0u) {
++ bounds.origin -= vec2<f32>(2.0);
++ bounds.size += vec2<f32>(4.0);
++ }
++ var out = BackdropGlassVarying();
++ out.position = to_device_position(unit_vertex, bounds);
++ out.blur_id = instance_id;
++ out.clip_distances = distance_from_clip_rect(unit_vertex, bounds, blur.content_mask);
++ return out;
++}
++
++// Clamped to the blurred region: texels beyond it hold an older pass.
++fn backdrop_blurred_uv(position: vec2<f32>) -> vec2<f32> {
++ let rect = backdrop_params.source_rect;
++ let factor = backdrop_params.params.x;
++ let texel = clamp((position - rect.xy) / factor, vec2<f32>(0.5), rect.zw / factor - 0.5);
++ return texel / backdrop_params.source_texture_size;
++}
++
++@fragment
++fn fs_backdrop_glass(input: BackdropGlassVarying) -> @location(0) vec4<f32> {
++ if (any(input.clip_distances < vec4<f32>(0.0))) {
++ discard;
++ }
++ let blur = b_backdrop_blurs[input.blur_id];
++ let position = input.position.xy;
++ let distance = quad_sdf(position, blur.bounds, blur.corner_radii);
++
++ if (blur.glass.enabled == 0u) {
++ if (distance > 0.0) {
++ discard;
++ }
++ return textureSampleLevel(t_backdrop, s_backdrop, backdrop_blurred_uv(position), 0.0);
++ }
++
++ let coverage = saturate(0.5 - distance);
++ let ring = saturate(1.5 - distance) - coverage;
++ if (coverage <= 0.0 && ring <= 0.0) {
++ discard;
++ }
++ let glass = blur.glass;
++ let edge = max(-distance, 0.0);
++ var normal = vec2<f32>(
++ quad_sdf(position + vec2<f32>(0.5, 0.0), blur.bounds, blur.corner_radii) -
++ quad_sdf(position - vec2<f32>(0.5, 0.0), blur.bounds, blur.corner_radii),
++ quad_sdf(position + vec2<f32>(0.0, 0.5), blur.bounds, blur.corner_radii) -
++ quad_sdf(position - vec2<f32>(0.0, 0.5), blur.bounds, blur.corner_radii));
++ normal = normal / max(length(normal), 1e-6);
++ let vertical = normal.x * normal.x;
++ let hair_a = mix(glass.hairline[2], glass.hairline[0], vertical);
++ let hair_b = mix(glass.hairline[3], glass.hairline[1], vertical);
++ var under = textureSampleLevel(t_backdrop_sharp, s_backdrop, position / backdrop_params.sharp_texture_size, 0.0);
++ let hair_color = saturate(min(under.rgb, hair_a * under.rgb + vec3<f32>(hair_b)));
++ under = vec4<f32>(mix(under.rgb, hair_color, ring), under.a);
++ if (coverage <= 0.0) {
++ return under;
++ }
++ let rim = 1.0 - pow(saturate(edge / glass.lens_rim), glass.lens_curve);
++ let sample_at = position + normal * glass.rim_bend * rim;
++ let backdrop = textureSampleLevel(t_backdrop, s_backdrop, backdrop_blurred_uv(sample_at), 0.0);
++ var color = select(vec3<f32>(0.0), backdrop.rgb / backdrop.a, backdrop.a > 0.0);
++ let alpha = mix(backdrop.a, 1.0, glass.opacity);
++
++ let c = color;
++ let features = array<f32, 10>(c.r, c.g, c.b, c.r * c.r, c.g * c.g, c.b * c.b,
++ c.r * c.g, c.g * c.b, c.r * c.b, 1.0);
++ color = vec3<f32>(0.0);
++ for (var term = 0; term < 10; term++) {
++ color += features[term] * vec3<f32>(glass.poly[term], glass.poly[10 + term], glass.poly[20 + term]);
++ }
++ let facing = dot(normal, vec2<f32>(glass.light_x, glass.light_y));
++ let line_term = exp(-edge / glass.line_width) * (glass.line_strength + glass.line_lit * facing);
++ let glow = exp(-edge / glass.glow_width) * glass.glow;
++ color = saturate(color * (1.0 + line_term) + vec3<f32>(glow));
++
++ return mix(under, vec4<f32>(color * alpha, alpha), coverage);
++}
+diff --git a/crates/gpui_wgpu/src/wgpu_renderer.rs b/crates/gpui_wgpu/src/wgpu_renderer.rs
+index 2bfbc4e..f60b1af 100644
+--- a/crates/gpui_wgpu/src/wgpu_renderer.rs
++++ b/crates/gpui_wgpu/src/wgpu_renderer.rs
+@@ -2,7 +2,8 @@ use crate::surface_frame::SurfaceFrame;
+ use crate::{CompositorGpuHint, WgpuAtlas, WgpuContext};
+ use bytemuck::{Pod, Zeroable};
+ use gpui::{
+- AtlasTextureId, AtlasTextureKind, Background, Bounds, DevicePixels, GpuSpecs, MonochromeSprite,
++ AtlasTextureId, AtlasTextureKind, BackdropBlur, Background, Bounds, DevicePixels, DrawOrder,
++ GpuSpecs, MonochromeSprite,
+ Path, Point, PolychromeSprite, PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size,
+ SubpixelSprite, Underline, get_gamma_correction_ratios,
+ };
+@@ -93,6 +94,9 @@ struct WgpuPipelines {
+ poly_sprites: wgpu::RenderPipeline,
+ #[allow(dead_code)]
+ surfaces: wgpu::RenderPipeline,
++ backdrop_downsample: wgpu::RenderPipeline,
++ backdrop_blur_pass: wgpu::RenderPipeline,
++ backdrop_glass: wgpu::RenderPipeline,
+ }
+
+ struct WgpuBindGroupLayouts {
+@@ -100,8 +104,34 @@ struct WgpuBindGroupLayouts {
+ instances: wgpu::BindGroupLayout,
+ instances_with_texture: wgpu::BindGroupLayout,
+ surfaces: wgpu::BindGroupLayout,
++ backdrop_pass: wgpu::BindGroupLayout,
++ backdrop_glass: wgpu::BindGroupLayout,
+ }
+
++/// Surface-sized textures for backdrop blurs: `sharp` holds the snapshot at
++/// the region's own position; `ping` and `pong` hold it downsampled and
++/// blurred from texel 0.
++struct BackdropTargets {
++ width: u32,
++ height: u32,
++ sharp: wgpu::Texture,
++ sharp_view: wgpu::TextureView,
++ ping_view: wgpu::TextureView,
++ pong_view: wgpu::TextureView,
++}
++
++#[repr(C)]
++#[derive(Clone, Copy, Pod, Zeroable)]
++struct BackdropParams {
++ source_rect: [f32; 4],
++ params: [f32; 4],
++ source_texture_size: [f32; 2],
++ sharp_texture_size: [f32; 2],
++}
++
++/// Frames without a backdrop blur before its textures are dropped.
++const BACKDROP_RELEASE_AFTER_FRAMES: u32 = 120;
++
+ /// Shared GPU context reference, used to coordinate device recovery across multiple windows.
+ pub type GpuContext = Rc<RefCell<Option<WgpuContext>>>;
+
+@@ -121,6 +151,8 @@ struct WgpuResources {
+ path_intermediate_view: Option<wgpu::TextureView>,
+ path_msaa_texture: Option<wgpu::Texture>,
+ path_msaa_view: Option<wgpu::TextureView>,
++ backdrop_sampler: wgpu::Sampler,
++ backdrop_targets: Option<BackdropTargets>,
+ }
+
+ impl WgpuResources {
+@@ -129,6 +161,7 @@ impl WgpuResources {
+ self.path_intermediate_view = None;
+ self.path_msaa_texture = None;
+ self.path_msaa_view = None;
++ self.backdrop_targets = None;
+ }
+ }
+
+@@ -159,6 +192,9 @@ pub struct WgpuRenderer {
+ device_lost: std::sync::Arc<std::sync::atomic::AtomicBool>,
+ surface_configured: bool,
+ needs_redraw: bool,
++ /// Whether the surface can be copied from, which backdrop blurs need.
++ backdrop_supported: bool,
++ blur_free_frames: u32,
+ }
+
+ impl WgpuRenderer {
+@@ -331,8 +367,13 @@ impl WgpuRenderer {
+ );
+ }
+
++ let backdrop_supported = surface_caps.usages.contains(wgpu::TextureUsages::COPY_SRC);
+ let surface_config = wgpu::SurfaceConfiguration {
+- usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
++ usage: if backdrop_supported {
++ wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC
++ } else {
++ wgpu::TextureUsages::RENDER_ATTACHMENT
++ },
+ format: surface_format,
+ width: clamped_width.max(1),
+ height: clamped_height.max(1),
+@@ -368,6 +409,12 @@ impl WgpuRenderer {
+ min_filter: wgpu::FilterMode::Linear,
+ ..Default::default()
+ });
++ let backdrop_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
++ label: Some("backdrop_sampler"),
++ mag_filter: wgpu::FilterMode::Linear,
++ min_filter: wgpu::FilterMode::Linear,
++ ..Default::default()
++ });
+
+ let uniform_alignment = device.limits().min_uniform_buffer_offset_alignment as u64;
+ let globals_size = std::mem::size_of::<GlobalParams>() as u64;
+@@ -464,6 +511,8 @@ impl WgpuRenderer {
+ path_intermediate_view: None,
+ path_msaa_texture: None,
+ path_msaa_view: None,
++ backdrop_sampler,
++ backdrop_targets: None,
+ };
+
+ Ok(Self {
+@@ -489,6 +538,8 @@ impl WgpuRenderer {
+ device_lost: context.device_lost_flag(),
+ surface_configured: true,
+ needs_redraw: false,
++ backdrop_supported,
++ blur_free_frames: 0,
+ })
+ }
+
+@@ -608,11 +659,44 @@ impl WgpuRenderer {
+ ],
+ });
+
++ let texture_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
++ binding,
++ visibility: wgpu::ShaderStages::FRAGMENT,
++ ty: wgpu::BindingType::Texture {
++ sample_type: wgpu::TextureSampleType::Float { filterable: true },
++ view_dimension: wgpu::TextureViewDimension::D2,
++ multisampled: false,
++ },
++ count: None,
++ };
++ let sampler_entry = wgpu::BindGroupLayoutEntry {
++ binding: 2,
++ visibility: wgpu::ShaderStages::FRAGMENT,
++ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
++ count: None,
++ };
++ let backdrop_pass = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
++ label: Some("backdrop_pass_layout"),
++ entries: &[storage_buffer_entry(0), texture_entry(1), sampler_entry],
++ });
++ let backdrop_glass = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
++ label: Some("backdrop_glass_layout"),
++ entries: &[
++ storage_buffer_entry(0),
++ texture_entry(1),
++ sampler_entry,
++ storage_buffer_entry(3),
++ texture_entry(4),
++ ],
++ });
++
+ WgpuBindGroupLayouts {
+ globals,
+ instances,
+ instances_with_texture,
+ surfaces,
++ backdrop_pass,
++ backdrop_glass,
+ }
+ }
+
+@@ -878,6 +962,46 @@ impl WgpuRenderer {
+ &shader_module,
+ );
+
++ // The backdrop passes write their final colour, coverage included.
++ let replace_target = [Some(wgpu::ColorTargetState {
++ format: surface_format,
++ blend: None,
++ write_mask: wgpu::ColorWrites::ALL,
++ })];
++ let backdrop_downsample = create_pipeline(
++ "backdrop_downsample",
++ "vs_backdrop_pass",
++ "fs_backdrop_downsample",
++ &layouts.globals,
++ &layouts.backdrop_pass,
++ wgpu::PrimitiveTopology::TriangleList,
++ &replace_target,
++ 1,
++ &shader_module,
++ );
++ let backdrop_blur_pass = create_pipeline(
++ "backdrop_blur_pass",
++ "vs_backdrop_pass",
++ "fs_backdrop_blur_pass",
++ &layouts.globals,
++ &layouts.backdrop_pass,
++ wgpu::PrimitiveTopology::TriangleList,
++ &replace_target,
++ 1,
++ &shader_module,
++ );
++ let backdrop_glass = create_pipeline(
++ "backdrop_glass",
++ "vs_backdrop_glass",
++ "fs_backdrop_glass",
++ &layouts.globals,
++ &layouts.backdrop_glass,
++ wgpu::PrimitiveTopology::TriangleStrip,
++ &replace_target,
++ 1,
++ &shader_module,
++ );
++
+ WgpuPipelines {
+ quads,
+ shadows,
+@@ -888,6 +1012,9 @@ impl WgpuRenderer {
+ subpixel_sprites,
+ poly_sprites,
+ surfaces,
++ backdrop_downsample,
++ backdrop_blur_pass,
++ backdrop_glass,
+ }
+ }
+
+@@ -1148,6 +1275,17 @@ impl WgpuRenderer {
+
+ // Now that we know the surface is healthy, ensure intermediate textures exist
+ self.ensure_intermediate_textures();
++ if scene.backdrop_blurs.is_empty() {
++ self.blur_free_frames = self.blur_free_frames.saturating_add(1);
++ if self.blur_free_frames >= BACKDROP_RELEASE_AFTER_FRAMES {
++ self.resources_mut().backdrop_targets = None;
++ }
++ } else {
++ self.blur_free_frames = 0;
++ if self.backdrop_supported {
++ self.ensure_backdrop_targets();
++ }
++ }
+
+ let frame_view = frame
+ .texture()
+@@ -1227,7 +1365,28 @@ impl WgpuRenderer {
+ ..Default::default()
+ });
+
+- for batch in scene.batches() {
++ let mut pending_blurs = scene.backdrop_blurs.iter().peekable();
++ 'batches: for batch in scene.batches() {
++ // Backdrop blurs interleave by draw order outside the
++ // batch stream: everything painted so far is copied,
++ // blurred and painted back before the batch above it.
++ while let Some(blur) = pending_blurs
++ .next_if(|blur| blur.order <= batch_first_order(scene, &batch))
++ {
++ drop(pass);
++ let ok = self.draw_backdrop_blur(
++ &mut encoder,
++ frame.texture(),
++ &frame_view,
++ blur,
++ &mut instance_offset,
++ );
++ pass = Self::continue_main_pass(&mut encoder, &frame_view);
++ if !ok {
++ overflow = true;
++ break 'batches;
++ }
++ }
+ let ok = match batch {
+ PrimitiveBatch::Quads(range) => {
+ self.draw_quads(&scene.quads[range], &mut instance_offset, &mut pass)
+@@ -1313,6 +1472,21 @@ impl WgpuRenderer {
+ break;
+ }
+ }
++ if !overflow {
++ drop(pass);
++ for blur in pending_blurs {
++ if !self.draw_backdrop_blur(
++ &mut encoder,
++ frame.texture(),
++ &frame_view,
++ blur,
++ &mut instance_offset,
++ ) {
++ overflow = true;
++ break;
++ }
++ }
++ }
+ }
+
+ if overflow {
+@@ -1365,6 +1539,276 @@ impl WgpuRenderer {
+ )
+ }
+
++ fn continue_main_pass<'a>(
++ encoder: &'a mut wgpu::CommandEncoder,
++ frame_view: &'a wgpu::TextureView,
++ ) -> wgpu::RenderPass<'a> {
++ encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
++ label: Some("main_pass_continued"),
++ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
++ view: frame_view,
++ resolve_target: None,
++ ops: wgpu::Operations {
++ load: wgpu::LoadOp::Load,
++ store: wgpu::StoreOp::Store,
++ },
++ depth_slice: None,
++ })],
++ depth_stencil_attachment: None,
++ ..Default::default()
++ })
++ }
++
++ fn ensure_backdrop_targets(&mut self) {
++ let (width, height) = (self.surface_config.width, self.surface_config.height);
++ let format = self.surface_config.format;
++ let resources = self.resources_mut();
++ if resources
++ .backdrop_targets
++ .as_ref()
++ .is_some_and(|targets| targets.width == width && targets.height == height)
++ {
++ return;
++ }
++ let texture = |label: &str, usage: wgpu::TextureUsages| {
++ resources.device.create_texture(&wgpu::TextureDescriptor {
++ label: Some(label),
++ size: wgpu::Extent3d {
++ width,
++ height,
++ depth_or_array_layers: 1,
++ },
++ mip_level_count: 1,
++ sample_count: 1,
++ dimension: wgpu::TextureDimension::D2,
++ format,
++ usage,
++ view_formats: &[],
++ })
++ };
++ let target_usage =
++ wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING;
++ let sharp = texture(
++ "backdrop_sharp",
++ wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
++ );
++ let view = |texture: &wgpu::Texture| texture.create_view(&Default::default());
++ let targets = BackdropTargets {
++ width,
++ height,
++ sharp_view: view(&sharp),
++ ping_view: view(&texture("backdrop_ping", target_usage)),
++ pong_view: view(&texture("backdrop_pong", target_usage)),
++ sharp,
++ };
++ resources.backdrop_targets = Some(targets);
++ }
++
++ /// Copy the region under `blur` (plus room for the gaussian tail and any
++ /// glass lensing), downsample and blur it, then paint it back. Returns
++ /// false only when the instance buffer is full. Downsampling keeps every
++ /// blur under ~13 taps per side: a gaussian that wide is
++ /// indistinguishable from one taken at full resolution.
++ fn draw_backdrop_blur(
++ &self,
++ encoder: &mut wgpu::CommandEncoder,
++ frame_texture: &wgpu::Texture,
++ frame_view: &wgpu::TextureView,
++ blur: &BackdropBlur,
++ instance_offset: &mut u64,
++ ) -> bool {
++ let resources = self.resources();
++ let Some(targets) = resources.backdrop_targets.as_ref() else {
++ return true;
++ };
++ let sigma = blur.blur_radius.0.max(1.0);
++ let padding = (sigma * 3.0 + blur.glass.reach()).ceil() + 2.0;
++ let visible = blur.bounds.intersect(&blur.content_mask.bounds);
++ let (width, height) = (targets.width as i64, targets.height as i64);
++ let x0 = ((visible.origin.x.0 - padding).floor() as i64).max(0);
++ let y0 = ((visible.origin.y.0 - padding).floor() as i64).max(0);
++ let x1 = ((visible.origin.x.0 + visible.size.width.0 + padding).ceil() as i64).min(width);
++ let y1 = ((visible.origin.y.0 + visible.size.height.0 + padding).ceil() as i64).min(height);
++ if x1 <= x0 || y1 <= y0 {
++ return true;
++ }
++ let factor = (sigma / 4.).floor().clamp(1., 8.);
++ let region = [x0 as f32, y0 as f32, (x1 - x0) as f32, (y1 - y0) as f32];
++ let reduced = [(region[2] / factor).ceil(), (region[3] / factor).ceil()];
++ let full = [targets.width as f32, targets.height as f32];
++
++ encoder.copy_texture_to_texture(
++ wgpu::TexelCopyTextureInfo {
++ texture: frame_texture,
++ mip_level: 0,
++ origin: wgpu::Origin3d {
++ x: x0 as u32,
++ y: y0 as u32,
++ z: 0,
++ },
++ aspect: wgpu::TextureAspect::All,
++ },
++ wgpu::TexelCopyTextureInfo {
++ texture: &targets.sharp,
++ mip_level: 0,
++ origin: wgpu::Origin3d {
++ x: x0 as u32,
++ y: y0 as u32,
++ z: 0,
++ },
++ aspect: wgpu::TextureAspect::All,
++ },
++ wgpu::Extent3d {
++ width: (x1 - x0) as u32,
++ height: (y1 - y0) as u32,
++ depth_or_array_layers: 1,
++ },
++ );
++
++ let mut run_pass = |pipeline: &wgpu::RenderPipeline,
++ source: &wgpu::TextureView,
++ target: &wgpu::TextureView,
++ params: BackdropParams|
++ -> bool {
++ let Some((offset, size)) =
++ self.write_to_instance_buffer(instance_offset, bytemuck::bytes_of(&params))
++ else {
++ return false;
++ };
++ let bind_group = resources.device.create_bind_group(&wgpu::BindGroupDescriptor {
++ label: None,
++ layout: &resources.bind_group_layouts.backdrop_pass,
++ entries: &[
++ wgpu::BindGroupEntry {
++ binding: 0,
++ resource: self.instance_binding(offset, size),
++ },
++ wgpu::BindGroupEntry {
++ binding: 1,
++ resource: wgpu::BindingResource::TextureView(source),
++ },
++ wgpu::BindGroupEntry {
++ binding: 2,
++ resource: wgpu::BindingResource::Sampler(&resources.backdrop_sampler),
++ },
++ ],
++ });
++ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
++ label: Some("backdrop_pass"),
++ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
++ view: target,
++ resolve_target: None,
++ ops: wgpu::Operations {
++ load: wgpu::LoadOp::Load,
++ store: wgpu::StoreOp::Store,
++ },
++ depth_slice: None,
++ })],
++ depth_stencil_attachment: None,
++ ..Default::default()
++ });
++ pass.set_viewport(0., 0., reduced[0], reduced[1], 0., 1.);
++ pass.set_pipeline(pipeline);
++ pass.set_bind_group(0, &resources.globals_bind_group, &[]);
++ pass.set_bind_group(1, &bind_group, &[]);
++ pass.draw(0..3, 0..1);
++ true
++ };
++
++ let pipelines = &resources.pipelines;
++ let reduced_rect = [0., 0., reduced[0], reduced[1]];
++ let (blur_source, blur_source_rect) = if factor > 1. {
++ if !run_pass(
++ &pipelines.backdrop_downsample,
++ &targets.sharp_view,
++ &targets.pong_view,
++ BackdropParams {
++ source_rect: region,
++ params: [0., 0., factor, 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ },
++ ) {
++ return false;
++ }
++ (&targets.pong_view, reduced_rect)
++ } else {
++ (&targets.sharp_view, region)
++ };
++ if !run_pass(
++ &pipelines.backdrop_blur_pass,
++ blur_source,
++ &targets.ping_view,
++ BackdropParams {
++ source_rect: blur_source_rect,
++ params: [1., 0., sigma / factor, 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ },
++ ) || !run_pass(
++ &pipelines.backdrop_blur_pass,
++ &targets.ping_view,
++ &targets.pong_view,
++ BackdropParams {
++ source_rect: reduced_rect,
++ params: [0., 1., sigma / factor, 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ },
++ ) {
++ return false;
++ }
++
++ let params = BackdropParams {
++ source_rect: region,
++ params: [factor, 0., 0., 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ };
++ let Some((params_offset, params_size)) =
++ self.write_to_instance_buffer(instance_offset, bytemuck::bytes_of(&params))
++ else {
++ return false;
++ };
++ let blur_bytes = unsafe { Self::instance_bytes(std::slice::from_ref(blur)) };
++ let Some((blur_offset, blur_size)) = self.write_to_instance_buffer(instance_offset, blur_bytes)
++ else {
++ return false;
++ };
++ let bind_group = resources.device.create_bind_group(&wgpu::BindGroupDescriptor {
++ label: None,
++ layout: &resources.bind_group_layouts.backdrop_glass,
++ entries: &[
++ wgpu::BindGroupEntry {
++ binding: 0,
++ resource: self.instance_binding(params_offset, params_size),
++ },
++ wgpu::BindGroupEntry {
++ binding: 1,
++ resource: wgpu::BindingResource::TextureView(&targets.pong_view),
++ },
++ wgpu::BindGroupEntry {
++ binding: 2,
++ resource: wgpu::BindingResource::Sampler(&resources.backdrop_sampler),
++ },
++ wgpu::BindGroupEntry {
++ binding: 3,
++ resource: self.instance_binding(blur_offset, blur_size),
++ },
++ wgpu::BindGroupEntry {
++ binding: 4,
++ resource: wgpu::BindingResource::TextureView(&targets.sharp_view),
++ },
++ ],
++ });
++ let mut pass = Self::continue_main_pass(encoder, frame_view);
++ pass.set_pipeline(&pipelines.backdrop_glass);
++ pass.set_bind_group(0, &resources.globals_bind_group, &[]);
++ pass.set_bind_group(1, &bind_group, &[]);
++ pass.draw(0..4, 0..1);
++ true
++ }
++
+ fn draw_shadows(
+ &self,
+ shadows: &[Shadow],
+@@ -1937,3 +2381,22 @@ impl RenderingParameters {
+ }
+ }
+ }
++
++/// The draw order of a batch's first primitive -- where a backdrop blur
++/// whose order is not above it must be painted first.
++fn batch_first_order(scene: &Scene, batch: &PrimitiveBatch) -> DrawOrder {
++ match batch {
++ PrimitiveBatch::Shadows(range) => scene.shadows[range.start].order,
++ PrimitiveBatch::Quads(range) => scene.quads[range.start].order,
++ PrimitiveBatch::Paths(range) => scene.paths[range.start].order,
++ PrimitiveBatch::Underlines(range) => scene.underlines[range.start].order,
++ PrimitiveBatch::MonochromeSprites { range, .. } => {
++ scene.monochrome_sprites[range.start].order
++ }
++ PrimitiveBatch::SubpixelSprites { range, .. } => scene.subpixel_sprites[range.start].order,
++ PrimitiveBatch::PolychromeSprites { range, .. } => {
++ scene.polychrome_sprites[range.start].order
++ }
++ PrimitiveBatch::Surfaces(range) => scene.surfaces[range.start].order,
++ }
++}
+diff --git a/crates/gpui_windows/build.rs b/crates/gpui_windows/build.rs
+index 1db9571..0a6b4a0 100644
+--- a/crates/gpui_windows/build.rs
++++ b/crates/gpui_windows/build.rs
+@@ -38,6 +38,9 @@ mod shader_compilation {
+ "monochrome_sprite",
+ "subpixel_sprite",
+ "polychrome_sprite",
++ "backdrop_downsample",
++ "backdrop_blur_pass",
++ "backdrop_glass",
+ ];
+
+ let rust_binding_path = format!("{}/shaders_bytes.rs", out_dir);
+diff --git a/crates/gpui_windows/src/directx_renderer.rs b/crates/gpui_windows/src/directx_renderer.rs
+index e4b605b..1a88981 100644
+--- a/crates/gpui_windows/src/directx_renderer.rs
++++ b/crates/gpui_windows/src/directx_renderer.rs
+@@ -53,6 +53,34 @@ pub(crate) struct DirectXRenderer {
+ /// In that case we want to discard the first frame that we draw as we got reset in the middle of a frame
+ /// meaning we lost all the allocated gpu textures and scene resources.
+ skip_draws: bool,
++
++ /// Snapshot and blur targets for backdrop blurs, dropped again after
++ /// [`BACKDROP_RELEASE_AFTER_FRAMES`] frames without one.
++ backdrop_textures: Option<BackdropTextures>,
++ blur_free_frames: u32,
++}
++
++const BACKDROP_RELEASE_AFTER_FRAMES: u32 = 120;
++
++/// Drawable-sized textures for backdrop blurs: `sharp` holds the snapshot
++/// at the region's own position; `ping` and `pong` hold it downsampled and
++/// blurred from texel 0.
++struct BackdropTextures {
++ width: u32,
++ height: u32,
++ sharp: ID3D11Texture2D,
++ sharp_view: Option<ID3D11ShaderResourceView>,
++ ping: (Option<ID3D11RenderTargetView>, Option<ID3D11ShaderResourceView>),
++ pong: (Option<ID3D11RenderTargetView>, Option<ID3D11ShaderResourceView>),
++}
++
++#[derive(Debug, Default, Clone, Copy)]
++#[repr(C)]
++struct BackdropParams {
++ source_rect: [f32; 4],
++ params: [f32; 4],
++ source_texture_size: [f32; 2],
++ sharp_texture_size: [f32; 2],
+ }
+
+ /// Direct3D objects
+@@ -91,11 +119,16 @@ struct DirectXRenderPipelines {
+ mono_sprites: PipelineState<MonochromeSprite>,
+ subpixel_sprites: PipelineState<SubpixelSprite>,
+ poly_sprites: PipelineState<PolychromeSprite>,
++ backdrop_glass: PipelineState<BackdropBlur>,
++ backdrop_downsample: (ID3D11VertexShader, ID3D11PixelShader),
++ backdrop_blur_pass: (ID3D11VertexShader, ID3D11PixelShader),
+ }
+
+ struct DirectXGlobalElements {
+ global_params_buffer: Option<ID3D11Buffer>,
+ sampler: Option<ID3D11SamplerState>,
++ backdrop_params_buffer: Option<ID3D11Buffer>,
++ backdrop_sampler: Option<ID3D11SamplerState>,
+ }
+
+ struct Annotation<'a>(&'a ID3DUserDefinedAnnotation);
+@@ -192,6 +225,8 @@ impl DirectXRenderer {
+ width: 1,
+ height: 1,
+ skip_draws: false,
++ backdrop_textures: None,
++ blur_free_frames: 0,
+ })
+ }
+
+@@ -314,6 +349,7 @@ impl DirectXRenderer {
+ self.resources = Some(resources);
+ self.globals = globals;
+ self.pipelines = pipelines;
++ self.backdrop_textures = None;
+ self.direct_composition = direct_composition;
+ self.skip_draws = true;
+ Ok(())
+@@ -336,12 +372,30 @@ impl DirectXRenderer {
+
+ self.upload_scene_buffers(scene)?;
+
++ if scene.backdrop_blurs.is_empty() {
++ self.blur_free_frames = self.blur_free_frames.saturating_add(1);
++ if self.blur_free_frames >= BACKDROP_RELEASE_AFTER_FRAMES {
++ self.backdrop_textures = None;
++ }
++ } else {
++ self.blur_free_frames = 0;
++ }
++
+ let annotation = self
+ .devices
+ .as_ref()
+ .and_then(|devices| devices.annotation.clone())
+ .filter(|annotation| unsafe { annotation.GetStatus().as_bool() });
++ let mut pending_blurs = scene.backdrop_blurs.iter().peekable();
+ for batch in scene.batches() {
++ // Backdrop blurs interleave by draw order outside the batch
++ // stream: everything painted so far is snapshotted, blurred and
++ // painted back before the batch drawn above it.
++ while let Some(blur) =
++ pending_blurs.next_if(|blur| blur.order <= batch_first_order(scene, &batch))
++ {
++ self.draw_backdrop_blur(blur)?;
++ }
+ let _annotation = annotation
+ .as_ref()
+ .map(|annotation| Annotation::new(annotation, HSTRING::from(batch.label())));
+@@ -380,9 +434,222 @@ impl DirectXRenderer {
+ )
+ })?;
+ }
++ for blur in pending_blurs {
++ self.draw_backdrop_blur(blur)?;
++ }
+ self.present()
+ }
+
++ fn ensure_backdrop_textures(&mut self) -> Result<()> {
++ let (width, height) = (self.width, self.height);
++ if self
++ .backdrop_textures
++ .as_ref()
++ .is_some_and(|textures| textures.width == width && textures.height == height)
++ {
++ return Ok(());
++ }
++ let device = &self.devices.as_ref().context("devices missing")?.device;
++ let texture = |bind: u32| -> Result<ID3D11Texture2D> {
++ let desc = D3D11_TEXTURE2D_DESC {
++ Width: width,
++ Height: height,
++ MipLevels: 1,
++ ArraySize: 1,
++ Format: RENDER_TARGET_FORMAT,
++ SampleDesc: DXGI_SAMPLE_DESC {
++ Count: 1,
++ Quality: 0,
++ },
++ Usage: D3D11_USAGE_DEFAULT,
++ BindFlags: bind,
++ CPUAccessFlags: 0,
++ MiscFlags: 0,
++ };
++ let mut output = None;
++ unsafe { device.CreateTexture2D(&desc, None, Some(&mut output))? };
++ Ok(output.unwrap())
++ };
++ let target = || -> Result<(Option<ID3D11RenderTargetView>, Option<ID3D11ShaderResourceView>)> {
++ let texture = texture((D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32)?;
++ let mut target_view = None;
++ let mut shader_view = None;
++ unsafe {
++ device.CreateRenderTargetView(&texture, None, Some(&mut target_view))?;
++ device.CreateShaderResourceView(&texture, None, Some(&mut shader_view))?;
++ }
++ Ok((target_view, shader_view))
++ };
++ let sharp = texture(D3D11_BIND_SHADER_RESOURCE.0 as u32)?;
++ let mut sharp_view = None;
++ unsafe { device.CreateShaderResourceView(&sharp, None, Some(&mut sharp_view))? };
++ self.backdrop_textures = Some(BackdropTextures {
++ width,
++ height,
++ sharp,
++ sharp_view,
++ ping: target()?,
++ pong: target()?,
++ });
++ Ok(())
++ }
++
++ /// Snapshot the region under `blur` (plus room for the gaussian tail and
++ /// any glass lensing), downsample and blur it, then paint it back.
++ /// Downsampling keeps every blur under ~13 taps per side: a gaussian
++ /// that wide is indistinguishable from one taken at full resolution.
++ fn draw_backdrop_blur(&mut self, blur: &BackdropBlur) -> Result<()> {
++ let sigma = blur.blur_radius.0.max(1.0);
++ let padding = (sigma * 3.0 + blur.glass.reach()).ceil() + 2.0;
++ let visible = blur.bounds.intersect(&blur.content_mask.bounds);
++ let (width, height) = (self.width as i64, self.height as i64);
++ let x0 = ((visible.origin.x.0 - padding).floor() as i64).max(0);
++ let y0 = ((visible.origin.y.0 - padding).floor() as i64).max(0);
++ let x1 = ((visible.origin.x.0 + visible.size.width.0 + padding).ceil() as i64).min(width);
++ let y1 = ((visible.origin.y.0 + visible.size.height.0 + padding).ceil() as i64).min(height);
++ if x1 <= x0 || y1 <= y0 {
++ return Ok(());
++ }
++ self.ensure_backdrop_textures()?;
++ let factor = (sigma / 4.).floor().clamp(1., 8.);
++ let region = [x0 as f32, y0 as f32, (x1 - x0) as f32, (y1 - y0) as f32];
++ let reduced = [(region[2] / factor).ceil(), (region[3] / factor).ceil()];
++ let full = [self.width as f32, self.height as f32];
++
++ let devices = self.devices.as_ref().context("devices missing")?;
++ let context = &devices.device_context;
++ let resources = self.resources.as_ref().context("resources missing")?;
++ let render_target = resources.render_target.as_ref().context("render target missing")?;
++ let textures = self.backdrop_textures.as_ref().context("backdrop textures missing")?;
++ let params_buffer = &self.globals.backdrop_params_buffer;
++ let sampler = slice::from_ref(&self.globals.backdrop_sampler);
++ let no_views: [Option<ID3D11ShaderResourceView>; 3] = [None, None, None];
++
++ let set_params = |params: BackdropParams| -> Result<()> {
++ if let Some(buffer) = params_buffer {
++ update_buffer(context, buffer, slice::from_ref(&params))?;
++ }
++ Ok(())
++ };
++ let reduced_viewport = D3D11_VIEWPORT {
++ TopLeftX: 0.,
++ TopLeftY: 0.,
++ Width: reduced[0],
++ Height: reduced[1],
++ MinDepth: 0.,
++ MaxDepth: 1.,
++ };
++ let run_pass = |shaders: &(ID3D11VertexShader, ID3D11PixelShader),
++ source: &Option<ID3D11ShaderResourceView>,
++ target: &Option<ID3D11RenderTargetView>,
++ params: BackdropParams|
++ -> Result<()> {
++ set_params(params)?;
++ unsafe {
++ context.PSSetShaderResources(1, Some(&no_views));
++ context.OMSetRenderTargets(Some(slice::from_ref(target)), None);
++ context.RSSetViewports(Some(slice::from_ref(&reduced_viewport)));
++ context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
++ context.VSSetShader(&shaders.0, None);
++ context.PSSetShader(&shaders.1, None);
++ context.PSSetConstantBuffers(1, Some(slice::from_ref(params_buffer)));
++ context.PSSetSamplers(1, Some(sampler));
++ context.PSSetShaderResources(2, Some(slice::from_ref(source)));
++ context.OMSetBlendState(&self.pipelines.backdrop_glass.blend_state, None, 0xFFFFFFFF);
++ context.Draw(3, 0);
++ context.PSSetShaderResources(2, Some(&[None]));
++ }
++ Ok(())
++ };
++
++ unsafe {
++ context.CopySubresourceRegion(
++ &textures.sharp,
++ 0,
++ x0 as u32,
++ y0 as u32,
++ 0,
++ render_target,
++ 0,
++ Some(&D3D11_BOX {
++ left: x0 as u32,
++ top: y0 as u32,
++ front: 0,
++ right: x1 as u32,
++ bottom: y1 as u32,
++ back: 1,
++ }),
++ );
++ }
++ let reduced_rect = [0., 0., reduced[0], reduced[1]];
++ let blur_source = if factor > 1. {
++ run_pass(
++ &self.pipelines.backdrop_downsample,
++ &textures.sharp_view,
++ &textures.pong.0,
++ BackdropParams {
++ source_rect: region,
++ params: [0., 0., factor, 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ },
++ )?;
++ (&textures.pong.1, reduced_rect)
++ } else {
++ (&textures.sharp_view, region)
++ };
++ run_pass(
++ &self.pipelines.backdrop_blur_pass,
++ blur_source.0,
++ &textures.ping.0,
++ BackdropParams {
++ source_rect: blur_source.1,
++ params: [1., 0., sigma / factor, 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ },
++ )?;
++ run_pass(
++ &self.pipelines.backdrop_blur_pass,
++ &textures.ping.1,
++ &textures.pong.0,
++ BackdropParams {
++ source_rect: reduced_rect,
++ params: [0., 1., sigma / factor, 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ },
++ )?;
++
++ set_params(BackdropParams {
++ source_rect: region,
++ params: [factor, 0., 0., 0.],
++ source_texture_size: full,
++ sharp_texture_size: full,
++ })?;
++ self.pipelines
++ .backdrop_glass
++ .update_buffer(&devices.device, context, slice::from_ref(blur))?;
++ unsafe {
++ context.OMSetRenderTargets(Some(slice::from_ref(&resources.render_target_view)), None);
++ context.PSSetConstantBuffers(1, Some(slice::from_ref(params_buffer)));
++ context.PSSetSamplers(1, Some(sampler));
++ context.PSSetShaderResources(2, Some(&[textures.pong.1.clone(), textures.sharp_view.clone()]));
++ }
++ self.pipelines.backdrop_glass.draw(
++ context,
++ slice::from_ref(&resources.viewport),
++ slice::from_ref(&self.globals.global_params_buffer),
++ D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,
++ 4,
++ 1,
++ )?;
++ unsafe {
++ context.PSSetShaderResources(2, Some(&[None, None]));
++ }
++ Ok(())
++ }
++
+ pub(crate) fn resize(&mut self, new_size: Size<DevicePixels>) -> Result<()> {
+ let width = new_size.width.0.max(1) as u32;
+ let height = new_size.height.0.max(1) as u32;
+@@ -917,6 +1184,22 @@ impl DirectXRenderPipelines {
+ create_blend_state(device)?,
+ )?;
+
++ let backdrop_glass = PipelineState::new(
++ device,
++ "backdrop_glass_pipeline",
++ ShaderModule::BackdropGlass,
++ 4,
++ create_replace_blend_state(device)?,
++ )?;
++ let pass_shaders = |module: ShaderModule| -> Result<(ID3D11VertexShader, ID3D11PixelShader)> {
++ let vertex = RawShaderBytes::new(module, ShaderTarget::Vertex)?;
++ let fragment = RawShaderBytes::new(module, ShaderTarget::Fragment)?;
++ Ok((
++ create_vertex_shader(device, vertex.as_bytes())?,
++ create_fragment_shader(device, fragment.as_bytes())?,
++ ))
++ };
++
+ Ok(Self {
+ shadow_pipeline,
+ quad_pipeline,
+@@ -926,6 +1209,9 @@ impl DirectXRenderPipelines {
+ mono_sprites,
+ subpixel_sprites,
+ poly_sprites,
++ backdrop_glass,
++ backdrop_downsample: pass_shaders(ShaderModule::BackdropDownsample)?,
++ backdrop_blur_pass: pass_shaders(ShaderModule::BackdropBlurPass)?,
+ })
+ }
+ }
+@@ -986,9 +1272,41 @@ impl DirectXGlobalElements {
+ output
+ };
+
++ let backdrop_params_buffer = unsafe {
++ let desc = D3D11_BUFFER_DESC {
++ ByteWidth: std::mem::size_of::<BackdropParams>() as u32,
++ Usage: D3D11_USAGE_DYNAMIC,
++ BindFlags: D3D11_BIND_CONSTANT_BUFFER.0 as u32,
++ CPUAccessFlags: D3D11_CPU_ACCESS_WRITE.0 as u32,
++ ..Default::default()
++ };
++ let mut buffer = None;
++ device.CreateBuffer(&desc, None, Some(&mut buffer))?;
++ buffer
++ };
++ let backdrop_sampler = unsafe {
++ let desc = D3D11_SAMPLER_DESC {
++ Filter: D3D11_FILTER_MIN_MAG_MIP_LINEAR,
++ AddressU: D3D11_TEXTURE_ADDRESS_CLAMP,
++ AddressV: D3D11_TEXTURE_ADDRESS_CLAMP,
++ AddressW: D3D11_TEXTURE_ADDRESS_CLAMP,
++ MipLODBias: 0.0,
++ MaxAnisotropy: 1,
++ ComparisonFunc: D3D11_COMPARISON_ALWAYS,
++ BorderColor: [0.0; 4],
++ MinLOD: 0.0,
++ MaxLOD: D3D11_FLOAT32_MAX,
++ };
++ let mut output = None;
++ device.CreateSamplerState(&desc, Some(&mut output))?;
++ output
++ };
++
+ Ok(Self {
+ global_params_buffer,
+ sampler,
++ backdrop_params_buffer,
++ backdrop_sampler,
+ })
+ }
+ }
+@@ -1432,6 +1750,18 @@ fn create_blend_state(device: &ID3D11Device) -> Result<ID3D11BlendState> {
+ }
+ }
+
++/// The backdrop passes write their final colour, coverage already applied.
++fn create_replace_blend_state(device: &ID3D11Device) -> Result<ID3D11BlendState> {
++ let mut desc = D3D11_BLEND_DESC::default();
++ desc.RenderTarget[0].BlendEnable = false.into();
++ desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL.0 as u8;
++ unsafe {
++ let mut state = None;
++ device.CreateBlendState(&desc, Some(&mut state))?;
++ Ok(state.unwrap())
++ }
++}
++
+ #[inline]
+ fn create_blend_state_for_subpixel_rendering(device: &ID3D11Device) -> Result<ID3D11BlendState> {
+ let mut desc = D3D11_BLEND_DESC::default();
+@@ -1614,6 +1944,25 @@ fn report_live_objects(device: &ID3D11Device) -> Result<()> {
+ Ok(())
+ }
+
++/// The draw order of a batch's first primitive -- where a backdrop blur
++/// whose order is not above it must be painted first.
++fn batch_first_order(scene: &Scene, batch: &PrimitiveBatch) -> DrawOrder {
++ match batch {
++ PrimitiveBatch::Shadows(range) => scene.shadows[range.start].order,
++ PrimitiveBatch::Quads(range) => scene.quads[range.start].order,
++ PrimitiveBatch::Paths(range) => scene.paths[range.start].order,
++ PrimitiveBatch::Underlines(range) => scene.underlines[range.start].order,
++ PrimitiveBatch::MonochromeSprites { range, .. } => {
++ scene.monochrome_sprites[range.start].order
++ }
++ PrimitiveBatch::SubpixelSprites { range, .. } => scene.subpixel_sprites[range.start].order,
++ PrimitiveBatch::PolychromeSprites { range, .. } => {
++ scene.polychrome_sprites[range.start].order
++ }
++ PrimitiveBatch::Surfaces(range) => scene.surfaces[range.start].order,
++ }
++}
++
+ const BUFFER_COUNT: usize = 3;
+
+ pub(crate) mod shader_resources {
+@@ -1639,6 +1988,9 @@ pub(crate) mod shader_resources {
+ SubpixelSprite,
+ PolychromeSprite,
+ EmojiRasterization,
++ BackdropDownsample,
++ BackdropBlurPass,
++ BackdropGlass,
+ }
+
+ #[derive(Copy, Clone, Debug, Eq, PartialEq)]
+@@ -1716,6 +2068,18 @@ pub(crate) mod shader_resources {
+ ShaderTarget::Vertex => EMOJI_RASTERIZATION_VERTEX_BYTES,
+ ShaderTarget::Fragment => EMOJI_RASTERIZATION_FRAGMENT_BYTES,
+ },
++ ShaderModule::BackdropDownsample => match target {
++ ShaderTarget::Vertex => BACKDROP_DOWNSAMPLE_VERTEX_BYTES,
++ ShaderTarget::Fragment => BACKDROP_DOWNSAMPLE_FRAGMENT_BYTES,
++ },
++ ShaderModule::BackdropBlurPass => match target {
++ ShaderTarget::Vertex => BACKDROP_BLUR_PASS_VERTEX_BYTES,
++ ShaderTarget::Fragment => BACKDROP_BLUR_PASS_FRAGMENT_BYTES,
++ },
++ ShaderModule::BackdropGlass => match target {
++ ShaderTarget::Vertex => BACKDROP_GLASS_VERTEX_BYTES,
++ ShaderTarget::Fragment => BACKDROP_GLASS_FRAGMENT_BYTES,
++ },
+ };
+ Self { inner: bytes }
+ }
+@@ -1803,6 +2167,9 @@ pub(crate) mod shader_resources {
+ ShaderModule::SubpixelSprite => "subpixel_sprite",
+ ShaderModule::PolychromeSprite => "polychrome_sprite",
+ ShaderModule::EmojiRasterization => "emoji_rasterization",
++ ShaderModule::BackdropDownsample => "backdrop_downsample",
++ ShaderModule::BackdropBlurPass => "backdrop_blur_pass",
++ ShaderModule::BackdropGlass => "backdrop_glass",
+ }
+ }
+ }
+diff --git a/crates/gpui_windows/src/shaders.hlsl b/crates/gpui_windows/src/shaders.hlsl
+index 78b1ada..24bd265 100644
+--- a/crates/gpui_windows/src/shaders.hlsl
++++ b/crates/gpui_windows/src/shaders.hlsl
+@@ -1289,3 +1289,198 @@ float4 polychrome_sprite_fragment(PolychromeSpriteFragmentInput input): SV_Targe
+ color.a *= sprite.opacity * saturate(0.5 - distance) * edge_fade_alpha(input.position.xy, sprite.fade);
+ return color;
+ }
++
++/*
++**
++** Backdrop blur and glass
++**
++*/
++
++// Mirrors gpui's `GlassStyle`; `line` is reserved in HLSL, hence
++// `line_strength` for the same field.
++struct GlassStyle {
++ uint enabled;
++ float lens_rim;
++ float lens_curve;
++ float rim_bend;
++ float poly[30];
++ float opacity;
++ float line_strength;
++ float line_lit;
++ float line_width;
++ float light_x;
++ float light_y;
++ float glow;
++ float glow_width;
++ float hairline[4];
++};
++
++struct BackdropBlur {
++ uint order;
++ float blur_radius;
++ Bounds bounds;
++ Bounds content_mask;
++ Corners corner_radii;
++ GlassStyle glass;
++};
++
++// One constant buffer serves every backdrop pass. For the downsample and
++// blur passes `source_rect` is the texel rect of the source texture being
++// read and `params` is (direction x, direction y, sigma in source texels or
++// the downsample factor, unused). For the glass pass `source_rect` is the
++// drawable rect that was blurred, `params.x` the factor it was blurred at
++// (the blurred texture holds it from texel 0, scaled down by that factor),
++// and `sharp_texture_size` the size of the drawable-sized unblurred copy.
++cbuffer BackdropParams: register(b1) {
++ float4 source_rect;
++ float4 params;
++ float2 source_texture_size;
++ float2 sharp_texture_size;
++};
++
++Texture2D<float4> t_backdrop: register(t2);
++Texture2D<float4> t_backdrop_sharp: register(t3);
++SamplerState s_backdrop: register(s1);
++
++struct BackdropPassOutput {
++ float4 position: SV_Position;
++ float2 uv: TEXCOORD0;
++};
++
++BackdropPassOutput backdrop_pass_vertex(uint vertex_id: SV_VertexID) {
++ float2 uv = float2((vertex_id << 1) & 2, vertex_id & 2);
++ BackdropPassOutput output;
++ output.position = float4(uv * float2(2., -2.) + float2(-1., 1.), 0., 1.);
++ output.uv = uv;
++ return output;
++}
++
++BackdropPassOutput backdrop_downsample_vertex(uint vertex_id: SV_VertexID) {
++ return backdrop_pass_vertex(vertex_id);
++}
++
++BackdropPassOutput backdrop_blur_pass_vertex(uint vertex_id: SV_VertexID) {
++ return backdrop_pass_vertex(vertex_id);
++}
++
++float2 backdrop_source_uv(float2 uv) {
++ return (source_rect.xy + uv * source_rect.zw) / source_texture_size;
++}
++
++float4 backdrop_downsample_fragment(BackdropPassOutput input): SV_Target {
++ float2 texel = 1. / source_texture_size;
++ float2 center = backdrop_source_uv(input.uv);
++ float2 quarter = texel * params.z * 0.25;
++ float2 lo = (source_rect.xy + 0.5) * texel;
++ float2 hi = (source_rect.xy + source_rect.zw - 0.5) * texel;
++ float4 sum = t_backdrop.SampleLevel(s_backdrop, clamp(center + float2(-quarter.x, -quarter.y), lo, hi), 0);
++ sum += t_backdrop.SampleLevel(s_backdrop, clamp(center + float2(quarter.x, -quarter.y), lo, hi), 0);
++ sum += t_backdrop.SampleLevel(s_backdrop, clamp(center + float2(-quarter.x, quarter.y), lo, hi), 0);
++ sum += t_backdrop.SampleLevel(s_backdrop, clamp(center + float2(quarter.x, quarter.y), lo, hi), 0);
++ return 0.25 * sum;
++}
++
++float4 backdrop_blur_pass_fragment(BackdropPassOutput input): SV_Target {
++ float sigma = max(params.z, 0.5);
++ int radius = (int)ceil(sigma * 3.);
++ float2 step = params.xy / source_texture_size;
++ float2 uv = backdrop_source_uv(input.uv);
++ float2 lo = (source_rect.xy + 0.5) / source_texture_size;
++ float2 hi = (source_rect.xy + source_rect.zw - 0.5) / source_texture_size;
++ float4 sum = 0.;
++ float total = 0.;
++ [loop]
++ for (int i = -radius; i <= radius; i++) {
++ float weight = exp(-(float)(i * i) / (2. * sigma * sigma));
++ sum += weight * t_backdrop.SampleLevel(s_backdrop, clamp(uv + step * (float)i, lo, hi), 0);
++ total += weight;
++ }
++ return sum / total;
++}
++
++StructuredBuffer<BackdropBlur> backdrop_blurs: register(t1);
++
++struct BackdropGlassVertexOutput {
++ float4 position: SV_Position;
++ nointerpolation uint blur_id: TEXCOORD0;
++ float4 clip_distance: SV_ClipDistance;
++};
++
++BackdropGlassVertexOutput backdrop_glass_vertex(uint vertex_id: SV_VertexID, uint blur_id: SV_InstanceID) {
++ float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
++ BackdropBlur blur = backdrop_blurs[blur_id];
++ // Glass draws a hairline in the pixel ring just outside its edge.
++ Bounds bounds = blur.bounds;
++ if (blur.glass.enabled != 0u) {
++ bounds.origin -= 2.;
++ bounds.size += 4.;
++ }
++ BackdropGlassVertexOutput output;
++ output.position = to_device_position(unit_vertex, bounds);
++ output.blur_id = blur_id;
++ output.clip_distance = distance_from_clip_rect(unit_vertex, bounds, blur.content_mask);
++ return output;
++}
++
++// Clamped to the blurred region: texels beyond it hold an older pass.
++float2 backdrop_blurred_uv(float2 position) {
++ float2 texel = (position - source_rect.xy) / params.x;
++ texel = clamp(texel, 0.5, source_rect.zw / params.x - 0.5);
++ return texel / source_texture_size;
++}
++
++float4 backdrop_glass_fragment(BackdropGlassVertexOutput input): SV_Target {
++ BackdropBlur blur = backdrop_blurs[input.blur_id];
++ float2 position = input.position.xy;
++ float distance = quad_sdf(position, blur.bounds, blur.corner_radii);
++
++ if (blur.glass.enabled == 0u) {
++ if (distance > 0.) {
++ discard;
++ }
++ return t_backdrop.SampleLevel(s_backdrop, backdrop_blurred_uv(position), 0);
++ }
++
++ float coverage = saturate(0.5 - distance);
++ float ring = saturate(1.5 - distance) - coverage;
++ if (coverage <= 0. && ring <= 0.) {
++ discard;
++ }
++ GlassStyle glass = blur.glass;
++ float edge = max(-distance, 0.);
++ float2 normal = float2(
++ quad_sdf(position + float2(0.5, 0.), blur.bounds, blur.corner_radii) -
++ quad_sdf(position - float2(0.5, 0.), blur.bounds, blur.corner_radii),
++ quad_sdf(position + float2(0., 0.5), blur.bounds, blur.corner_radii) -
++ quad_sdf(position - float2(0., 0.5), blur.bounds, blur.corner_radii));
++ normal /= max(length(normal), 1e-6);
++ float vertical = normal.x * normal.x;
++ float hair_a = lerp(glass.hairline[2], glass.hairline[0], vertical);
++ float hair_b = lerp(glass.hairline[3], glass.hairline[1], vertical);
++ float4 under = t_backdrop_sharp.SampleLevel(s_backdrop, position / sharp_texture_size, 0);
++ float3 hair_color = saturate(min(under.rgb, hair_a * under.rgb + hair_b));
++ under.rgb = lerp(under.rgb, hair_color, ring);
++ if (coverage <= 0.) {
++ return under;
++ }
++ float rim = 1. - pow(saturate(edge / glass.lens_rim), glass.lens_curve);
++ float2 sample_at = position + normal * glass.rim_bend * rim;
++ float4 backdrop = t_backdrop.SampleLevel(s_backdrop, backdrop_blurred_uv(sample_at), 0);
++ float3 color = backdrop.a > 0. ? backdrop.rgb / backdrop.a : float3(0., 0., 0.);
++ float alpha = lerp(backdrop.a, 1., glass.opacity);
++
++ float features[10] = {color.r, color.g, color.b, color.r * color.r, color.g * color.g,
++ color.b * color.b, color.r * color.g, color.g * color.b,
++ color.r * color.b, 1.};
++ color = float3(0., 0., 0.);
++ [unroll]
++ for (int term = 0; term < 10; term++) {
++ color += features[term] * float3(glass.poly[term], glass.poly[10 + term], glass.poly[20 + term]);
++ }
++ float facing = dot(normal, float2(glass.light_x, glass.light_y));
++ float line_term = exp(-edge / glass.line_width) * (glass.line_strength + glass.line_lit * facing);
++ float glow = exp(-edge / glass.glow_width) * glass.glow;
++ color = saturate(color * (1. + line_term) + glow);
++
++ return lerp(under, float4(color * alpha, alpha), coverage);
++}
diff --git a/apps/desktop-gpui/src/glass_probe.rs b/apps/desktop-gpui/src/glass_probe.rs
new file mode 100644
index 000000000..40760fee5
--- /dev/null
+++ b/apps/desktop-gpui/src/glass_probe.rs
@@ -0,0 +1,256 @@
+use std::{path::PathBuf, sync::Arc, time::Duration};
+
+use cap_glass::{Rgba, Shape, metrics, model, scenes};
+use cap_recording::sources::screen_capture::ScreenCaptureTarget;
+use gpui::{
+ App, AppContext as _, AsyncApp, Context, IntoElement, ParentElement, Render, RenderImage,
+ Styled, Window, WindowBackgroundAppearance, WindowBounds, WindowHandle, WindowKind,
+ WindowOptions, div, img, point, px, size,
+};
+
+use crate::ui::glass::{self, GlassKind};
+
+const PRESETS: [(&str, GlassKind, bool); 4] = [
+ ("regular-light", GlassKind::Regular, false),
+ ("regular-dark", GlassKind::Regular, true),
+ ("clear-light", GlassKind::Clear, false),
+ ("clear-dark", GlassKind::Clear, true),
+];
+
+/// `CAP_GLASS_PROBE_OUTPUT=<dir>`: draw every scene in `cap_glass::scenes`
+/// with glass panes in each preset, capture the window, and score it against
+/// the CPU model of the same material. Writes the captures, the model's
+/// renders and `report.json`, whose `passed` is the CI verdict.
+pub fn run(cx: &mut App) {
+ let Some(output) = std::env::var_os("CAP_GLASS_PROBE_OUTPUT").map(PathBuf::from) else {
+ return;
+ };
+ if let Err(error) = std::fs::create_dir_all(&output) {
+ tracing::error!(%error, "glass probe: cannot create its output directory");
+ return;
+ }
+ // An X11 window capture reads whatever is on screen over the window.
+ crate::app_windows::hide_main_window(cx);
+ let handle = cx.open_window(
+ WindowOptions {
+ window_bounds: Some(WindowBounds::Windowed(gpui::Bounds {
+ origin: point(px(120.), px(120.)),
+ size: size(px(scenes::CANVAS.0), px(scenes::CANVAS.1)),
+ })),
+ titlebar: None,
+ kind: WindowKind::Normal,
+ focus: false,
+ show: true,
+ is_resizable: false,
+ window_background: WindowBackgroundAppearance::Opaque,
+ ..Default::default()
+ },
+ |_, cx| cx.new(|_| ProbeView::default()),
+ );
+ let handle = match handle {
+ Ok(handle) => handle,
+ Err(error) => {
+ tracing::error!("glass probe: window failed to open: {error:#}");
+ return;
+ }
+ };
+ cx.spawn(async move |cx| {
+ let report = probe(handle, &output, cx).await;
+ if let Err(error) = std::fs::write(output.join("report.json"), report.to_string()) {
+ tracing::error!(%error, "glass probe: could not write its report");
+ }
+ cx.update(|cx| cx.quit());
+ })
+ .detach();
+}
+
+#[derive(Default)]
+struct ProbeView {
+ scene: Option<Arc<RenderImage>>,
+ preset: Option<(GlassKind, bool)>,
+}
+
+impl Render for ProbeView {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ let mut root = div()
+ .size_full()
+ .relative()
+ .bg(gpui::white())
+ .children(self.scene.clone().map(|scene| img(scene).absolute().size_full()));
+ if let Some((kind, dark)) = self.preset {
+ for shape in scenes::SHAPES {
+ root = root.child(
+ glass::pane(kind, dark, shape.radius)
+ .absolute()
+ .left(px(shape.x))
+ .top(px(shape.y))
+ .w(px(shape.width))
+ .h(px(shape.height)),
+ );
+ }
+ }
+ root
+ }
+}
+
+async fn sleep(cx: &mut AsyncApp, millis: u64) {
+ cx.background_executor()
+ .timer(Duration::from_millis(millis))
+ .await;
+}
+
+async fn probe(
+ handle: WindowHandle<ProbeView>,
+ output: &std::path::Path,
+ cx: &mut AsyncApp,
+) -> serde_json::Value {
+ sleep(cx, 1500).await;
+ let scale = handle
+ .update(cx, |_, window, _| window.scale_factor())
+ .unwrap_or(1.);
+ let Some(target) = handle
+ .update(cx, |_, window, _| window_target(window))
+ .ok()
+ .flatten()
+ else {
+ return serde_json::json!({ "passed": false, "error": "no window id to capture" });
+ };
+
+ let mut cases = Vec::new();
+ for scene_name in scenes::NAMES {
+ let Some(scene) = scenes::render(scene_name, scale) else {
+ continue;
+ };
+ let image = to_rgba_image(&scene);
+ let render_image = crate::library::rgba_to_render_image(image);
+ for (preset_name, kind, dark) in PRESETS {
+ handle
+ .update(cx, |view, window, cx| {
+ view.scene = Some(render_image.clone());
+ view.preset = Some((kind, dark));
+ cx.notify();
+ window.refresh();
+ })
+ .ok();
+ sleep(cx, 400).await;
+ let name = format!("{scene_name}-{preset_name}");
+ let captured = {
+ let target = target.clone();
+ gpui_tokio::Tokio::spawn(cx, async move {
+ cap_recording::screenshot::capture_screenshot(target).await
+ })
+ .await
+ };
+ let captured = match captured {
+ Ok(Ok(image)) => image.to_rgba8(),
+ Ok(Err(error)) => {
+ cases.push(serde_json::json!({ "name": name, "error": format!("{error:#}") }));
+ continue;
+ }
+ Err(error) => {
+ cases.push(serde_json::json!({ "name": name, "error": error.to_string() }));
+ continue;
+ }
+ };
+ let captured = crop_to(&captured, scene.width as u32, scene.height as u32);
+ let captured = Rgba {
+ width: captured.width() as usize,
+ height: captured.height() as usize,
+ pixels: captured.as_raw().iter().map(|value| *value as f32).collect(),
+ };
+ let expected = expected_render(&scene, glass::material(kind, dark), scale);
+ let mask = glass_mask(scale, captured.width, captured.height);
+ let ssim = metrics::ssim(&captured, &expected, &mask);
+ let (delta_e, delta_e95) = metrics::delta_e(&captured, &expected, &mask);
+ let _ = captured.save(&output.join(format!("{name}.png")));
+ let _ = expected.save(&output.join(format!("{name}-model.png")));
+ cases.push(serde_json::json!({
+ "name": name,
+ "ssim": ssim,
+ "deltaE": delta_e,
+ "deltaE95": delta_e95,
+ "passed": ssim >= 0.97 && delta_e <= 2.0,
+ }));
+ }
+ }
+ let passed = !cases.is_empty()
+ && cases
+ .iter()
+ .all(|case| case["passed"].as_bool() == Some(true));
+ serde_json::json!({ "passed": passed, "scale": scale, "cases": cases })
+}
+
+fn to_rgba_image(image: &Rgba) -> image::RgbaImage {
+ let bytes = image
+ .pixels
+ .iter()
+ .map(|value| value.round().clamp(0., 255.) as u8)
+ .collect();
+ image::RgbaImage::from_raw(image.width as u32, image.height as u32, bytes)
+ .expect("scene buffer matches its size")
+}
+
+/// A window capture can carry a frame or shadow around the content; the
+/// content is the centred `width` x `height`.
+fn crop_to(image: &image::RgbaImage, width: u32, height: u32) -> image::RgbaImage {
+ if image.width() == width && image.height() == height {
+ return image.clone();
+ }
+ let x = image.width().saturating_sub(width) / 2;
+ let y = image.height().saturating_sub(height) / 2;
+ image::imageops::crop_imm(image, x, y, width.min(image.width()), height.min(image.height()))
+ .to_image()
+}
+
+fn scaled(shape: &Shape, scale: f32) -> Shape {
+ Shape {
+ x: shape.x * scale,
+ y: shape.y * scale,
+ width: shape.width * scale,
+ height: shape.height * scale,
+ radius: shape.radius * scale,
+ }
+}
+
+fn expected_render(scene: &Rgba, material: &model::GlassMaterial, scale: f32) -> Rgba {
+ let mut out = scene.clone();
+ for shape in scenes::SHAPES {
+ let style = material.resolve(shape.width, shape.height).scaled(scale);
+ let device = scaled(&shape, scale);
+ model::composite(&style, &device, &scene.blurred(style.blur), &mut out);
+ }
+ out
+}
+
+fn glass_mask(scale: f32, width: usize, height: usize) -> Vec<bool> {
+ (0..width * height)
+ .map(|index| {
+ let (x, y) = ((index % width) as f32 + 0.5, (index / width) as f32 + 0.5);
+ scenes::SHAPES
+ .iter()
+ .any(|shape| scaled(shape, scale).sdf(x, y) < 1.5)
+ })
+ .collect()
+}
+
+#[cfg(not(target_os = "linux"))]
+fn window_target(window: &Window) -> Option<ScreenCaptureTarget> {
+ let number = crate::platform::window_number(window)?;
+ Some(ScreenCaptureTarget::Window {
+ id: number.to_string().parse().ok()?,
+ })
+}
+
+#[cfg(target_os = "linux")]
+fn window_target(window: &Window) -> Option<ScreenCaptureTarget> {
+ use raw_window_handle::{HasWindowHandle, RawWindowHandle};
+
+ let number = match HasWindowHandle::window_handle(window).ok()?.as_raw() {
+ RawWindowHandle::Xlib(handle) => handle.window as u64,
+ RawWindowHandle::Xcb(handle) => u64::from(handle.window.get()),
+ _ => return None,
+ };
+ Some(ScreenCaptureTarget::Window {
+ id: number.to_string().parse().ok()?,
+ })
+}
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index db32a0fa6..bed80670a 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -69,6 +69,8 @@ mod permissions_ui;
#[cfg(test)]
mod picker_benchmark;
#[cfg(debug_assertions)]
+mod glass_probe;
+#[cfg(debug_assertions)]
mod picker_ui_benchmark;
mod platform;
mod presets;
@@ -806,6 +808,8 @@ fn main() {
// synthetic clicks are dropped.
#[cfg(debug_assertions)]
picker_ui_benchmark::run(window_handle, cx);
+ #[cfg(debug_assertions)]
+ glass_probe::run(cx);
if let Ok(auto) = std::env::var("CAP_GPUI_AUTO_RECORD")
&& let Some((mode, secs)) = parse_auto_record(&auto)
{
diff --git a/apps/desktop-gpui/src/ui/glass.rs b/apps/desktop-gpui/src/ui/glass.rs
new file mode 100644
index 000000000..4b37e28e1
--- /dev/null
+++ b/apps/desktop-gpui/src/ui/glass.rs
@@ -0,0 +1,107 @@
+//! Cap Glass: panes that blur, lens and tint whatever is painted beneath
+//! them, with materials fitted against Apple's own glass (`glass/`, and the
+//! `glass-reference` capture that feeds it).
+//!
+//! A pane paints itself first and its children over it, so content sits on
+//! the glass, never under it. The glass samples what is already in the
+//! window: on a transparent window, paint the backdrop it should show (the
+//! picker paints the captured desktop) before the pane.
+
+use std::sync::OnceLock;
+
+use cap_glass::model::{GlassMaterial, PaneStyle};
+use gpui::{Bounds, Corners, Div, ParentElement, Pixels, Styled, Window, canvas, div, px};
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum GlassKind {
+ /// Frosted: reads as a surface, for panels and toolbars.
+ Regular,
+ /// Barely frosted: for controls floating over busy content.
+ Clear,
+}
+
+struct Presets {
+ regular_light: GlassMaterial,
+ regular_dark: GlassMaterial,
+ clear_light: GlassMaterial,
+ clear_dark: GlassMaterial,
+}
+
+fn presets() -> &'static Presets {
+ static PRESETS: OnceLock<Presets> = OnceLock::new();
+ PRESETS.get_or_init(|| {
+ let all: serde_json::Value = serde_json::from_str(cap_glass::PRESETS_JSON)
+ .expect("glass presets are valid JSON");
+ let get = |name: &str| -> GlassMaterial {
+ serde_json::from_value(all[name].clone()).expect("glass preset is complete")
+ };
+ Presets {
+ regular_light: get("regular-light"),
+ regular_dark: get("regular-dark"),
+ clear_light: get("clear-light"),
+ clear_dark: get("clear-dark"),
+ }
+ })
+}
+
+pub fn material(kind: GlassKind, dark: bool) -> &'static GlassMaterial {
+ let presets = presets();
+ match (kind, dark) {
+ (GlassKind::Regular, false) => &presets.regular_light,
+ (GlassKind::Regular, true) => &presets.regular_dark,
+ (GlassKind::Clear, false) => &presets.clear_light,
+ (GlassKind::Clear, true) => &presets.clear_dark,
+ }
+}
+
+pub fn gpui_style(style: &PaneStyle) -> gpui::GlassStyle {
+ gpui::GlassStyle {
+ enabled: 1,
+ lens_rim: style.lens_rim,
+ lens_curve: style.lens_curve,
+ rim_bend: style.rim_bend,
+ poly: style.poly,
+ opacity: style.opacity,
+ line: style.line,
+ line_lit: style.line_lit,
+ line_width: style.line_width,
+ light_x: style.light[0],
+ light_y: style.light[1],
+ glow: style.glow,
+ glow_width: style.glow_width,
+ hairline: style.hairline,
+ }
+}
+
+/// Paint one glass pane over whatever the window already holds at `bounds`.
+pub fn paint(window: &mut Window, bounds: Bounds<Pixels>, radius: f32, material: &GlassMaterial) {
+ let (width, height) = (f32::from(bounds.size.width), f32::from(bounds.size.height));
+ if width <= 0. || height <= 0. {
+ return;
+ }
+ let style = material.resolve(width, height);
+ let radius = radius.min(width / 2.).min(height / 2.);
+ window.paint_glass(
+ bounds,
+ Corners::all(px(radius)),
+ px(style.blur),
+ gpui_style(&style),
+ );
+}
+
+/// A rounded glass pane: a `Div` whose first child is the glass layer, so
+/// everything added after it paints on the glass. `radius` is clamped to the
+/// pane like CSS clamps `border-radius`.
+pub fn pane(kind: GlassKind, dark: bool, radius: f32) -> Div {
+ let material = *material(kind, dark);
+ div().relative().rounded(px(radius)).child(
+ canvas(
+ |_, _, _| {},
+ move |bounds, _, window, _| paint(window, bounds, radius, &material),
+ )
+ .absolute()
+ .top_0()
+ .left_0()
+ .size_full(),
+ )
+}
diff --git a/apps/desktop-gpui/src/ui/mod.rs b/apps/desktop-gpui/src/ui/mod.rs
index 4d3b32d94..8473ee64a 100644
--- a/apps/desktop-gpui/src/ui/mod.rs
+++ b/apps/desktop-gpui/src/ui/mod.rs
@@ -45,6 +45,7 @@ mod collapsible;
mod color_picker;
mod editor_button;
mod field;
+pub mod glass;
mod kbd;
pub mod keyboard_nav;
mod menu;
diff --git a/scripts/glass-probe/run.sh b/scripts/glass-probe/run.sh
new file mode 100755
index 000000000..82e55fff8
--- /dev/null
+++ b/scripts/glass-probe/run.sh
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+# Runs cap-gpui's glass probe (src/glass_probe.rs) in a throwaway profile:
+# every scene in cap_glass::scenes with glass panes in each preset, captured
+# from the real window and scored against the CPU model. Fails unless every
+# case passes.
+#
+# scripts/glass-probe/run.sh <cap-gpui binary> <out dir>
+set -euo pipefail
+
+APP=$1
+OUT=$2
+mkdir -p "$OUT/profile/recordings" "$OUT/profile/logs" "$OUT/captures"
+cat > "$OUT/profile/store" <<'JSON'
+{"general_settings":{"hasCompletedOnboarding":true,"hasCompletedStartup":true,"enableNotifications":false,"enableTelemetry":false,"recordingCountdown":0},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false}}
+JSON
+report="$OUT/captures/report.json"
+rm -f "$report"
+
+env CAP_GPUI_APP_DATA_DIR="$OUT/profile" \
+ CAP_GPUI_RECORDINGS_DIR="$OUT/profile/recordings" \
+ CAP_GPUI_LOG_DIR="$OUT/profile/logs" \
+ CAP_GPUI_AUTO_NO_MIC=1 \
+ CAP_GLASS_PROBE_OUTPUT="$OUT/captures" \
+ RUST_LOG="${RUST_LOG:-cap_gpui=info}" \
+ "$APP" > "$OUT/app.log" 2>&1 &
+pid=$!
+
+for _ in $(seq 1 600); do
+ [ -s "$report" ] && break
+ kill -0 "$pid" 2>/dev/null || break
+ sleep 0.5
+done
+kill "$pid" 2>/dev/null || true
+
+if [ ! -s "$report" ]; then
+ echo "glass probe wrote no report" >&2
+ tail -n 80 "$OUT/app.log" >&2 || true
+ exit 1
+fi
+python3 - "$report" <<'PY'
+import json, sys
+report = json.load(open(sys.argv[1]))
+print(f"scale {report.get('scale')} passed {report['passed']}")
+for case in report.get("cases", []):
+ if "error" in case:
+ print(f" {case['name']:28s} error: {case['error']}")
+ else:
+ mark = "ok " if case["passed"] else "BAD"
+ print(f" {mark} {case['name']:28s} ssim {case['ssim']:.4f} dE {case['deltaE']:.2f} dE95 {case['deltaE95']:.2f}")
+sys.exit(0 if report["passed"] else 1)
+PY
diff --git a/scripts/glass-reference/reference.swift b/scripts/glass-reference/reference.swift
index 05e1a145a..eb492fceb 100644
--- a/scripts/glass-reference/reference.swift
+++ b/scripts/glass-reference/reference.swift
@@ -8,6 +8,7 @@
// Needs Screen Recording for the calling process (screencapture -l).
import AppKit
import Metal
+import SwiftUI
let canvas = NSSize(width: 960, height: 600)
@@ -106,11 +107,48 @@ func drawScene(_ name: String, in ctx: CGContext, size: NSSize) {
let gradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB), colors: [color, clear] as CFArray, locations: [0, 1])!
ctx.drawRadialGradient(gradient, startCenter: center, startRadius: 0, endCenter: center, endRadius: radius, options: [])
}
+ case "uv":
+ // R and G encode the sampled point, so a capture reads back where
+ // each glass pixel's light came from.
+ let width = Int(size.width), height = Int(size.height)
+ for row in 0..<height {
+ for col in 0..<width {
+ ctx.setFillColor(CGColor(srgbRed: CGFloat(col) / CGFloat(width - 1),
+ green: CGFloat(row) / CGFloat(height - 1), blue: 0.5, alpha: 1))
+ ctx.fill(CGRect(x: col, y: row, width: 1, height: 1))
+ }
+ }
+ case "uvfine":
+ let width = Int(size.width), height = Int(size.height)
+ for row in 0..<height {
+ for col in 0..<width {
+ ctx.setFillColor(CGColor(srgbRed: CGFloat(col % 32) / 31, green: CGFloat(row % 32) / 31, blue: 0.5, alpha: 1))
+ ctx.fill(CGRect(x: col, y: row, width: 1, height: 1))
+ }
+ }
+ case "split":
+ fill(0, 0, 0)
+ ctx.setFillColor(CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 1))
+ ctx.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height / 2))
+ ctx.fill(CGRect(x: 0, y: 0, width: 170, height: size.height))
+ ctx.fill(CGRect(x: 670, y: 0, width: size.width - 670, height: size.height))
+ case "dots":
+ fill(0, 0, 0)
+ ctx.setFillColor(CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 1))
+ var y: CGFloat = 12
+ while y < size.height {
+ var x: CGFloat = 12
+ while x < size.width {
+ ctx.fill(CGRect(x: x, y: y, width: 2, height: 2))
+ x += 40
+ }
+ y += 40
+ }
default: fill(1, 0, 1)
}
}
-let scenes = ["white", "black", "gray", "text", "darktext", "stripes", "grid", "checker", "hues", "blobs"]
+let scenes = ["white", "black", "gray", "text", "darktext", "stripes", "grid", "checker", "hues", "blobs", "uv", "uvfine", "split", "dots"]
let styles: [(String, NSGlassEffectView.Style)] = [("regular", .regular), ("clear", .clear)]
let appearances: [(String, NSAppearance.Name)] = [("light", .aqua), ("dark", .darkAqua)]
let tints: [(String, NSColor?)] = [("none", nil), ("blue", NSColor.systemBlue.withAlphaComponent(0.6))]
@@ -134,6 +172,52 @@ func writePNG(_ image: CGImage, to url: URL) {
try! rep.representation(using: .png, properties: [:])!.write(to: url)
}
+func capture(_ window: NSWindow, to url: URL) {
+ let process = Process()
+ process.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture")
+ process.arguments = ["-x", "-o", "-l\(window.windowNumber)", url.path]
+ try! process.run()
+ process.waitUntilExit()
+ let convert = Process()
+ convert.executableURL = URL(fileURLWithPath: "/usr/bin/sips")
+ convert.arguments = ["--matchTo", "/System/Library/ColorSync/Profiles/sRGB Profile.icc", url.path]
+ convert.standardOutput = FileHandle.nullDevice
+ try! convert.run()
+ convert.waitUntilExit()
+}
+
+struct ControlsPanel: View {
+ @State var on = true
+ @State var off = false
+ @State var level = 0.35
+ @State var choice = 1
+ var body: some View {
+ VStack(alignment: .leading, spacing: 28) {
+ HStack(spacing: 28) {
+ Toggle("", isOn: $on).toggleStyle(.switch).labelsHidden()
+ Toggle("", isOn: $off).toggleStyle(.switch).labelsHidden()
+ Toggle("", isOn: $on).toggleStyle(.switch).labelsHidden().controlSize(.large)
+ }
+ Slider(value: $level).frame(width: 320)
+ Picker("", selection: $choice) {
+ Text("Free").tag(0)
+ Text("16:9").tag(1)
+ Text("4:3").tag(2)
+ Text("1:1").tag(3)
+ }
+ .pickerStyle(.segmented).labelsHidden().frame(width: 320)
+ HStack(spacing: 20) {
+ Button("Glass") {}.buttonStyle(.glass)
+ Button("Prominent") {}.buttonStyle(.glassProminent)
+ Button("Large") {}.buttonStyle(.glass).controlSize(.large)
+ Button {} label: { Image(systemName: "xmark") }.buttonStyle(.glass).buttonBorderShape(.circle)
+ }
+ }
+ .padding(48)
+ .frame(width: 960, height: 600, alignment: .topLeading)
+ }
+}
+
func pump(_ seconds: TimeInterval) {
RunLoop.current.run(until: Date().addingTimeInterval(seconds))
}
@@ -141,6 +225,8 @@ func pump(_ seconds: TimeInterval) {
let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : "glass-reference")
try FileManager.default.createDirectory(at: out.appendingPathComponent("scenes"), withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: out.appendingPathComponent("apple"), withIntermediateDirectories: true)
+try FileManager.default.createDirectory(at: out.appendingPathComponent("bare"), withIntermediateDirectories: true)
+try FileManager.default.createDirectory(at: out.appendingPathComponent("controls"), withIntermediateDirectories: true)
let app = NSApplication.shared
app.setActivationPolicy(.regular)
@@ -149,6 +235,7 @@ app.finishLaunching()
let window = NSWindow(contentRect: NSRect(origin: NSPoint(x: 80, y: 80), size: canvas),
styleMask: [.borderless], backing: .buffered, defer: false)
window.isOpaque = true
+window.colorSpace = .sRGB
window.hasShadow = false
window.level = .floating
let root = FlippedView(frame: NSRect(origin: .zero, size: canvas))
@@ -167,6 +254,9 @@ for scene in scenes {
let image = renderScene(scene, scale: scale)
writePNG(image, to: out.appendingPathComponent("scenes/\(scene).png"))
backdrop.image = NSImage(cgImage: image, size: canvas)
+ window.appearance = NSAppearance(named: .aqua)
+ pump(0.4)
+ capture(window, to: out.appendingPathComponent("bare/\(scene).png"))
for (styleName, style) in styles {
for (appearanceName, appearance) in appearances {
for (tintName, tint) in tints {
@@ -185,12 +275,7 @@ for scene in scenes {
}
pump(0.6)
let name = "\(scene)-\(styleName)-\(appearanceName)-\(tintName)"
- let path = out.appendingPathComponent("apple/\(name).png").path
- let capture = Process()
- capture.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture")
- capture.arguments = ["-x", "-o", "-l\(window.windowNumber)", path]
- try capture.run()
- capture.waitUntilExit()
+ capture(window, to: out.appendingPathComponent("apple/\(name).png"))
cases.append([
"name": name, "scene": scene, "style": styleName,
"appearance": appearanceName, "tint": tintName,
@@ -201,7 +286,24 @@ for scene in scenes {
}
}
+var controlCases: [String] = []
+for scene in ["blobs", "white", "darktext"] {
+ backdrop.image = NSImage(cgImage: renderScene(scene, scale: scale), size: canvas)
+ for (appearanceName, appearance) in appearances {
+ window.appearance = NSAppearance(named: appearance)
+ let host = NSHostingView(rootView: ControlsPanel())
+ host.frame = root.bounds
+ root.addSubview(host)
+ pump(0.8)
+ let name = "\(scene)-\(appearanceName)"
+ capture(window, to: out.appendingPathComponent("controls/\(name).png"))
+ controlCases.append(name)
+ host.removeFromSuperview()
+ }
+}
+
let manifest: [String: Any] = [
+ "controls": controlCases,
"canvas": [canvas.width, canvas.height],
"scale": scale,
"os": ProcessInfo.processInfo.operatingSystemVersionString,
diff --git a/scripts/prepare-gpui-dependency.sh b/scripts/prepare-gpui-dependency.sh
index bd26ac9bf..cce93449c 100755
--- a/scripts/prepare-gpui-dependency.sh
+++ b/scripts/prepare-gpui-dependency.sh
@@ -12,6 +12,7 @@ patch_paths=(
"apps/desktop-gpui/patches/zed-wayland-visibility.patch"
"apps/desktop-gpui/patches/zed-wayland-activation.patch"
"apps/desktop-gpui/patches/zed-closed-window-events.patch"
+ "apps/desktop-gpui/patches/zed-glass.patch"
)
patch_files=("${patch_paths[@]/#/$repo_root/}")
base_revision="5d1f83d9f27a19bec1fb241dc33b42238af9cf8d"
diff --git a/scripts/prepare-gpui-dependency.test.mjs b/scripts/prepare-gpui-dependency.test.mjs
index 45a5b1eee..8b92595aa 100644
--- a/scripts/prepare-gpui-dependency.test.mjs
+++ b/scripts/prepare-gpui-dependency.test.mjs
@@ -152,6 +152,8 @@ test("dependency verification handles overlapping patches without changing the c
patch("zed-wayland-activation.patch");
writeFileSync(path.join(upstream, "closed.rs"), "quiet closed windows\n");
patch("zed-closed-window-events.patch");
+ writeFileSync(path.join(upstream, "glass.rs"), "glass panes\n");
+ patch("zed-glass.patch");
const script = path.join(cap, "scripts/prepare-gpui-dependency.sh");
writeFileSync(
script,