[][src]Struct crevice::std140::Writer

pub struct Writer<W> { /* fields omitted */ }

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::Vector3<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.

for light in &lights {
    writer.write(light)?;

    // Crevice will also pad between each array element.
}

unmap_gpu_buffer();

Implementations

impl<W: Write> Writer<W>[src]

pub fn new(writer: W) -> Self[src]

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

pub fn write<T>(&mut self, value: &T) -> Result<usize> where
    T: AsStd140
[src]

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.

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

Write a slice of values to the underlying buffer.

pub fn len(&self) -> usize[src]

Returns the amount of data written by this Writer.

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.