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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
//  def_programs!
//
/// Define a list of shaders with source paths and a list of shader programs
/// using those shaders.
///
/// This will create two enums in local scope named `ShaderId` and `ProgramId`
/// with variants from the given identifiers.
#[macro_export]
macro_rules! def_programs {
  //
  //  main rule
  //
  (
    SHADERS [
      $($shader:ident, $sourcepath:expr),*
    ]
    PROGRAMS [
      $(
      program $program:ident {
        vertex_shader:   $vert_shader:ident
        $(tessellation_control_shader: $tess_ctrl_shader:ident
        tessellation_evaluation_shader: $tess_eval_shader:ident)*
        $(geometry_shader: $geom_shader:ident)*
        fragment_shader: $frag_shader:ident
      }
      )*
    ]

  ) => {

    def_programs!{
      @def_shaders
      $($shader, $sourcepath),*
    }

    def_programs!{
      @def_programs
      SHADERS [ $($shader),* ]
      PROGRAMS [
        $(
        program $program {
          vertex_shader:   $vert_shader
          $(tessellation_control_shader: $tess_ctrl_shader
          tessellation_evaluation_shader: $tess_eval_shader)*
          $(geometry_shader: $geom_shader)*
          fragment_shader: $frag_shader
        }
        )*
      ]
    }
  };

  //
  //  @def_programs
  //
  ( @def_programs
    SHADERS [ $($shader:ident),* ]
    PROGRAMS [
      $(
      program $program:ident {
        vertex_shader:   $vert_shader:ident
        $(tessellation_control_shader: $tess_ctrl_shader:ident
        tessellation_evaluation_shader: $tess_eval_shader:ident)*
        $(geometry_shader: $geom_shader:ident)*
        fragment_shader: $frag_shader:ident
      }
      )*
    ]
  ) => {
    enum_unitary! {
      pub enum ProgramId (ProgramIdVariants) {
        $($program),*
      }
    }

    impl ProgramId {
      fn create_program (&self,
        glium_facade : &::glium::backend::Facade,
        shaders      : &::vec_map::VecMap <String>
      ) -> ::glium::Program {
        match *self {
          $(
          ProgramId::$program => {
            let vertex_shader
              = shaders.get (ShaderId::$vert_shader as usize).unwrap().as_str();
            let tessellation_control_shader
              = def_programs!(@option_shader shaders [$($tess_ctrl_shader)*]);
            let tessellation_evaluation_shader
              = def_programs!(@option_shader shaders [$($tess_eval_shader)*]);
            let geometry_shader
              = def_programs!(@option_shader shaders [$($geom_shader)*]);
            let fragment_shader
              = shaders.get (ShaderId::$frag_shader as usize).unwrap().as_str();
            ::glium::Program::new (
              glium_facade,
              ::glium::program::ProgramCreationInput::SourceCode {
                vertex_shader,
                tessellation_control_shader,
                tessellation_evaluation_shader,
                geometry_shader,
                fragment_shader,
                transform_feedback_varyings: None,
                outputs_srgb:                true,
                uses_point_size:             false
              }
            ).unwrap()
          }
          )*
        }
      }
    }

    pub fn build_programs (glium_facade : &::glium::backend::Facade)
      -> Result <::vec_map::VecMap <::glium::Program>, ::std::io::Error>
    {
      debug!("building shader programs...");

      let shaders = try!{ load_shaders() };
      let mut programs = ::vec_map::VecMap::new();
      for program_id in ProgramId::iter_variants() {
        let pid = program_id as usize;
        assert!(
          programs.insert (pid, program_id.create_program (glium_facade, &shaders)
        ).is_none());
      }

      debug!("...building shader programs");
      Ok (programs)
    }
  };

  //
  //  @def_shaders
  //
  (
    @def_shaders $($shader:ident, $sourcepath:expr),*
  ) => {

    enum_unitary! {
      pub enum ShaderId (IdVariants) {
        $($shader),*
      }
    }

    impl ShaderId {
      pub fn load_source (&self) -> Result <String, ::std::io::Error> {
        use ::std::io::Read;
        let mut source_file   = try!{ ::std::fs::File::open (self.sourcepath()) };
        let mut source_string = ::std::string::String::new();
        let _ = source_file.read_to_string (&mut source_string).unwrap();
        Ok (source_string)
      }

      pub fn sourcepath (&self) -> &'static str {
        match *self {
          $(ShaderId::$shader => $sourcepath),*
        }
      }
    }

    pub fn load_shaders()
      -> Result <::vec_map::VecMap <String>, ::std::io::Error>
    {
      let mut shaders = ::vec_map::VecMap::new();
      for shader_id in ShaderId::iter_variants() {
        let source_string = try!{ shader_id.load_source() };
        let sid = shader_id as usize;
        assert!{ shaders.insert (sid, source_string).is_none() }
      }
      Ok (shaders)
    }

  };

  //
  //  @option_shader: Some
  //
  (@option_shader $shaders:ident [$shader_id:ident]) => {
    Some ($shaders.get (ShaderId::$shader_id as usize).unwrap().as_str())
  };

  //
  //  @option_shader: None
  //
  (@option_shader $shaders:ident []) => { None };

} // end def_programs!

