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.
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).
Sourcepub fn is_available() -> bool
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-trueif awebgl2context could be acquired.
Sourcepub fn init(config: &RenderConfig) -> Result<WebGlRenderer, WebGlInitError>
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.
Sourcepub fn create_program(
&self,
vertex_source: &str,
fragment_source: &str,
) -> Result<WebGlProgram, WebGlProgramError>
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.
Sourcepub fn set_uniform_2f(&self, program: &WebGlProgram, name: &str, x: f32, y: f32)
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.
Sourcepub fn set_uniform_4fv(&self, program: &WebGlProgram, name: &str, data: &[f32])
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.
Sourcepub fn render_frame(
&self,
program: &WebGlProgram,
clear_color: (f64, f64, f64, f64),
vertex_count: i32,
)
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.
Sourcepub fn resize(&mut self, physical_width: u32, physical_height: u32)
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
impl WebGlRenderer
pub fn get_context(&self) -> &WebGl2RenderingContext
pub fn get_mut_context(&mut self) -> &mut WebGl2RenderingContext
pub fn set_context(&mut self, val: WebGl2RenderingContext) -> &mut Self
pub fn get_canvas(&self) -> &HtmlCanvasElement
pub fn get_mut_canvas(&mut self) -> &mut HtmlCanvasElement
pub fn set_canvas(&mut self, val: HtmlCanvasElement) -> &mut Self
pub fn get_width(&self) -> u32
pub fn get_mut_width(&mut self) -> &mut u32
pub fn set_width(&mut self, val: u32) -> &mut Self
pub fn get_height(&self) -> u32
pub fn get_mut_height(&mut self) -> &mut u32
pub fn set_height(&mut self, val: u32) -> &mut Self
Trait Implementations§
Source§impl Clone for WebGlRenderer
impl Clone for WebGlRenderer
Source§fn clone(&self) -> WebGlRenderer
fn clone(&self) -> WebGlRenderer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more