Struct tetra::graphics::mesh::IndexBuffer
source · pub struct IndexBuffer { /* private fields */ }
Expand description
Index data, stored in GPU memory.
An index buffer can be used as part of a Mesh
, in order to describe which vertex data should be drawn,
and what order it should be drawn in.
For example, to draw a square with raw vertex data, you need to use six vertices (two triangles, with three vertices each). This is inefficient, as two of those vertices are shared by the two triangles! Using an index buffer, you can instruct the graphics card to use vertices multiple times while constructing your square.
Index data is made up of u32
values, each of which correspond to the zero-based index of a vertex.
For example, to get the mesh to draw the third vertex, then the first, then the second, you would
create an index buffer containing [2, 0, 1]
.
Performance
When you create or modify an index buffer, you are effectively ‘uploading’ data to the GPU, which can be relatively slow. You should try to minimize how often you do this - for example, if a piece of geometry does not change from frame to frame, reuse the buffer instead of recreating it.
You can clone an index buffer cheaply, as it is a reference-counted
handle to a GPU resource. However, this does mean that modifying a buffer (e.g.
calling set_data
) will also affect any clones that exist of it.
Implementations§
source§impl IndexBuffer
impl IndexBuffer
sourcepub fn new(ctx: &mut Context, indices: &[u32]) -> Result<IndexBuffer>
pub fn new(ctx: &mut Context, indices: &[u32]) -> Result<IndexBuffer>
Creates a new index buffer.
The buffer will be created with the BufferUsage::Dynamic
usage hint - this can
be overridden via the with_usage
constructor.
Errors
TetraError::PlatformError
will be returned if the underlying graphics API encounters an error.
sourcepub fn with_usage(
ctx: &mut Context,
indices: &[u32],
usage: BufferUsage
) -> Result<IndexBuffer>
pub fn with_usage( ctx: &mut Context, indices: &[u32], usage: BufferUsage ) -> Result<IndexBuffer>
Creates a new index buffer, with the specified usage hint.
The GPU may optionally use the usage hint to optimize data storage and access.
Errors
TetraError::PlatformError
will be returned if the underlying graphics API encounters an error.
Trait Implementations§
source§impl Clone for IndexBuffer
impl Clone for IndexBuffer
source§fn clone(&self) -> IndexBuffer
fn clone(&self) -> IndexBuffer
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for IndexBuffer
impl Debug for IndexBuffer
source§impl PartialEq<IndexBuffer> for IndexBuffer
impl PartialEq<IndexBuffer> for IndexBuffer
source§fn eq(&self, other: &IndexBuffer) -> bool
fn eq(&self, other: &IndexBuffer) -> bool
self
and other
values to be equal, and is used
by ==
.