From f24f601e0505c3525e13aed5c65fc57b41f38de2 Mon Sep 17 00:00:00 2001 From: Son Date: Wed, 7 Aug 2024 22:12:26 +1000 Subject: [PATCH] Adjust erf estimation function (#15423) Release Notes: - Fixed a (potential) small error in erf estimation. Technically, the error is negligible. I am not sure where the current calculation for erf come from and if it is intended or a simple mistake. However it looks slightly different from the official calculation, notably [this](https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions) from Wikipedia. I will add a comment if it is intended. --- crates/gpui/Cargo.toml | 4 +++ crates/gpui/examples/shadow.rs | 29 +++++++++++++++++++++ crates/gpui/src/platform/blade/shaders.wgsl | 2 +- crates/gpui/src/platform/mac/shaders.metal | 6 ++--- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 crates/gpui/examples/shadow.rs diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index cc8ec90d67f34eb26813d7c9bf3fa718d7e498fd..10f85a01dccafd2777322b8b6a512330b76e18eb 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -172,3 +172,7 @@ path = "examples/window_shadow.rs" [[example]] name = "input" path = "examples/input.rs" + +[[example]] +name = "shadow" +path = "examples/shadow.rs" diff --git a/crates/gpui/examples/shadow.rs b/crates/gpui/examples/shadow.rs new file mode 100644 index 0000000000000000000000000000000000000000..cdf3ba42e9e2b0222055d77a50676b34b6b35fc1 --- /dev/null +++ b/crates/gpui/examples/shadow.rs @@ -0,0 +1,29 @@ +use gpui::*; + +struct Shadow {} + +impl Render for Shadow { + fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { + div() + .flex() + .bg(rgb(0xffffff)) + .size_full() + .justify_center() + .items_center() + .child(div().size_8().shadow_sm()) + } +} + +fn main() { + App::new().run(|cx: &mut AppContext| { + let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx); + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(bounds)), + ..Default::default() + }, + |cx| cx.new_view(|_cx| Shadow {}), + ) + .unwrap(); + }); +} diff --git a/crates/gpui/src/platform/blade/shaders.wgsl b/crates/gpui/src/platform/blade/shaders.wgsl index 0eba972b6902bbb9f252e757a30dac1ad63fb8cc..77f14e8264b84d3766db29696c0c0b1969409077 100644 --- a/crates/gpui/src/platform/blade/shaders.wgsl +++ b/crates/gpui/src/platform/blade/shaders.wgsl @@ -150,7 +150,7 @@ fn gaussian(x: f32, sigma: f32) -> f32{ fn erf(v: vec2) -> vec2 { let s = sign(v); let a = abs(v); - let r1 = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a; + let r1 = 1.0 + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a; let r2 = r1 * r1; return s - s / (r2 * r2); } diff --git a/crates/gpui/src/platform/mac/shaders.metal b/crates/gpui/src/platform/mac/shaders.metal index c1089dbbec6f4f074e3c9e40c173c40463bdc948..e7afe2c11fe691ace58525f8f8793bc1ec66e944 100644 --- a/crates/gpui/src/platform/mac/shaders.metal +++ b/crates/gpui/src/platform/mac/shaders.metal @@ -657,9 +657,9 @@ float gaussian(float x, float sigma) { float2 erf(float2 x) { float2 s = sign(x); float2 a = abs(x); - x = 1. + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a; - x *= x; - return s - s / (x * x); + float2 r1 = 1. + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a; + float2 r2 = r1 * r1; + return s - s / (r2 * r2); } float blur_along_x(float x, float y, float sigma, float corner,