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

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

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.

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

Write an Std140 type to the underlying buffer.

👎 Deprecated since 0.6.0:

Use write instead – it now works on slices.

Write a slice of values to the underlying buffer.

Returns the amount of data written by this Writer.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

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

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

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.