1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! A stage in the rendering pipeline.

use hetseq::*;

use error::{Error, Result};
use fnv::FnvHashMap as HashMap;
use pipe::{Target, Targets};
use pipe::pass::{CompiledPass, Pass, PassData};
use specs::SystemData;

use types::{Encoder, Factory};

/// A stage in the rendering pipeline.
#[derive(Clone, Debug)]
pub struct Stage<L> {
    clear_color: Option<[f32; 4]>,
    clear_depth: Option<f32>,
    enabled: bool,
    passes: L,
    target_name: String,
    target: Target,
}

impl Stage<List<()>> {
    /// Builds a new `PolyStage` which outputs to the `Target` with the given name.
    pub fn with_target<N: Into<String>>(target_name: N) -> StageBuilder<Queue<()>> {
        StageBuilder::new(target_name.into())
    }

    /// Builds a new `PolyStage` which outputs straight into the backbuffer.
    pub fn with_backbuffer() -> StageBuilder<Queue<()>> {
        StageBuilder::new("")
    }
}

impl<L> Stage<L> {
    /// Enables the `PolyStage` so it will execute on every frame.
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disables the `PolyStage`, preventing it from being executed on every frame.
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Returns whether this `PolyStage` is enabled.
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }
}

pub trait PassesData<'a> {
    type Data: SystemData<'a> + Send;
}

pub trait Passes: for<'a> PassesData<'a> {
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoder: &mut Encoder,
        factory: Factory,
        data: <Self as PassesData<'b>>::Data,
    );

    /// Distributes new targets
    fn new_target(&mut self, new_target: &Target);
}

impl<'a, HP> PassesData<'a> for List<(CompiledPass<HP>, List<()>)>
where
    HP: Pass,
{
    type Data = <HP as PassData<'a>>::Data;
}

impl<HP> Passes for List<(CompiledPass<HP>, List<()>)>
where
    HP: Pass,
{
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoder: &mut Encoder,
        factory: Factory,
        hd: <HP as PassData<'b>>::Data,
    ) {
        let List((ref mut hp, _)) = *self;
        hp.apply(encoder, factory, hd);
    }

    fn new_target(&mut self, new_target: &Target) {
        let List((ref mut hp, _)) = *self;
        hp.new_target(new_target);
    }
}

impl<'a, HP, TP> PassesData<'a> for List<(CompiledPass<HP>, TP)>
where
    HP: Pass,
    TP: Passes,
{
    type Data = (<HP as PassData<'a>>::Data, <TP as PassesData<'a>>::Data);
}

impl<HP, TP> Passes for List<(CompiledPass<HP>, TP)>
where
    HP: Pass,
    TP: Passes,
{
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoder: &mut Encoder,
        factory: Factory,
        (hd, td): (<HP as PassData<'b>>::Data, <TP as PassesData<'b>>::Data),
    ) {
        let List((ref mut hp, ref mut tp)) = *self;
        hp.apply(encoder, factory.clone(), hd);
        tp.apply(encoder, factory, td);
    }

    fn new_target(&mut self, new_target: &Target) {
        let List((ref mut hp, ref mut tp)) = *self;
        hp.new_target(new_target);
        tp.new_target(new_target);
    }
}

/// Data requested by the pass from the specs::World.
pub trait StageData<'a> {
    type Data: SystemData<'a> + Send;
}

/// A stage in the rendering.  Contains multiple passes.
pub trait PolyStage: for<'a> StageData<'a> {
    ///
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoder: &mut Encoder,
        factory: Factory,
        data: <Self as StageData<'b>>::Data,
    );

    /// Distributes new targets
    fn new_targets(&mut self, new_targets: &HashMap<String, Target>);
}

impl<'a, L> StageData<'a> for Stage<L>
where
    L: Passes,
{
    type Data = <L as PassesData<'a>>::Data;
}

