1 // Copyright (c) 2021 The vulkano developers 2 // Licensed under the Apache License, Version 2.0 3 // <LICENSE-APACHE or 4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT 5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, 6 // at your option. All files in the project carrying such 7 // notice may not be copied, modified, or distributed except 8 // according to those terms. 9 10 use crate::buffer::Subbuffer; 11 use std::mem; 12 13 /// A collection of vertex buffers. 14 pub trait VertexBuffersCollection { 15 /// Converts `self` into a list of buffers. 16 // TODO: better than a Vec into_vec(self) -> Vec<Subbuffer<[u8]>>17 fn into_vec(self) -> Vec<Subbuffer<[u8]>>; 18 } 19 20 impl VertexBuffersCollection for () { 21 #[inline] into_vec(self) -> Vec<Subbuffer<[u8]>>22 fn into_vec(self) -> Vec<Subbuffer<[u8]>> { 23 Vec::new() 24 } 25 } 26 27 impl<T: ?Sized> VertexBuffersCollection for Subbuffer<T> { into_vec(self) -> Vec<Subbuffer<[u8]>>28 fn into_vec(self) -> Vec<Subbuffer<[u8]>> { 29 vec![self.into_bytes()] 30 } 31 } 32 33 impl<T: ?Sized> VertexBuffersCollection for Vec<Subbuffer<T>> { into_vec(self) -> Vec<Subbuffer<[u8]>>34 fn into_vec(self) -> Vec<Subbuffer<[u8]>> { 35 assert!(mem::size_of::<Subbuffer<T>>() == mem::size_of::<Subbuffer<[u8]>>()); 36 assert!(mem::align_of::<Subbuffer<T>>() == mem::align_of::<Subbuffer<[u8]>>()); 37 38 // SAFETY: All `Subbuffer`s share the same layout. 39 unsafe { mem::transmute::<Vec<Subbuffer<T>>, Vec<Subbuffer<[u8]>>>(self) } 40 } 41 } 42 43 impl<T: ?Sized, const N: usize> VertexBuffersCollection for [Subbuffer<T>; N] { into_vec(self) -> Vec<Subbuffer<[u8]>>44 fn into_vec(self) -> Vec<Subbuffer<[u8]>> { 45 self.into_iter().map(Subbuffer::into_bytes).collect() 46 } 47 } 48 49 macro_rules! impl_collection { 50 ($first:ident $(, $others:ident)*) => ( 51 impl<$first: ?Sized $(, $others: ?Sized)*> VertexBuffersCollection 52 for (Subbuffer<$first>, $(Subbuffer<$others>),*) 53 { 54 #[inline] 55 #[allow(non_snake_case)] 56 fn into_vec(self) -> Vec<Subbuffer<[u8]>> { 57 let ($first, $($others,)*) = self; 58 vec![$first.into_bytes() $(, $others.into_bytes())*] 59 } 60 } 61 62 impl_collection!($($others),*); 63 ); 64 () => {} 65 } 66 67 impl_collection!(Z, Y, X, W, V, U, T, S, R, Q, P, O, N, M, L, K, J, I, H, G, F, E, D, C, B, A); 68