From 24e8d72a4a1d99b2183004d067caa1fd7f4d1b63 Mon Sep 17 00:00:00 2001 From: Daniel Tang Date: Sun, 14 Mar 2021 09:58:42 -0400 Subject: [PATCH] Allow `const` usage of features and `BufferUsage` (#1502) --- CHANGELOG_VULKANO.md | 2 ++ examples/src/bin/basic-compute-shader.rs | 18 ++++++++++------ vulkano/src/buffer/usage.rs | 26 ++++++++++++------------ vulkano/src/extensions.rs | 8 ++++---- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/CHANGELOG_VULKANO.md b/CHANGELOG_VULKANO.md index 91486a6b..fad77f8e 100644 --- a/CHANGELOG_VULKANO.md +++ b/CHANGELOG_VULKANO.md @@ -17,6 +17,8 @@ - `UnsafeImageView` no longer holds image usage information, nor does it check for valid usage. - Replaced deprecated `compare_and_swap` with `compare_exchange`. +- Allow `const` usage of features and `BufferUsage` + # Version 0.21.0 (2021-03-05) - **Breaking** `Message::layer_prefix` turned to Option to prevent segfaults when Vulkan message didn't provide `pMessageIdName` value diff --git a/examples/src/bin/basic-compute-shader.rs b/examples/src/bin/basic-compute-shader.rs index 98d0d6a5..dfe89104 100644 --- a/examples/src/bin/basic-compute-shader.rs +++ b/examples/src/bin/basic-compute-shader.rs @@ -25,6 +25,16 @@ use vulkano::sync::GpuFuture; use std::sync::Arc; +const DEVICE_EXTENSIONS: DeviceExtensions = DeviceExtensions { + khr_storage_buffer_storage_class: true, + ..DeviceExtensions::none() +}; + +const BUFFER_USAGE: BufferUsage = BufferUsage { + storage_buffer: true, + ..BufferUsage::none() +}; + fn main() { // As with other examples, the first step is to create an instance. let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); @@ -45,10 +55,7 @@ fn main() { let (device, mut queues) = Device::new( physical, physical.supported_features(), - &DeviceExtensions { - khr_storage_buffer_storage_class: true, - ..DeviceExtensions::none() - }, + &DEVICE_EXTENSIONS, [(queue_family, 0.5)].iter().cloned(), ) .unwrap(); @@ -107,8 +114,7 @@ fn main() { // Iterator that produces the data. let data_iter = (0..65536u32).map(|n| n); // Builds the buffer and fills it with this iterator. - CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), false, data_iter) - .unwrap() + CpuAccessibleBuffer::from_iter(device.clone(), BUFFER_USAGE, false, data_iter).unwrap() }; // In order to let the shader access the buffer, we need to build a *descriptor set* that diff --git a/vulkano/src/buffer/usage.rs b/vulkano/src/buffer/usage.rs index 0b8c7193..8cd904f8 100644 --- a/vulkano/src/buffer/usage.rs +++ b/vulkano/src/buffer/usage.rs @@ -71,7 +71,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with all values set to false. #[inline] - pub fn none() -> BufferUsage { + pub const fn none() -> BufferUsage { BufferUsage { transfer_source: false, transfer_destination: false, @@ -88,7 +88,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with all values set to true. Can be used for quick prototyping. #[inline] - pub fn all() -> BufferUsage { + pub const fn all() -> BufferUsage { BufferUsage { transfer_source: true, transfer_destination: true, @@ -105,7 +105,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `transfer_source` set to true and the rest to false. #[inline] - pub fn transfer_source() -> BufferUsage { + pub const fn transfer_source() -> BufferUsage { BufferUsage { transfer_source: true, ..BufferUsage::none() @@ -114,7 +114,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `transfer_destination` set to true and the rest to false. #[inline] - pub fn transfer_destination() -> BufferUsage { + pub const fn transfer_destination() -> BufferUsage { BufferUsage { transfer_destination: true, ..BufferUsage::none() @@ -123,7 +123,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `vertex_buffer` set to true and the rest to false. #[inline] - pub fn vertex_buffer() -> BufferUsage { + pub const fn vertex_buffer() -> BufferUsage { BufferUsage { vertex_buffer: true, ..BufferUsage::none() @@ -133,7 +133,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `vertex_buffer` and `transfer_destination` set to true and the rest /// to false. #[inline] - pub fn vertex_buffer_transfer_destination() -> BufferUsage { + pub const fn vertex_buffer_transfer_destination() -> BufferUsage { BufferUsage { vertex_buffer: true, transfer_destination: true, @@ -143,7 +143,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `index_buffer` set to true and the rest to false. #[inline] - pub fn index_buffer() -> BufferUsage { + pub const fn index_buffer() -> BufferUsage { BufferUsage { index_buffer: true, ..BufferUsage::none() @@ -152,7 +152,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `index_buffer` and `transfer_destination` set to true and the rest to false. #[inline] - pub fn index_buffer_transfer_destination() -> BufferUsage { + pub const fn index_buffer_transfer_destination() -> BufferUsage { BufferUsage { index_buffer: true, transfer_destination: true, @@ -162,7 +162,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `uniform_buffer` set to true and the rest to false. #[inline] - pub fn uniform_buffer() -> BufferUsage { + pub const fn uniform_buffer() -> BufferUsage { BufferUsage { uniform_buffer: true, ..BufferUsage::none() @@ -172,7 +172,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `uniform_buffer` and `transfer_destination` set to true and the rest /// to false. #[inline] - pub fn uniform_buffer_transfer_destination() -> BufferUsage { + pub const fn uniform_buffer_transfer_destination() -> BufferUsage { BufferUsage { uniform_buffer: true, transfer_destination: true, @@ -182,7 +182,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `indirect_buffer` set to true and the rest to false. #[inline] - pub fn indirect_buffer() -> BufferUsage { + pub const fn indirect_buffer() -> BufferUsage { BufferUsage { indirect_buffer: true, ..BufferUsage::none() @@ -192,7 +192,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `indirect_buffer` and `transfer_destination` set to true and the rest /// to false. #[inline] - pub fn indirect_buffer_transfer_destination() -> BufferUsage { + pub const fn indirect_buffer_transfer_destination() -> BufferUsage { BufferUsage { indirect_buffer: true, transfer_destination: true, @@ -202,7 +202,7 @@ impl BufferUsage { /// Builds a `BufferUsage` with `device_address` set to true and the rest to false. #[inline] - pub fn device_address() -> BufferUsage { + pub const fn device_address() -> BufferUsage { BufferUsage { device_address: true, ..BufferUsage::none() diff --git a/vulkano/src/extensions.rs b/vulkano/src/extensions.rs index ff8dcac2..14b07af9 100644 --- a/vulkano/src/extensions.rs +++ b/vulkano/src/extensions.rs @@ -34,7 +34,7 @@ macro_rules! extensions { impl $sname { /// Returns an `Extensions` object with all members set to `false`. #[inline] - pub fn none() -> $sname { + pub const fn none() -> $sname { $sname { $($ext: false,)* _unbuildable: Unbuildable(()) @@ -43,7 +43,7 @@ macro_rules! extensions { /// Returns the union of this list and another list. #[inline] - pub fn union(&self, other: &$sname) -> $sname { + pub const fn union(&self, other: &$sname) -> $sname { $sname { $( $ext: self.$ext || other.$ext, @@ -54,7 +54,7 @@ macro_rules! extensions { /// Returns the intersection of this list and another list. #[inline] - pub fn intersection(&self, other: &$sname) -> $sname { + pub const fn intersection(&self, other: &$sname) -> $sname { $sname { $( $ext: self.$ext && other.$ext, @@ -65,7 +65,7 @@ macro_rules! extensions { /// Returns the difference of another list from this list. #[inline] - pub fn difference(&self, other: &$sname) -> $sname { + pub const fn difference(&self, other: &$sname) -> $sname { $sname { $( $ext: self.$ext && !other.$ext,