From bf7bcd97ae64d95bf1f60e2fd33db109e761077e Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 19 May 2017 14:25:10 +0200 Subject: [PATCH] Move Usage to own module and rename to BufferUsage --- vulkano/src/buffer/cpu_access.rs | 16 +-- vulkano/src/buffer/cpu_pool.rs | 14 +-- vulkano/src/buffer/device_local.rs | 8 +- vulkano/src/buffer/immutable.rs | 48 ++++---- vulkano/src/buffer/mod.rs | 3 +- vulkano/src/buffer/sys.rs | 186 ++--------------------------- vulkano/src/buffer/usage.rs | 176 +++++++++++++++++++++++++++ vulkano/src/buffer/view.rs | 26 ++-- vulkano/src/pipeline/vertex.rs | 2 +- 9 files changed, 247 insertions(+), 232 deletions(-) create mode 100644 vulkano/src/buffer/usage.rs diff --git a/vulkano/src/buffer/cpu_access.rs b/vulkano/src/buffer/cpu_access.rs index d3f67411..6f866f1d 100644 --- a/vulkano/src/buffer/cpu_access.rs +++ b/vulkano/src/buffer/cpu_access.rs @@ -31,7 +31,7 @@ use smallvec::SmallVec; use buffer::sys::BufferCreationError; use buffer::sys::SparseLevel; use buffer::sys::UnsafeBuffer; -use buffer::sys::Usage; +use buffer::BufferUsage; use buffer::traits::BufferAccess; use buffer::traits::BufferInner; use buffer::traits::Buffer; @@ -77,7 +77,7 @@ impl CpuAccessibleBuffer { /// Deprecated. Use `from_data` instead. #[deprecated] #[inline] - pub fn new<'a, I>(device: &Arc, usage: &Usage, queue_families: I) + pub fn new<'a, I>(device: &Arc, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { @@ -87,7 +87,7 @@ impl CpuAccessibleBuffer { } /// Builds a new buffer with some data in it. Only allowed for sized data. - pub fn from_data<'a, I>(device: &Arc, usage: &Usage, queue_families: I, data: T) + pub fn from_data<'a, I>(device: &Arc, usage: &BufferUsage, queue_families: I, data: T) -> Result>, OomError> where I: IntoIterator>, T: Content + 'static, @@ -112,7 +112,7 @@ impl CpuAccessibleBuffer { /// Builds a new uninitialized buffer. Only allowed for sized data. #[inline] - pub unsafe fn uninitialized<'a, I>(device: &Arc, usage: &Usage, queue_families: I) + pub unsafe fn uninitialized<'a, I>(device: &Arc, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { @@ -123,7 +123,7 @@ impl CpuAccessibleBuffer { impl CpuAccessibleBuffer<[T]> { /// Builds a new buffer that contains an array `T`. The initial data comes from an iterator /// that produces that list of Ts. - pub fn from_iter<'a, I, Q>(device: &Arc, usage: &Usage, queue_families: Q, data: I) + pub fn from_iter<'a, I, Q>(device: &Arc, usage: &BufferUsage, queue_families: Q, data: I) -> Result>, OomError> where I: ExactSizeIterator, T: Content + 'static, @@ -154,7 +154,7 @@ impl CpuAccessibleBuffer<[T]> { // TODO: remove #[inline] #[deprecated] - pub fn array<'a, I>(device: &Arc, len: usize, usage: &Usage, queue_families: I) + pub fn array<'a, I>(device: &Arc, len: usize, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { @@ -165,7 +165,7 @@ impl CpuAccessibleBuffer<[T]> { /// Builds a new buffer. Can be used for arrays. #[inline] - pub unsafe fn uninitialized_array<'a, I>(device: &Arc, len: usize, usage: &Usage, + pub unsafe fn uninitialized_array<'a, I>(device: &Arc, len: usize, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> @@ -181,7 +181,7 @@ impl CpuAccessibleBuffer { /// /// You must ensure that the size that you pass is correct for `T`. /// - pub unsafe fn raw<'a, I>(device: &Arc, size: usize, usage: &Usage, queue_families: I) + pub unsafe fn raw<'a, I>(device: &Arc, size: usize, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { diff --git a/vulkano/src/buffer/cpu_pool.rs b/vulkano/src/buffer/cpu_pool.rs index 89e480b0..0ee95240 100644 --- a/vulkano/src/buffer/cpu_pool.rs +++ b/vulkano/src/buffer/cpu_pool.rs @@ -21,7 +21,7 @@ use smallvec::SmallVec; use buffer::sys::BufferCreationError; use buffer::sys::SparseLevel; use buffer::sys::UnsafeBuffer; -use buffer::sys::Usage; +use buffer::BufferUsage; use buffer::traits::BufferAccess; use buffer::traits::BufferInner; use buffer::traits::Buffer; @@ -43,7 +43,7 @@ use OomError; /// /// This buffer is especially suitable when you want to upload or download some data at each frame. /// -/// # Usage +/// # BufferUsage /// /// A `CpuBufferPool` is a bit similar to a `Vec`. You start by creating an empty pool, then you /// grab elements from the pool and use them, and if the pool is full it will automatically grow @@ -71,7 +71,7 @@ pub struct CpuBufferPool> where A: MemoryPool one_size: usize, // Buffer usage. - usage: Usage, + usage: BufferUsage, // Queue families allowed to access this buffer. queue_families: SmallVec<[u32; 4]>, @@ -131,7 +131,7 @@ pub struct CpuBufferPoolSubbuffer where A: MemoryPool { impl CpuBufferPool { #[inline] - pub fn new<'a, I>(device: Arc, usage: &Usage, queue_families: I) + pub fn new<'a, I>(device: Arc, usage: &BufferUsage, queue_families: I) -> CpuBufferPool where I: IntoIterator> { @@ -146,13 +146,13 @@ impl CpuBufferPool { /// family accesses. #[inline] pub fn upload(device: Arc) -> CpuBufferPool { - CpuBufferPool::new(device, &Usage::transfer_source(), iter::empty()) + CpuBufferPool::new(device, &BufferUsage::transfer_source(), iter::empty()) } } impl CpuBufferPool<[T]> { #[inline] - pub fn array<'a, I>(device: Arc, len: usize, usage: &Usage, queue_families: I) + pub fn array<'a, I>(device: Arc, len: usize, usage: &BufferUsage, queue_families: I) -> CpuBufferPool<[T]> where I: IntoIterator> { @@ -164,7 +164,7 @@ impl CpuBufferPool<[T]> { impl CpuBufferPool { pub unsafe fn raw<'a, I>(device: Arc, one_size: usize, - usage: &Usage, queue_families: I) -> CpuBufferPool + usage: &BufferUsage, queue_families: I) -> CpuBufferPool where I: IntoIterator> { let queue_families = queue_families.into_iter().map(|f| f.id()) diff --git a/vulkano/src/buffer/device_local.rs b/vulkano/src/buffer/device_local.rs index 03a7b637..e98d1458 100644 --- a/vulkano/src/buffer/device_local.rs +++ b/vulkano/src/buffer/device_local.rs @@ -22,7 +22,7 @@ use smallvec::SmallVec; use buffer::sys::BufferCreationError; use buffer::sys::SparseLevel; use buffer::sys::UnsafeBuffer; -use buffer::sys::Usage; +use buffer::BufferUsage; use buffer::traits::BufferAccess; use buffer::traits::BufferInner; use buffer::traits::Buffer; @@ -63,7 +63,7 @@ pub struct DeviceLocalBuffer> where A: MemoryP impl DeviceLocalBuffer { /// Builds a new buffer. Only allowed for sized data. #[inline] - pub fn new<'a, I>(device: &Arc, usage: &Usage, queue_families: I) + pub fn new<'a, I>(device: &Arc, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { @@ -76,7 +76,7 @@ impl DeviceLocalBuffer { impl DeviceLocalBuffer<[T]> { /// Builds a new buffer. Can be used for arrays. #[inline] - pub fn array<'a, I>(device: &Arc, len: usize, usage: &Usage, queue_families: I) + pub fn array<'a, I>(device: &Arc, len: usize, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { @@ -93,7 +93,7 @@ impl DeviceLocalBuffer { /// /// You must ensure that the size that you pass is correct for `T`. /// - pub unsafe fn raw<'a, I>(device: &Arc, size: usize, usage: &Usage, queue_families: I) + pub unsafe fn raw<'a, I>(device: &Arc, size: usize, usage: &BufferUsage, queue_families: I) -> Result>, OomError> where I: IntoIterator> { diff --git a/vulkano/src/buffer/immutable.rs b/vulkano/src/buffer/immutable.rs index 79834262..097cece4 100644 --- a/vulkano/src/buffer/immutable.rs +++ b/vulkano/src/buffer/immutable.rs @@ -32,7 +32,7 @@ use buffer::CpuAccessibleBuffer; use buffer::sys::BufferCreationError; use buffer::sys::SparseLevel; use buffer::sys::UnsafeBuffer; -use buffer::sys::Usage; +use buffer::BufferUsage; use buffer::traits::BufferAccess; use buffer::traits::BufferInner; use buffer::traits::Buffer; @@ -93,12 +93,12 @@ impl ImmutableBuffer { /// the initial upload operation. In order to be allowed to use the `ImmutableBuffer`, you must /// either submit your operation after this future, or execute this future and wait for it to /// be finished before submitting your own operation. - pub fn from_data<'a, I>(data: T, usage: &Usage, queue_families: I, queue: Arc) + pub fn from_data<'a, I>(data: T, usage: &BufferUsage, queue_families: I, queue: Arc) -> Result<(Arc>, ImmutableBufferFromBufferFuture), OomError> where I: IntoIterator>, T: 'static + Send + Sync + Sized, { - let source = CpuAccessibleBuffer::from_data(queue.device(), &Usage::transfer_source(), + let source = CpuAccessibleBuffer::from_data(queue.device(), &BufferUsage::transfer_source(), iter::once(queue.family()), data)?; ImmutableBuffer::from_buffer(source, usage, queue_families, queue) } @@ -109,7 +109,7 @@ impl ImmutableBuffer { /// the initial upload operation. In order to be allowed to use the `ImmutableBuffer`, you must /// either submit your operation after this future, or execute this future and wait for it to /// be finished before submitting your own operation. - pub fn from_buffer<'a, B, I>(source: B, usage: &Usage, queue_families: I, queue: Arc) + pub fn from_buffer<'a, B, I>(source: B, usage: &BufferUsage, queue_families: I, queue: Arc) -> Result<(Arc>, ImmutableBufferFromBufferFuture), OomError> where B: Buffer + TypedBuffer + DeviceOwned, // TODO: remove + DeviceOwned once Buffer requires it B::Access: 'static + Clone + Send + Sync, @@ -140,7 +140,7 @@ impl ImmutableBuffer { } /// Builds an `ImmutableBuffer` that copies its data from another buffer. - pub fn from_buffer_with_builder<'a, B, I, Cb, O>(source: B, usage: &Usage, queue_families: I, + pub fn from_buffer_with_builder<'a, B, I, Cb, O>(source: B, usage: &BufferUsage, queue_families: I, builder: Cb) -> Result<(Arc>, O), ImmutableBufferFromBufferWithBuilderError> where B: Buffer + TypedBuffer + DeviceOwned, // TODO: remove + DeviceOwned once Buffer requires it @@ -150,7 +150,7 @@ impl ImmutableBuffer { { unsafe { // We automatically set `transfer_dest` to true in order to avoid annoying errors. - let actual_usage = Usage { + let actual_usage = BufferUsage { transfer_dest: true, .. *usage }; @@ -182,7 +182,7 @@ impl ImmutableBuffer { /// data, otherwise the content is undefined. /// #[inline] - pub unsafe fn uninitialized<'a, I>(device: Arc, usage: &Usage, queue_families: I) + pub unsafe fn uninitialized<'a, I>(device: Arc, usage: &BufferUsage, queue_families: I) -> Result<(Arc>, ImmutableBufferInitialization), OomError> where I: IntoIterator> { @@ -191,13 +191,13 @@ impl ImmutableBuffer { } impl ImmutableBuffer<[T]> { - pub fn from_iter<'a, D, I>(data: D, usage: &Usage, queue_families: I, queue: Arc) + pub fn from_iter<'a, D, I>(data: D, usage: &BufferUsage, queue_families: I, queue: Arc) -> Result<(Arc>, ImmutableBufferFromBufferFuture), OomError> where I: IntoIterator>, D: ExactSizeIterator, T: 'static + Send + Sync + Sized, { - let source = CpuAccessibleBuffer::from_iter(queue.device(), &Usage::transfer_source(), + let source = CpuAccessibleBuffer::from_iter(queue.device(), &BufferUsage::transfer_source(), iter::once(queue.family()), data)?; ImmutableBuffer::from_buffer(source, usage, queue_families, queue) } @@ -219,7 +219,7 @@ impl ImmutableBuffer<[T]> { /// data, otherwise the content is undefined. /// #[inline] - pub unsafe fn uninitialized_array<'a, I>(device: Arc, len: usize, usage: &Usage, + pub unsafe fn uninitialized_array<'a, I>(device: Arc, len: usize, usage: &BufferUsage, queue_families: I) -> Result<(Arc>, ImmutableBufferInitialization<[T]>), OomError> where I: IntoIterator> @@ -245,7 +245,7 @@ impl ImmutableBuffer { /// data. /// #[inline] - pub unsafe fn raw<'a, I>(device: Arc, size: usize, usage: &Usage, queue_families: I) + pub unsafe fn raw<'a, I>(device: Arc, size: usize, usage: &BufferUsage, queue_families: I) -> Result<(Arc>, ImmutableBufferInitialization), OomError> where I: IntoIterator> { @@ -255,7 +255,7 @@ impl ImmutableBuffer { // Internal implementation of `raw`. This is separated from `raw` so that it doesn't need to be // inlined. - unsafe fn raw_impl(device: Arc, size: usize, usage: &Usage, + unsafe fn raw_impl(device: Arc, size: usize, usage: &BufferUsage, queue_families: SmallVec<[u32; 4]>) -> Result<(Arc>, ImmutableBufferInitialization), OomError> { @@ -517,7 +517,7 @@ mod tests { use std::iter; use buffer::cpu_access::CpuAccessibleBuffer; use buffer::immutable::ImmutableBuffer; - use buffer::sys::Usage; + use buffer::BufferUsage; use command_buffer::AutoCommandBufferBuilder; use command_buffer::CommandBuffer; use command_buffer::CommandBufferBuilder; @@ -527,11 +527,11 @@ mod tests { fn from_data_working() { let (device, queue) = gfx_dev_and_queue!(); - let (buffer, _) = ImmutableBuffer::from_data(12u32, &Usage::all(), + let (buffer, _) = ImmutableBuffer::from_data(12u32, &BufferUsage::all(), iter::once(queue.family()), queue.clone()).unwrap(); - let dest = CpuAccessibleBuffer::from_data(&device, &Usage::all(), + let dest = CpuAccessibleBuffer::from_data(&device, &BufferUsage::all(), iter::once(queue.family()), 0).unwrap(); let _ = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap() @@ -548,11 +548,11 @@ mod tests { fn from_iter_working() { let (device, queue) = gfx_dev_and_queue!(); - let (buffer, _) = ImmutableBuffer::from_iter((0 .. 512u32).map(|n| n * 2), &Usage::all(), + let (buffer, _) = ImmutableBuffer::from_iter((0 .. 512u32).map(|n| n * 2), &BufferUsage::all(), iter::once(queue.family()), queue.clone()).unwrap(); - let dest = CpuAccessibleBuffer::from_iter(&device, &Usage::all(), + let dest = CpuAccessibleBuffer::from_iter(&device, &BufferUsage::all(), iter::once(queue.family()), (0 .. 512).map(|_| 0u32)).unwrap(); @@ -573,7 +573,7 @@ mod tests { fn writing_forbidden() { let (device, queue) = gfx_dev_and_queue!(); - let (buffer, _) = ImmutableBuffer::from_data(12u32, &Usage::all(), + let (buffer, _) = ImmutableBuffer::from_data(12u32, &BufferUsage::all(), iter::once(queue.family()), queue.clone()).unwrap(); @@ -590,11 +590,11 @@ mod tests { let (device, queue) = gfx_dev_and_queue!(); let (buffer, _) = unsafe { - ImmutableBuffer::::uninitialized(device.clone(), &Usage::all(), + ImmutableBuffer::::uninitialized(device.clone(), &BufferUsage::all(), iter::once(queue.family())).unwrap() }; - let src = CpuAccessibleBuffer::from_data(&device, &Usage::all(), + let src = CpuAccessibleBuffer::from_data(&device, &BufferUsage::all(), iter::once(queue.family()), 0).unwrap(); let _ = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap() @@ -609,11 +609,11 @@ mod tests { let (device, queue) = gfx_dev_and_queue!(); let (buffer, init) = unsafe { - ImmutableBuffer::::uninitialized(device.clone(), &Usage::all(), + ImmutableBuffer::::uninitialized(device.clone(), &BufferUsage::all(), iter::once(queue.family())).unwrap() }; - let src = CpuAccessibleBuffer::from_data(&device, &Usage::all(), + let src = CpuAccessibleBuffer::from_data(&device, &BufferUsage::all(), iter::once(queue.family()), 0).unwrap(); let _ = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap() @@ -630,11 +630,11 @@ mod tests { let (device, queue) = gfx_dev_and_queue!(); let (buffer, init) = unsafe { - ImmutableBuffer::::uninitialized(device.clone(), &Usage::all(), + ImmutableBuffer::::uninitialized(device.clone(), &BufferUsage::all(), iter::once(queue.family())).unwrap() }; - let src = CpuAccessibleBuffer::from_data(&device, &Usage::all(), + let src = CpuAccessibleBuffer::from_data(&device, &BufferUsage::all(), iter::once(queue.family()), 0).unwrap(); let cb1 = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap() diff --git a/vulkano/src/buffer/mod.rs b/vulkano/src/buffer/mod.rs index 6aabba06..e038bb2f 100644 --- a/vulkano/src/buffer/mod.rs +++ b/vulkano/src/buffer/mod.rs @@ -68,12 +68,12 @@ pub use self::device_local::DeviceLocalBuffer; pub use self::immutable::ImmutableBuffer; pub use self::slice::BufferSlice; pub use self::sys::BufferCreationError; -pub use self::sys::Usage as BufferUsage; pub use self::traits::BufferAccess; pub use self::traits::BufferInner; pub use self::traits::Buffer; pub use self::traits::TypedBuffer; pub use self::traits::TypedBufferAccess; +pub use self::usage::BufferUsage; pub use self::view::BufferView; pub use self::view::BufferViewRef; @@ -86,3 +86,4 @@ pub mod view; mod slice; mod traits; +mod usage; diff --git a/vulkano/src/buffer/sys.rs b/vulkano/src/buffer/sys.rs index 7eee5336..efa990a5 100644 --- a/vulkano/src/buffer/sys.rs +++ b/vulkano/src/buffer/sys.rs @@ -31,6 +31,8 @@ use std::ptr; use std::sync::Arc; use smallvec::SmallVec; +use buffer::BufferUsage; +use buffer::usage::usage_to_bits; use device::Device; use device::DeviceOwned; use memory::DeviceMemory; @@ -62,14 +64,14 @@ impl UnsafeBuffer { /// Panics if `sparse.sparse` is false and `sparse.sparse_residency` or /// `sparse.sparse_aliased` is true. /// - pub unsafe fn new<'a, I>(device: &Arc, size: usize, usage: &Usage, sharing: Sharing, - sparse: SparseLevel) + pub unsafe fn new<'a, I>(device: &Arc, size: usize, usage: &BufferUsage, + sharing: Sharing, sparse: SparseLevel) -> Result<(UnsafeBuffer, MemoryRequirements), BufferCreationError> where I: Iterator { let vk = device.pointers(); - let usage_bits = usage.to_usage_bits(); + let usage_bits = usage_to_bits(&usage); // Checking sparse features. assert!(sparse.sparse || !sparse.sparse_residency, "Can't enable sparse residency without \ @@ -301,170 +303,6 @@ impl SparseLevel { } } -/// Describes how a buffer is going to be used. This is **not** an optimization. -/// -/// If you try to use a buffer in a way that you didn't declare, a panic will happen. -/// -/// Some methods are provided to build `Usage` structs for some common situations. However -/// there is no restriction in the combination of usages that can be enabled. -#[derive(Debug, Copy, Clone)] -pub struct Usage { - pub transfer_source: bool, - pub transfer_dest: bool, - pub uniform_texel_buffer: bool, - pub storage_texel_buffer: bool, - pub uniform_buffer: bool, - pub storage_buffer: bool, - pub index_buffer: bool, - pub vertex_buffer: bool, - pub indirect_buffer: bool, -} - -impl Usage { - /// Builds a `Usage` with all values set to false. - #[inline] - pub fn none() -> Usage { - Usage { - transfer_source: false, - transfer_dest: false, - uniform_texel_buffer: false, - storage_texel_buffer: false, - uniform_buffer: false, - storage_buffer: false, - index_buffer: false, - vertex_buffer: false, - indirect_buffer: false, - } - } - - /// Builds a `Usage` with all values set to true. Can be used for quick prototyping. - #[inline] - pub fn all() -> Usage { - Usage { - transfer_source: true, - transfer_dest: true, - uniform_texel_buffer: true, - storage_texel_buffer: true, - uniform_buffer: true, - storage_buffer: true, - index_buffer: true, - vertex_buffer: true, - indirect_buffer: true, - } - } - - /// Builds a `Usage` with `transfer_source` set to true and the rest to false. - #[inline] - pub fn transfer_source() -> Usage { - Usage { - transfer_source: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `transfer_dest` set to true and the rest to false. - #[inline] - pub fn transfer_dest() -> Usage { - Usage { - transfer_dest: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `vertex_buffer` set to true and the rest to false. - #[inline] - pub fn vertex_buffer() -> Usage { - Usage { - vertex_buffer: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `vertex_buffer` and `transfer_dest` set to true and the rest to false. - #[inline] - pub fn vertex_buffer_transfer_dest() -> Usage { - Usage { - vertex_buffer: true, - transfer_dest: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `index_buffer` set to true and the rest to false. - #[inline] - pub fn index_buffer() -> Usage { - Usage { - index_buffer: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `index_buffer` and `transfer_dest` set to true and the rest to false. - #[inline] - pub fn index_buffer_transfer_dest() -> Usage { - Usage { - index_buffer: true, - transfer_dest: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `uniform_buffer` set to true and the rest to false. - #[inline] - pub fn uniform_buffer() -> Usage { - Usage { - uniform_buffer: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `uniform_buffer` and `transfer_dest` set to true and the rest - /// to false. - #[inline] - pub fn uniform_buffer_transfer_dest() -> Usage { - Usage { - uniform_buffer: true, - transfer_dest: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `indirect_buffer` set to true and the rest to false. - #[inline] - pub fn indirect_buffer() -> Usage { - Usage { - indirect_buffer: true, - .. Usage::none() - } - } - - /// Builds a `Usage` with `indirect_buffer` and `transfer_dest` set to true and the rest - /// to false. - #[inline] - pub fn indirect_buffer_transfer_dest() -> Usage { - Usage { - indirect_buffer: true, - transfer_dest: true, - .. Usage::none() - } - } - - #[inline] - fn to_usage_bits(&self) -> vk::BufferUsageFlagBits { - let mut result = 0; - if self.transfer_source { result |= vk::BUFFER_USAGE_TRANSFER_SRC_BIT; } - if self.transfer_dest { result |= vk::BUFFER_USAGE_TRANSFER_DST_BIT; } - if self.uniform_texel_buffer { result |= vk::BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; } - if self.storage_texel_buffer { result |= vk::BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; } - if self.uniform_buffer { result |= vk::BUFFER_USAGE_UNIFORM_BUFFER_BIT; } - if self.storage_buffer { result |= vk::BUFFER_USAGE_STORAGE_BUFFER_BIT; } - if self.index_buffer { result |= vk::BUFFER_USAGE_INDEX_BUFFER_BIT; } - if self.vertex_buffer { result |= vk::BUFFER_USAGE_VERTEX_BUFFER_BIT; } - if self.indirect_buffer { result |= vk::BUFFER_USAGE_INDIRECT_BUFFER_BIT; } - result - } -} - /// Error that can happen when creating a buffer. #[derive(Clone, Debug, PartialEq, Eq)] pub enum BufferCreationError { @@ -536,7 +374,7 @@ mod tests { use super::BufferCreationError; use super::SparseLevel; use super::UnsafeBuffer; - use super::Usage; + use super::BufferUsage; use device::Device; use device::DeviceOwned; @@ -546,7 +384,7 @@ mod tests { fn create() { let (device, _) = gfx_dev_and_queue!(); let (buf, reqs) = unsafe { - UnsafeBuffer::new(&device, 128, &Usage::all(), Sharing::Exclusive::>, + UnsafeBuffer::new(&device, 128, &BufferUsage::all(), Sharing::Exclusive::>, SparseLevel::none()) }.unwrap(); @@ -561,7 +399,7 @@ mod tests { let (device, _) = gfx_dev_and_queue!(); let sparse = SparseLevel { sparse: false, sparse_residency: true, sparse_aliased: false }; let _ = unsafe { - UnsafeBuffer::new(&device, 128, &Usage::all(), Sharing::Exclusive::>, + UnsafeBuffer::new(&device, 128, &BufferUsage::all(), Sharing::Exclusive::>, sparse) }; } @@ -572,7 +410,7 @@ mod tests { let (device, _) = gfx_dev_and_queue!(); let sparse = SparseLevel { sparse: false, sparse_residency: false, sparse_aliased: true }; let _ = unsafe { - UnsafeBuffer::new(&device, 128, &Usage::all(), Sharing::Exclusive::>, + UnsafeBuffer::new(&device, 128, &BufferUsage::all(), Sharing::Exclusive::>, sparse) }; } @@ -582,7 +420,7 @@ mod tests { let (device, _) = gfx_dev_and_queue!(); let sparse = SparseLevel { sparse: true, sparse_residency: false, sparse_aliased: false }; unsafe { - match UnsafeBuffer::new(&device, 128, &Usage::all(), Sharing::Exclusive::>, + match UnsafeBuffer::new(&device, 128, &BufferUsage::all(), Sharing::Exclusive::>, sparse) { Err(BufferCreationError::SparseBindingFeatureNotEnabled) => (), @@ -596,7 +434,7 @@ mod tests { let (device, _) = gfx_dev_and_queue!(sparse_binding); let sparse = SparseLevel { sparse: true, sparse_residency: true, sparse_aliased: false }; unsafe { - match UnsafeBuffer::new(&device, 128, &Usage::all(), Sharing::Exclusive::>, + match UnsafeBuffer::new(&device, 128, &BufferUsage::all(), Sharing::Exclusive::>, sparse) { Err(BufferCreationError::SparseResidencyBufferFeatureNotEnabled) => (), @@ -610,7 +448,7 @@ mod tests { let (device, _) = gfx_dev_and_queue!(sparse_binding); let sparse = SparseLevel { sparse: true, sparse_residency: false, sparse_aliased: true }; unsafe { - match UnsafeBuffer::new(&device, 128, &Usage::all(), Sharing::Exclusive::>, + match UnsafeBuffer::new(&device, 128, &BufferUsage::all(), Sharing::Exclusive::>, sparse) { Err(BufferCreationError::SparseResidencyAliasedFeatureNotEnabled) => (), diff --git a/vulkano/src/buffer/usage.rs b/vulkano/src/buffer/usage.rs new file mode 100644 index 00000000..6c698deb --- /dev/null +++ b/vulkano/src/buffer/usage.rs @@ -0,0 +1,176 @@ +// Copyright (c) 2016 The vulkano developers +// Licensed under the Apache License, Version 2.0 +// or the MIT +// license , +// at your option. All files in the project carrying such +// notice may not be copied, modified, or distributed except +// according to those terms. + +use vk; + +/// Describes how a buffer is going to be used. This is **not** an optimization. +/// +/// If you try to use a buffer in a way that you didn't declare, a panic will happen. +/// +/// Some methods are provided to build `BufferUsage` structs for some common situations. However +/// there is no restriction in the combination of BufferUsages that can be enabled. +#[derive(Debug, Copy, Clone)] +pub struct BufferUsage { + pub transfer_source: bool, + pub transfer_dest: bool, + pub uniform_texel_buffer: bool, + pub storage_texel_buffer: bool, + pub uniform_buffer: bool, + pub storage_buffer: bool, + pub index_buffer: bool, + pub vertex_buffer: bool, + pub indirect_buffer: bool, +} + +impl BufferUsage { + /// Builds a `BufferUsage` with all values set to false. + #[inline] + pub fn none() -> BufferUsage { + BufferUsage { + transfer_source: false, + transfer_dest: false, + uniform_texel_buffer: false, + storage_texel_buffer: false, + uniform_buffer: false, + storage_buffer: false, + index_buffer: false, + vertex_buffer: false, + indirect_buffer: false, + } + } + + /// Builds a `BufferUsage` with all values set to true. Can be used for quick prototyping. + #[inline] + pub fn all() -> BufferUsage { + BufferUsage { + transfer_source: true, + transfer_dest: true, + uniform_texel_buffer: true, + storage_texel_buffer: true, + uniform_buffer: true, + storage_buffer: true, + index_buffer: true, + vertex_buffer: true, + indirect_buffer: true, + } + } + + /// Builds a `BufferUsage` with `transfer_source` set to true and the rest to false. + #[inline] + pub fn transfer_source() -> BufferUsage { + BufferUsage { + transfer_source: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `transfer_dest` set to true and the rest to false. + #[inline] + pub fn transfer_dest() -> BufferUsage { + BufferUsage { + transfer_dest: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `vertex_buffer` set to true and the rest to false. + #[inline] + pub fn vertex_buffer() -> BufferUsage { + BufferUsage { + vertex_buffer: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `vertex_buffer` and `transfer_dest` set to true and the rest + /// to false. + #[inline] + pub fn vertex_buffer_transfer_dest() -> BufferUsage { + BufferUsage { + vertex_buffer: true, + transfer_dest: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `index_buffer` set to true and the rest to false. + #[inline] + pub fn index_buffer() -> BufferUsage { + BufferUsage { + index_buffer: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `index_buffer` and `transfer_dest` set to true and the rest to false. + #[inline] + pub fn index_buffer_transfer_dest() -> BufferUsage { + BufferUsage { + index_buffer: true, + transfer_dest: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `uniform_buffer` set to true and the rest to false. + #[inline] + pub fn uniform_buffer() -> BufferUsage { + BufferUsage { + uniform_buffer: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `uniform_buffer` and `transfer_dest` set to true and the rest + /// to false. + #[inline] + pub fn uniform_buffer_transfer_dest() -> BufferUsage { + BufferUsage { + uniform_buffer: true, + transfer_dest: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `indirect_buffer` set to true and the rest to false. + #[inline] + pub fn indirect_buffer() -> BufferUsage { + BufferUsage { + indirect_buffer: true, + .. BufferUsage::none() + } + } + + /// Builds a `BufferUsage` with `indirect_buffer` and `transfer_dest` set to true and the rest + /// to false. + #[inline] + pub fn indirect_buffer_transfer_dest() -> BufferUsage { + BufferUsage { + indirect_buffer: true, + transfer_dest: true, + .. BufferUsage::none() + } + } +} + +/// Turns a `BufferUsage` into raw bits. +#[inline] +pub fn usage_to_bits(usage: &BufferUsage) -> vk::BufferUsageFlagBits { + let mut result = 0; + if usage.transfer_source { result |= vk::BUFFER_USAGE_TRANSFER_SRC_BIT; } + if usage.transfer_dest { result |= vk::BUFFER_USAGE_TRANSFER_DST_BIT; } + if usage.uniform_texel_buffer { result |= vk::BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; } + if usage.storage_texel_buffer { result |= vk::BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; } + if usage.uniform_buffer { result |= vk::BUFFER_USAGE_UNIFORM_BUFFER_BIT; } + if usage.storage_buffer { result |= vk::BUFFER_USAGE_STORAGE_BUFFER_BIT; } + if usage.index_buffer { result |= vk::BUFFER_USAGE_INDEX_BUFFER_BIT; } + if usage.vertex_buffer { result |= vk::BUFFER_USAGE_VERTEX_BUFFER_BIT; } + if usage.indirect_buffer { result |= vk::BUFFER_USAGE_INDIRECT_BUFFER_BIT; } + result +} diff --git a/vulkano/src/buffer/view.rs b/vulkano/src/buffer/view.rs index 387b6436..9254f726 100644 --- a/vulkano/src/buffer/view.rs +++ b/vulkano/src/buffer/view.rs @@ -20,15 +20,15 @@ //! ``` //! # use std::sync::Arc; //! use vulkano::buffer::immutable::ImmutableBuffer; -//! use vulkano::buffer::sys::Usage; +//! use vulkano::buffer::BufferUsage; //! use vulkano::buffer::BufferView; //! use vulkano::format; //! //! # let device: Arc = return; //! # let queue: Arc = return; -//! let usage = Usage { +//! let usage = BufferUsage { //! storage_texel_buffer: true, -//! .. Usage::none() +//! .. BufferUsage::none() //! }; //! //! let (buffer, _future) = ImmutableBuffer::<[u32]>::from_iter((0..128).map(|n| n), &usage, @@ -321,7 +321,7 @@ impl From for BufferViewCreationError { #[cfg(test)] mod tests { use buffer::BufferView; - use buffer::sys::Usage; + use buffer::BufferUsage; use buffer::view::BufferViewCreationError; use buffer::immutable::ImmutableBuffer; use format; @@ -331,9 +331,9 @@ mod tests { // `VK_FORMAT_R8G8B8A8_UNORM` guaranteed to be a supported format let (device, queue) = gfx_dev_and_queue!(); - let usage = Usage { + let usage = BufferUsage { uniform_texel_buffer: true, - .. Usage::none() + .. BufferUsage::none() }; let (buffer, _) = ImmutableBuffer::<[[u8; 4]]>::from_iter((0..128).map(|_| [0; 4]), &usage, @@ -348,9 +348,9 @@ mod tests { // `VK_FORMAT_R8G8B8A8_UNORM` guaranteed to be a supported format let (device, queue) = gfx_dev_and_queue!(); - let usage = Usage { + let usage = BufferUsage { storage_texel_buffer: true, - .. Usage::none() + .. BufferUsage::none() }; let (buffer, _) = ImmutableBuffer::<[[u8; 4]]>::from_iter((0..128).map(|_| [0; 4]), &usage, @@ -366,9 +366,9 @@ mod tests { // `VK_FORMAT_R32_UINT` guaranteed to be a supported format for atomics let (device, queue) = gfx_dev_and_queue!(); - let usage = Usage { + let usage = BufferUsage { storage_texel_buffer: true, - .. Usage::none() + .. BufferUsage::none() }; let (buffer, _) = ImmutableBuffer::<[u32]>::from_iter((0..128).map(|_| 0), &usage, @@ -386,7 +386,7 @@ mod tests { let (device, queue) = gfx_dev_and_queue!(); let (buffer, _) = ImmutableBuffer::<[[u8; 4]]>::from_iter((0..128).map(|_| [0; 4]), - &Usage::none(), + &BufferUsage::none(), Some(queue.family()), queue.clone()).unwrap(); @@ -400,10 +400,10 @@ mod tests { fn unsupported_format() { let (device, queue) = gfx_dev_and_queue!(); - let usage = Usage { + let usage = BufferUsage { uniform_texel_buffer: true, storage_texel_buffer: true, - .. Usage::none() + .. BufferUsage::none() }; let (buffer, _) = ImmutableBuffer::<[[f64; 4]]>::from_iter((0..128).map(|_| [0.0; 4]), diff --git a/vulkano/src/pipeline/vertex.rs b/vulkano/src/pipeline/vertex.rs index ce1ee30c..afc16056 100644 --- a/vulkano/src/pipeline/vertex.rs +++ b/vulkano/src/pipeline/vertex.rs @@ -39,7 +39,7 @@ //! # use vulkano::device::Device; //! # use vulkano::device::Queue; //! use vulkano::buffer::BufferAccess; -//! use vulkano::buffer::Usage as BufferUsage; +//! use vulkano::buffer::BufferUsage; //! use vulkano::memory::HostVisible; //! use vulkano::pipeline::vertex::; //! # let device: Arc = return;