Struct vulkano::padded::Padded

source ·
#[repr(C)]
pub struct Padded<T, const N: usize> { /* private fields */ }
Expand description

A newtype wrapper around T, with N bytes of trailing padding.

In Vulkan, the layout of buffer contents is not necessarily as one would expect from the type signature in the shader code. For example, the extended layout or std140 layout in GLSL, which is used for uniform buffers by default, requires that array elements are aligned to 16 bytes at minimum. That means that even if the array contains a scalar type like u32 for example, it must be aligned to 16 bytes. We can not enforce that with primitive Rust types alone. In such cases, we can use Padded to enforce correct alignment on the Rust side.

See also the shader module documentation for more information about layout in shaders.

Examples

Aligning struct members

Consider this GLSL code:

layout(binding = 0) uniform MyData {
    int x;
    vec3 y;
    vec4 z;
};

By default, the alignment rules require that y and z are placed at an offset that is an integer multiple of 16. However, x is only 4 bytes, which means that there must be 12 bytes of padding between x and y. Furthermore, y is only 12 bytes, which means that there must be 4 bytes of padding between y and z.

We can model this in Rust using Padded:

#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: Padded<i32, 12>,
    y: Padded<[f32; 3], 4>,
    z: [f32; 4],
}

let data = MyData {
    x: Padded(42),
    y: Padded([1.0, 2.0, 3.0]),
    z: [10.0; 4],
};

But note that this layout is extremely suboptimal. What you should do instead is reorder your fields such that you don’t need any padding:

layout(binding = 0) uniform MyData {
    vec3 y;
    int x;
    vec4 z;
};
#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    y: [f32; 3],
    x: i32,
    z: [f32; 4],
}

This way, the fields are aligned naturally. But reordering fields is not always an option: the notable case being when your structure only contains vec3s and vec4s, or vec3s and vec2s, so that there are no scalar fields to fill the gaps with.

Aligning array elements

If you need an array of vec3s, then that necessitates that each array element has 4 bytes of trailing padding. The same goes for a matrix with 3 rows, each column will have to have 4 bytes of trailing padding (assuming its column-major).

We can model those using Padded too:

layout(binding = 0) uniform MyData {
    vec3 x[10];
    mat3 y;
};
#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: [Padded<[f32; 3], 4>; 10],
    y: [Padded<[f32; 3], 4>; 3],
}

Another example would be if you have an array of scalars or vec2s inside a uniform block:

layout(binding = 0) uniform MyData {
    int x[10];
    vec2 y[10];
};

By default, arrays inside uniform blocks must have their elements aligned to 16 bytes at minimum, which would look like this in Rust:

#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: [Padded<i32, 12>; 10],
    y: [Padded<[f32; 2], 8>; 10],
}

But note again, that this layout is suboptimal. You can instead use a buffer block instead of the uniform block, if memory usage could become an issue:

layout(binding = 0) buffer MyData {
    int x[10];
    vec2 y[10];
};
#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: [i32; 10],
    y: [[f32; 2]; 10],
}

You may also want to consider using the uniform_buffer_standard_layout feature.

Trait Implementations§

source§

impl<T, const N: usize> AsMut<T> for Padded<T, N>

source§

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T, const N: usize> AsRef<T> for Padded<T, N>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T, const N: usize> BufferContents for Padded<T, N>where T: BufferContents,

source§

const LAYOUT: BufferContentsLayout = _

The layout of the contents.
source§

impl<T, const N: usize> Clone for Padded<T, N>where T: Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T, const N: usize> Debug for Padded<T, N>where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl<T, const N: usize> Default for Padded<T, N>where T: Default,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T, const N: usize> Deref for Padded<T, N>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T, const N: usize> DerefMut for Padded<T, N>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T, const N: usize> Display for Padded<T, N>where T: Display,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
source§

impl<T, const N: usize> From<T> for Padded<T, N>

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl<T, const N: usize> Hash for Padded<T, N>where T: Hash,

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T, const N: usize> Ord for Padded<T, N>where T: Ord,

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T, const N: usize> PartialEq for Padded<T, N>where T: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, const N: usize> PartialOrd for Padded<T, N>where T: PartialOrd,

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, const N: usize> Copy for Padded<T, N>where T: Copy,

source§

impl<T, const N: usize> Eq for Padded<T, N>where T: Eq,

Auto Trait Implementations§

§

impl<T, const N: usize> RefUnwindSafe for Padded<T, N>where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Padded<T, N>where T: Send,

§

impl<T, const N: usize> Sync for Padded<T, N>where T: Sync,

§

impl<T, const N: usize> Unpin for Padded<T, N>where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Padded<T, N>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DeviceOwned for Twhere T: Deref, <T as Deref>::Target: DeviceOwned,

source§

fn device(&self) -> &Arc<Device>

Returns the device that owns self.
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> InstanceOwned for Twhere T: Deref, <T as Deref>::Target: InstanceOwned,

source§

fn instance(&self) -> &Arc<Instance>

Returns the instance that owns self.
source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.