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
use hetseq::*;
use specs::SystemData;

use super::stage::*;
use super::target::*;

// use color::Rgba;
use error::{Error, Result};
use fnv::FnvHashMap as HashMap;
use types::{Encoder, Factory};


/// Defines how the rendering pipeline should be configured.
#[derive(Clone, Debug)]
pub struct Pipeline<L> {
    stages: L,
    targets: HashMap<String, Target>,
}

impl Pipeline<List<()>> {
    /// Builds a new renderer pipeline.
    pub fn build() -> PipelineBuilder<Queue<()>> {
        PipelineBuilder::new()
    }
}

impl<L> Pipeline<L> {
    /// Returns an immutable reference to all targets and their name strings.
    pub fn targets(&self) -> &HashMap<String, Target> {
        &self.targets
    }
}

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

///
pub trait PolyStages: for<'a> StagesData<'a> {
    ///
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoders: &mut Encoder,
        factory: Factory,
        data: <Self as StagesData<'b>>::Data,
    );

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

impl<'a, HS> StagesData<'a> for List<(HS, List<()>)>
where
    HS: PolyStage,
{
    type Data = <HS as StageData<'a>>::Data;
}

impl<HS> PolyStages for List<(HS, List<()>)>
where
    HS: PolyStage,
{
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoders: &mut Encoder,
        factory: Factory,
        hd: <HS as StageData<'b>>::Data,
    ) {
        let List((ref mut hs, _)) = *self;
        hs.apply(encoders, factory, hd);
    }

    fn new_targets(&mut self, new_targets: &HashMap<String, Target>) {
        let List((ref mut hs, _)) = *self;
        HS::new_targets(hs, new_targets);
    }
}

impl<'a, HS, TS> StagesData<'a> for List<(HS, TS)>
where
    HS: PolyStage,
    TS: PolyStages,
{
    type Data = (<HS as StageData<'a>>::Data, <TS as StagesData<'a>>::Data);
}

impl<HS, TS> PolyStages for List<(HS, TS)>
where
    HS: PolyStage,
    TS: PolyStages,
{
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoders: &mut Encoder,
        factory: Factory,
        (hd, td): <Self as StagesData<'b>>::Data,
    ) {
        let List((ref mut hs, ref mut ts)) = *self;
        hs.apply(encoders, factory.clone(), hd);
        ts.apply(encoders, factory, td);
    }

    fn new_targets(&mut self, new_targets: &HashMap<String, Target>) {
        let List((ref mut hs, ref mut ts)) = *self;
        HS::new_targets(hs, new_targets);
        TS::new_targets(ts, new_targets);
    }
}

/// The data requested from the `specs::World` by the Pipeline.
pub trait PipelineData<'a> {
    /// The data itself
    type Data: SystemData<'a> + Send;
}

/// Trait used for the pipeline.
pub trait PolyPipeline: for<'a> PipelineData<'a> {
    /// Retuns `ParallelIterator` which apply data to all stages
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoder: &mut Encoder,
        factory: Factory,
        data: <Self as PipelineData<'b>>::Data,
    );

    /// Resizes the pipeline targets
    fn new_targets(&mut self, new_targets: HashMap<String, Target>);

    /// Returns an immutable reference to all targets and their name strings.
    fn targets(&self) -> &HashMap<String, Target>;
}

impl<'a, L> PipelineData<'a> for Pipeline<L>
where
    L: PolyStages,
{
    type Data = <L as StagesData<'a>>::Data;
}

impl<L> PolyPipeline for Pipeline<L>
where
    L: PolyStages,
{
    fn apply<'a, 'b: 'a>(
        &'a mut self,
        encoders: &mut Encoder,
        factory: Factory,
        data: <L as StagesData<'b>>::Data,
    ) {
        self.stages.apply(encoders, factory, data);
    }

    fn new_targets(&mut self, new_targets: HashMap<String, Target>) {
        self.stages.new_targets(&new_targets);
        self.targets = new_targets;
    }

    /// Returns an immutable reference to all targets and their name strings.
    fn targets(&self) -> &HashMap<String, Target> {
        self.targets()
    }
}

