1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::{
layout::DescriptorSetLayout,
pool::{
DescriptorPoolAlloc, DescriptorPoolAllocError, DescriptorSetAllocateInfo,
UnsafeDescriptorPool, UnsafeDescriptorPoolCreateInfo,
},
sys::UnsafeDescriptorSet,
DescriptorSet, DescriptorSetCreationError, DescriptorSetInner, DescriptorSetResources,
WriteDescriptorSet,
};
use crate::{
device::{Device, DeviceOwned},
OomError, VulkanObject,
};
use crossbeam_queue::SegQueue;
use std::{
hash::{Hash, Hasher},
sync::Arc,
};
pub struct SingleLayoutDescSetPool {
inner: Option<Arc<SingleLayoutPool>>,
device: Arc<Device>,
set_count: usize,
layout: Arc<DescriptorSetLayout>,
}
impl SingleLayoutDescSetPool {
pub fn new(layout: Arc<DescriptorSetLayout>) -> Self {
assert!(
!layout.push_descriptor(),
"the provided descriptor set layout is for push descriptors, and cannot be used to build a descriptor set object"
);
assert!(
layout.variable_descriptor_count() == 0,
"the provided descriptor set layout has a binding with a variable descriptor count, which cannot be used with SingleLayoutDescSetPool"
);
Self {
inner: None,
device: layout.device().clone(),
set_count: 4,
layout,
}
}
#[inline]
pub fn next(
&mut self,
descriptor_writes: impl IntoIterator<Item = WriteDescriptorSet>,
) -> Result<Arc<SingleLayoutDescSet>, DescriptorSetCreationError> {
let alloc = self.next_alloc()?;
let inner = DescriptorSetInner::new(
alloc.inner().internal_object(),
self.layout.clone(),
0,
descriptor_writes,
)?;
Ok(Arc::new(SingleLayoutDescSet { alloc, inner }))
}
fn next_alloc(&mut self) -> Result<SingleLayoutPoolAlloc, OomError> {
loop {
let mut not_enough_sets = false;
if let Some(ref mut p_inner) = self.inner {
if let Some(existing) = p_inner.reserve.pop() {
return Ok(SingleLayoutPoolAlloc {
pool: p_inner.clone(),
inner: Some(existing),
});
} else {
not_enough_sets = true;
}
}
if not_enough_sets {
self.set_count *= 2;
}
let mut unsafe_pool = UnsafeDescriptorPool::new(
self.device.clone(),
UnsafeDescriptorPoolCreateInfo {
max_sets: self.set_count as u32,
pool_sizes: self
.layout
.descriptor_counts()
.iter()
.map(|(&ty, &count)| (ty, count * self.set_count as u32))
.collect(),
..Default::default()
},
)?;
let reserve = unsafe {
match unsafe_pool.allocate_descriptor_sets((0..self.set_count).map(|_| {
DescriptorSetAllocateInfo {
layout: self.layout.as_ref(),
variable_descriptor_count: 0,
}
})) {
Ok(alloc_iter) => {
let reserve = SegQueue::new();
for alloc in alloc_iter {
reserve.push(alloc);
}
reserve
}
Err(DescriptorPoolAllocError::OutOfHostMemory) => {
return Err(OomError::OutOfHostMemory);
}
Err(DescriptorPoolAllocError::OutOfDeviceMemory) => {
return Err(OomError::OutOfDeviceMemory);
}
Err(DescriptorPoolAllocError::FragmentedPool) => {
unreachable!()
}
Err(DescriptorPoolAllocError::OutOfPoolMemory) => unreachable!(),
}
};
self.inner = Some(Arc::new(SingleLayoutPool {
inner: unsafe_pool,
reserve,
}));
}
}
}
struct SingleLayoutPool {
inner: UnsafeDescriptorPool,
reserve: SegQueue<UnsafeDescriptorSet>,
}
struct SingleLayoutPoolAlloc {
pool: Arc<SingleLayoutPool>,
inner: Option<UnsafeDescriptorSet>,
}
impl DescriptorPoolAlloc for SingleLayoutPoolAlloc {
#[inline]
fn inner(&self) -> &UnsafeDescriptorSet {
self.inner.as_ref().unwrap()
}
#[inline]
fn inner_mut(&mut self) -> &mut UnsafeDescriptorSet {
self.inner.as_mut().unwrap()
}
}
impl Drop for SingleLayoutPoolAlloc {
fn drop(&mut self) {
let inner = self.inner.take().unwrap();
self.pool.reserve.push(inner);
}
}
pub struct SingleLayoutDescSet {
alloc: SingleLayoutPoolAlloc,
inner: DescriptorSetInner,
}
unsafe impl DescriptorSet for SingleLayoutDescSet {
#[inline]
fn inner(&self) -> &UnsafeDescriptorSet {
self.alloc.inner()
}
#[inline]
fn layout(&self) -> &Arc<DescriptorSetLayout> {
self.inner.layout()
}
#[inline]
fn resources(&self) -> &DescriptorSetResources {
self.inner.resources()
}
}
unsafe impl DeviceOwned for SingleLayoutDescSet {
#[inline]
fn device(&self) -> &Arc<Device> {
self.inner.layout().device()
}
}
impl PartialEq for SingleLayoutDescSet {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.inner().internal_object() == other.inner().internal_object()
&& self.device() == other.device()
}
}
impl Eq for SingleLayoutDescSet {}
impl Hash for SingleLayoutDescSet {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner().internal_object().hash(state);
self.device().hash(state);
}
}