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
use common::assets::{self, AssetExt, AssetHandle};
use hashbrown::HashMap;

/// Load from a GLSL file.
pub struct Glsl(pub String);

impl From<String> for Glsl {
    fn from(s: String) -> Glsl { Glsl(s) }
}

impl assets::Asset for Glsl {
    type Loader = assets::LoadFrom<String, assets::StringLoader>;

    const EXTENSION: &'static str = "glsl";
}

// Note: we use this clone to send the shaders to a background thread
// TODO: use Arc-ed asset and clone that instead
#[derive(Clone)]
pub struct Shaders {
    shaders: HashMap<String, AssetHandle<Glsl>>,
}

impl assets::Compound for Shaders {
    // TODO: Taking the specifier argument as a base for shaders specifiers
    // would allow to use several shaders groups easily
    fn load(_: assets::AnyCache, _: &assets::SharedString) -> Result<Shaders, assets::BoxedError> {
        let shaders = [
            "include.constants",
            "include.globals",
            "include.sky",
            "include.light",
            "include.srgb",
            "include.random",
            "include.lod",
            "include.shadows",
            "include.rain_occlusion",
            "include.point_glow",
            "include.fxaa",
            "antialias.none",
            "antialias.bilinear",
            "antialias.fxaa",
            "antialias.msaa-x4",
            "antialias.msaa-x8",
            "antialias.msaa-x16",
            "antialias.hqx",
            "antialias.fxupscale",
            "include.cloud.none",
            "include.cloud.regular",
            "figure-vert",
            "light-shadows-figure-vert",
            "light-shadows-directed-vert",
            "light-shadows-debug-vert",
            "rain-occlusion-figure-vert",
            "rain-occlusion-directed-vert",
            "point-light-shadows-vert",
            "skybox-vert",
            "skybox-frag",
            "debug-vert",
            "debug-frag",
            "figure-frag",
            "rope-vert",
            "rope-frag",
            "terrain-vert",
            "terrain-frag",
            "fluid-vert",
            "fluid-frag.cheap",
            "fluid-frag.shiny",
            "sprite-vert",
            "sprite-frag",
            "lod-object-vert",
            "lod-object-frag",
            "particle-vert",
            "particle-frag",
            "trail-vert",
            "trail-frag",
            "ui-vert",
            "ui-frag",
            "premultiply-alpha-vert",
            "premultiply-alpha-frag",
            "lod-terrain-vert",
            "lod-terrain-frag",
            "clouds-vert",
            "clouds-frag",
            "dual-downsample-filtered-frag",
            "dual-downsample-frag",
            "dual-upsample-frag",
            "clouds-frag",
            "postprocess-vert",
            "postprocess-frag",
            "blit-vert",
            "blit-frag",
            //"player-shadow-frag",
            //"light-shadows-geom",
        ];

        let shaders = shaders
            .iter()
            .map(|shader| {
                let full_specifier = ["voxygen.shaders.", shader].concat();
                let asset = AssetExt::load(&full_specifier)?;
                Ok((String::from(*shader), asset))
            })
            .collect::<Result<HashMap<_, _>, assets::Error>>()?;

        Ok(Self { shaders })
    }
}

impl Shaders {
    pub fn get(&self, shader: &str) -> Option<impl core::ops::Deref<Target = Glsl>> {
        self.shaders.get(shader).map(|a| a.read())
    }
}