impl<L> PolyStage for Stage<L>
where
    L: Passes + Length,
{
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoder: &mut Encoder,
        factory: Factory,
        data: <L as PassesData<'b>>::Data,
    ) {
        self.clear_color
            .map(|c| self.target.clear_color(encoder, c));
        self.clear_depth
            .map(|d| self.target.clear_depth_stencil(encoder, d));

        self.passes.apply(encoder, factory, data);
    }

    fn new_targets(&mut self, new_targets: &HashMap<String, Target>) {
        match new_targets.get(&self.target_name) {
            Some(target) => {
                self.target = target.clone();
                self.passes.new_target(target);
            }
            None => {
                eprintln!("Target name {:?} not found!", self.target_name);
            }
        }
    }
}

/// Constructs a new rendering stage.
#[derive(Derivative)]
#[derivative(Clone, Debug)]
pub struct StageBuilder<Q> {
    clear_color: Option<[f32; 4]>,
    clear_depth: Option<f32>,
    enabled: bool,
    passes: Q,
    target_name: String,
}

impl StageBuilder<Queue<()>> {
    /// Creates a new `StageBuilder` using the given target.
    pub fn new<T: Into<String>>(target_name: T) -> Self {
        StageBuilder {
            clear_color: None,
            clear_depth: None,
            enabled: true,
            passes: Queue::new(),
            target_name: target_name.into(),
        }
    }
}

impl<Q> StageBuilder<Q> {
    /// Clears the stage's target.
    pub fn clear_target<R, C, D>(mut self, color_val: C, depth_val: D) -> Self
    where
        R: Into<[f32; 4]>,
        C: Into<Option<R>>,
        D: Into<Option<f32>>,
    {
        self.clear_color = color_val.into().map(|c| c.into());
        self.clear_depth = depth_val.into();
        self
    }

    /// Sets whether the `PolyStage` is turned on by default.
    pub fn enabled(mut self, val: bool) -> Self {
        self.enabled = val;
        self
    }

    pub(crate) fn build<'a, L, Z, R>(
        self,
        fac: &'a mut Factory,
        targets: &'a Targets,
    ) -> Result<Stage<R>>
    where
        Q: IntoList<List = L>,
        L: for<'b> Functor<CompilePass<'b>, Output = Z>,
        Z: Try<Error, Ok = R>,
        R: Passes,
    {
        let out = targets
            .get(&self.target_name)
            .cloned()
            .ok_or(Error::NoSuchTarget(self.target_name.clone()))?;

        let passes = self.passes
            .into_list()
            .fmap(CompilePass::new(fac, &out))
            .try()?;

        Ok(Stage {
            clear_color: self.clear_color,
            clear_depth: self.clear_depth,
            enabled: self.enabled,
            passes,
            target: out,
            target_name: self.target_name,
        })
    }
}

impl<Q> StageBuilder<Queue<Q>> {
    /// Appends another `Pass` to the stage.
    pub fn with_pass<P: Pass>(self, pass: P) -> StageBuilder<Queue<(Queue<Q>, P)>> {
        StageBuilder {
            clear_color: self.clear_color,
            clear_depth: self.clear_depth,
            enabled: self.enabled,
            passes: self.passes.push(pass),
            target_name: self.target_name,
        }
    }
}



pub struct CompilePass<'a> {
    factory: &'a mut Factory,
    target: &'a Target,
}

impl<'a> CompilePass<'a> {
    fn new(factory: &'a mut Factory, target: &'a Target) -> Self {
        CompilePass { factory, target }
    }
}

impl<'a, P> HetFnOnce<(P,)> for CompilePass<'a>
where
    P: Pass,
{
    type Output = Result<CompiledPass<P>>;
    fn call_once(self, (pass,): (P,)) -> Result<CompiledPass<P>> {
        CompiledPass::compile(pass, self.factory, self.target)
    }
}
impl<'a, P> HetFnMut<(P,)> for CompilePass<'a>
where
    P: Pass,
{
    fn call_mut(&mut self, (pass,): (P,)) -> Result<CompiledPass<P>> {
        CompiledPass::compile(pass, self.factory, self.target)
    }
}