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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use mesh::Mesh;
use util::get_ctx;
use std::{collections::HashMap, rc::Rc, ops::Deref};
use web_sys::*;

pub mod mesh;
pub mod texture;
pub mod util;
pub mod attributes;

use crate::texture::*;

#[derive(Clone)]
pub struct Ctx(Rc<WebGlRenderingContext>);

impl Ctx {
    pub fn from(canvas_name: &str) -> Result<Self, String> {
        let ctx = get_ctx(canvas_name, "webgl").map_err(|e| format!("{:?}", e))?;
        Self::new(ctx)
    }
    pub fn new(ctx: WebGlRenderingContext) -> Result<Self, String> {
        ctx.get_extension("WEBGL_depth_texture").map_err(|e| format!("no depth textures available {:?}", e))?;
        ctx.get_extension("OES_texture_float").map_err(|e| format!("no float textures available {:?}", e))?;
        ctx.enable(GL::DEPTH_TEST);
        ctx.enable(GL::CULL_FACE);

        Ok(Self(Rc::new(ctx)))
    }
}

impl Deref for Ctx {
    type Target = WebGlRenderingContext;

    fn deref(&self) -> &WebGlRenderingContext {
        &self.0
    }
}

pub type GL = WebGlRenderingContext;

pub struct Program {
    ctx: Ctx,
    program: WebGlProgram,
}

impl Program {
    pub fn new(ctx: &Ctx, vertex: &str, fragment: &str) -> Result<Program, String> {
        Program::new_with_mode(ctx, vertex, fragment)
    }

    pub fn new_with_mode(ctx: &Ctx, vertex: &str, fragment: &str) -> Result<Program, String> {
        let vertex_id = Program::shader(ctx, GL::VERTEX_SHADER, vertex)?;
        let fragment_id = Program::shader(ctx, GL::FRAGMENT_SHADER, fragment)?;

        let program = ctx.create_program().ok_or("Failed to create program")?;
        ctx.attach_shader(&program, &vertex_id);
        ctx.attach_shader(&program, &fragment_id);
        ctx.link_program(&program);

        Ok(Program {
            ctx: ctx.clone(),
            program,
        })
    }

    fn shader(ctx: &Ctx, shader_type: u32, source: &str) -> Result<WebGlShader, String> {
        let shader = ctx
            .create_shader(shader_type)
            .ok_or(format!("Failed to create shader {}", shader_type))?;
        ctx.shader_source(&shader, source);
        ctx.compile_shader(&shader);

        if ctx
            .get_shader_parameter(&shader, GL::COMPILE_STATUS)
            .as_bool()
            .unwrap_or(false)
        {
            Ok(shader)
        } else {
            Err(format!("Failed to compile shader {:?}", ctx.get_shader_info_log(&shader)))
        }
    }
}

impl Drop for Program {
    fn drop(&mut self) {
        self.ctx.delete_program(Some(&self.program));
    }
}

pub enum UniformData<'a> {
    Scalar(f32),
    Vector2([f32; 2]),
    Vector3([f32; 3]),
    Vector4([f32; 4]),
    Matrix4([f32; 16]),
    Texture(&'a mut UploadedTexture),
}

pub struct Pipeline {
    ctx: Ctx,
    clear_color: Option<[f32; 4]>,
    clear_depth: Option<f32>,
    clear_stencil: Option<i32>,
}

impl Pipeline {
    pub fn new(ctx: &Ctx) -> Self {
        let s = Self {
            ctx: ctx.clone(),
            clear_color: Some([0., 0., 0., 1.]),
            clear_depth: Some(1.),
            clear_stencil: Some(0),
        };

        if let Some(col) = s.clear_color {
            ctx.clear_color(col[0], col[1], col[2], col[3]);
        }
        if let Some(d) = s.clear_depth {
            ctx.clear_depth(d);
        }
        if let Some(s) = s.clear_stencil {
            ctx.clear_stencil(s);
        };

        s
    }

    pub fn shade<'a, T: Framebuffer>(
        &mut self,
        program: &Program,
        uni_values: HashMap<&'static str, UniformData>,
        objects: Vec<&'a mut Mesh>,
        output: &'a mut T,
    ) -> Result<&Self, String> {
        output.bind();

        if self.clear_color.is_some() {
            self.ctx.clear(GL::COLOR_BUFFER_BIT);
        }
        if self.clear_depth.is_some() {
            self.ctx.clear(GL::DEPTH_BUFFER_BIT);
        }
        if self.clear_stencil.is_some() {
            self.ctx.clear(GL::STENCIL_BUFFER_BIT);
        }

        self.ctx.use_program(Some(&program.program));
        self.set_uniforms(&program, uni_values)?;

        for obj in objects.into_iter() {
            obj.draw(program)?;
        }

        Ok(self)
    }

    fn set_uniforms(
        &self,
        program: &Program,
        uniform_values: HashMap<&'static str, UniformData>,
    ) -> Result<&Self, String> {
        let mut tex_inc = 0;
        for (name, uni_val) in uniform_values.into_iter() {
            if let Some(loc) = self.ctx.get_uniform_location(&program.program, name) {
                match uni_val {
                    UniformData::Scalar(v) => self.ctx.uniform1f(Some(&loc), v.clone()),
                    UniformData::Vector2(v) => self.ctx.uniform2fv_with_f32_array(Some(&loc), &v),
                    UniformData::Vector3(v) => self.ctx.uniform3fv_with_f32_array(Some(&loc), &v),
                    UniformData::Vector4(v) => self.ctx.uniform4fv_with_f32_array(Some(&loc), &v),
                    UniformData::Matrix4(m) => self.ctx.uniform_matrix4fv_with_f32_array(Some(&loc), false, &m),
                    UniformData::Texture(tex) => {
                        self.ctx.active_texture(GL::TEXTURE0 + tex_inc);
                        tex.bind();

                        // todo: double check on safely disposing uniforms data
                        self.ctx.uniform1i(Some(&loc), tex_inc as i32);
                        tex_inc += 1;
                    }
                }
            }
        }
        Ok(self)
    }
}