pub struct Writer<W> { /* private fields */ }
Expand description
Type that enables writing correctly aligned std140
values to a buffer.
Writer
is useful when many values need to be laid out in a row that cannot be
represented by a struct alone, like dynamically sized arrays or dynamically
laid-out values.
§Example
In this example, we’ll write a length-prefixed list of lights to a buffer.
std140::Writer
helps align correctly, even across multiple structs, which can
be tricky and error-prone otherwise.
struct PointLight {
vec3 position;
vec3 color;
float brightness;
};
buffer POINT_LIGHTS {
uint len;
PointLight[] lights;
} point_lights;
use crevice::std140::{self, AsStd140};
#[derive(AsStd140)]
struct PointLight {
position: mint::Point3<f32>,
color: mint::Vector3<f32>,
brightness: f32,
}
let lights = vec![
PointLight {
position: [0.0, 1.0, 0.0].into(),
color: [1.0, 0.0, 0.0].into(),
brightness: 0.6,
},
PointLight {
position: [0.0, 4.0, 3.0].into(),
color: [1.0, 1.0, 1.0].into(),
brightness: 1.0,
},
];
let target_buffer = map_gpu_buffer_for_write();
let mut writer = std140::Writer::new(target_buffer);
let light_count = lights.len() as u32;
writer.write(&light_count)?;
// Crevice will automatically insert the required padding to align the
// PointLight structure correctly. In this case, there will be 12 bytes of
// padding between the length field and the light list.
writer.write(lights.as_slice())?;
unmap_gpu_buffer();
Implementations§
source§impl<W: Write> Writer<W>
impl<W: Write> Writer<W>
sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
Create a new Writer
, wrapping a buffer, file, or other type that
implements std::io::Write
.
sourcepub fn write<T>(&mut self, value: &T) -> Result<usize>where
T: WriteStd140 + ?Sized,
pub fn write<T>(&mut self, value: &T) -> Result<usize>where
T: WriteStd140 + ?Sized,
Write a new value to the underlying buffer, writing zeroed padding where necessary.
Returns the offset into the buffer that the value was written to.
sourcepub fn write_iter<I, T>(&mut self, iter: I) -> Result<usize>where
I: IntoIterator<Item = T>,
T: WriteStd140,
pub fn write_iter<I, T>(&mut self, iter: I) -> Result<usize>where
I: IntoIterator<Item = T>,
T: WriteStd140,
Write an iterator of values to the underlying buffer.
Returns the offset into the buffer that the first value was written to.
If no values were written, returns the len()
.
sourcepub fn write_std140<T>(&mut self, value: &T) -> Result<usize>where
T: Std140,
pub fn write_std140<T>(&mut self, value: &T) -> Result<usize>where
T: Std140,
Write an Std140
type to the underlying buffer.
Auto Trait Implementations§
impl<W> Freeze for Writer<W>where
W: Freeze,
impl<W> RefUnwindSafe for Writer<W>where
W: RefUnwindSafe,
impl<W> Send for Writer<W>where
W: Send,
impl<W> Sync for Writer<W>where
W: Sync,
impl<W> Unpin for Writer<W>where
W: Unpin,
impl<W> UnwindSafe for Writer<W>where
W: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.