Skip to content

Commit

Permalink
Support arrays in graphics in addition to from_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
danielzgtg committed Mar 28, 2021
1 parent c38f484 commit a772cf3
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_VULKANO.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Implemented synchronization for `SyncCommandBufferBuilder::execute_commands`.
- `AutoCommandBufferBuilder::execute_commands` is now fully safe to use.
- `SyncCommandBufferBuilder` now becomes poisoned when it returns an error, to prevent using the builder in an inconsistent state.
- Add support in `AutoCommandbufferBuilder::draw` for taking `[T; N]` in addition to `[T]`

# Version 0.21.0 (2021-03-05)

Expand Down
42 changes: 42 additions & 0 deletions vulkano/src/buffer/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,48 @@ impl<T, B> BufferSlice<[T], B> {
}
}

impl<T, B, const N: usize> BufferSlice<[T; N], B> {
/// Returns the number of elements in this slice.
#[inline]
pub fn len(&self) -> usize {
N
}

/// Reduces the slice to just one element of the array.
///
/// Returns `None` if out of range.
#[inline]
pub fn index(self, index: usize) -> Option<BufferSlice<T, B>> {
if index >= N {
return None;
}

Some(BufferSlice {
marker: PhantomData,
resource: self.resource,
offset: self.offset + index * mem::size_of::<T>(),
size: mem::size_of::<T>(),
})
}

/// Reduces the slice to just a range of the array.
///
/// Returns `None` if out of range.
#[inline]
pub fn slice(self, range: Range<usize>) -> Option<BufferSlice<[T], B>> {
if range.end > N {
return None;
}

Some(BufferSlice {
marker: PhantomData,
resource: self.resource,
offset: self.offset + range.start * mem::size_of::<T>(),
size: (range.end - range.start) * mem::size_of::<T>(),
})
}
}

unsafe impl<T: ?Sized, B> BufferAccess for BufferSlice<T, B>
where
B: BufferAccess,
Expand Down
2 changes: 1 addition & 1 deletion vulkano/src/buffer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub unsafe trait BufferAccess: DeviceOwned {
where
Self: Sized + TypedBufferAccess<Content = [T]>,
{
BufferSlice::slice(self.as_buffer_slice(), range)
<BufferSlice<[T], &Self>>::slice(self.as_buffer_slice(), range)
}

/// Builds a `BufferSlice` object holding the buffer by value.
Expand Down
11 changes: 11 additions & 0 deletions vulkano/src/pipeline/vertex/instance_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ where
(vec![Box::new(source) as Box<_>], 1, len)
}
}

unsafe impl<'a, B, V, const N: usize> VertexSource<[B; 1]> for SingleInstanceBufferDefinition<V>
where
B: TypedBufferAccess<Content = [V; N]> + Send + Sync + 'static,
V: Vertex,
{
#[inline]
fn decode(&self, [source]: [B; 1]) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
(vec![Box::new(source) as Box<_>], 1, N)
}
}
65 changes: 57 additions & 8 deletions vulkano/src/pipeline/vertex/one_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,62 @@ where
Bu: TypedBufferAccess<Content = [U]> + Send + Sync + 'static,
{
#[inline]
fn decode(&self, source: (Bt, Bu)) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let s1l = source.0.len();
let s2l = source.1.len();
(
vec![Box::new(source.0) as Box<_>, Box::new(source.1) as Box<_>],
s1l,
s2l,
)
fn decode(&self, (t, u): (Bt, Bu)) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let t_l = t.len();
let u_l = u.len();
(vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>], t_l, u_l)
}
}

unsafe impl<'a, T, U, Bt, Bu, const T_N: usize> VertexSource<([Bt; 1], Bu)>
for OneVertexOneInstanceDefinition<T, U>
where
T: Vertex,
Bt: TypedBufferAccess<Content = [T; T_N]> + Send + Sync + 'static,
U: Vertex,
Bu: TypedBufferAccess<Content = [U]> + Send + Sync + 'static,
{
#[inline]
fn decode(
&self,
([t], u): ([Bt; 1], Bu),
) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let u_l = u.len();
(vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>], T_N, u_l)
}
}

