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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::check_errors;
use crate::descriptor_set::layout::DescriptorDesc;
use crate::descriptor_set::layout::DescriptorDescTy;
use crate::descriptor_set::layout::DescriptorSetCompatibilityError;
use crate::descriptor_set::layout::DescriptorSetDesc;
use crate::descriptor_set::pool::DescriptorsCount;
use crate::device::Device;
use crate::device::DeviceOwned;
use crate::OomError;
use crate::Version;
use crate::VulkanObject;
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::Arc;
#[derive(Debug)]
pub struct DescriptorSetLayout {
handle: ash::vk::DescriptorSetLayout,
device: Arc<Device>,
desc: DescriptorSetDesc,
descriptors_count: DescriptorsCount,
variable_descriptor_count: u32,
}
impl DescriptorSetLayout {
pub fn new<D>(
device: Arc<Device>,
desc: D,
) -> Result<DescriptorSetLayout, DescriptorSetLayoutError>
where
D: Into<DescriptorSetDesc>,
{
let desc = desc.into();
let mut descriptors_count = DescriptorsCount::zero();
let mut variable_descriptor_count = 0;
let bindings = desc.bindings();
let mut bindings_vk = Vec::with_capacity(bindings.len());
let mut binding_flags_vk = Vec::with_capacity(bindings.len());
let mut immutable_samplers_vk: Vec<Box<[ash::vk::Sampler]>> = Vec::new();
for (binding, desc) in bindings.iter().enumerate() {
let desc = match desc {
Some(d) => d,
None => continue,
};
let ty = desc.ty.ty();
descriptors_count.add_num(ty, desc.descriptor_count);
let mut binding_flags = ash::vk::DescriptorBindingFlags::empty();
let immutable_samplers = desc.ty.immutable_samplers();
let p_immutable_samplers = if !immutable_samplers.is_empty() {
if desc.descriptor_count != immutable_samplers.len() as u32 {
return Err(DescriptorSetLayoutError::ImmutableSamplersCountMismatch {
descriptor_count: desc.descriptor_count,
sampler_count: immutable_samplers.len() as u32,
});
}
let sampler_handles = immutable_samplers
.iter()
.map(|s| s.internal_object())
.collect::<Vec<_>>()
.into_boxed_slice();
let p_immutable_samplers = sampler_handles.as_ptr();
immutable_samplers_vk.push(sampler_handles);
p_immutable_samplers
} else {
ptr::null()
};
if desc.variable_count {
if binding != bindings.len() - 1 {
return Err(DescriptorSetLayoutError::VariableCountDescMustBeLast);
}
if desc.ty == DescriptorDescTy::UniformBufferDynamic
|| desc.ty == DescriptorDescTy::StorageBufferDynamic
{
return Err(DescriptorSetLayoutError::VariableCountDescMustNotBeDynamic);
}
if !device.enabled_features().runtime_descriptor_array {
return Err(DescriptorSetLayoutError::VariableCountIncompatibleDevice(
IncompatibleDevice::MissingFeature(MissingFeature::RuntimeDescriptorArray),
));
}
if !device
.enabled_features()
.descriptor_binding_variable_descriptor_count
{
return Err(DescriptorSetLayoutError::VariableCountIncompatibleDevice(
IncompatibleDevice::MissingFeature(
MissingFeature::DescriptorBindingVariableDescriptorCount,
),
));
}
if !device.enabled_features().descriptor_binding_partially_bound {
return Err(DescriptorSetLayoutError::VariableCountIncompatibleDevice(
IncompatibleDevice::MissingFeature(
MissingFeature::DescriptorBindingPartiallyBound,
),
));
}
variable_descriptor_count = desc.descriptor_count;
binding_flags |= ash::vk::DescriptorBindingFlags::VARIABLE_DESCRIPTOR_COUNT;
binding_flags |= ash::vk::DescriptorBindingFlags::PARTIALLY_BOUND;
}
bindings_vk.push(ash::vk::DescriptorSetLayoutBinding {
binding: binding as u32,
descriptor_type: ty.into(),
descriptor_count: desc.descriptor_count,
stage_flags: desc.stages.into(),
p_immutable_samplers,
});
binding_flags_vk.push(binding_flags);
}
let handle = unsafe {
let binding_flags_infos = if device.api_version() >= Version::V1_2
|| device.enabled_extensions().ext_descriptor_indexing
{
Some(ash::vk::DescriptorSetLayoutBindingFlagsCreateInfo {
binding_count: binding_flags_vk.len() as u32,
p_binding_flags: binding_flags_vk.as_ptr(),
..Default::default()
})
} else {
None
};
let infos = ash::vk::DescriptorSetLayoutCreateInfo {
flags: ash::vk::DescriptorSetLayoutCreateFlags::empty(),
binding_count: bindings_vk.len() as u32,
p_bindings: bindings_vk.as_ptr(),
p_next: if let Some(next) = binding_flags_infos.as_ref() {
next as *const _ as *const _
} else {
ptr::null()
},
..Default::default()
};
let mut output = MaybeUninit::uninit();
let fns = device.fns();
check_errors(fns.v1_0.create_descriptor_set_layout(
device.internal_object(),
&infos,
ptr::null(),
output.as_mut_ptr(),
))
.map_err(|e| OomError::from(e))?;
output.assume_init()
};
Ok(DescriptorSetLayout {
handle,
device,
desc,
descriptors_count,
variable_descriptor_count,
})
}
pub(crate) fn desc(&self) -> &DescriptorSetDesc {
&self.desc
}
#[inline]
pub fn descriptors_count(&self) -> &DescriptorsCount {
&self.descriptors_count
}
#[inline]
pub fn variable_descriptor_count(&self) -> u32 {
self.variable_descriptor_count
}
#[inline]
pub fn num_bindings(&self) -> u32 {
self.desc.bindings().len() as u32
}
#[inline]
pub fn descriptor(&self, binding: u32) -> Option<DescriptorDesc> {
self.desc
.bindings()
.get(binding as usize)
.cloned()
.unwrap_or(None)
}
#[inline]
pub fn is_compatible_with(&self, other: &DescriptorSetLayout) -> bool {
self.handle == other.handle || self.desc.is_compatible_with(&other.desc)
}
pub fn ensure_compatible_with_bind(
&self,
other: &DescriptorSetLayout,
) -> Result<(), DescriptorSetCompatibilityError> {
if self.handle == other.handle {
return Ok(());
}
self.desc.ensure_compatible_with_bind(&other.desc)
}
}
unsafe impl DeviceOwned for DescriptorSetLayout {
#[inline]
fn device(&self) -> &Arc<Device> {
&self.device
}
}
unsafe impl VulkanObject for DescriptorSetLayout {
type Object = ash::vk::DescriptorSetLayout;
#[inline]
fn internal_object(&self) -> ash::vk::DescriptorSetLayout {
self.handle
}
}
impl Drop for DescriptorSetLayout {
#[inline]
fn drop(&mut self) {
unsafe {
let fns = self.device.fns();
fns.v1_0.destroy_descriptor_set_layout(
self.device.internal_object(),
self.handle,
ptr::null(),
);
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DescriptorSetLayoutError {
OomError(OomError),
ImmutableSamplersCountMismatch {
descriptor_count: u32,
sampler_count: u32,
},
VariableCountDescMustBeLast,
VariableCountDescMustNotBeDynamic,
VariableCountIncompatibleDevice(IncompatibleDevice),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IncompatibleDevice {
MissingExtension(MissingExtension),
MissingFeature(MissingFeature),
InsufficientApiVersion {
required: Version,
obtained: Version,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MissingFeature {
RuntimeDescriptorArray,
DescriptorBindingVariableDescriptorCount,
DescriptorBindingPartiallyBound,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MissingExtension {
DescriptorIndexing,
}
impl From<OomError> for DescriptorSetLayoutError {
fn from(error: OomError) -> Self {
Self::OomError(error)
}
}
impl std::error::Error for DescriptorSetLayoutError {}
impl std::fmt::Display for DescriptorSetLayoutError {
#[inline]
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(
fmt,
"{}",
match *self {
Self::OomError(_) => "out of memory",
Self::ImmutableSamplersCountMismatch { .. } =>
"the number of immutable samplers does not match the descriptor count",
Self::VariableCountDescMustBeLast =>
"variable count descriptor must be last binding",
Self::VariableCountDescMustNotBeDynamic =>
"variable count descriptor must not be a dynamic buffer",
Self::VariableCountIncompatibleDevice(_) =>
"device is not compatible with variable count descriptors",
}
)
}
}
#[cfg(test)]
mod tests {
use crate::descriptor_set::layout::DescriptorDesc;
use crate::descriptor_set::layout::DescriptorDescTy;
use crate::descriptor_set::layout::DescriptorSetDesc;
use crate::descriptor_set::layout::DescriptorSetLayout;
use crate::descriptor_set::pool::DescriptorsCount;
use crate::pipeline::shader::ShaderStages;
use std::iter;
#[test]
fn empty() {
let (device, _) = gfx_dev_and_queue!();
let _layout = DescriptorSetLayout::new(device, DescriptorSetDesc::empty());
}
#[test]
fn basic_create() {
let (device, _) = gfx_dev_and_queue!();
let layout = DescriptorDesc {
ty: DescriptorDescTy::UniformBuffer,
descriptor_count: 1,
stages: ShaderStages::all_graphics(),
mutable: false,
variable_count: false,
};
let sl = DescriptorSetLayout::new(
device.clone(),
DescriptorSetDesc::new(iter::once(Some(layout))),
)
.unwrap();
assert_eq!(
sl.descriptors_count(),
&DescriptorsCount {
uniform_buffer: 1,
..DescriptorsCount::zero()
}
);
}
}