Struct crevice::std140::Writer

source ·
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>

source

pub fn new(writer: W) -> Self

Create a new Writer, wrapping a buffer, file, or other type that implements std::io::Write.

source

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.

source

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().

source

pub fn write_std140<T>(&mut self, value: &T) -> Result<usize>
where T: Std140,

Write an Std140 type to the underlying buffer.

source

pub fn write_slice<T>(&mut self, slice: &[T]) -> Result<usize>
where T: AsStd140,

👎Deprecated since 0.6.0: Use write instead – it now works on slices.

Write a slice of values to the underlying buffer.

source

pub fn len(&self) -> usize

Returns the amount of data written by this Writer.

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> 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> 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> Same for T

§

type Output = T

Should always be Self
source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.