Skip to main content

WebGlRenderer

Struct WebGlRenderer 

Source
pub struct WebGlRenderer { /* private fields */ }
Expand description

A WebGL 2 rendering backend wrapping the WebGl2RenderingContext.

Unlike WebGpuRenderer, which stores all GPU handles as opaque JsValues, WebGL exposes concrete web_sys types, so the context and canvas are kept as strongly typed values. Shader programs created via WebGlRenderer::create_program are managed by the caller.

Construct via WebGlRenderer::init, which resolves the canvas from the RenderConfig, applies device-pixel-ratio scaling to the backing store, and acquires the webgl2 context.

Implementations§

Source§

impl WebGlRenderer

Implements WebGlRenderer context acquisition, shader program management, and per-frame drawing.

All methods are synchronous: WebGL has no Promise-based initialization. The renderer never logs; initialization failures are returned as WebGlInitError and shader failures as WebGlProgramError so the caller can surface them (typically via Console::error on the example side).

Source

pub fn is_available() -> bool

Probes whether the browser can create a WebGL 2 context.

Creates a throwaway off-DOM canvas and requests a webgl2 context. The probe is cheap (no shaders are compiled) and has no side effects on the page.

§Returns
  • bool - true if a webgl2 context could be acquired.
Source

pub fn init(config: &RenderConfig) -> Result<WebGlRenderer, WebGlInitError>

Initializes a WebGL 2 renderer from a render configuration.

Resolves the canvas element from config.canvas_selector, scales the backing store by the device pixel ratio, acquires the webgl2 context, and sets the initial viewport.

§Arguments
  • &RenderConfig - The rendering configuration.
§Returns
  • Result<WebGlRenderer, WebGlInitError> - The initialized renderer, or a typed error describing the specific failure.
Source

pub fn create_program( &self, vertex_source: &str, fragment_source: &str, ) -> Result<WebGlProgram, WebGlProgramError>

Compiles and links a shader program from GLSL ES 3.00 sources.

Both shaders are compiled, attached, and linked; on success the intermediate shader objects are deleted (the program keeps the compiled code). On failure the browser info log is returned so the caller can surface the exact GLSL diagnostic.

§Arguments
  • &str - The vertex shader source (#version 300 es).
  • &str - The fragment shader source (#version 300 es).
§Returns
  • Result<WebGlProgram, WebGlProgramError> - The linked program, or the compile/link info log.
Source

pub fn set_uniform_2f(&self, program: &WebGlProgram, name: &str, x: f32, y: f32)

Sets a vec2 uniform on the given program.

The uniform location is resolved per call; for the per-frame interaction uniforms used by the examples this lookup cost is negligible. A missing uniform (optimized out by the GLSL compiler) is silently ignored, matching raw WebGL semantics.

§Arguments
  • &WebGlProgram - The program owning the uniform.
  • &str - The uniform name.
  • f32 - The x component.
  • f32 - The y component.
Source

pub fn set_uniform_4fv(&self, program: &WebGlProgram, name: &str, data: &[f32])

Uploads a flat float slice into a vec4 or vec4[] uniform.

Used by the game demos to push per-frame instance data (ball positions and colors, cube transforms) into shaders that index the array with gl_VertexID. data.len() must be a multiple of 4. For array uniforms pass the name with an explicit [0] index, per the WebGL getUniformLocation spec. The upload writes only data.len() / 4 elements; untouched elements keep their previous values.

§Arguments
  • &WebGlProgram - The program owning the uniform.
  • &str - The uniform name (e.g. "u_balls[0]").
  • &[f32] - The packed float data.
Source

pub fn render_frame( &self, program: &WebGlProgram, clear_color: (f64, f64, f64, f64), vertex_count: i32, )

Renders a complete frame: clears the canvas and draws a triangle-list primitive whose vertices are generated inside the vertex shader.

Mirrors WebGpuRenderer::render_frame: the vertex shader uses gl_VertexID so no vertex buffers are involved. The given program is bound before drawing; set its uniforms first via WebGlRenderer::set_uniform_2f when the shader reads per-frame interaction data.

§Arguments
  • &WebGlProgram - The program to draw with.
  • (f64, f64, f64, f64) - The clear color as (r, g, b, a) in 0.0–1.0 range.
  • i32 - The number of vertices to draw.
Source

pub fn resize(&mut self, physical_width: u32, physical_height: u32)

Resizes the canvas backing store and updates the GL viewport.

Call this when the CSS layout size changes (window resize, DPR change) so the drawing buffer matches the visible region.

§Arguments
  • u32 - The new physical pixel width (already multiplied by DPR).
  • u32 - The new physical pixel height.
Source§

impl WebGlRenderer

Source

pub fn get_context(&self) -> &WebGl2RenderingContext

Source

pub fn get_mut_context(&mut self) -> &mut WebGl2RenderingContext

Source

pub fn set_context(&mut self, val: WebGl2RenderingContext) -> &mut Self

Source

pub fn get_canvas(&self) -> &HtmlCanvasElement

Source

pub fn get_mut_canvas(&mut self) -> &mut HtmlCanvasElement

Source

pub fn set_canvas(&mut self, val: HtmlCanvasElement) -> &mut Self

Source

pub fn get_width(&self) -> u32

Source

pub fn get_mut_width(&mut self) -> &mut u32

Source

pub fn set_width(&mut self, val: u32) -> &mut Self

Source

pub fn get_height(&self) -> u32

Source

pub fn get_mut_height(&mut self) -> &mut u32

Source

pub fn set_height(&mut self, val: u32) -> &mut Self

Trait Implementations§

Source§

impl Clone for WebGlRenderer

Source§

fn clone(&self) -> WebGlRenderer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more