• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //! A pool from which descriptor sets can be allocated.
11 
12 pub use self::standard::StdDescriptorPool;
13 pub use self::sys::DescriptorPoolAllocError;
14 pub use self::sys::UnsafeDescriptorPool;
15 pub use self::sys::UnsafeDescriptorPoolAllocIter;
16 use crate::descriptor_set::layout::DescriptorSetLayout;
17 use crate::descriptor_set::layout::DescriptorType;
18 use crate::descriptor_set::UnsafeDescriptorSet;
19 use crate::device::DeviceOwned;
20 use crate::OomError;
21 use std::cmp;
22 use std::ops;
23 
24 pub mod standard;
25 mod sys;
26 
27 /// A pool from which descriptor sets can be allocated.
28 ///
29 /// Since the destructor of `Alloc` must free the descriptor set, this trait is usually implemented
30 /// on `Arc<T>` or `&'a T` and not `T` directly, so that the `Alloc` object can hold the pool.
31 pub unsafe trait DescriptorPool: DeviceOwned {
32     /// Object that represented an allocated descriptor set.
33     ///
34     /// The destructor of this object should free the descriptor set.
35     type Alloc: DescriptorPoolAlloc;
36 
37     /// Allocates a descriptor set.
alloc(&mut self, layout: &DescriptorSetLayout) -> Result<Self::Alloc, OomError>38     fn alloc(&mut self, layout: &DescriptorSetLayout) -> Result<Self::Alloc, OomError>;
39 }
40 
41 /// An allocated descriptor set.
42 pub trait DescriptorPoolAlloc {
43     /// Returns the inner unsafe descriptor set object.
inner(&self) -> &UnsafeDescriptorSet44     fn inner(&self) -> &UnsafeDescriptorSet;
45 
46     /// Returns the inner unsafe descriptor set object.
inner_mut(&mut self) -> &mut UnsafeDescriptorSet47     fn inner_mut(&mut self) -> &mut UnsafeDescriptorSet;
48 }
49 
50 macro_rules! descriptors_count {
51     ($($name:ident,)+) => (
52         /// Number of available descriptors slots in a pool.
53         ///
54         /// # Example
55         ///
56         /// ```
57         /// use vulkano::descriptor_set::pool::DescriptorsCount;
58         ///
59         /// let _descriptors = DescriptorsCount {
60         ///     uniform_buffer: 10,
61         ///     input_attachment: 5,
62         ///     .. DescriptorsCount::zero()
63         /// };
64         /// ```
65         ///
66         #[derive(Debug, Copy, Clone)]
67         pub struct DescriptorsCount {
68             $(
69                 pub $name: u32,
70             )+
71         }
72 
73         impl DescriptorsCount {
74             /// Returns a `DescriptorsCount` object with all fields set to 0.
75             #[inline]
76             pub fn zero() -> DescriptorsCount {
77                 DescriptorsCount {
78                     $(
79                         $name: 0,
80                     )+
81                 }
82             }
83             /// Adds one descriptor of the given type to the count.
84             #[inline]
85             pub fn add_one(&mut self, ty: DescriptorType) {
86                 self.add_num(ty, 1);
87             }
88 
89             /// Adds `num` descriptors of the given type to the count.
90             #[inline]
91             pub fn add_num(&mut self, ty: DescriptorType, num: u32) {
92                 match ty {
93                     DescriptorType::Sampler => self.sampler += num,
94                     DescriptorType::CombinedImageSampler => self.combined_image_sampler += num,
95                     DescriptorType::SampledImage => self.sampled_image += num,
96                     DescriptorType::StorageImage => self.storage_image += num,
97                     DescriptorType::UniformTexelBuffer => self.uniform_texel_buffer += num,
98                     DescriptorType::StorageTexelBuffer => self.storage_texel_buffer += num,
99                     DescriptorType::UniformBuffer => self.uniform_buffer += num,
100                     DescriptorType::StorageBuffer => self.storage_buffer += num,
101                     DescriptorType::UniformBufferDynamic => self.uniform_buffer_dynamic += num,
102                     DescriptorType::StorageBufferDynamic => self.storage_buffer_dynamic += num,
103                     DescriptorType::InputAttachment => self.input_attachment += num,
104                 };
105             }
106         }
107 
108         impl cmp::PartialEq for DescriptorsCount {
109             #[inline]
110             fn eq(&self, other: &DescriptorsCount) -> bool {
111                 self.partial_cmp(other) == Some(cmp::Ordering::Equal)
112             }
113         }
114 
115         impl cmp::Eq for DescriptorsCount {
116         }
117 
118         impl cmp::PartialOrd for DescriptorsCount {
119             fn partial_cmp(&self, other: &DescriptorsCount) -> Option<cmp::Ordering> {
120                 if $(self.$name > other.$name)&&+ {
121                     Some(cmp::Ordering::Greater)
122                 } else if $(self.$name < other.$name)&&+ {
123                     Some(cmp::Ordering::Less)
124                 } else if $(self.$name == other.$name)&&+ {
125                     Some(cmp::Ordering::Equal)
126                 } else {
127                     None
128                 }
129             }
130 
131             fn le(&self, other: &DescriptorsCount) -> bool {
132                 $(self.$name <= other.$name)&&+
133             }
134 
135             fn ge(&self, other: &DescriptorsCount) -> bool {
136                 $(self.$name >= other.$name)&&+
137             }
138         }
139 
140         impl ops::Sub for DescriptorsCount {
141             type Output = DescriptorsCount;
142 
143             #[inline]
144             fn sub(self, rhs: DescriptorsCount) -> DescriptorsCount {
145                 DescriptorsCount {
146                     $(
147                         $name: self.$name - rhs.$name,
148                     )+
149                 }
150             }
151         }
152 
153         impl ops::SubAssign for DescriptorsCount {
154             #[inline]
155             fn sub_assign(&mut self, rhs: DescriptorsCount) {
156                 $(
157                     self.$name -= rhs.$name;
158                 )+
159             }
160         }
161 
162         impl ops::Add for DescriptorsCount {
163             type Output = DescriptorsCount;
164 
165             #[inline]
166             fn add(self, rhs: DescriptorsCount) -> DescriptorsCount {
167                 DescriptorsCount {
168                     $(
169                         $name: self.$name + rhs.$name,
170                     )+
171                 }
172             }
173         }
174 
175         impl ops::AddAssign for DescriptorsCount {
176             #[inline]
177             fn add_assign(&mut self, rhs: DescriptorsCount) {
178                 $(
179                     self.$name += rhs.$name;
180                 )+
181             }
182         }
183 
184         impl ops::Mul<u32> for DescriptorsCount {
185             type Output = DescriptorsCount;
186 
187             #[inline]
188             fn mul(self, rhs: u32) -> DescriptorsCount {
189                 DescriptorsCount {
190                     $(
191                         $name: self.$name * rhs,
192                     )+
193                 }
194             }
195         }
196 
197         impl ops::MulAssign<u32> for DescriptorsCount {
198             #[inline]
199             fn mul_assign(&mut self, rhs: u32) {
200                 $(
201                     self.$name *= rhs;
202                 )+
203             }
204         }
205     );
206 }
207 
208 descriptors_count! {
209     uniform_buffer,
210     storage_buffer,
211     uniform_buffer_dynamic,
212     storage_buffer_dynamic,
213     uniform_texel_buffer,
214     storage_texel_buffer,
215     sampled_image,
216     storage_image,
217     sampler,
218     combined_image_sampler,
219     input_attachment,
220 }
221