use crate::egl::{ContextRef, CurrentContext, Dimensions, Texture};
use glium::{program, uniform};
use std::error::Error;
use super::Shader;
pub struct Crop;
impl Crop {
pub fn new(facade: &impl glium::backend::Facade, dims: (u32, u32))
-> Result<Shader<Self>, Box<dyn Error>>
{
Shader::<Self>::new_with_facade(facade, dims)
}
}
impl Shader<Crop> {
fn new_with_facade<F: glium::backend::Facade>(
facade: &F,
size: (u32, u32),
) -> Result<Self, Box<dyn Error>> {
let (vertices, indices) = super::covering_vertices(facade, size)?;
Ok(Self {
program: program!(
facade,
120 => {
vertex: include_str!("crop/vert.glsl"),
fragment: include_str!("crop/frag.glsl"),
}
)?,
vertices,
indices,
_data: Default::default(),
})
}
pub fn convert<F: glium::backend::Facade>(
&self,
facade: &F,
source_tex: Texture,
target: &mut impl glium::Surface,
) -> Result<(), Box<dyn Error>>{
let Dimensions { width: ws, height: hs } = source_tex.dimensions();
let (wt, ht ) = target.get_dimensions();
if wt > ws || ht > hs {
Err(super::Error::BadDimensions("Neither target dimensions can exceed the source dimension"))?;
}
self.draw_any(
facade,
source_tex,
target,
uniform! {},
)
}
}