maia_wasm/
render.rs

1//! Render engine.
2//!
3//! This module implements a WebGL2 render engine that is used to render the
4//! waterfall. The render engine is organized around a [`RenderEngine`] to which
5//! [`RenderObject`]'s are added.
6
7use std::cell::Cell;
8use std::rc::Rc;
9use web_sys::{WebGl2RenderingContext, WebGlProgram, WebGlVertexArrayObject};
10
11pub use engine::{
12    CanvasDims, RenderEngine, TextsDimensions, Texture, TextureBuilder, TextureInternalFormat,
13    TextureMagFilter, TextureMinFilter, TextureParameter, TextureWrap, VaoBuilder,
14};
15pub use uniform::{Uniform, UniformType, UniformValue};
16
17mod engine;
18mod uniform;
19
20/// Render object.
21///
22/// Render objects describe a scene, and are added to the render engine using
23/// [`RenderEngine::add_object`]. Render objects are drawn using the
24/// `drawElements()` WebGL2 function.
25pub struct RenderObject {
26    /// Controls whether the object is enabled.
27    ///
28    /// If `enabled` is `false`, the object is not rendered.
29    pub enabled: Rc<Cell<bool>>,
30    /// WebGL2 program used to render the object.
31    pub program: Rc<WebGlProgram>,
32    /// VAO storing all the vertex arrays for the object.
33    ///
34    /// This VAO contains the element array as well as any arrays needed for the
35    /// vertex shader.
36    pub vao: Rc<WebGlVertexArrayObject>,
37    /// Draw mode for the object.
38    pub draw_mode: DrawMode,
39    /// Number of elements to draw.
40    ///
41    /// This parameter is passed to `drawElements()`.
42    pub draw_num_indices: Rc<Cell<u32>>,
43    /// Offset in the element array buffer to use for drawing.
44    ///
45    /// Unlike in the `drawElements()` WebGL2 function, this field uses units of
46    /// elements rather than bytes. The value is converted to bytes and passed
47    /// to `drawElements()`.
48    pub draw_offset_elements: Rc<Cell<usize>>,
49    /// Uniforms used by the object.
50    pub uniforms: Box<[Rc<dyn UniformValue>]>,
51    /// Textures used by the object.
52    pub textures: Box<[Texture]>,
53}
54
55/// Draw mode.
56///
57/// This enum lists the draw modes supported by WebGL2.
58#[derive(Debug, Clone, Copy, Eq, PartialEq)]
59#[repr(u32)]
60pub enum DrawMode {
61    /// Draw as points.
62    Points = WebGl2RenderingContext::POINTS,
63    /// Draw as a line strip.
64    LineStrip = WebGl2RenderingContext::LINE_STRIP,
65    /// Draw as a line loop.
66    LineLoop = WebGl2RenderingContext::LINE_LOOP,
67    /// Draw as lines.
68    Lines = WebGl2RenderingContext::LINES,
69    /// Draw as a triangle strip.
70    TriangleStrip = WebGl2RenderingContext::TRIANGLE_STRIP,
71    /// Draw as a triangle fan.
72    TriangleFan = WebGl2RenderingContext::TRIANGLE_FAN,
73    /// Draw as triangles.
74    Triangles = WebGl2RenderingContext::TRIANGLES,
75}
76
77/// WebGL2 shader program source.
78///
79/// This contains the source for the vertex and fragment shaders of a WebGL2
80/// program that is to be compiled.
81#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
82pub struct ProgramSource<'a> {
83    /// Source for the vertex shader.
84    pub vertex_shader: &'a str,
85    /// Source for the fragment shader.
86    pub fragment_shader: &'a str,
87}
88
89pub mod texture_formats {
90    //! WebGL2 internal texture formats.
91    //!
92    //! This module defines the formats that can be used with the
93    //! [`TextureInternalFormat`](super::TextureInternalFormat) trait.
94
95    pub use super::engine::{LuminanceAlpha, R16f, Rgb, Rgba};
96}