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
extern crate gfx;
extern crate notify;

pub mod static_shaders;
pub mod dynamic_shaders;

#[cfg(not(feature = "dynamic_shaders"))]
pub use static_shaders::*;
#[cfg(feature = "dynamic_shaders")]
pub use dynamic_shaders::*;

pub struct ShaderSource {
    pub source: Option<Vec<u8>>,
    pub filenames: Option<Vec<String>>,
}

#[macro_export]
#[cfg(not(feature = "dynamic_shaders"))]
macro_rules! shader_source {
    ($directory:expr, $( $filename:expr ),+ ) => {
        $crate::ShaderSource{
            source: Some({
                let mut tmp_vec = Vec::new();
                $( tmp_vec.extend_from_slice(
                    include_bytes!(concat!($directory, "/", $filename)));
                )*
                tmp_vec
            }),
            filenames: None,
        }
    };
}

#[macro_export]
#[cfg(feature = "dynamic_shaders")]
macro_rules! shader_source {
    ($directory:expr, $( $filename:expr ),+ ) => {
        $crate::ShaderSource {
            source: None,
            filenames: Some({
                let mut tmp_vec = Vec::new();
                $( tmp_vec.push($filename.to_string()); )*
                tmp_vec
            }),
        }
    };
}

fn print_shader_error(error: &gfx::shade::core::CreateShaderError) {
    use gfx::shade::core::CreateShaderError::*;
    match *error {
        ModelNotSupported => eprintln!("Attempted to use unsupported shader model"),
        StageNotSupported(ref stage) => eprintln!("Shader stage '{:?}' not supported", stage),
        CompilationFailed(ref msg) => eprintln!("Shader complilation failed: \n{}", msg),
    }
}

fn create_vertex_shader<R: gfx::Resources, F: gfx::Factory<R>>(
    factory: &mut F,
    source: &[u8],
) -> Result<gfx::VertexShader<R>, gfx::shade::core::CreateShaderError> {
    match factory.create_shader_vertex(source) {
        Ok(shader) => Ok(shader),
        Err(error) => {
            print_shader_error(&error);
            Err(error)
        }
    }
}
fn create_pixel_shader<R: gfx::Resources, F: gfx::Factory<R>>(
    factory: &mut F,
    source: &[u8],
) -> Result<gfx::PixelShader<R>, gfx::shade::core::CreateShaderError> {
    match factory.create_shader_pixel(source) {
        Ok(shader) => Ok(shader),
        Err(error) => {
            print_shader_error(&error);
            Err(error)
        }
    }
}