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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use crate::ElementBuffer;
use core::convert::TryFrom;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::convert::TryInto;
use std::rc::Rc;
use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader, WebGlUniformLocation};

use super::data_buffer::{Item, ItemsBuffer};
use super::gl::Gl;
use super::gl::GlError;
use super::settings::Settings;
use super::texture::{Texture, TEXTURES_COUNT};
use super::types::DataType;
use crate::uniforms::{UniformValue, Uniforms};

#[repr(u32)]
#[derive(Clone, Copy, Debug, TryFromPrimitive, IntoPrimitive, PartialEq, Eq)]
pub enum PrimitiveType {
    Points = WebGlRenderingContext::POINTS,
    LineStrip = WebGlRenderingContext::LINE_STRIP,
    LineLoop = WebGlRenderingContext::LINE_LOOP,
    Lines = WebGlRenderingContext::LINES,
    TriangleStrip = WebGlRenderingContext::TRIANGLE_STRIP,
    TriangleFan = WebGlRenderingContext::TRIANGLE_FAN,
    Triangles = WebGlRenderingContext::TRIANGLES,
}

#[derive(Clone, Debug)]
struct AttributeInfo {
    name: String,
    location: u32,
    data_type: DataType,
}

#[derive(Clone, Debug)]
struct UniformInfo {
    name: String,
    location: WebGlUniformLocation,
    data_type: DataType,
}

#[derive(Clone, Debug)]
struct Shader {
    gl: Gl,
    handle: WebGlShader,
    source: String,
}

impl PartialEq for Shader {
    fn eq(&self, other: &Shader) -> bool {
        self.handle == other.handle
    }
}

impl Drop for Shader {
    fn drop(&mut self) {
        self.gl.context().delete_shader(Some(&self.handle));
    }
}

impl Shader {
    fn new(gl: Gl, source: &str, shader_type: u32) -> Result<Shader, GlError> {
        let ctx = gl.context();
        let handle = ctx
            .create_shader(shader_type)
            .ok_or_else(|| GlError::UnknownError(None))?;

        ctx.shader_source(&handle, source);
        ctx.compile_shader(&handle);

        let status = ctx
            .get_shader_parameter(&handle, WebGlRenderingContext::COMPILE_STATUS)
            .as_bool()
            .ok_or_else(|| GlError::UnknownError(None))?;

        if !status {
            return Err(GlError::ShaderCompilationError {
                source: source.into(),
                info: ctx.get_shader_info_log(&handle),
            });
        }

        return Ok(Shader {
            gl,
            handle,
            source: source.into(),
        });
    }
}

#[derive(Debug, Clone)]
struct ProgramData {
    gl: Gl,
    handle: WebGlProgram,
    vertex_shader: Shader,
    fragment_shader: Shader,
    attributes: Vec<AttributeInfo>,
    uniforms: Vec<UniformInfo>,
}

impl Drop for ProgramData {
    fn drop(&mut self) {
        let wgl = self.gl.context();
        wgl.delete_program(Some(&self.handle));
    }
}

#[derive(Debug, Clone)]
pub struct Program {
    data: Rc<ProgramData>,
}

impl PartialEq for Program {
    fn eq(&self, other: &Program) -> bool {
        self.data.handle == other.data.handle
    }
}

impl Eq for Program {}

impl Program {
    fn collect_attributes(
        ctx: &WebGlRenderingContext,
        program: &WebGlProgram,
    ) -> Result<Vec<AttributeInfo>, GlError> {
        let attributes_count = ctx
            .get_program_parameter(&program, WebGlRenderingContext::ACTIVE_ATTRIBUTES)
            .as_f64()
            .ok_or_else(|| GlError::UnknownError(Some("Failed to get attributes count".into())))?
            as u32;

        let mut result = Vec::with_capacity(attributes_count as usize);

        for i in 0..attributes_count {
            let info = ctx.get_active_attrib(&program, i).ok_or_else(|| {
                GlError::UnknownError(Some("Failed to get attribute info".into()))
            })?;

            // Arrays are not supported
            if info.size() != 1 {
                return Err(GlError::UnsupportedType(Some(info.name())));
            }

            let location = ctx.get_attrib_location(&program, &info.name());
            result.push(AttributeInfo {
                name: info.name(),
                data_type: DataType::try_from(info.type_())
                    .map_err(|_| GlError::UnsupportedType(Some(info.name())))?,
                location: location.try_into().map_err(|_| {
                    GlError::UnknownError(Some("Negative attribute location".to_string()))
                })?,
            });
        }
        return Ok(result);
    }

