1use std::cell::RefCell;
12use std::rc::Rc;
13
14#[macro_use]
15mod slice;
16
17mod buffer;
18mod framebuffer;
19mod pipeline;
20mod pixel;
21mod shader;
22mod state;
23mod tess;
24mod texture;
25
26use glow::Context as GlowContext;
27
28use state::GlowState;
29pub use state::StateQueryError;
30
31#[derive(Debug, Clone, Copy)]
36pub enum ShaderVersion {
37 Gles3,
38 Gles1,
39}
40
41pub struct Context {
43 glow_context: GlowContext,
44 is_webgl1: bool,
45 shader_version: ShaderVersion,
46}
47
48impl Context {
49 #[cfg(not(wasm))]
51 pub unsafe fn from_loader_function<F>(loader_function: F, shader_version: ShaderVersion) -> Self
52 where
53 F: FnMut(&str) -> *const std::os::raw::c_void,
54 {
55 Self {
56 glow_context: GlowContext::from_loader_function(loader_function),
57 is_webgl1: false,
58 shader_version,
59 }
60 }
61
62 #[cfg(wasm)]
68 pub fn from_webgl1_context(context: web_sys::WebGlRenderingContext) -> Self {
69 Self {
70 glow_context: GlowContext::from_webgl1_context(context),
71 is_webgl1: true,
72 shader_version: ShaderVersion::Gles1,
73 }
74 }
75
76 #[cfg(wasm)]
78 pub fn from_webgl2_context(
79 context: web_sys::WebGl2RenderingContext,
80 shader_version: ShaderVersion,
81 ) -> Self {
82 Self {
83 glow_context: GlowContext::from_webgl2_context(context),
84 is_webgl1: false,
85 shader_version,
86 }
87 }
88}
89
90#[derive(Debug)]
92pub struct Glow {
93 pub(crate) state: Rc<RefCell<GlowState>>,
94}
95
96impl Glow {
97 pub fn from_context(ctx: Context) -> Result<Self, StateQueryError> {
99 let Context {
100 glow_context,
101 is_webgl1,
102 shader_version,
103 } = ctx;
104 GlowState::new(glow_context, is_webgl1, shader_version).map(|state| Glow {
105 state: Rc::new(RefCell::new(state)),
106 })
107 }
108}