//
//  def_programs_include!
//
/// Like `def_programs!` except that the shader source will be compiled-in with
/// `include_str!`. The original source path is still available through the
/// `source_path` method on shader IDs, but it is not required to be present
/// when loading shaders.
///
/// Note that unlike `def_programs!` where paths are relative to the
/// *executable* invocation directory, here paths are relative to the *source
/// file* which is invoking the `def_programs_include!` macro.
#[macro_export]
macro_rules! def_programs_include {
  //
  //  main rule
  //
  ( SHADERS [
      $($shader:ident, $sourcepath:expr),*
    ]
    PROGRAMS [
      $(
      program $program:ident {
        vertex_shader:   $vert_shader:ident
        $(tessellation_control_shader: $tess_ctrl_shader:ident
        tessellation_evaluation_shader: $tess_eval_shader:ident)*
        $(geometry_shader: $geom_shader:ident)*
        fragment_shader: $frag_shader:ident
      }
      )*
    ]
  ) => {
    def_programs_include!{
      @def_shaders
      $($shader, $sourcepath),*
    }
    def_programs_include!{
      @def_programs
      SHADERS [ $($shader),* ]
      PROGRAMS [
        $(
        program $program {
          vertex_shader:   $vert_shader
          $(tessellation_control_shader: $tess_ctrl_shader
          tessellation_evaluation_shader: $tess_eval_shader)*
          $(geometry_shader: $geom_shader)*
          fragment_shader: $frag_shader
        }
        )*
      ]
    }
  };

  //
  //  @def_programs
  //
  ( @def_programs
    SHADERS [ $($shader:ident),* ]
    PROGRAMS [
      $(
      program $program:ident {
        vertex_shader:   $vert_shader:ident
        $(tessellation_control_shader: $tess_ctrl_shader:ident
        tessellation_evaluation_shader: $tess_eval_shader:ident)*
        $(geometry_shader: $geom_shader:ident)*
        fragment_shader: $frag_shader:ident
      }
      )*
    ]
  ) => {
    enum_unitary! {
      pub enum ProgramId (ProgramIdVariants) {
        $($program),*
      }
    }

    impl ProgramId {
      fn create_program (&self,
        glium_facade : &::glium::backend::Facade,
        shaders      : &::vec_map::VecMap <&'static str>
      ) -> ::glium::Program {
        debug!("create shader program \"{:?}\"", self);
        match *self {
          $(
          ProgramId::$program => {
            let vertex_shader
              = shaders.get (ShaderId::$vert_shader as usize).unwrap();
            let tessellation_control_shader    = def_programs_include!(
              @option_shader shaders [$($tess_ctrl_shader)*]);
            let tessellation_evaluation_shader = def_programs_include!(
              @option_shader shaders [$($tess_eval_shader)*]);
            let geometry_shader                = def_programs_include!(
              @option_shader shaders [$($geom_shader)*]);
            let fragment_shader
              = shaders.get (ShaderId::$frag_shader as usize).unwrap();
            ::glium::Program::new (
              glium_facade,
              ::glium::program::ProgramCreationInput::SourceCode {
                vertex_shader,
                tessellation_control_shader,
                tessellation_evaluation_shader,
                geometry_shader,
                fragment_shader,
                transform_feedback_varyings: None,
                outputs_srgb:                true,
                uses_point_size:             false
              }
            ).unwrap()
          }
          )*
        }
      }
    }

    pub fn build_programs (glium_facade : &::glium::backend::Facade)
      -> Result <::vec_map::VecMap <::glium::Program>, ::std::io::Error>
    {
      debug!("building shader programs...");

      let shaders = try!{ load_shaders() };
      let mut programs = ::vec_map::VecMap::new();
      for program_id in ProgramId::iter_variants() {
        let pid = program_id as usize;
        assert!(
          programs.insert (pid, program_id.create_program (glium_facade, &shaders)
        ).is_none());
      }

      debug!("...building shader programs");
      Ok (programs)
    }
  };

  //
  //  @def_shaders
  //
  (
    @def_shaders $($shader:ident, $sourcepath:expr),*
  ) => {
    enum_unitary! {
      pub enum ShaderId (IdVariants) {
        $($shader),*
      }
    }
    impl ShaderId {
      pub fn source_str (&self) -> &'static str {
        match *self {
          $(
          ShaderId::$shader => include_str!($sourcepath)
          ),*
        }
      }
      pub fn sourcepath (&self) -> &'static str {
        match *self {
          $(ShaderId::$shader => $sourcepath),*
        }
      }
    }
    pub fn load_shaders()
      -> Result <::vec_map::VecMap <&'static str>, ::std::io::Error>
    {
      let mut shaders = ::vec_map::VecMap::new();
      for shader_id in ShaderId::iter_variants() {
        let source_str = shader_id.source_str();
        let sid = shader_id as usize;
        assert!(shaders.insert (sid, source_str).is_none());
      }
      Ok (shaders)
    }
  };

  //
  //  @option_shader: Some
  //
  (@option_shader $shaders:ident [$shader_id:ident]) => {
    Some (*$shaders.get (ShaderId::$shader_id as usize).unwrap())
  };

  //
  //  @option_shader: None
  //
  (@option_shader $shaders:ident []) => { None };

} // end def_programs_include!