Skip to main content

VertexEncoding

Struct VertexEncoding 

Source
pub struct VertexEncoding { /* private fields */ }
Expand description

Vertex codec format version and compression level (the version and level of meshopt_encodeVertexBufferLevel).

Implementations§

Source§

impl VertexEncoding

Source

pub const DEFAULT: Self

Upstream defaults of meshopt_encodeVertexBuffer: version 1, level 2.

Source

pub const fn new(version: u8, level: u8) -> Result<Self, Error>

Check a version (0 or 1) and level (0 through 9, the range the pinned reference accepts; 1.3 behaves identically for levels 3 through 9). Version 0 ignores the level, as upstream does.

§Errors

UnsupportedVersion if the version is not 0 or 1; InvalidParameter if the vertex encoding level exceeds 9.

Examples found in repository?
examples/buffer_stream_bench.rs (line 63)
32fn main() {
33    let n = 65536;
34    let stride = 32;
35    // Smooth per-vertex attributes with small deterministic noise.
36    let mut rng = 1u32;
37    let vertices: Vec<u8> = (0..n)
38        .flat_map(|v| {
39            (0..stride / 4)
40                .flat_map(|k| {
41                    rng ^= rng << 13;
42                    rng ^= rng >> 17;
43                    rng ^= rng << 5;
44                    let value = (v * (k + 1) * 3) as u32 + (rng & 7);
45                    value.to_le_bytes()
46                })
47                .collect::<Vec<_>>()
48        })
49        .collect();
50    // A 256 x 256 vertex grid of quads, two triangles each, in row order.
51    let indices: Vec<u32> = (0..255u32)
52        .flat_map(|y| (0..255u32).map(move |x| (x, y)))
53        .flat_map(|(x, y)| {
54            let a = y * 256 + x;
55            [a, a + 1, a + 256, a + 1, a + 257, a + 256]
56        })
57        .collect();
58    for version in 0..=1 {
59        let v = encode_vertex_buffer(
60            &vertices,
61            n,
62            stride,
63            VertexEncoding::new(version, 2).unwrap(),
64            &mut Workspace::default(),
65        )
66        .unwrap();
67        let t = encode_index_buffer(
68            &indices,
69            IndexEncoding::new(version).unwrap(),
70            &mut Workspace::default(),
71        )
72        .unwrap();
73        let mut vo = vec![0; vertices.len()];
74        let mut to = vec![0; indices.len() * 4];
75        let vb = vo.len();
76        let tb = to.len();
77        let vm = measure(
78            &format!("v{version}:vertex:oneshot"),
79            || {
80                decode_vertex_buffer_into(
81                    black_box(&mut vo),
82                    n,
83                    stride,
84                    &v,
85                    &mut Workspace::default(),
86                )
87                .unwrap()
88            },
89            vb,
90        );
91        let tm = measure(
92            &format!("v{version}:tri:oneshot"),
93            || {
94                decode_index_buffer_into(
95                    black_box(&mut to),
96                    indices.len(),
97                    4,
98                    &t,
99                    &mut Workspace::default(),
100                )
101                .unwrap()
102            },
103            tb,
104        );
105        println!("v{version} one-shot vertex={vm:.2} triangle={tm:.2} MB/s decoded");
106        for chunk in [4096, 65536] {
107            let tail = (stride + if version == 0 { 0 } else { stride / 4 }).max(if version == 0 {
108                32
109            } else {
110                24
111            });
112            let end = v.len() - tail;
113            let size = vo.len();
114            let rate = measure(
115                &format!("v{version}:vertex:{chunk}"),
116                || {
117                    let mut d = VertexBufferDecoder::new(
118                        n,
119                        stride,
120                        v[0],
121                        &v[end..],
122                        end - 1,
123                        Limits::default(),
124                    )
125                    .unwrap();
126                    let (mut at, mut out) = (1, 0);
127                    loop {
128                        let p = d
129                            .push(&v[at..(at + chunk).min(end)], &mut vo[out..])
130                            .unwrap();
131                        at += p.consumed;
132                        out += p.written;
133                        if p.status == Status::Done {
134                            break;
135                        }
136                    }
137                    d.finish().unwrap();
138                    black_box(&vo);
139                },
140                size,
141            );
142            let mut output = vec![0u32; indices.len()];
143            let rate_t = measure(
144                &format!("v{version}:tri:{chunk}"),
145                || {
146                    let start = 1 + indices.len() / 3;
147                    let end = t.len() - 16;
148                    let mut d = IndexBufferDecoder::new(
149                        indices.len(),
150                        &t[..start],
151                        &t[end..],
152                        end - start,
153                        Limits::default(),
154                    )
155                    .unwrap();
156                    let (mut at, mut out) = (start, 0);
157                    loop {
158                        let p = d
159                            .push(&t[at..(at + chunk).min(end)], &mut output[out..])
160                            .unwrap();
161                        at += p.consumed;
162                        out += p.written;
163                        if p.status == Status::Done {
164                            break;
165                        }
166                    }
167                    d.finish().unwrap();
168                    black_box(&output);
169                },
170                to.len(),
171            );
172            println!(
173                "v{version} chunk={chunk} vertex={rate:.2} ({:.3}x) triangle={rate_t:.2} ({:.3}x)",
174                rate / vm,
175                rate_t / tm
176            );
177        }
178    }
179}
Source

pub const fn version(self) -> u8

Format version.

Source

pub const fn level(self) -> u8

Compression level.

Trait Implementations§

Source§

impl Clone for VertexEncoding

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for VertexEncoding

Source§

impl Debug for VertexEncoding

Source§

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

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

impl Default for VertexEncoding

Source§

fn default() -> Self

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

impl Eq for VertexEncoding

Source§

impl Hash for VertexEncoding

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 PartialEq for VertexEncoding

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for VertexEncoding

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.