unsafe impl<'a, T, U, Bt, Bu, const U_N: usize> VertexSource<(Bt, [Bu; 1])>
for OneVertexOneInstanceDefinition<T, U>
where
T: Vertex,
Bt: TypedBufferAccess<Content = [T]> + Send + Sync + 'static,
U: Vertex,
Bu: TypedBufferAccess<Content = [U; U_N]> + Send + Sync + 'static,
{
#[inline]
fn decode(
&self,
(t, [u]): (Bt, [Bu; 1]),
) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let t_l = t.len();
(vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>], t_l, U_N)
}
}

unsafe impl<'a, T, U, Bt, Bu, const T_N: usize, const U_N: usize> VertexSource<([Bt; 1], [Bu; 1])>
for OneVertexOneInstanceDefinition<T, U>
where
T: Vertex,
Bt: TypedBufferAccess<Content = [T; T_N]> + Send + Sync + 'static,
U: Vertex,
Bu: TypedBufferAccess<Content = [U; U_N]> + Send + Sync + 'static,
{
#[inline]
fn decode(
&self,
([t], [u]): ([Bt; 1], [Bu; 1]),
) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
(vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>], T_N, U_N)
}
}
11 changes: 11 additions & 0 deletions vulkano/src/pipeline/vertex/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,14 @@ where
(vec![Box::new(source) as Box<_>], len, 1)
}
}

unsafe impl<'a, B, V, const N: usize> VertexSource<[B; 1]> for SingleBufferDefinition<V>
where
B: TypedBufferAccess<Content = [V; N]> + Send + Sync + 'static,
V: Vertex,
{
#[inline]
fn decode(&self, [source]: [B; 1]) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
(vec![Box::new(source) as Box<_>], N, 1)
}
}
75 changes: 68 additions & 7 deletions vulkano/src/pipeline/vertex/two.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,77 @@ where
Bu: TypedBufferAccess<Content = [U]> + Send + Sync + 'static,
{
#[inline]
fn decode(&self, source: (Bt, Bu)) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let vertices = [source.0.len(), source.1.len()]
.iter()
.cloned()
.min()
.unwrap();
fn decode(&self, (t, u): (Bt, Bu)) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let vertices = t.len().min(u.len());
(
vec![Box::new(source.0) as Box<_>, Box::new(source.1) as Box<_>],
vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>],
vertices,
1,
)
}
}

unsafe impl<'a, T, U, Bt, Bu, const T_N: usize> VertexSource<([Bt; 1], Bu)>
for TwoBuffersDefinition<T, U>
where
T: Vertex,
Bt: TypedBufferAccess<Content = [T; T_N]> + Send + Sync + 'static,
U: Vertex,
Bu: TypedBufferAccess<Content = [U]> + Send + Sync + 'static,
{
#[inline]
fn decode(
&self,
([t], u): ([Bt; 1], Bu),
) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let u_l = u.len();
(
vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>],
T_N.min(u_l),
1,
)
}
}

unsafe impl<'a, T, U, Bt, Bu, const U_N: usize> VertexSource<(Bt, [Bu; 1])>
for TwoBuffersDefinition<T, U>
where
T: Vertex,
Bt: TypedBufferAccess<Content = [T]> + Send + Sync + 'static,
U: Vertex,
Bu: TypedBufferAccess<Content = [U; U_N]> + Send + Sync + 'static,
{
#[inline]
fn decode(
&self,
(t, [u]): (Bt, [Bu; 1]),
) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
let t_l = t.len();
(
vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>],
t_l.min(U_N),
1,
)
}
}

unsafe impl<'a, T, U, Bt, Bu, const T_N: usize, const U_N: usize> VertexSource<([Bt; 1], [Bu; 1])>
for TwoBuffersDefinition<T, U>
where
T: Vertex,
Bt: TypedBufferAccess<Content = [T; T_N]> + Send + Sync + 'static,
U: Vertex,
Bu: TypedBufferAccess<Content = [U; U_N]> + Send + Sync + 'static,
{
#[inline]
fn decode(
&self,
([t], [u]): ([Bt; 1], [Bu; 1]),
) -> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize) {
(
vec![Box::new(t) as Box<_>, Box::new(u) as Box<_>],
T_N.min(U_N),
1,
)
}
}

0 comments on commit a772cf3

Please sign in to comment.