Skip to content

Commit

Permalink
Refactor VertexBuffersCollection to acccept Arc<dyn BufferAccess> (#1824
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AustinJ235 authored Feb 13, 2022
1 parent acaa8c9 commit 901d2b2
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/src/bin/texture_array/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use vulkano::pipeline::graphics::vertex_input::BuffersDefinition;
use vulkano::pipeline::graphics::viewport::{Viewport, ViewportState};
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
use vulkano::render_pass::{Framebuffer, RenderPass, Subpass};
use vulkano::sampler::{Sampler};
use vulkano::sampler::Sampler;
use vulkano::swapchain::{self, AcquireError, Swapchain, SwapchainCreationError};
use vulkano::sync::{self, FlushError, GpuFuture};
use vulkano::Version;
Expand Down
12 changes: 12 additions & 0 deletions vulkano/src/buffer/cpu_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use crate::buffer::sys::BufferCreationError;
use crate::buffer::sys::UnsafeBuffer;
use crate::buffer::traits::BufferAccess;
use crate::buffer::traits::BufferAccessObject;
use crate::buffer::traits::BufferInner;
use crate::buffer::traits::TypedBufferAccess;
use crate::buffer::BufferUsage;
Expand Down Expand Up @@ -485,6 +486,17 @@ where
}
}

impl<T: ?Sized, A> BufferAccessObject for Arc<CpuAccessibleBuffer<T, A>>
where
T: Send + Sync + 'static,
A: Send + Sync + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

unsafe impl<T: ?Sized, A> TypedBufferAccess for CpuAccessibleBuffer<T, A>
where
T: Send + Sync + 'static,
Expand Down
23 changes: 23 additions & 0 deletions vulkano/src/buffer/cpu_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use crate::buffer::sys::BufferCreationError;
use crate::buffer::sys::UnsafeBuffer;
use crate::buffer::traits::BufferAccess;
use crate::buffer::traits::BufferAccessObject;
use crate::buffer::traits::BufferInner;
use crate::buffer::traits::TypedBufferAccess;
use crate::buffer::BufferUsage;
Expand Down Expand Up @@ -716,6 +717,17 @@ where
}
}

impl<T, A> BufferAccessObject for Arc<CpuBufferPoolChunk<T, A>>
where
T: Send + Sync + 'static,
A: MemoryPool + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

impl<T, A> Drop for CpuBufferPoolChunk<T, A>
where
A: MemoryPool,
Expand Down Expand Up @@ -836,6 +848,17 @@ where
}
}

impl<T, A> BufferAccessObject for Arc<CpuBufferPoolSubbuffer<T, A>>
where
T: Send + Sync + 'static,
A: MemoryPool + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

unsafe impl<T, A> TypedBufferAccess for CpuBufferPoolSubbuffer<T, A>
where
T: Send + Sync,
Expand Down
12 changes: 12 additions & 0 deletions vulkano/src/buffer/device_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use crate::buffer::sys::BufferCreationError;
use crate::buffer::sys::UnsafeBuffer;
use crate::buffer::traits::BufferAccess;
use crate::buffer::traits::BufferAccessObject;
use crate::buffer::traits::BufferInner;
use crate::buffer::traits::TypedBufferAccess;
use crate::buffer::BufferUsage;
Expand Down Expand Up @@ -396,6 +397,17 @@ where
}
}

impl<T: ?Sized, A> BufferAccessObject for Arc<DeviceLocalBuffer<T, A>>
where
T: Send + Sync + 'static,
A: Send + Sync + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

unsafe impl<T: ?Sized, A> TypedBufferAccess for DeviceLocalBuffer<T, A>
where
T: Send + Sync + 'static,
Expand Down
23 changes: 23 additions & 0 deletions vulkano/src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::buffer::sys::BufferCreationError;
use crate::buffer::sys::UnsafeBuffer;
use crate::buffer::traits::BufferAccess;
use crate::buffer::traits::BufferAccessObject;
use crate::buffer::traits::BufferInner;
use crate::buffer::traits::TypedBufferAccess;
use crate::buffer::BufferUsage;
Expand Down Expand Up @@ -413,6 +414,17 @@ where
unsafe fn unlock(&self) {}
}

impl<T, A> BufferAccessObject for Arc<ImmutableBuffer<T, A>>
where
T: Send + Sync + ?Sized + 'static,
A: Send + Sync + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

