Skip to main content

GlProgram

Struct GlProgram 

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

A linked GLSL program together with its cached uniform locations.

Uniform locations are stable for the lifetime of a linked program, but resolving one costs a round trip into the GL frontend that walks the program’s uniform table. Caching them in a name-keyed map means a per-frame set_uniform_1f is a hash lookup plus the upload with no re-resolution, and a name the GLSL compiler optimized out caches the None so the miss is paid exactly once rather than every frame.

Implementations§

Source§

impl GlProgram

Implements shader compilation, program linking, and cached uniform upload.

§Performance

Every uniform name this program has ever been asked about is resolved exactly once and cached, including the negative result for a name the GLSL compiler optimized out. A per-frame set_uniform_1f is therefore a hash lookup plus the upload, with no trip into the GL frontend’s uniform table. Matrix uploads additionally go through a fixed-size stack array rather than a heap one, so uploading a Matrix4x4 costs no allocation on any frame, including the first.

Source

pub fn create( context: &WebGl2RenderingContext, vertex_source: &str, fragment_source: &str, ) -> Result<GlProgram, WebGlProgramError>

Compiles and links a vertex and fragment shader into a program.

Both shader objects are deleted once linking succeeds, since the program keeps the compiled code and the shader objects are pure intermediate products. On failure the browser’s info log is returned verbatim, because a GLSL diagnostic is only actionable in the driver’s own wording.

§Arguments
  • &WebGl2RenderingContext - The context to compile against.
  • &str - The vertex shader source.
  • &str - The fragment shader source.
§Returns
  • Result<GlProgram, WebGlProgramError> - The linked program with an empty uniform cache, or the compile and link info log.
Source

pub fn bind(&self, context: &WebGl2RenderingContext)

Makes the program current, so subsequent uniform writes and draws target it.

§Arguments
  • &WebGl2RenderingContext - The context to bind against.
Source

pub fn uniform( &mut self, context: &WebGl2RenderingContext, name: &str, ) -> Option<WebGlUniformLocation>

Resolves a uniform’s location once and caches it, including the case where the GLSL compiler removed the uniform entirely.

Caching the None matters as much as caching the location: a uniform the compiler optimized out is the common case for a per-feature flag, and re-querying it every frame would walk the program’s uniform table for a result that can never change.

§Arguments
  • &WebGl2RenderingContext - The context to resolve against.
  • &str - The uniform name, with an explicit [0] index for array uniforms per the getUniformLocation spec.
§Returns
  • Option<WebGlUniformLocation> - The location, or None when the uniform does not exist in the program.
Source

pub fn set_uniform_1f( &mut self, context: &WebGl2RenderingContext, name: &str, value: f32, )

Uploads a float uniform through its cached location.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • &str - The uniform name.
  • f32 - The value to upload.
Source

pub fn set_uniform_2f( &mut self, context: &WebGl2RenderingContext, name: &str, x: f32, y: f32, )

Uploads a vec2 uniform through its cached location.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • &str - The uniform name.
  • f32 - The x component.
  • f32 - The y component.
Source

pub fn set_uniform_3f( &mut self, context: &WebGl2RenderingContext, name: &str, x: f32, y: f32, z: f32, )

Uploads a vec3 uniform through its cached location.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • &str - The uniform name.
  • f32 - The x component.
  • f32 - The y component.
  • f32 - The z component.
Source

pub fn set_uniform_4f( &mut self, context: &WebGl2RenderingContext, name: &str, x: f32, y: f32, z: f32, w: f32, )

Uploads a vec4 uniform through its cached location.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • &str - The uniform name.
  • f32 - The x component.
  • f32 - The y component.
  • f32 - The z component.
  • f32 - The w component.
Source

pub fn set_uniform_mat4( &mut self, context: &WebGl2RenderingContext, name: &str, matrix: &Matrix4x4, )

Uploads a mat4 uniform from the engine’s Matrix4x4, with no allocation.

This is the single most important primitive the WebGL path was missing: the engine’s matrices are f64 and GL’s uniforms are f32, so every upload needs a conversion, and doing that into a freshly allocated Vec would put a heap allocation in the middle of the draw path. The conversion goes through a fixed-size stack array, so it is a sixteen-element copy into memory that already exists.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • &str - The uniform name.
  • &Matrix4x4 - The matrix to upload.
Source

pub fn set_uniform_vec4_array( &mut self, context: &WebGl2RenderingContext, name: &str, data: &[f32], )

Uploads a flat vec4 array uniform from a caller-owned slice.

The slice is handed to the driver directly, so there is no staging copy and no allocation: the only cost is the borrow for the duration of the call. data.len() should be a multiple of four, since a vec4 array element is four floats; a length that is not is the caller’s bug, and GL rejects it.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • &str - The uniform name, with an explicit [0] index.
  • &[f32] - The packed float data.
Source

pub fn bind_uniform_block( &mut self, context: &WebGl2RenderingContext, name: &str, binding: u32, ) -> bool

Points a named uniform block at a binding index, and caches the block’s index so the lookup happens once.

The driver’s answer for a block name the program does not declare is UNIFORM_BLOCK_INDEX, which is not a valid index. That negative result is deliberately not cached: a miss here usually means the shader changed under the program, and caching it would make the mismatch permanent rather than recoverable on the next call.

§Arguments
  • &WebGl2RenderingContext - The context to bind against.
  • &str - The block name as written in the shader.
  • u32 - The binding point the block should read from.
§Returns
  • bool - true when the block exists and was bound.
Source

pub fn delete(&mut self, context: &WebGl2RenderingContext)

Releases the program and drops its uniform cache.

§Arguments
  • &WebGl2RenderingContext - The context to release against.
Source§

impl GlProgram

Source

pub fn get_program(&self) -> &WebGlProgram

Source

pub fn get_mut_program(&mut self) -> &mut WebGlProgram

Source

pub fn set_program(&mut self, val: WebGlProgram) -> &mut Self

Source

pub fn get_uniforms(&self) -> HashMap<String, Option<WebGlUniformLocation>>

Source

pub fn get_mut_uniforms( &mut self, ) -> &mut HashMap<String, Option<WebGlUniformLocation>>

Source

pub fn set_uniforms( &mut self, val: HashMap<String, Option<WebGlUniformLocation>>, ) -> &mut Self

Source

pub fn get_matrix_scratch(&self) -> &[f32; 16]

Source

pub fn get_mut_matrix_scratch(&mut self) -> &mut [f32; 16]

Source

pub fn set_matrix_scratch(&mut self, val: [f32; 16]) -> &mut Self

Source

pub fn get_blocks(&self) -> HashMap<String, u32>

Source

pub fn get_mut_blocks(&mut self) -> &mut HashMap<String, u32>

Source

pub fn set_blocks(&mut self, val: HashMap<String, u32>) -> &mut Self

Trait Implementations§

Source§

impl Clone for GlProgram

Source§

fn clone(&self) -> Self

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
Source§

impl Debug for GlProgram

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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