summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/shader.rs
blob: 9ccb9fc8b07125b64234f1cf57ba7851702937a7 (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
use crate::error::WasmError;
use super::webgl;
use super::webgl::{WebGl2, ShaderType};

pub const MAIN_VERTEX_SHADER: &str = include_str!("main.vs");
pub const MAIN_FRAGMENT_SHADER: &str = include_str!("main.fs");

pub struct ShaderProgram {
    program: webgl::WebGlProgram,
}

impl ShaderProgram {
    pub fn from_sources(gl: &WebGl2, sources: &[(ShaderType, String)]) -> Result<Self, WasmError> {
        let program = gl.create_program()
            .map_err(|_| WasmError::Shader(format!("glCreateProgram failed ({})", gl.get_error())))?;
        for (shader_type, source) in sources {
            let shader = gl.create_shader(shader_type)
                .map_err(|_| WasmError::Shader(format!("glCreateShader failed ({})", gl.get_error())))?;
            gl.shader_source(&shader, source);
            gl.compile_shader(&shader)
                .map_err(|e| WasmError::Shader(format!("compile error in {} shader: {}", shader_type, e)))?;
            gl.attach_shader(&program, &shader)
        }
        gl.link_program(&program)
            .map_err(|e| WasmError::Shader(format!("linker error in program: {}", e)))?;
        Ok(Self {
            program
        })
    }

    pub fn run(&self, gl: &WebGl2) {
        gl.use_program(&self.program)
    }
}