summaryrefslogtreecommitdiff
path: root/WebInterface/wasm/asm-paint/src/shader.rs
blob: ac7c7670e39c730b506c48a357d6d9fca867e1cf (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
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>,
}

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

    pub 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()))
        }
    }

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

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

    pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> {
        Ok(())
    }
}