Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ repository = "https://github.com/kogical/fluctus"
lto = "thin"
codegen-units = 1
panic = "abort"

# The KWin effect links liquidglass as a staticlib. Its FFI catches Rust panics and returns an
# error instead; with panic = "abort" a panic would take down KWin (and the session) instead.
[profile.release-ffi]
inherits = "release"
panic = "unwind"
28 changes: 28 additions & 0 deletions crates/liquidglass/include/liquidglass.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ typedef enum LgStatus {
LG_STATUS_GL_COMPILE_ERROR = 3,
LG_STATUS_GL_FRAMEBUFFER_ERROR = 4,
LG_STATUS_UNSUPPORTED = 5,
/**
* The renderer hit an internal error (a Rust panic caught at the FFI boundary).
*/
LG_STATUS_INTERNAL = 6,
} LgStatus;

/**
Expand Down Expand Up @@ -167,6 +171,14 @@ typedef struct LgMaterial {
float pointer_radius;
} LgMaterial;

/**
* C ABI form of [`KawaseStep`]: Dual Kawase iteration count and sample offset.
*/
typedef struct LgKawaseStep {
uint32_t iterations;
float offset;
} LgKawaseStep;

/**
* Host callback resolving OpenGL function pointers by null-terminated symbol name
* (e.g. `eglGetProcAddress` or `glXGetProcAddress`).
Expand Down Expand Up @@ -369,6 +381,22 @@ float lg_material_draw_margin(const struct LgMaterial *material);
uint32_t lg_kawase_expand_for_frost(float frost,
float scale);

/**
* Maps `frost` (`0..1`) to a KWin blur strength (`0` = sharp, `1..=15`).
*/
uint32_t lg_strength_for_frost(float frost);

/**
* Resolves a KWin blur strength (`0..=15`) at a HiDPI `scale` into Dual Kawase iterations + offset
* (KWin Blur's own table; `strength == 0` gives zero iterations).
*/
struct LgKawaseStep lg_kawase_for_strength(uint32_t strength, float scale);

/**
* Minimum backdrop expansion (device px) that `iterations` Dual Kawase passes sample beyond a rect.
*/
uint32_t lg_kawase_expand_for_iterations(uint32_t iterations);

