use amethyst_core::specs::prelude::SystemData;
use crate::{
error::Result,
pipe::{Effect, NewEffect, Target},
types::{Encoder, Factory},
};
pub trait PassData<'a> {
type Data: SystemData<'a> + Send;
}
pub trait Pass: for<'a> PassData<'a> {
fn compile(&mut self, effect: NewEffect<'_>) -> Result<Effect>;
fn apply<'a, 'b: 'a>(
&'a mut self,
encoder: &mut Encoder,
effect: &mut Effect,
factory: Factory,
data: <Self as PassData<'b>>::Data,
);
}
#[derive(Clone, Debug)]
pub struct CompiledPass<P> {
effect: Effect,
inner: P,
}
impl<P> CompiledPass<P>
where
P: Pass,
{
pub(super) fn compile(
mut pass: P,
fac: &mut Factory,
out: &Target,
multisampling: u16,
) -> Result<Self> {
let effect = pass.compile(NewEffect::new(fac, out, multisampling))?;
Ok(CompiledPass {
effect,
inner: pass,
})
}
}
impl<P> CompiledPass<P> {
pub fn apply<'a, 'b: 'a>(
&'a mut self,
encoder: &mut Encoder,
factory: Factory,
data: <P as PassData<'b>>::Data,
) where
P: Pass,
{
self.inner.apply(encoder, &mut self.effect, factory, data)
}
pub fn new_target(&mut self, target: &Target) {
self.effect.data.out_colors.clear();
self.effect
.data
.out_colors
.extend(target.color_bufs().iter().map(|cb| &cb.as_output).cloned());
self.effect.data.out_blends.clear();
self.effect
.data
.out_blends
.extend(target.color_bufs().iter().map(|cb| &cb.as_output).cloned());
self.effect.data.out_depth = target.depth_buf().map(|db| (db.as_output.clone(), (0, 0)));
}
}