Skip to main content

GlBuffer

Struct GlBuffer 

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

A GPU buffer holding vertex, index, or uniform data.

Unlike the WebGPU backend, a WebGL buffer carries its own allocation size, so this wrapper remembers how many bytes were last written and can answer GlBuffer::fits without asking the driver. That is what lets GlBuffer::upload re-issue bufferData on a resize instead of creating a new WebGlBuffer object and leaving the old one to the garbage collector.

Implementations§

Source§

impl GlBuffer

Implements buffer creation, upload, and lifetime for a vertex, index, or uniform buffer.

§Performance

The wrapper tracks its own byte capacity so GlBuffer::upload can choose between two paths without asking the driver. A payload that fits the existing allocation goes through bufferSubData and leaves the driver’s storage untouched; one that does not goes through bufferData with the new size, which orphans the old allocation so the driver can hand the same memory back without a free-and-realloc pair. That second path is the buffer-orphaning trick, and it is what stops a per-frame vertex stream that grows by one vertex every frame from allocating a new WebGlBuffer and leaving the old one for the garbage collector.

Source

pub fn create( context: &WebGl2RenderingContext, target: u32, size: u32, usage: u32, ) -> Option<GlBuffer>

Allocates a buffer sized for size bytes.

The allocation carries no contents, so the first GlBuffer::upload re-orphans it at the exact size it needs. Binding to target first is required: bufferData sizes whichever buffer is bound to that target, so allocating into an unbound target silently resizes whatever was bound last.

§Arguments
  • &WebGl2RenderingContext - The context to allocate against.
  • u32 - The buffer target, as returned by [gl_buffer_target].
  • u32 - The size in bytes to reserve.
  • u32 - The bufferData usage hint, as returned by [gl_buffer_usage_hint].
§Returns
  • Option<GlBuffer> - The allocated buffer, or None when the driver refused to create one.
Source

pub fn create_for( context: &WebGl2RenderingContext, usage: BufferUsage, size: u32, ) -> Option<GlBuffer>

Allocates a buffer for a role, deriving both the target and the usage hint from the role itself.

§Arguments
  • &WebGl2RenderingContext - The context to allocate against.
  • BufferUsage - The role the buffer will play.
  • u32 - The size in bytes to reserve.
§Returns
  • Option<GlBuffer> - The allocated buffer, or None when the driver refused to create one.
Source

pub fn fits(&self, size: u32) -> bool

Reports whether the buffer’s current allocation can hold size bytes without re-allocating.

Exposed separately from GlBuffer::upload so a caller batching several regions into one buffer can size the whole batch once, rather than growing the buffer on every region.

§Arguments
  • u32 - The byte count to test.
§Returns
  • bool - true when size is within the current capacity.
Source

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

Binds the buffer to target.

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

pub fn upload( &mut self, context: &WebGl2RenderingContext, target: u32, data: &[u8], )

Writes data over the whole buffer, re-allocating only when the payload outgrows the current capacity.

The reallocation path binds first, because bufferData operates on whichever buffer is bound to the target rather than on a buffer argument. The sub-data path binds too, so a caller that bound something else since the last upload still writes to the right object.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • u32 - The buffer target the buffer is bound under.
  • &[u8] - The bytes to write.
Source

pub fn update( &mut self, context: &WebGl2RenderingContext, target: u32, offset: u32, data: &[u8], ) -> bool

Writes data at offset bytes into the buffer, leaving the rest untouched.

The write is rejected outright when it would run past the end of the allocation. Checking here rather than letting the driver reject it is what turns a caller’s off-by-one into a false return instead of a write outside the buffer.

§Arguments
  • &WebGl2RenderingContext - The context to upload through.
  • u32 - The buffer target the buffer is bound under.
  • u32 - The byte offset to write at.
  • &[u8] - The bytes to write.
§Returns
  • bool - true when the write was issued, false when the range fell outside the allocation.
Source

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

Releases the buffer’s driver-side storage and marks the wrapper empty.

The capacity is zeroed so a later GlBuffer::upload against the same wrapper takes the reallocation path instead of writing into storage the driver has already reclaimed. Unbinding first matters because deleteBuffer on a bound buffer silently unbinds it, leaving the target’s binding ambiguous for the next caller.

§Arguments
  • &WebGl2RenderingContext - The context to release against.
  • u32 - The buffer target the buffer is bound under.
Source§

impl GlBuffer

Source

pub fn get_buffer(&self) -> &WebGlBuffer

Source

pub fn get_mut_buffer(&mut self) -> &mut WebGlBuffer

Source

pub fn set_buffer(&mut self, val: WebGlBuffer) -> &mut Self

Source

pub fn get_capacity(&self) -> u32

Source

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

Source

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

Source

pub fn get_usage(&self) -> u32

Source

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

Source

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

Trait Implementations§

Source§

impl Clone for GlBuffer

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 GlBuffer

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