veloren_voxygen/render/renderer/
shaders.rs

1use common::assets::{
2    self, Asset, AssetCache, AssetExt, AssetHandle, BoxedError, FileAsset, SharedString,
3};
4use hashbrown::HashMap;
5use std::borrow::Cow;
6
7/// Load from a GLSL file.
8pub struct Glsl(pub String);
9
10impl FileAsset for Glsl {
11    const EXTENSION: &'static str = "glsl";
12
13    fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, BoxedError> {
14        Ok(String::from_utf8(bytes.into()).map(Self)?)
15    }
16}
17
18// Note: we use this clone to send the shaders to a background thread
19// TODO: use Arc-ed asset and clone that instead
20#[derive(Clone)]
21pub struct Shaders {
22    shaders: HashMap<String, AssetHandle<Glsl>>,
23}
24
25impl Asset for Shaders {
26    // TODO: Taking the specifier argument as a base for shaders specifiers
27    // would allow to use several shaders groups easily
28    fn load(_: &AssetCache, _: &SharedString) -> Result<Self, BoxedError> {
29        let shaders = [
30            "include.constants",
31            "include.globals",
32            "include.sky",
33            "include.light",
34            "include.srgb",
35            "include.random",
36            "include.lod",
37            "include.shadows",
38            "include.rain_occlusion",
39            "include.point_glow",
40            "include.fxaa",
41            "antialias.none",
42            "antialias.bilinear",
43            "antialias.fxaa",
44            "antialias.msaa-x4",
45            "antialias.msaa-x8",
46            "antialias.msaa-x16",
47            "antialias.hqx",
48            "antialias.fxupscale",
49            "include.cloud.none",
50            "include.cloud.regular",
51            "figure-vert",
52            "light-shadows-figure-vert",
53            "light-shadows-directed-vert",
54            "light-shadows-debug-vert",
55            "rain-occlusion-figure-vert",
56            "rain-occlusion-directed-vert",
57            "point-light-shadows-vert",
58            "skybox-vert",
59            "skybox-frag",
60            "debug-vert",
61            "debug-frag",
62            "figure-frag",
63            "rope-vert",
64            "rope-frag",
65            "terrain-vert",
66            "terrain-frag",
67            "fluid-vert",
68            "fluid-frag.cheap",
69            "fluid-frag.shiny",
70            "sprite-vert",
71            "sprite-frag",
72            "lod-object-vert",
73            "lod-object-frag",
74            "particle-vert",
75            "particle-frag",
76            "trail-vert",
77            "trail-frag",
78            "ui-vert",
79            "ui-frag",
80            "premultiply-alpha-vert",
81            "premultiply-alpha-frag",
82            "lod-terrain-vert",
83            "lod-terrain-frag",
84            "clouds-vert",
85            "clouds-frag",
86            "dual-downsample-filtered-frag",
87            "dual-downsample-frag",
88            "dual-upsample-frag",
89            "clouds-frag",
90            "postprocess-vert",
91            "postprocess-frag",
92            "blit-vert",
93            "blit-frag",
94            //"player-shadow-frag",
95            //"light-shadows-geom",
96        ];
97
98        let shaders = shaders
99            .iter()
100            .map(|shader| {
101                let full_specifier = ["voxygen.shaders.", shader].concat();
102                let asset = AssetExt::load(&full_specifier)?;
103                Ok((String::from(*shader), asset))
104            })
105            .collect::<Result<HashMap<_, _>, assets::Error>>()?;
106
107        Ok(Self { shaders })
108    }
109}
110
111impl Shaders {
112    pub fn get(&self, shader: &str) -> Option<impl core::ops::Deref<Target = Glsl> + use<>> {
113        self.shaders.get(shader).map(|a| a.read())
114    }
115}