SmartFox: launcher, Siri Remote driver, Debian image and installer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
49
crates/sf-launcher/src/glass/mod.rs
Normal file
49
crates/sf-launcher/src/glass/mod.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
//! "Liquid Glass" layer: an OpenGL pass under the Slint UI that draws the
|
||||
//! animated backdrop and the frosted, refracting glass panels.
|
||||
|
||||
mod renderer;
|
||||
mod shaders;
|
||||
|
||||
pub use renderer::GlassRenderer;
|
||||
|
||||
/// Panels drawn per frame at most (each one is its own small draw call).
|
||||
pub const MAX_RECTS: usize = 48;
|
||||
/// Members of one merged (liquid) group at most.
|
||||
pub const MAX_GROUP: usize = 4;
|
||||
|
||||
/// One glass panel in physical pixels (top-left origin).
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Panel {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
pub w: f32,
|
||||
pub h: f32,
|
||||
pub radius: f32,
|
||||
/// 0..1, brightens the rim and adds the accent glow.
|
||||
pub focus: f32,
|
||||
pub alpha: f32,
|
||||
/// Mip level used for the frost (higher = blurrier). Negative = nested glass.
|
||||
pub blur: f32,
|
||||
pub glow: [f32; 3],
|
||||
/// Consecutive panels with the same group (> 0) melt into one shape.
|
||||
pub group: f32,
|
||||
/// rgb + amount.
|
||||
pub tint: [f32; 4],
|
||||
}
|
||||
|
||||
/// Everything the renderer needs for one frame.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Frame {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub scale: f32,
|
||||
pub time: f32,
|
||||
/// Backdrop palette (highlight, mid, deep), already crossfaded.
|
||||
pub palette: [f32; 9],
|
||||
/// 1 = top shelf visible (bright backdrop), 0 = scrolled down into the grid.
|
||||
pub shelf: f32,
|
||||
pub light: [f32; 2],
|
||||
/// Backdrop zoom (1 = none), used for the launch fly-in.
|
||||
pub zoom: f32,
|
||||
pub rects: Vec<Panel>,
|
||||
}
|
||||
425
crates/sf-launcher/src/glass/renderer.rs
Normal file
425
crates/sf-launcher/src/glass/renderer.rs
Normal file
@@ -0,0 +1,425 @@
|
||||
//! OpenGL underlay drawn before Slint renders its items.
|
||||
//!
|
||||
//! Pass 1 renders the backdrop (a smooth gradient) into a small, mipmapped
|
||||
//! texture. Pass 2 upscales it to the window. Pass 3 draws every glass panel
|
||||
//! as its own quad, covering only the panel and its shadow, blended onto the
|
||||
//! window. Uniform locations are looked up once.
|
||||
|
||||
use glow::HasContext;
|
||||
|
||||
use super::shaders;
|
||||
use super::{Frame, MAX_GROUP, MAX_RECTS, Panel};
|
||||
|
||||
/// The backdrop is blurry by design, so it never needs more than this width.
|
||||
const SCENE_MAX_W: i32 = 640;
|
||||
|
||||
type Loc = Option<glow::UniformLocation>;
|
||||
|
||||
struct SceneLocs {
|
||||
res: Loc,
|
||||
time: Loc,
|
||||
pal: Loc,
|
||||
zoom: Loc,
|
||||
shelf: Loc,
|
||||
}
|
||||
|
||||
struct BlitLocs {
|
||||
scene: Loc,
|
||||
time: Loc,
|
||||
}
|
||||
|
||||
struct GlassLocs {
|
||||
box_: Loc,
|
||||
res: Loc,
|
||||
scene: Loc,
|
||||
scale: Loc,
|
||||
time: Loc,
|
||||
light: Loc,
|
||||
count: Loc,
|
||||
rect: Loc,
|
||||
param: Loc,
|
||||
glow: Loc,
|
||||
tint: Loc,
|
||||
}
|
||||
|
||||
pub struct GlassRenderer {
|
||||
gl: glow::Context,
|
||||
vao: glow::VertexArray,
|
||||
scene_prog: glow::Program,
|
||||
blit_prog: glow::Program,
|
||||
glass_prog: glow::Program,
|
||||
scene_locs: SceneLocs,
|
||||
blit_locs: BlitLocs,
|
||||
glass_locs: GlassLocs,
|
||||
fbo: glow::Framebuffer,
|
||||
tex: glow::Texture,
|
||||
tex_size: (i32, i32),
|
||||
/// RGBA16F if the driver can render to it (no banding in dark gradients).
|
||||
half_float: bool,
|
||||
}
|
||||
|
||||
/// GL state we touch and must hand back to the Slint renderer untouched.
|
||||
struct SavedState {
|
||||
program: i32,
|
||||
vao: i32,
|
||||
array_buffer: i32,
|
||||
framebuffer: i32,
|
||||
active_texture: i32,
|
||||
texture0: i32,
|
||||
viewport: [i32; 4],
|
||||
blend: bool,
|
||||
blend_func: [i32; 4],
|
||||
blend_eq: [i32; 2],
|
||||
scissor: bool,
|
||||
depth: bool,
|
||||
stencil: bool,
|
||||
cull: bool,
|
||||
}
|
||||
|
||||
impl GlassRenderer {
|
||||
/// # Safety
|
||||
/// Must be called with the Slint OpenGL context current.
|
||||
pub unsafe fn new(gl: glow::Context) -> anyhow::Result<Self> {
|
||||
unsafe {
|
||||
let version = gl.get_parameter_string(glow::VERSION);
|
||||
let is_es = version.contains("OpenGL ES");
|
||||
log::info!("OpenGL: {version} ({})", gl.get_parameter_string(glow::RENDERER));
|
||||
let header = if is_es {
|
||||
"#version 300 es\nprecision highp float;\nprecision highp int;\n"
|
||||
} else {
|
||||
"#version 330 core\n"
|
||||
};
|
||||
|
||||
let scene_prog = link(&gl, header, shaders::VERTEX, shaders::SCENE)?;
|
||||
let blit_prog = link(&gl, header, shaders::VERTEX, shaders::BLIT)?;
|
||||
let glass_prog = link(&gl, header, shaders::QUAD_VERTEX, shaders::GLASS)?;
|
||||
let loc = |p: glow::Program, name: &str| gl.get_uniform_location(p, name);
|
||||
let scene_locs = SceneLocs {
|
||||
res: loc(scene_prog, "uRes"),
|
||||
time: loc(scene_prog, "uTime"),
|
||||
pal: loc(scene_prog, "uPal"),
|
||||
zoom: loc(scene_prog, "uZoom"),
|
||||
shelf: loc(scene_prog, "uShelf"),
|
||||
};
|
||||
let blit_locs = BlitLocs { scene: loc(blit_prog, "uScene"), time: loc(blit_prog, "uTime") };
|
||||
let glass_locs = GlassLocs {
|
||||
box_: loc(glass_prog, "uBox"),
|
||||
res: loc(glass_prog, "uRes"),
|
||||
scene: loc(glass_prog, "uScene"),
|
||||
scale: loc(glass_prog, "uScale"),
|
||||
time: loc(glass_prog, "uTime"),
|
||||
light: loc(glass_prog, "uLight"),
|
||||
count: loc(glass_prog, "uCount"),
|
||||
rect: loc(glass_prog, "uRect"),
|
||||
param: loc(glass_prog, "uParam"),
|
||||
glow: loc(glass_prog, "uGlow"),
|
||||
tint: loc(glass_prog, "uTint"),
|
||||
};
|
||||
let vao = gl.create_vertex_array().map_err(anyhow::Error::msg)?;
|
||||
let fbo = gl.create_framebuffer().map_err(anyhow::Error::msg)?;
|
||||
let tex = gl.create_texture().map_err(anyhow::Error::msg)?;
|
||||
|
||||
Ok(Self {
|
||||
gl,
|
||||
vao,
|
||||
scene_prog,
|
||||
blit_prog,
|
||||
glass_prog,
|
||||
scene_locs,
|
||||
blit_locs,
|
||||
glass_locs,
|
||||
fbo,
|
||||
tex,
|
||||
tex_size: (0, 0),
|
||||
half_float: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn save(&self) -> SavedState {
|
||||
let gl = &self.gl;
|
||||
unsafe {
|
||||
let mut viewport = [0; 4];
|
||||
gl.get_parameter_i32_slice(glow::VIEWPORT, &mut viewport);
|
||||
let active_texture = gl.get_parameter_i32(glow::ACTIVE_TEXTURE);
|
||||
gl.active_texture(glow::TEXTURE0);
|
||||
SavedState {
|
||||
program: gl.get_parameter_i32(glow::CURRENT_PROGRAM),
|
||||
vao: gl.get_parameter_i32(glow::VERTEX_ARRAY_BINDING),
|
||||
array_buffer: gl.get_parameter_i32(glow::ARRAY_BUFFER_BINDING),
|
||||
framebuffer: gl.get_parameter_i32(glow::FRAMEBUFFER_BINDING),
|
||||
active_texture,
|
||||
texture0: gl.get_parameter_i32(glow::TEXTURE_BINDING_2D),
|
||||
viewport,
|
||||
blend: gl.is_enabled(glow::BLEND),
|
||||
blend_func: [
|
||||
gl.get_parameter_i32(glow::BLEND_SRC_RGB),
|
||||
gl.get_parameter_i32(glow::BLEND_DST_RGB),
|
||||
gl.get_parameter_i32(glow::BLEND_SRC_ALPHA),
|
||||
gl.get_parameter_i32(glow::BLEND_DST_ALPHA),
|
||||
],
|
||||
blend_eq: [
|
||||
gl.get_parameter_i32(glow::BLEND_EQUATION_RGB),
|
||||
gl.get_parameter_i32(glow::BLEND_EQUATION_ALPHA),
|
||||
],
|
||||
scissor: gl.is_enabled(glow::SCISSOR_TEST),
|
||||
depth: gl.is_enabled(glow::DEPTH_TEST),
|
||||
stencil: gl.is_enabled(glow::STENCIL_TEST),
|
||||
cull: gl.is_enabled(glow::CULL_FACE),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn restore(&self, s: SavedState) {
|
||||
let gl = &self.gl;
|
||||
let nz = |v: i32| std::num::NonZeroU32::new(v as u32);
|
||||
unsafe {
|
||||
gl.use_program(nz(s.program).map(glow::NativeProgram));
|
||||
gl.bind_vertex_array(nz(s.vao).map(glow::NativeVertexArray));
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, nz(s.array_buffer).map(glow::NativeBuffer));
|
||||
gl.bind_framebuffer(glow::FRAMEBUFFER, nz(s.framebuffer).map(glow::NativeFramebuffer));
|
||||
gl.active_texture(glow::TEXTURE0);
|
||||
gl.bind_texture(glow::TEXTURE_2D, nz(s.texture0).map(glow::NativeTexture));
|
||||
gl.active_texture(s.active_texture as u32);
|
||||
gl.viewport(s.viewport[0], s.viewport[1], s.viewport[2], s.viewport[3]);
|
||||
let [sr, dr, sa, da] = s.blend_func.map(|v| v as u32);
|
||||
gl.blend_func_separate(sr, dr, sa, da);
|
||||
gl.blend_equation_separate(s.blend_eq[0] as u32, s.blend_eq[1] as u32);
|
||||
let set = |cap, on: bool| if on { gl.enable(cap) } else { gl.disable(cap) };
|
||||
set(glow::BLEND, s.blend);
|
||||
set(glow::SCISSOR_TEST, s.scissor);
|
||||
set(glow::DEPTH_TEST, s.depth);
|
||||
set(glow::STENCIL_TEST, s.stencil);
|
||||
set(glow::CULL_FACE, s.cull);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn alloc_target(&self, w: i32, h: i32, half_float: bool) -> bool {
|
||||
let gl = &self.gl;
|
||||
unsafe {
|
||||
gl.bind_texture(glow::TEXTURE_2D, Some(self.tex));
|
||||
let (internal, ty) =
|
||||
if half_float { (glow::RGBA16F, glow::HALF_FLOAT) } else { (glow::RGBA8, glow::UNSIGNED_BYTE) };
|
||||
gl.tex_image_2d(
|
||||
glow::TEXTURE_2D,
|
||||
0,
|
||||
internal as i32,
|
||||
w,
|
||||
h,
|
||||
0,
|
||||
glow::RGBA,
|
||||
ty,
|
||||
glow::PixelUnpackData::Slice(None),
|
||||
);
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::LINEAR_MIPMAP_LINEAR as i32);
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::LINEAR as i32);
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32);
|
||||
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32);
|
||||
gl.generate_mipmap(glow::TEXTURE_2D);
|
||||
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo));
|
||||
gl.framebuffer_texture_2d(glow::FRAMEBUFFER, glow::COLOR_ATTACHMENT0, glow::TEXTURE_2D, Some(self.tex), 0);
|
||||
gl.check_framebuffer_status(glow::FRAMEBUFFER) == glow::FRAMEBUFFER_COMPLETE
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn ensure_target(&mut self, w: i32, h: i32) {
|
||||
if self.tex_size == (w, h) {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
if self.half_float && !self.alloc_target(w, h, true) {
|
||||
log::info!("glass: RGBA16F not renderable, using RGBA8 for the backdrop");
|
||||
self.half_float = false;
|
||||
}
|
||||
if !self.half_float {
|
||||
self.alloc_target(w, h, false);
|
||||
}
|
||||
}
|
||||
self.tex_size = (w, h);
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// Must be called from the Slint rendering notifier with the context current.
|
||||
pub unsafe fn render(&mut self, f: &Frame) {
|
||||
let (w, h) = (f.width as i32, f.height as i32);
|
||||
if w <= 0 || h <= 0 {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
let saved = self.save();
|
||||
let gl = &self.gl;
|
||||
gl.disable(glow::BLEND);
|
||||
gl.disable(glow::SCISSOR_TEST);
|
||||
gl.disable(glow::DEPTH_TEST);
|
||||
gl.disable(glow::STENCIL_TEST);
|
||||
gl.disable(glow::CULL_FACE);
|
||||
gl.bind_vertex_array(Some(self.vao));
|
||||
|
||||
// Pass 1: backdrop at low resolution (it is a smooth gradient).
|
||||
let sw = (w / 4).clamp(1, SCENE_MAX_W);
|
||||
let sh = ((sw as i64 * h as i64 / w as i64) as i32).max(1);
|
||||
self.ensure_target(sw, sh);
|
||||
let gl = &self.gl;
|
||||
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo));
|
||||
gl.viewport(0, 0, sw, sh);
|
||||
let l = &self.scene_locs;
|
||||
gl.use_program(Some(self.scene_prog));
|
||||
gl.uniform_2_f32(l.res.as_ref(), sw as f32, sh as f32);
|
||||
gl.uniform_1_f32(l.time.as_ref(), f.time);
|
||||
gl.uniform_3_f32_slice(l.pal.as_ref(), &f.palette);
|
||||
gl.uniform_1_f32(l.zoom.as_ref(), f.zoom);
|
||||
gl.uniform_1_f32(l.shelf.as_ref(), f.shelf);
|
||||
gl.draw_arrays(glow::TRIANGLES, 0, 3);
|
||||
|
||||
gl.bind_texture(glow::TEXTURE_2D, Some(self.tex));
|
||||
gl.generate_mipmap(glow::TEXTURE_2D);
|
||||
|
||||
// Pass 2: upscale to the window framebuffer.
|
||||
gl.bind_framebuffer(
|
||||
glow::FRAMEBUFFER,
|
||||
std::num::NonZeroU32::new(saved.framebuffer as u32).map(glow::NativeFramebuffer),
|
||||
);
|
||||
gl.viewport(0, 0, w, h);
|
||||
let l = &self.blit_locs;
|
||||
gl.use_program(Some(self.blit_prog));
|
||||
gl.uniform_1_i32(l.scene.as_ref(), 0);
|
||||
gl.uniform_1_f32(l.time.as_ref(), f.time);
|
||||
gl.draw_arrays(glow::TRIANGLES, 0, 3);
|
||||
|
||||
// Pass 3: glass, one quad per panel or merged group: dst * a + rgb.
|
||||
gl.enable(glow::BLEND);
|
||||
gl.blend_equation_separate(glow::FUNC_ADD, glow::FUNC_ADD);
|
||||
gl.blend_func_separate(glow::ONE, glow::SRC_ALPHA, glow::ZERO, glow::ONE);
|
||||
let l = &self.glass_locs;
|
||||
gl.use_program(Some(self.glass_prog));
|
||||
gl.uniform_1_i32(l.scene.as_ref(), 0);
|
||||
gl.uniform_2_f32(l.res.as_ref(), w as f32, h as f32);
|
||||
gl.uniform_1_f32(l.scale.as_ref(), f.scale);
|
||||
gl.uniform_1_f32(l.time.as_ref(), f.time);
|
||||
gl.uniform_2_f32(l.light.as_ref(), f.light[0], f.light[1]);
|
||||
|
||||
let rects = &f.rects[..f.rects.len().min(MAX_RECTS)];
|
||||
let mut rect = [0f32; MAX_GROUP * 4];
|
||||
let mut param = [0f32; MAX_GROUP * 4];
|
||||
let mut glow_c = [0f32; MAX_GROUP * 4];
|
||||
let mut tint = [0f32; MAX_GROUP * 4];
|
||||
for run in runs(rects) {
|
||||
let n = run.len();
|
||||
// Bounds plus room for the shadow (spread + downward offset).
|
||||
let margin = 132.0 * f.scale;
|
||||
let (mut x0, mut y0, mut x1, mut y1) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
|
||||
for (i, r) in run.iter().enumerate() {
|
||||
rect[i * 4..i * 4 + 4].copy_from_slice(&[r.x, r.y, r.w, r.h]);
|
||||
param[i * 4..i * 4 + 4].copy_from_slice(&[r.radius, r.focus, r.alpha, r.blur]);
|
||||
glow_c[i * 4..i * 4 + 4].copy_from_slice(&[r.glow[0], r.glow[1], r.glow[2], 0.0]);
|
||||
tint[i * 4..i * 4 + 4].copy_from_slice(&r.tint);
|
||||
x0 = x0.min(r.x);
|
||||
y0 = y0.min(r.y);
|
||||
x1 = x1.max(r.x + r.w);
|
||||
y1 = y1.max(r.y + r.h);
|
||||
}
|
||||
let bx = [
|
||||
(x0 - margin).max(0.0),
|
||||
(y0 - margin).max(0.0),
|
||||
(x1 + margin).min(w as f32),
|
||||
(y1 + margin + 46.0 * f.scale).min(h as f32),
|
||||
];
|
||||
if bx[0] >= bx[2] || bx[1] >= bx[3] {
|
||||
continue;
|
||||
}
|
||||
gl.uniform_4_f32(l.box_.as_ref(), bx[0], bx[1], bx[2], bx[3]);
|
||||
gl.uniform_1_i32(l.count.as_ref(), n as i32);
|
||||
gl.uniform_4_f32_slice(l.rect.as_ref(), &rect[..n * 4]);
|
||||
gl.uniform_4_f32_slice(l.param.as_ref(), ¶m[..n * 4]);
|
||||
gl.uniform_4_f32_slice(l.glow.as_ref(), &glow_c[..n * 4]);
|
||||
gl.uniform_4_f32_slice(l.tint.as_ref(), &tint[..n * 4]);
|
||||
gl.draw_arrays(glow::TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
self.restore(saved);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the current window framebuffer as top-down RGBA8 (dev snapshots).
|
||||
///
|
||||
/// # Safety
|
||||
/// Must be called with the context current, after Slint has rendered.
|
||||
pub unsafe fn read_pixels(&self, w: u32, h: u32) -> Vec<u8> {
|
||||
let mut buf = vec![0u8; (w * h * 4) as usize];
|
||||
unsafe {
|
||||
self.gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1);
|
||||
self.gl.read_pixels(
|
||||
0,
|
||||
0,
|
||||
w as i32,
|
||||
h as i32,
|
||||
glow::RGBA,
|
||||
glow::UNSIGNED_BYTE,
|
||||
glow::PixelPackData::Slice(Some(&mut buf)),
|
||||
);
|
||||
}
|
||||
let row = (w * 4) as usize;
|
||||
let mut flipped = Vec::with_capacity(buf.len());
|
||||
for y in (0..h as usize).rev() {
|
||||
flipped.extend_from_slice(&buf[y * row..(y + 1) * row]);
|
||||
}
|
||||
flipped
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// Must be called with the context current.
|
||||
pub unsafe fn destroy(&mut self) {
|
||||
unsafe {
|
||||
self.gl.delete_program(self.scene_prog);
|
||||
self.gl.delete_program(self.blit_prog);
|
||||
self.gl.delete_program(self.glass_prog);
|
||||
self.gl.delete_vertex_array(self.vao);
|
||||
self.gl.delete_framebuffer(self.fbo);
|
||||
self.gl.delete_texture(self.tex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits the panels into draw runs: consecutive panels sharing a group
|
||||
/// (> 0) melt together and are drawn as one; the rest are drawn alone.
|
||||
fn runs(rects: &[Panel]) -> impl Iterator<Item = &[Panel]> {
|
||||
let mut rest = rects;
|
||||
std::iter::from_fn(move || {
|
||||
let first = rest.first()?;
|
||||
let mut n = 1;
|
||||
if first.group > 0.5 {
|
||||
while n < rest.len() && n < MAX_GROUP && (rest[n].group - first.group).abs() < 0.5 {
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
let (run, tail) = rest.split_at(n);
|
||||
rest = tail;
|
||||
Some(run)
|
||||
})
|
||||
}
|
||||
|
||||
unsafe fn link(gl: &glow::Context, header: &str, vs: &str, fs: &str) -> anyhow::Result<glow::Program> {
|
||||
unsafe {
|
||||
let program = gl.create_program().map_err(anyhow::Error::msg)?;
|
||||
let mut shaders = Vec::new();
|
||||
for (kind, src) in [(glow::VERTEX_SHADER, vs), (glow::FRAGMENT_SHADER, fs)] {
|
||||
let sh = gl.create_shader(kind).map_err(anyhow::Error::msg)?;
|
||||
gl.shader_source(sh, &format!("{header}{src}"));
|
||||
gl.compile_shader(sh);
|
||||
if !gl.get_shader_compile_status(sh) {
|
||||
anyhow::bail!("shader compile failed: {}", gl.get_shader_info_log(sh));
|
||||
}
|
||||
gl.attach_shader(program, sh);
|
||||
shaders.push(sh);
|
||||
}
|
||||
gl.link_program(program);
|
||||
if !gl.get_program_link_status(program) {
|
||||
anyhow::bail!("program link failed: {}", gl.get_program_info_log(program));
|
||||
}
|
||||
for sh in shaders {
|
||||
gl.detach_shader(program, sh);
|
||||
gl.delete_shader(sh);
|
||||
}
|
||||
Ok(program)
|
||||
}
|
||||
}
|
||||
384
crates/sf-launcher/src/glass/shaders.rs
Normal file
384
crates/sf-launcher/src/glass/shaders.rs
Normal file
@@ -0,0 +1,384 @@
|
||||
//! GLSL sources. The `#version` line is added at runtime (desktop GL 3.3 core or GLES 3.0).
|
||||
|
||||
/// Fullscreen triangle without vertex buffers.
|
||||
pub const VERTEX: &str = r#"
|
||||
out vec2 vUv;
|
||||
void main() {
|
||||
vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
|
||||
vUv = p;
|
||||
gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
/// A quad over `uBox` (x0, y0, x1, y1 in px, top-left origin), drawn as a
|
||||
/// 4-vertex triangle strip. Glass panels only shade the pixels they can reach.
|
||||
pub const QUAD_VERTEX: &str = r#"
|
||||
uniform vec4 uBox;
|
||||
uniform vec2 uRes;
|
||||
void main() {
|
||||
vec2 c = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1));
|
||||
vec2 p = mix(uBox.xy, uBox.zw, c);
|
||||
gl_Position = vec4(p.x / uRes.x * 2.0 - 1.0, 1.0 - p.y / uRes.y * 2.0, 0.0, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
/// Backdrop: a dark, slowly flowing mesh gradient (soft colour fields in the
|
||||
/// focused app's palette), like Apple's dark dynamic wallpapers.
|
||||
///
|
||||
/// It is smooth by design, so it is rendered at low resolution and upscaled.
|
||||
/// Cost per pixel: a handful of exp() calls, no noise, no loops over shapes.
|
||||
pub const SCENE: &str = r#"
|
||||
in vec2 vUv;
|
||||
out vec4 fragColor;
|
||||
|
||||
uniform vec2 uRes;
|
||||
uniform float uTime;
|
||||
uniform vec3 uPal[3]; // highlight, mid, deep
|
||||
uniform float uZoom; // launch fly-in
|
||||
uniform float uShelf; // 1 = top shelf showing, 0 = scrolled into the grid
|
||||
|
||||
float field(vec2 d, float r) {
|
||||
return exp(-dot(d, d) / (r * r));
|
||||
}
|
||||
|
||||
// Rotates the hue around the grey axis (keeps brightness roughly the same).
|
||||
vec3 hueRot(vec3 c, float ang) {
|
||||
const vec3 k = vec3(0.57735);
|
||||
float ca = cos(ang);
|
||||
return max(c * ca + cross(k, c) * sin(ang) + k * dot(k, c) * (1.0 - ca), 0.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 uv = vec2(vUv.x, 1.0 - vUv.y); // top-left origin
|
||||
uv = (uv - 0.5) / uZoom + 0.5;
|
||||
float aspect = uRes.x / uRes.y;
|
||||
float t = uTime;
|
||||
|
||||
// Gentle flow: two layers of low-frequency sine warp make the fields drift
|
||||
// and bend like liquid without any visible shapes.
|
||||
vec2 w = uv;
|
||||
w += 0.050 * vec2(sin(uv.y * 3.1 + t * 0.110), sin(uv.x * 2.3 - t * 0.090));
|
||||
w += 0.022 * vec2(sin(uv.y * 5.7 - t * 0.070 + 1.3), sin(uv.x * 4.9 + t * 0.130 + 0.7));
|
||||
vec2 a = vec2(aspect, 1.0);
|
||||
|
||||
vec3 hi = uPal[0];
|
||||
vec3 mid = uPal[1];
|
||||
vec3 deep = uPal[2];
|
||||
|
||||
// Near-black base; colour lives in a few soft fields with dark gaps between.
|
||||
vec3 col = deep * 0.35 + vec3(0.003, 0.003, 0.005);
|
||||
// Fields on slow Lissajous paths. Two are hue-shifted for depth, like
|
||||
// Apple's dark gradients (never a single flat hue).
|
||||
vec2 c0 = vec2(0.76 + 0.10 * sin(t * 0.050), 0.18 + 0.08 * cos(t * 0.043));
|
||||
vec2 c1 = vec2(0.20 + 0.12 * cos(t * 0.037 + 1.0), 0.26 + 0.10 * sin(t * 0.041));
|
||||
vec2 c2 = vec2(0.52 + 0.20 * sin(t * 0.029 + 2.0), 0.62 + 0.08 * cos(t * 0.033 + 0.5));
|
||||
vec2 c3 = vec2(0.96 + 0.05 * cos(t * 0.045), 0.88 + 0.06 * sin(t * 0.052));
|
||||
vec2 c4 = vec2(0.04 + 0.06 * sin(t * 0.031 + 4.0), 0.94 + 0.05 * cos(t * 0.027));
|
||||
col += hi * 0.34 * field((w - c0) * a, 0.40);
|
||||
col += mid * 0.62 * field((w - c1) * a, 0.48);
|
||||
col += hueRot(mix(mid, hi, 0.4), 0.55) * 0.34 * field((w - c2) * a, 0.38);
|
||||
col += hueRot(mid, -0.70) * 0.40 * field((w - c3) * a, 0.36);
|
||||
col += mid * 0.30 * field((w - c4) * a, 0.34);
|
||||
|
||||
// Soft shoulder so bright palettes stay "dark mode".
|
||||
col = col / (1.0 + col * 1.2);
|
||||
|
||||
// Calmer at the bottom where the icons sit; dimmer when the shelf is gone.
|
||||
col *= mix(1.0, 0.62, smoothstep(0.45, 1.0, uv.y));
|
||||
col *= mix(0.42, 1.0, uShelf);
|
||||
vec2 v = uv - 0.5;
|
||||
col *= 1.0 - 0.30 * dot(v, v);
|
||||
|
||||
fragColor = vec4(col, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
/// Upscales the backdrop to the window with dithering (no banding).
|
||||
pub const BLIT: &str = r#"
|
||||
in vec2 vUv;
|
||||
out vec4 fragColor;
|
||||
uniform sampler2D uScene;
|
||||
uniform float uTime;
|
||||
|
||||
float hash12(vec2 p) {
|
||||
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
|
||||
p3 += dot(p3, p3.yzx + 33.33);
|
||||
return fract((p3.x + p3.y) * p3.z);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 col = textureLod(uScene, vUv, 0.0).rgb;
|
||||
col += (hash12(gl_FragCoord.xy + fract(uTime) * 61.0) - 0.5) / 255.0;
|
||||
fragColor = vec4(col, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
/// One Liquid Glass panel (or one merged group of panels), drawn as a quad
|
||||
/// over its bounds plus the shadow margin.
|
||||
///
|
||||
/// The model follows Apple's Liquid Glass: light is *bent*, not scattered.
|
||||
/// Each panel is a slab of glass (IOR ~1.5) whose edge is a convex squircle
|
||||
/// bevel. The backdrop seen through it is displaced by a real refraction ray
|
||||
/// (Snell), with per-channel IOR for dispersion. Bigger panels are thicker:
|
||||
/// wider bevel, stronger lensing, deeper shadow. On top: Fresnel reflection,
|
||||
/// a geometry-aware specular rim, light from within when focused, and
|
||||
/// "materialize" (alpha ramps the lensing, not just the opacity).
|
||||
///
|
||||
/// The result is written as `dst * a + rgb` (blend ONE, SRC_ALPHA), so the
|
||||
/// shader never needs to read the framebuffer: shadows multiply, glass
|
||||
/// replaces, and nested glass (on another pane) lights what is already there.
|
||||
pub const GLASS: &str = r#"
|
||||
out vec4 fragColor;
|
||||
|
||||
#define MAX_GROUP 4
|
||||
|
||||
uniform sampler2D uScene;
|
||||
uniform vec2 uRes; // physical px
|
||||
uniform float uScale; // physical px per design px
|
||||
uniform float uTime;
|
||||
uniform vec2 uLight; // direction towards the key light (screen space, y down)
|
||||
uniform int uCount; // members of this run (> 1 = merged group)
|
||||
uniform vec4 uRect[MAX_GROUP]; // x, y, w, h (px, top-left origin)
|
||||
uniform vec4 uParam[MAX_GROUP]; // radius, focus, alpha, frost (mip lod; < 0 = nested on other glass)
|
||||
uniform vec4 uGlow[MAX_GROUP]; // accent rgb, unused
|
||||
uniform vec4 uTint[MAX_GROUP]; // tint rgb, tint amount
|
||||
|
||||
float sdRoundRect(vec2 p, vec2 b, float r) {
|
||||
vec2 q = abs(p) - b + r;
|
||||
return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r;
|
||||
}
|
||||
|
||||
// Analytic outward gradient of sdRoundRect.
|
||||
vec2 gradRoundRect(vec2 p, vec2 b, float r) {
|
||||
vec2 w = abs(p) - b + r;
|
||||
vec2 q = max(w, 0.0);
|
||||
vec2 g = (max(w.x, w.y) > 0.0) ? q / max(length(q), 1e-5) : ((w.x > w.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0));
|
||||
return sign(p) * g;
|
||||
}
|
||||
|
||||
float sdPanel(int i, vec2 p) {
|
||||
vec4 R = uRect[i];
|
||||
vec2 hs = R.zw * 0.5;
|
||||
float r = min(uParam[i].x, min(hs.x, hs.y));
|
||||
return sdRoundRect(p - (R.xy + hs), hs, r);
|
||||
}
|
||||
|
||||
// Polynomial smooth minimum: the "liquid bridge" between merged panels.
|
||||
float smin(float a, float b, float k) {
|
||||
float h = max(k - abs(a - b), 0.0) / k;
|
||||
return min(a, b) - h * h * k * 0.25;
|
||||
}
|
||||
|
||||
float sdGroup(vec2 p, float k) {
|
||||
float d = 1e5;
|
||||
for (int i = 0; i < MAX_GROUP; i++) {
|
||||
if (i >= uCount) break;
|
||||
if (uParam[i].z <= 0.001) continue;
|
||||
d = smin(d, sdPanel(i, p), k);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
// uv has a top-left origin; the scene texture is stored GL-style (bottom row first).
|
||||
vec3 sceneAt(vec2 uv, float lod) {
|
||||
uv = clamp(uv, vec2(0.001), vec2(0.999));
|
||||
return textureLod(uScene, vec2(uv.x, 1.0 - uv.y), lod).rgb;
|
||||
}
|
||||
|
||||
float luma(vec3 c) { return dot(c, vec3(0.2126, 0.7152, 0.0722)); }
|
||||
|
||||
// Convex squircle bevel: height (0 at the rim, 1 on the flat top) and slope.
|
||||
// u = distance from the rim / bevel width.
|
||||
vec2 squircle(float u) {
|
||||
u = clamp(u, 0.0, 1.0);
|
||||
float a = 1.0 - u;
|
||||
float a4 = a * a * a * a;
|
||||
float h = pow(max(1.0 - a4, 1e-4), 0.25);
|
||||
float dh = a * a * a * pow(max(1.0 - a4, 1e-3), -0.75);
|
||||
return vec2(h, dh);
|
||||
}
|
||||
|
||||
// Where a vertical ray through the glass lands on the backdrop (px offset).
|
||||
vec2 refractOffset(vec3 N, float height, float ior) {
|
||||
vec3 t = refract(vec3(0.0, 0.0, -1.0), N, 1.0 / ior);
|
||||
return t.xy / max(-t.z, 0.2) * height;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 p = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y); // px, top-left
|
||||
float s = uScale;
|
||||
float mergeK = 30.0 * s;
|
||||
bool merged = uCount > 1;
|
||||
|
||||
// Combined distance, and parameters blended towards the nearest member.
|
||||
float d = 1e5;
|
||||
float wsum = 0.0;
|
||||
vec4 P = vec4(0.0);
|
||||
vec3 accent = vec3(0.0);
|
||||
vec4 tint = vec4(0.0);
|
||||
vec2 center = vec2(0.0);
|
||||
vec2 halfSize = vec2(0.0);
|
||||
for (int k = 0; k < MAX_GROUP; k++) {
|
||||
if (k >= uCount) break;
|
||||
float a = uParam[k].z;
|
||||
if (a <= 0.001) continue;
|
||||
float dk = sdPanel(k, p);
|
||||
d = merged ? smin(d, dk, mergeK) : dk;
|
||||
float w = exp(-max(dk, 0.0) / (12.0 * s)) + 1e-4;
|
||||
wsum += w;
|
||||
P += uParam[k] * w;
|
||||
accent += uGlow[k].rgb * w;
|
||||
tint += uTint[k] * w;
|
||||
vec4 R = uRect[k];
|
||||
center += (R.xy + R.zw * 0.5) * w;
|
||||
halfSize += R.zw * 0.5 * w;
|
||||
}
|
||||
if (wsum <= 0.0) discard;
|
||||
P /= wsum; accent /= wsum; tint /= wsum; center /= wsum; halfSize /= wsum;
|
||||
|
||||
float focus = P.y;
|
||||
float alpha = P.z;
|
||||
float minHalf = min(halfSize.x, halfSize.y);
|
||||
bool nested = P.w < 0.0;
|
||||
// Size-dependent thickness: small controls are full domes (a drop of
|
||||
// liquid), big panes are thick slabs with a wide rounded edge.
|
||||
float bezel = min(minHalf * 0.80, 9.0 * s + minHalf * 0.10);
|
||||
bezel = clamp(bezel, 5.0 * s, 40.0 * s);
|
||||
float sizeK = clamp(minHalf / (240.0 * s), 0.0, 1.0);
|
||||
float small = 1.0 - clamp(minHalf / (90.0 * s), 0.0, 1.0);
|
||||
|
||||
// Output is dst * mul + add.
|
||||
float mul = 1.0;
|
||||
vec3 add = vec3(0.0);
|
||||
|
||||
// ---- outside: grounding shadow and a little accent light spill ----
|
||||
if (d > 0.0) {
|
||||
vec2 off = vec2(0.0, (10.0 + 18.0 * focus + 14.0 * sizeK) * s);
|
||||
float ds = merged ? sdGroup(p - off, mergeK) : sdPanel(0, p - off);
|
||||
float spread = (34.0 + 34.0 * focus + 50.0 * sizeK) * s * (nested ? 0.5 : 1.0);
|
||||
float shadow = (1.0 - smoothstep(-spread * 0.5, spread, ds)) * (0.26 + 0.22 * focus + 0.18 * sizeK);
|
||||
shadow += (1.0 - smoothstep(0.0, 6.0 * s, d)) * 0.10;
|
||||
mul = 1.0 - shadow * alpha;
|
||||
add = accent * exp(-d / (22.0 * s)) * focus * 0.16 * alpha;
|
||||
if (d > 1.0) {
|
||||
if (mul > 0.998 && dot(add, vec3(1.0)) < 0.002) discard;
|
||||
fragColor = vec4(add, mul);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- surface geometry ----
|
||||
vec2 n2;
|
||||
if (merged) {
|
||||
vec2 e = vec2(0.75, 0.0);
|
||||
vec2 g2 = vec2(sdGroup(p + e.xy, mergeK) - sdGroup(p - e.xy, mergeK),
|
||||
sdGroup(p + e.yx, mergeK) - sdGroup(p - e.yx, mergeK));
|
||||
n2 = g2 / max(length(g2), 1e-5);
|
||||
} else {
|
||||
vec4 R = uRect[0];
|
||||
vec2 hs = R.zw * 0.5;
|
||||
n2 = gradRoundRect(p - (R.xy + hs), hs, min(uParam[0].x, min(hs.x, hs.y)));
|
||||
}
|
||||
|
||||
float inside = max(-d, 0.0);
|
||||
float u = inside / bezel;
|
||||
vec2 hp = squircle(u);
|
||||
float thick = bezel * (1.1 + 0.3 * focus); // glass height in px
|
||||
float height = thick * hp.x;
|
||||
float slope = (u < 1.0) ? hp.y * thick / bezel : 0.0;
|
||||
slope = min(slope, 6.0);
|
||||
vec3 N = normalize(vec3(n2 * slope, 1.0));
|
||||
|
||||
// Materialize: lensing and frost ramp up with alpha instead of a plain fade.
|
||||
float mat = smoothstep(0.0, 1.0, alpha);
|
||||
float lens = (1.25 + 0.35 * focus) * mat;
|
||||
vec2 mag = -(p - center) * (0.025 + 0.035 * focus) * mat;
|
||||
float lod = abs(P.w) * mix(0.35, 1.0, mat);
|
||||
|
||||
// The glass colour is affine in what is underneath: g = dst * A + B.
|
||||
float A = 0.0;
|
||||
vec3 B;
|
||||
if (nested) {
|
||||
A = 1.0;
|
||||
B = vec3(0.0);
|
||||
} else if (slope > 0.02) {
|
||||
// Bevel: refract each channel with its own IOR (dispersion).
|
||||
B.r = sceneAt((p + refractOffset(N, height, 1.46) * lens + mag) / uRes, lod).r;
|
||||
B.g = sceneAt((p + refractOffset(N, height, 1.50) * lens + mag) / uRes, lod).g;
|
||||
B.b = sceneAt((p + refractOffset(N, height, 1.56) * lens + mag) / uRes, lod).b;
|
||||
} else {
|
||||
B = sceneAt((p + mag) / uRes, lod);
|
||||
}
|
||||
|
||||
// ---- body ----
|
||||
// Adaptive tone: lift glass over dark content, hold it back over bright content.
|
||||
float bgL = luma(sceneAt(center / uRes, 6.0));
|
||||
float kb = 1.02 + 0.10 * focus;
|
||||
A *= kb;
|
||||
B = B * kb + mix(0.045, -0.01, clamp(bgL * 2.0, 0.0, 1.0)) + (nested ? 0.025 : 0.0);
|
||||
A *= 1.0 - tint.a;
|
||||
B = mix(B, tint.rgb, tint.a);
|
||||
|
||||
// Light from within when focused: starts at the center, spreads to the rim.
|
||||
vec2 qn = (p - center) / max(halfSize, vec2(1.0));
|
||||
float core = exp(-dot(qn, qn) * 1.3);
|
||||
vec3 inner = mix(vec3(1.0), accent, 0.45);
|
||||
B += inner * focus * (0.06 + 0.12 * core);
|
||||
|
||||
// Focus sheen: a soft diagonal band of light glides across the glass now and then.
|
||||
float sweep = fract(uTime * 0.16 + center.x * 0.0007);
|
||||
float band = (qn.x * 0.8 + qn.y * 0.45) * 0.5 + 0.5;
|
||||
float sheen = exp(-pow((band - (sweep * 1.8 - 0.4)) * 7.0, 2.0));
|
||||
B += vec3(1.0) * sheen * focus * 0.10 * mat;
|
||||
|
||||
// Soft vertical sheen, as if lit from above.
|
||||
float vy = clamp((p.y - (center.y - halfSize.y)) / (2.0 * halfSize.y), 0.0, 1.0);
|
||||
B += vec3(0.06 + 0.05 * focus) * pow(1.0 - vy, 2.5);
|
||||
|
||||
// ---- light ----
|
||||
float wob = focus * 0.35 * sin(uTime * 0.9);
|
||||
vec2 L2 = mat2(cos(wob), sin(wob), -sin(wob), cos(wob)) * uLight;
|
||||
vec3 L = normalize(vec3(L2, 0.55));
|
||||
vec3 V = vec3(0.0, 0.0, 1.0);
|
||||
|
||||
// Fresnel: steep bevel reflects the (blurred, brightened) surroundings.
|
||||
float cosT = clamp(N.z, 0.0, 1.0);
|
||||
float F = 0.04 + 0.96 * pow(1.0 - cosT, 5.0);
|
||||
float Fm = clamp(F, 0.0, 1.0) * 0.35 * mat;
|
||||
vec3 env = mix(sceneAt((p + n2 * 60.0 * s) / uRes, 5.0) * 1.6, vec3(0.92), 0.35);
|
||||
A *= 1.0 - Fm;
|
||||
B = mix(B, env, Fm);
|
||||
|
||||
// Specular on the bevel: key light, plus a weaker bounce on the far side.
|
||||
vec3 H = normalize(L + V);
|
||||
vec3 H2 = normalize(vec3(-L2, 0.55) + V);
|
||||
float spec = pow(max(dot(N, H), 0.0), 26.0);
|
||||
float spec2 = pow(max(dot(N, H2), 0.0), 26.0);
|
||||
float bevelMask = 1.0 - smoothstep(0.0, 1.0, u);
|
||||
B += vec3(1.0) * (spec * 0.60 + spec2 * 0.38) * bevelMask * (0.8 + 0.6 * focus) * mat;
|
||||
|
||||
// Glossy highlight on small domes, like a water drop.
|
||||
vec2 hl = qn - uLight * 0.42;
|
||||
float gloss = exp(-dot(hl * vec2(1.4, 2.2), hl * vec2(1.4, 2.2)) * 3.0);
|
||||
B += vec3(1.0) * gloss * small * 0.14 * mat;
|
||||
|
||||
// Crisp rim: a bright hairline all around, strongest towards the light
|
||||
// and again on the far side where light exits the glass.
|
||||
float facing = max(dot(n2, uLight), 0.0);
|
||||
float away = max(dot(n2, -uLight), 0.0);
|
||||
float rimLine = exp(-inside / (1.0 * s));
|
||||
float rimGlow = exp(-inside / (6.0 * s));
|
||||
B += rimLine * (0.22 + 0.80 * pow(facing, 2.0) + 0.55 * pow(away, 2.0) + 0.22 * focus) * mat;
|
||||
B += rimGlow * (0.06 + 0.10 * facing + 0.06 * away + 0.08 * focus) * mat;
|
||||
B += accent * rimGlow * focus * 0.10 * mat;
|
||||
|
||||
// Subtle inner shade on the side away from the light gives the slab depth.
|
||||
float shade = 1.0 - 0.08 * bevelMask * away * mat;
|
||||
A *= shade;
|
||||
B *= shade;
|
||||
|
||||
float cov = clamp(0.5 - d, 0.0, 1.0) * clamp(alpha * 2.0, 0.0, 1.0);
|
||||
float keep = 1.0 - cov + cov * A;
|
||||
fragColor = vec4(add * keep + cov * B, mul * keep);
|
||||
}
|
||||
"#;
|
||||
Reference in New Issue
Block a user