use anyhow::{Result, ensure};
use gpui::{Bounds, Pixels, SceneShaderProgram, ShaderOutputEncoding, Window};
use std::{marker::PhantomData, sync::Arc};
mod border;
mod compiler;
pub mod presets;
mod shadow;
pub use border::{AnimatedBorder, AnimatedBorderLayer, AnimatedBorderStyle};
pub use shadow::{ShadowLayer, ShadowPainter, ShadowStop, ShadowStyle};
#[derive(Clone, Copy, Debug)]
pub struct InstanceField {
pub name: &'static str,
pub offset: usize,
pub components: usize,
}
pub trait ShaderInstance: bytemuck::Pod {
const FIELDS: &'static [InstanceField];
}
pub struct ShaderProgram<T> {
program: Arc<SceneShaderProgram>,
pub(crate) source: Arc<str>,
_instance: PhantomData<fn(T)>,
}
impl<T> Clone for ShaderProgram<T> {
fn clone(&self) -> Self {
Self {
program: self.program.clone(),
source: self.source.clone(),
_instance: PhantomData,
}
}
}
impl<T: ShaderInstance> ShaderProgram<T> {
pub fn new(source: &str) -> Result<Self> {
Self::with_encoding(source, ShaderOutputEncoding::EncodedSrgb)
}
pub fn new_linear_p3(source: &str) -> Result<Self> {
Self::with_encoding(source, ShaderOutputEncoding::LinearDisplayP3)
}
pub fn new_linear_rec2020(source: &str) -> Result<Self> {
Self::with_encoding(source, ShaderOutputEncoding::LinearRec2020)
}
fn with_encoding(source: &str, encoding: ShaderOutputEncoding) -> Result<Self> {
Ok(Self {
program: Arc::new(compiler::compile::<T>(source, encoding)?),
source: Arc::from(source),
_instance: PhantomData,
})
}
pub(crate) fn glyph_program(source: &str) -> Result<Self> {
Ok(Self {
program: Arc::new(compiler::compile_glyph::<T>(source)?),
source: Arc::from(source),
_instance: PhantomData,
})
}
pub(crate) fn path_program(source: &str) -> Result<Self> {
Ok(Self {
program: Arc::new(compiler::compile_path::<T>(source)?),
source: Arc::from(source),
_instance: PhantomData,
})
}
pub fn scene_program(&self) -> Arc<SceneShaderProgram> {
self.program.clone()
}
pub fn id(&self) -> u64 {
self.program.id()
}
pub(crate) fn pack(&self, instance: &T) -> Result<[[f32; 4]; gpui::SHADER_DATA_SLOTS]> {
let mut slots = [[0.; 4]; gpui::SHADER_DATA_SLOTS];
for (index, bytes) in bytemuck::bytes_of(instance).chunks_exact(4).enumerate() {
let value = f32::from_ne_bytes(bytes.try_into().unwrap());
ensure!(
value.is_finite(),
"shader instance contains non-finite data at byte {}",
index * 4
);
slots[index / 4][index % 4] = value;
}
Ok(slots)
}
}
pub trait WindowShaderExt {
fn paint_custom_shader<T: ShaderInstance>(
&mut self,
bounds: Bounds<Pixels>,
program: &ShaderProgram<T>,
instance: T,
) -> Result<()>;
}
impl WindowShaderExt for Window {
fn paint_custom_shader<T: ShaderInstance>(
&mut self,
bounds: Bounds<Pixels>,
program: &ShaderProgram<T>,
instance: T,
) -> Result<()> {
ensure!(
[
f32::from(bounds.origin.x),
f32::from(bounds.origin.y),
f32::from(bounds.size.width),
f32::from(bounds.size.height)
]
.iter()
.all(|v| v.is_finite()),
"non-finite shader bounds"
);
if program.program.output_encoding() != ShaderOutputEncoding::EncodedSrgb {
ensure!(
self.color_status().effective() == gpui::WindowColorMode::WideGamutSdr,
"linear P3 shaders require an active native WideGamutSdr window"
);
}
let data = program.pack(&instance)?;
self.paint_shader(bounds, program.program.clone(), data);
Ok(())
}
}
#[cfg(test)]
mod tests;