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