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
use std::fs;
use std::path::Path;
use std::rc::Rc;
use crate::error::Result;
use crate::graphics::opengl::GLProgram;
use crate::Context;
#[derive(Debug, Clone, PartialEq)]
pub struct Shader {
pub(crate) handle: Rc<GLProgram>,
}
impl Shader {
pub fn new<P>(ctx: &mut Context, vertex_path: P, fragment_path: P) -> Result<Shader>
where
P: AsRef<Path>,
{
Shader::from_string(
ctx,
&fs::read_to_string(vertex_path)?,
&fs::read_to_string(fragment_path)?,
)
}
pub fn from_string(
ctx: &mut Context,
vertex_shader: &str,
fragment_shader: &str,
) -> Result<Shader> {
ctx.gl
.compile_program(vertex_shader, fragment_shader)
.map(Shader::from_handle)
}
pub(crate) fn from_handle(handle: GLProgram) -> Shader {
Shader {
handle: Rc::new(handle),
}
}
}