summaryrefslogtreecommitdiff
path: root/WebInterface/wasm/asm-paint/src/shader.rs
blob: 3352bcf4441856003035936b1852e9e7e04a3dfc (plain)
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
use web_sys::{WebGlProgram, WebGl2RenderingContext};

const VERTEX_SHADER: &str =
r#"#version 300 es
in vec4 pos;
void main() {
    gl_Position = pos;
}
"#;

const FRAGMENT_SHADER: &str =
r#"#version 300 es
precision mediump float;
out vec4 color;

void main() {
    color = vec4(1, 0, 0, 1);
}
"#;

pub struct Shaders {
    program: Option<WebGlProgram>,
    pos_loc: i32,
}

impl Shaders {
    pub fn new() -> Self {
        Self {
            program: None,
            pos_loc: -1
        }
    }

    fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> {
        self.program = Some(ctx.create_program().ok_or("could not create program id")?);
        Ok(())
    }

    fn create_shader(&mut self, ctx: &WebGl2RenderingContext,
                     shader_type: u32, source: &str) -> Result<(), String> {
        let program = self.program.as_ref().ok_or("could not find created program")?;
        let shader = ctx.create_shader(shader_type)
            .ok_or("could not create shader")?;
        ctx.shader_source(&shader, source);
        ctx.compile_shader(&shader);
        let status = ctx.get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS);
        if status == wasm_bindgen::JsValue::TRUE {
            ctx.attach_shader(program, &shader);
            Ok(())
        } else {
            Err(format!("\n{}", ctx.get_shader_info_log(&shader).unwrap_or_default()))
        }
    }

    fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> {
        self.create_shader(ctx, WebGl2RenderingContext::VERTEX_SHADER, VERTEX_SHADER)
    }

    fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> {
        self.create_shader(ctx, WebGl2RenderingContext::FRAGMENT_SHADER, FRAGMENT_SHADER)
    }

    fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> {
        let program = self.program.as_ref().ok_or("could not find created program")?;
        ctx.link_program(program);
        let status = ctx.get_program_parameter(program, WebGl2RenderingContext::LINK_STATUS);
        if status == wasm_bindgen::JsValue::TRUE {
            Ok(())
        } else {
            Err(format!("\n{}", ctx.get_program_info_log(program).unwrap_or_default()))
        }
    }

    pub fn init(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> {
        debug!("create program");
        self.create_program(ctx)
            .map_err(|e| { error!("webgl2 create program: {}", e); e})?;
        debug!("create vertex shader");
        self.create_vertex_shader(ctx)
            .map_err(|e| { error!("webgl2 create vertex shader: {}", e); e})?;
        debug!("create fragment shader");
        self.create_fragment_shader(ctx)
            .map_err(|e| { error!("webgl2 create fragment shader: {}", e); e})?;
        debug!("compile shader program");
        self.compile(ctx)
            .map_err(|e| { error!("webgl2 shader: {}", e); e})?;
        let program = self.program.as_ref().ok_or("could not find created program")?;
        self.pos_loc = ctx.get_attrib_location(program, "pos");
        trace!("got attrib location 'pos'({})", self.pos_loc);
        info!("initialised shader program");
        Ok(())
    }

    pub fn remove(&mut self, ctx: &WebGl2RenderingContext) {
        ctx.delete_program(self.program.as_ref())
    }
}