/**
* Creates a zero-copy OpenGL / GLES Liquid Glass renderer on the currently active GL context.
*
Expand Down
20 changes: 7 additions & 13 deletions crates/liquidglass/shaders/liquidglass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct GlassUniforms {
inner_border: vec4<f32>, // straight-alpha RGBA
outer_border: vec4<f32>, // straight-alpha RGBA
shadow_color: vec4<f32>, // straight-alpha RGBA
tex_and_blur: vec4<f32>, // (tex_w, tex_h, blur_half_x, blur_half_y)
tex_and_blur: vec4<f32>, // (tex_w, tex_h, blur_half_x, blur_half_y); blur half-pixels unused since the full-res upsample
optics_a: vec4<f32>, // (radius, bezel, refraction, dispersion)
optics_b: vec4<f32>, // (magnify, edge_bend, saturation, brightness)
rim_params: vec4<f32>, // (rim_light, rim_width, light_dir_x, light_dir_y)
Expand Down Expand Up @@ -136,29 +136,23 @@ fn to_uv(p: vec2<f32>) -> vec2<f32> {
return clamp(uv, u.uv_clamp.xy, u.uv_clamp.zw);
}

// The host's blur chain ends with a full-resolution Kawase upsample, so the blurred backdrop is a
// single tap here (the 5 dispersion samples used to re-run the 8-tap upsample each). Every lookup uses
// level 0 explicitly: they sit in non-uniform control flow, where implicit derivatives are undefined.
fn sample_backdrop_blur(p: vec2<f32>) -> vec4<f32> {
let uv = to_uv(p);
if (u.pointer_b.w < 0.5) {
return textureSample(sharp_tex, sharp_samp, uv);
return textureSampleLevel(sharp_tex, sharp_samp, uv, 0.0);
}
let h = u.tex_and_blur.zw * u.misc.x;
var sum = textureSample(blur_tex, blur_samp, uv + vec2<f32>(-h.x * 2.0, 0.0));
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(-h.x, h.y)) * 2.0;
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(0.0, h.y * 2.0));
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(h.x, h.y)) * 2.0;
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(h.x * 2.0, 0.0));
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(h.x, -h.y)) * 2.0;
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(0.0, -h.y * 2.0));
sum += textureSample(blur_tex, blur_samp, uv + vec2<f32>(-h.x, -h.y)) * 2.0;
return sum / 12.0;
return textureSampleLevel(blur_tex, blur_samp, uv, 0.0);
}

fn sample_backdrop(p: vec2<f32>, clarity: f32) -> vec4<f32> {
let blurred = sample_backdrop_blur(p);
if (u.pointer_b.w < 0.5 || clarity <= 0.001) {
return blurred;
}
let sharp = textureSample(sharp_tex, sharp_samp, to_uv(p));
let sharp = textureSampleLevel(sharp_tex, sharp_samp, to_uv(p), 0.0);
return mix(blurred, sharp, clarity);
}

Expand Down
10 changes: 10 additions & 0 deletions crates/liquidglass/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub enum LgStatus {
GlCompileError = 3,
GlFramebufferError = 4,
Unsupported = 5,
/// The renderer hit an internal error (a Rust panic caught at the FFI boundary).
Internal = 6,
}

/// Straight-alpha RGBA color in the output color space (SDR `0..1`, or HDR linear/mapped).
Expand Down Expand Up @@ -426,6 +428,14 @@ pub struct KawaseStep {
pub offset: f32,
}

/// C ABI form of [`KawaseStep`]: Dual Kawase iteration count and sample offset.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LgKawaseStep {
pub iterations: u32,
pub offset: f32,
}

#[derive(Debug, Clone, Copy)]
struct KawaseLevel {
min_offset: f32,
Expand Down
80 changes: 59 additions & 21 deletions crates/liquidglass/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@

use core::ffi::{c_char, c_void};

#[cfg(feature = "gl")]
use std::panic::{catch_unwind, AssertUnwindSafe};

use crate::core::{
expand_for_iterations, kawase_for_strength, strength_for_frost, LgMaterial, LgRenderParams,
LgStatus,
expand_for_iterations, kawase_for_strength, strength_for_frost, LgKawaseStep, LgMaterial,
};
#[cfg(feature = "gl")]
use crate::core::{LgRenderParams, LgStatus};

/// Runs `f`, turning a Rust panic into `LgStatus::Internal`: unwinding across `extern "C"` would
/// abort the host process (for KWin, the whole session).
#[cfg(feature = "gl")]
fn guarded(f: impl FnOnce() -> LgStatus) -> LgStatus {
catch_unwind(AssertUnwindSafe(f)).unwrap_or(LgStatus::Internal)
}

/// Opaque handle to a zero-copy OpenGL / OpenGL ES Liquid Glass renderer.
#[repr(C)]
Expand Down Expand Up @@ -65,6 +76,26 @@ pub extern "C" fn lg_kawase_expand_for_frost(frost: f32, scale: f32) -> u32 {
expand_for_iterations(step.iterations)
}

/// Maps `frost` (`0..1`) to a KWin blur strength (`0` = sharp, `1..=15`).
#[unsafe(no_mangle)]
pub extern "C" fn lg_strength_for_frost(frost: f32) -> u32 {
strength_for_frost(frost)
}

/// Resolves a KWin blur strength (`0..=15`) at a HiDPI `scale` into Dual Kawase iterations + offset
/// (KWin Blur's own table; `strength == 0` gives zero iterations).
#[unsafe(no_mangle)]
pub extern "C" fn lg_kawase_for_strength(strength: u32, scale: f32) -> LgKawaseStep {
let step = kawase_for_strength(strength, scale);
LgKawaseStep { iterations: step.iterations as u32, offset: step.offset }
}

/// Minimum backdrop expansion (device px) that `iterations` Dual Kawase passes sample beyond a rect.
#[unsafe(no_mangle)]
pub extern "C" fn lg_kawase_expand_for_iterations(iterations: u32) -> u32 {
expand_for_iterations(iterations as usize)
}

/// Creates a zero-copy OpenGL / GLES Liquid Glass renderer on the currently active GL context.
///
/// # Safety
Expand All @@ -87,23 +118,24 @@ pub unsafe extern "C" fn lg_gl_create(
return LgStatus::NullPointer;
};

let res = unsafe {
crate::gl_backend::GlRenderer::new(|symbol: &str| {
let Ok(cstr) = CString::new(symbol) else {
return core::ptr::null();
};
loader_fn(cstr.as_ptr(), user_data)
})
};

match res {
Ok(renderer) => {
let boxed = Box::new(renderer);
unsafe { *out_renderer = Box::into_raw(boxed).cast::<LgGlRenderer>() };
LgStatus::Ok
guarded(|| {
let res = unsafe {
crate::gl_backend::GlRenderer::new(|symbol: &str| {
let Ok(cstr) = CString::new(symbol) else {
return core::ptr::null();
};
loader_fn(cstr.as_ptr(), user_data)
})
};
match res {
Ok(renderer) => {
let boxed = Box::new(renderer);
unsafe { *out_renderer = Box::into_raw(boxed).cast::<LgGlRenderer>() };
LgStatus::Ok
}
Err(status) => status,
}
Err(status) => status,
}
})
}

/// Renders one frame of Liquid Glass shapes from a borrowed backdrop texture into a borrowed target FBO.
Expand All @@ -123,7 +155,7 @@ pub unsafe extern "C" fn lg_gl_render(
}
let r = unsafe { &mut *renderer.cast::<crate::gl_backend::GlRenderer>() };
let p = unsafe { &*params };
unsafe { r.render(p) }
guarded(|| unsafe { r.render(p) })
}

/// Releases pooled internal scratch blur textures/FBOs without destroying compiled shaders.
Expand All @@ -137,7 +169,10 @@ pub unsafe extern "C" fn lg_gl_invalidate_caches(renderer: *mut LgGlRenderer) {
return;
}
let r = unsafe { &mut *renderer.cast::<crate::gl_backend::GlRenderer>() };
unsafe { r.invalidate_caches() };
let _ = guarded(|| {
unsafe { r.invalidate_caches() };
LgStatus::Ok
});
}

/// Destroys all owned GL resources and frees the renderer handle.
Expand All @@ -152,5 +187,8 @@ pub unsafe extern "C" fn lg_gl_destroy(renderer: *mut LgGlRenderer) {
return;
}
let boxed = unsafe { Box::from_raw(renderer.cast::<crate::gl_backend::GlRenderer>()) };
unsafe { boxed.destroy() };
let _ = guarded(|| {
unsafe { boxed.destroy() };
LgStatus::Ok
});
}
Loading
Loading