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
use super::funcs::FuncSettings;
use super::misc::MiscSettings;
use super::toggles::ToggleFlags;
use super::{
get_webgl_context_1, get_webgl_context_2, BufferTarget, GlQuery, Id, ProgramInfo, TextureInfo,
TextureTarget, WebGlCommon, WebGlContextOptions,
};
use crate::data::clone_to_vec_f32;
use crate::errors::Error;
use beach_map::{BeachMap, DefaultVersion};
use rustc_hash::FxHashMap;
use std::cell::Cell;
use wasm_bindgen::prelude::JsValue;
use web_sys::{HtmlCanvasElement, WebGlBuffer, WebGlTexture, WebGlVertexArrayObject};
use web_sys::{WebGl2RenderingContext, WebGlRenderingContext};
pub type WebGl1Renderer = WebGlRenderer<WebGlRenderingContext>;
pub type WebGl2Renderer = WebGlRenderer<WebGl2RenderingContext>;
pub struct WebGlRenderer<T: WebGlCommon> {
pub gl: T,
pub canvas: HtmlCanvasElement,
pub(super) last_width: u32,
pub(super) last_height: u32,
pub(super) current_program_id: Option<Id>,
pub(super) program_lookup: BeachMap<DefaultVersion, ProgramInfo>,
pub(super) current_buffer_id: Cell<Option<Id>>,
pub(super) current_buffer_target: Cell<Option<BufferTarget>>,
pub(super) current_buffer_index: Cell<Option<u32>>,
pub(super) buffer_lookup: BeachMap<DefaultVersion, WebGlBuffer>,
pub(super) current_texture_id: Option<Id>,
pub(super) texture_lookup: BeachMap<DefaultVersion, TextureInfo>,
pub(super) texture_sampler_lookup: Vec<Option<Id>>,
pub(super) extension_lookup: FxHashMap<String, js_sys::Object>,
pub(super) ubo_global_loc_lookup: Vec<String>,
pub(super) current_vao_id: Cell<Option<Id>>,
pub(super) vao_lookup: BeachMap<DefaultVersion, WebGlVertexArrayObject>,
pub(super) toggle_flags: ToggleFlags,
pub(super) func_settings: FuncSettings,
pub(super) misc_settings: MiscSettings,
}
impl<T: WebGlCommon> WebGlRenderer<T> {
pub fn new(gl: T) -> Result<Self, Error> {
let canvas = gl.awsm_get_canvas()?;
let max_texture_units: usize =
gl.awsm_get_parameter_usize(GlQuery::MaxTextureImageUnits)?;
let mut texture_sampler_lookup = Vec::with_capacity(max_texture_units);
for _ in 0..max_texture_units {
texture_sampler_lookup.push(None);
}
let blend_color: Vec<f32> = gl.awsm_get_parameter_vf32(GlQuery::BlendColor)?;
Ok(Self {
gl,
canvas,
last_width: 0,
last_height: 0,
current_program_id: None,
program_lookup: BeachMap::default(),
current_buffer_id: Cell::new(None),
current_buffer_target: Cell::new(None),
current_buffer_index: Cell::new(None),
buffer_lookup: BeachMap::default(),
current_texture_id: None,
texture_lookup: BeachMap::default(),
texture_sampler_lookup,
extension_lookup: FxHashMap::default(),
current_vao_id: Cell::new(None),
vao_lookup: BeachMap::default(),
ubo_global_loc_lookup: Vec::new(),
toggle_flags: ToggleFlags::default(),
func_settings: FuncSettings {
blend_color: (
blend_color[0],
blend_color[1],
blend_color[2],
blend_color[3],
),
..FuncSettings::default()
},
misc_settings: MiscSettings::default(),
})
}
}