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.
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.
Sourcepub fn create(
context: &WebGl2RenderingContext,
target: u32,
size: u32,
usage: u32,
) -> Option<GlBuffer>
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- ThebufferDatausage hint, as returned by [gl_buffer_usage_hint].
§Returns
Option<GlBuffer>- The allocated buffer, orNonewhen the driver refused to create one.
Sourcepub fn create_for(
context: &WebGl2RenderingContext,
usage: BufferUsage,
size: u32,
) -> Option<GlBuffer>
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, orNonewhen the driver refused to create one.
Sourcepub fn fits(&self, size: u32) -> bool
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-truewhensizeis within the current capacity.
Sourcepub fn bind(&self, context: &WebGl2RenderingContext, target: u32)
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.
Sourcepub fn upload(
&mut self,
context: &WebGl2RenderingContext,
target: u32,
data: &[u8],
)
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.
Sourcepub fn update(
&mut self,
context: &WebGl2RenderingContext,
target: u32,
offset: u32,
data: &[u8],
) -> bool
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-truewhen the write was issued,falsewhen the range fell outside the allocation.
Sourcepub fn delete(&mut self, context: &WebGl2RenderingContext, target: u32)
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.