    fn collect_uniforms(
        ctx: &WebGlRenderingContext,
        program: &WebGlProgram,
    ) -> Result<Vec<UniformInfo>, GlError> {
        let uniforms_count = ctx
            .get_program_parameter(&program, WebGlRenderingContext::ACTIVE_UNIFORMS)
            .as_f64()
            .ok_or_else(|| GlError::UnknownError(Some("Failed to get uniforms count".into())))?
            as u32;

        let mut result = Vec::with_capacity(uniforms_count as usize);

        for i in 0..uniforms_count {
            let info = ctx
                .get_active_uniform(&program, i)
                .ok_or_else(|| GlError::UnknownError(Some("Failed to get uniform info".into())))?;

            // Arrays are not supported
            if info.size() != 1 {
                return Err(GlError::UnsupportedType(Some(info.name())));
            }

            let location = ctx
                .get_uniform_location(&program, &info.name())
                .ok_or_else(|| {
                    GlError::UnknownError(Some("Failed to get uniform location".into()))
                })?;
            result.push(UniformInfo {
                name: info.name(),
                data_type: DataType::try_from(info.type_())
                    .map_err(|_| GlError::UnsupportedType(Some(info.name())))?,
                location,
            });
        }

        return Ok(result);
    }

    pub(crate) fn new(
        gl: Gl,
        fragment_shader_source: &str,
        vertex_shader_source: &str,
    ) -> Result<Self, GlError> {
        let ctx: &WebGlRenderingContext = gl.context();

        let vertex_shader = Shader::new(
            gl.clone(),
            vertex_shader_source,
            WebGlRenderingContext::VERTEX_SHADER,
        )?;
        let fragment_shader = Shader::new(
            gl.clone(),
            fragment_shader_source,
            WebGlRenderingContext::FRAGMENT_SHADER,
        )?;

        let program = ctx.create_program().unwrap();
        ctx.attach_shader(&program, &vertex_shader.handle);
        ctx.attach_shader(&program, &fragment_shader.handle);
        ctx.link_program(&program);

        let link_status = ctx
            .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS)
            .as_bool()
            .ok_or_else(|| GlError::UnknownError(Some("Failed to get linking status".into())))?;

        if !link_status {
            return Err(GlError::ProgramLinkingError {
                vertex: vertex_shader_source.into(),
                fragment: fragment_shader_source.into(),
                info: ctx.get_program_info_log(&program),
            });
        }

