logo
pub unsafe trait SpecializationConstants {
    fn descriptors() -> &'static [SpecializationMapEntry];
}
Expand description

Trait for types that contain specialization data for shaders.

Shader modules can contain what is called specialization constants. They are the same as constants except that their values can be defined when you create a compute pipeline or a graphics pipeline. Doing so is done by passing a type that implements the SpecializationConstants trait and that stores the values in question. The descriptors() method of this trait indicates how to grab them.

Boolean specialization constants must be stored as 32bits integers, where 0 means false and any non-zero value means true. Integer and floating-point specialization constants are stored as their Rust equivalent.

This trait is implemented on () for shaders that don’t have any specialization constant.

Example

use vulkano::shader::SpecializationConstants;
use vulkano::shader::SpecializationMapEntry;

#[repr(C)]      // `#[repr(C)]` guarantees that the struct has a specific layout
struct MySpecConstants {
    my_integer_constant: i32,
    a_boolean: u32,
    floating_point: f32,
}

unsafe impl SpecializationConstants for MySpecConstants {
    fn descriptors() -> &'static [SpecializationMapEntry] {
        static DESCRIPTORS: [SpecializationMapEntry; 3] = [
            SpecializationMapEntry {
                constant_id: 0,
                offset: 0,
                size: 4,
            },
            SpecializationMapEntry {
                constant_id: 1,
                offset: 4,
                size: 4,
            },
            SpecializationMapEntry {
                constant_id: 2,
                offset: 8,
                size: 4,
            },
        ];

        &DESCRIPTORS
    }
}

Safety

  • The SpecializationMapEntry returned must contain valid offsets and sizes.
  • The size of each SpecializationMapEntry must match the size of the corresponding constant (4 for booleans).

Required Methods

Returns descriptors of the struct’s layout.

Implementations on Foreign Types

Implementors