/// Constructs a new pipeline with the given render targets and layers.
#[derive(Clone, Debug)]
pub struct PipelineBuilder<Q> {
    stages: Q,
    targets: Vec<TargetBuilder>,
}

impl PipelineBuilder<Queue<()>> {
    /// Creates a new PipelineBuilder.
    pub fn new() -> Self {
        Default::default()
    }
}

impl Default for PipelineBuilder<Queue<()>> {
    fn default() -> Self {
        PipelineBuilder {
            stages: Queue::new(),
            targets: Vec::new(),
        }
    }
}

impl<Q> PipelineBuilder<Queue<Q>> {
    /// Constructs a new stage in this pipeline.
    pub fn with_stage<P>(
        self,
        sb: StageBuilder<P>,
    ) -> PipelineBuilder<Queue<(Queue<Q>, StageBuilder<P>)>> {
        PipelineBuilder {
            stages: self.stages.push(sb),
            targets: self.targets,
        }
    }
}


impl<Q> PipelineBuilder<Q> {
    /// Constructs a new render target for this pipeline.
    pub fn with_target(mut self, tb: TargetBuilder) -> Self {
        self.targets.push(tb);
        self
    }
}

///
pub trait PipelineBuild {
    /// Resuling pipeline
    type Pipeline: PolyPipeline;

    /// Build pipeline
    fn build(self, fac: &mut Factory, out: &Target, multisampling: u16) -> Result<Self::Pipeline>;
}

impl<L, Z, R, Q> PipelineBuild for PipelineBuilder<Q>
where
    Q: IntoList<List = L>,
    L: for<'a> Functor<BuildStage<'a>, Output = Z>,
    Z: Try<Error, Ok = R>,
    R: PolyStages,
{
    type Pipeline = Pipeline<R>;
    fn build(mut self, fac: &mut Factory, out: &Target, multisampling: u16) -> Result<Pipeline<R>> {
        let mut targets = self.targets
            .drain(..)
            .map(|tb| tb.build(fac, out.size()))
            .collect::<Result<Targets>>()?;

        targets.insert("".into(), out.clone());

        let stages = self.stages
            .into_list()
            .fmap(BuildStage::new(fac, &targets, multisampling))
            .try()?;

        Ok(Pipeline { stages, targets })
    }
}

pub struct BuildStage<'a> {
    factory: &'a mut Factory,
    targets: &'a Targets,
    multisampling: u16,
}

impl<'a> BuildStage<'a> {
    fn new(factory: &'a mut Factory, targets: &'a Targets, multisampling: u16) -> Self {
        BuildStage { factory, targets, multisampling }
    }
}

impl<'a, Q, L, Z, R> HetFnOnce<(StageBuilder<Q>,)> for BuildStage<'a>
where
    Q: IntoList<List = L>,
    L: for<'b> Functor<CompilePass<'b>, Output = Z>,
    Z: Try<Error, Ok = R>,
    R: Passes,
{
    type Output = Result<Stage<R>>;
    fn call_once(self, (stage,): (StageBuilder<Q>,)) -> Result<Stage<R>> {
        stage.build(self.factory, self.targets, self.multisampling)
    }
}

impl<'a, Q, L, Z, R> HetFnMut<(StageBuilder<Q>,)> for BuildStage<'a>
where
    Q: IntoList<List = L>,
    L: for<'b> Functor<CompilePass<'b>, Output = Z>,
    Z: Try<Error, Ok = R>,
    R: Passes,
{
    fn call_mut(&mut self, (stage,): (StageBuilder<Q>,)) -> Result<Stage<R>> {
        stage.build(self.factory, self.targets, self.multisampling)
    }
}