unsafe impl<T, A> TypedBufferAccess for ImmutableBuffer<T, A>
where
T: Send + Sync + ?Sized,
Expand Down Expand Up @@ -516,6 +528,17 @@ where
}
}

impl<T, A> BufferAccessObject for Arc<ImmutableBufferInitialization<T, A>>
where
T: Send + Sync + ?Sized + 'static,
A: Send + Sync + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

unsafe impl<T, A> TypedBufferAccess for ImmutableBufferInitialization<T, A>
where
T: Send + Sync + ?Sized,
Expand Down
1 change: 1 addition & 0 deletions vulkano/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub use self::immutable::ImmutableBuffer;
pub use self::slice::BufferSlice;
pub use self::sys::BufferCreationError;
pub use self::traits::BufferAccess;
pub use self::traits::BufferAccessObject;
pub use self::traits::BufferInner;
pub use self::traits::TypedBufferAccess;
pub use self::usage::BufferUsage;
Expand Down
12 changes: 12 additions & 0 deletions vulkano/src/buffer/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// according to those terms.

use crate::buffer::traits::BufferAccess;
use crate::buffer::traits::BufferAccessObject;
use crate::buffer::traits::BufferInner;
use crate::buffer::traits::TypedBufferAccess;
use crate::device::Device;
Expand Down Expand Up @@ -250,6 +251,17 @@ where
}
}

impl<T, B> BufferAccessObject for Arc<BufferSlice<T, B>>
where
B: BufferAccess + 'static,
T: Send + Sync + ?Sized + 'static,
{
#[inline]
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

unsafe impl<T, B> TypedBufferAccess for BufferSlice<T, B>
where
T: Send + Sync + ?Sized,
Expand Down
10 changes: 10 additions & 0 deletions vulkano/src/buffer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ pub unsafe trait BufferAccess: DeviceOwned + Send + Sync {
}
}

pub trait BufferAccessObject {
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess>;
}

impl BufferAccessObject for Arc<dyn BufferAccess> {
fn as_buffer_access_object(&self) -> Arc<dyn BufferAccess> {
self.clone()
}
}

/// Inner information about a buffer.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct BufferInner<'a> {
Expand Down
36 changes: 17 additions & 19 deletions vulkano/src/pipeline/graphics/vertex_input/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,57 @@
// according to those terms.

use crate::buffer::BufferAccess;
use crate::buffer::BufferAccessObject;
use std::sync::Arc;

/// A collection of vertex buffers.
pub unsafe trait VertexBuffersCollection {
pub trait VertexBuffersCollection {
/// Converts `self` into a list of buffers.
// TODO: better than a Vec
fn into_vec(self) -> Vec<Arc<dyn BufferAccess>>;
}

unsafe impl VertexBuffersCollection for () {
impl VertexBuffersCollection for () {
#[inline]
fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
vec![]
Vec::new()
}
}

unsafe impl<T> VertexBuffersCollection for Arc<T>
where
T: BufferAccess + 'static,
{
impl<T: BufferAccessObject> VertexBuffersCollection for T {
#[inline]
fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
vec![self as Arc<_>]
vec![self.as_buffer_access_object()]
}
}

unsafe impl<T> VertexBuffersCollection for Vec<Arc<T>>
where
T: BufferAccess + 'static,
{
impl<T: BufferAccessObject> VertexBuffersCollection for Vec<T> {
#[inline]
fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
self.into_iter().map(|source| source as Arc<_>).collect()
self.into_iter()
.map(|src| src.as_buffer_access_object())
.collect()
}
}

macro_rules! impl_collection {
($first:ident $(, $others:ident)+) => (
unsafe impl<$first$(, $others)+> VertexBuffersCollection for (Arc<$first>, $(Arc<$others>),+)
where $first: BufferAccess + 'static
$(, $others: BufferAccess + 'static)*
impl<$first$(, $others)+> VertexBuffersCollection for ($first, $($others),+)
where $first: BufferAccessObject
$(, $others: BufferAccessObject)*
{
#[inline]
fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
#![allow(non_snake_case)]

let ($first, $($others,)*) = self;

let mut list = Vec::new();
list.push($first as Arc<_>);
list.push($first.as_buffer_access_object());

$(
list.push($others as Arc<_>);
list.push($others.as_buffer_access_object());
)+

list
}
}
Expand Down

0 comments on commit 901d2b2

Please sign in to comment.