        return Ok(Program {
            data: Rc::new(ProgramData {
                gl: gl.clone(),
                handle: program.clone(),
                vertex_shader,
                fragment_shader,
                attributes: Program::collect_attributes(&ctx, &program)?,
                uniforms: Program::collect_uniforms(&ctx, &program)?,
            }),
        });
    }

    pub(crate) fn handle(&self) -> WebGlProgram {
        self.data.handle.clone()
    }

    pub(self) fn set_attributes<T: Item>(&self, buffer: &ItemsBuffer<T>) {
        let gl: &WebGlRenderingContext = self.data.gl.context();
        let mut offset: usize = 0;

        self.data.gl.apply(
            Gl::settings()
                .items_buffer((*buffer).clone())
                .program(self.clone()),
            || {
                for item in T::layout() {
                    (&self.data.attributes)
                        .iter()
                        .find(|i| i.name == item.name)
                        .map(|info| {
                            gl.vertex_attrib_pointer_with_i32(
                                info.location,
                                item.data_type.size_in_floats().unwrap().try_into().unwrap(),
                                WebGlRenderingContext::FLOAT,
                                false,
                                (T::stride() * 4).try_into().unwrap(),
                                (offset * 4).try_into().unwrap(),
                            );
                        });
                    offset += item.data_type.size_in_floats().unwrap();
                }
            },
        );
    }

    pub(self) fn enable_attributes<R, F: FnOnce() -> R>(&self, callback: F) -> R {
        let attributes: Vec<u32> = (&self.data.attributes).iter().map(|v| v.location).collect();
        self.data
            .gl
            .apply(Gl::settings().enabled_attributes(&attributes), callback)
    }

    pub(self) fn set_uniforms<R, F: FnOnce() -> R>(
        &self,
        uniforms: &impl Uniforms,
        callback: F,
    ) -> R {
        let items = uniforms.uniforms();
        let info = &self.data.uniforms;
        let gl = &self.data.gl;
        let context: &WebGlRenderingContext = gl.context();

        let mut textures: Vec<Texture> = Vec::with_capacity(TEXTURES_COUNT.try_into().unwrap());

        gl.apply(Gl::settings().program(self.clone()), || {
            for i in items.iter() {
                info.iter().find(|info| info.name == i.name).map(|info| {
                    let location = Some(&info.location);
                    match &i.value {
                        UniformValue::None => match info.data_type {
                            DataType::Boolean => context.uniform1i(location, 0),
                            DataType::Float => context.uniform1f(location, 0.0),
                            DataType::Vec2 => {
                                context.uniform2f(location, 0.0, 0.0);
                            }
                            DataType::Vec3 => {
                                context.uniform3f(location, 0.0, 0.0, 0.0);
                            }
                            DataType::Vec4 => {
                                context.uniform4f(location, 0.0, 0.0, 0.0, 0.0);
                            }
                            DataType::Mat2 => {
                                let mat = [0.0; 4];
                                context.uniform_matrix2fv_with_f32_array(location, false, &mat)
                            }
                            DataType::Mat3 => {
                                let mat = [0.0; 9];
                                context.uniform_matrix3fv_with_f32_array(location, false, &mat)
                            }
                            DataType::Mat4 => {
                                let mat = [0.0; 16];
                                context.uniform_matrix4fv_with_f32_array(location, false, &mat)
                            }
                            DataType::Sampler => {
                                context.uniform1i(location, -1);
                            }
                        },
                        UniformValue::Boolean(value) => {
                            context.uniform1i(location, if *value { 1 } else { 0 })
                        }
                        UniformValue::Float(value) => context.uniform1f(location, *value),
                        UniformValue::Vec2(value) => {
                            context.uniform2fv_with_f32_array(location, value)
                        }
                        UniformValue::Vec3(value) => {
                            context.uniform3fv_with_f32_array(location, value)
                        }
                        UniformValue::Vec4(value) => {
                            context.uniform4fv_with_f32_array(location, value)
                        }
                        UniformValue::Mat2(value) => {
                            context.uniform_matrix2fv_with_f32_array(location, false, value)
                        }
                        UniformValue::Mat3(value) => {
                            context.uniform_matrix3fv_with_f32_array(location, false, value)
                        }
                        UniformValue::Mat4(value) => {
                            context.uniform_matrix4fv_with_f32_array(location, false, value)
                        }
                        UniformValue::Texture(value) => {
                            context.uniform1i(location, textures.len().try_into().unwrap());
                            textures.push(value.clone())
                        }
                    }
                });
            }
        });

        gl.apply(Gl::settings().texture_list(textures), callback)
    }

    pub fn draw_arrays<T: Item, U: Uniforms>(
        &self,
        primitive_type: PrimitiveType,
        uniforms: &U,
        attributes: &ItemsBuffer<T>,
    ) {
        let gl = &self.data.gl;
        gl.apply(Gl::settings().program(self.clone()), || {
            self.enable_attributes(|| {
                self.set_uniforms(uniforms, || {
                    self.set_attributes(attributes);
                    gl.context().draw_arrays(
                        primitive_type.into(),
                        0,
                        attributes.len().try_into().unwrap(),
                    )
                });
            });
        });
    }

    pub fn draw_instances<T: Item, I: Item, U: Uniforms>(
        &self,
        primitive_type: PrimitiveType,
        uniforms: &U,
        attributes: &ItemsBuffer<T>,
        instances: &ItemsBuffer<I>,
    ) {
        let gl = &self.data.gl;
        gl.apply(Gl::settings().program(self.clone()), || {
            self.enable_attributes(|| {
                self.set_uniforms(uniforms, || {
                    self.set_attributes(attributes);
                    self.set_attributes(instances);
                    gl.instanced_arrays().draw_arrays_instanced_angle(
                        primitive_type.into(),
                        0,
                        attributes.len().try_into().unwrap(),
                        instances.len().try_into().unwrap(),
                    );
                });
            });
        });
    }

    pub fn draw_element_arrays<T: Item, U: Uniforms>(
        &self,
        primitive_type: PrimitiveType,
        uniforms: &U,
        attributes: &ItemsBuffer<T>,
        elements: &ElementBuffer,
    ) {
        let gl = &self.data.gl;
        gl.apply(
            Gl::settings()
                .program(self.clone())
                .element_buffer(elements.clone()),
            || {
                self.enable_attributes(|| {
                    self.set_uniforms(uniforms, || {
                        self.set_attributes(attributes);
                        gl.context().draw_elements_with_i32(
                            primitive_type.into(),
                            elements.len() as i32,
                            WebGlRenderingContext::UNSIGNED_INT,
                            0,
                        )
                    });
                });
            },
        );
    }

    pub fn draw_element_instances<T: Item, I: Item, U: Uniforms>(
        &self,
        primitive_type: PrimitiveType,
        uniforms: &U,
        attributes: &ItemsBuffer<T>,
        elements: &ElementBuffer,
        instances: &ItemsBuffer<I>,
    ) {
        let gl = &self.data.gl;
        gl.apply(
            Gl::settings()
                .program(self.clone())
                .element_buffer(elements.clone()),
            || {
                self.enable_attributes(|| {
                    self.set_uniforms(uniforms, || {
                        self.set_attributes(attributes);
                        self.set_attributes(instances);
                        gl.instanced_arrays()
                            .draw_elements_instanced_angle_with_i32(
                                primitive_type.into(),
                                elements.len() as i32,
                                WebGlRenderingContext::UNSIGNED_INT,
                                0,
                                instances.len() as i32,
                            );
                    });
                });
            },
        );
    }

    pub fn vertex_source(&self) -> &String {
        &self.data.vertex_shader.source
    }

    pub fn fragment_source(&self) -> &String {
        &self.data.fragment_shader.source
    }
}