maia 0.1.1

Safe low-level Vulkan bindings
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright 2022 Google LLC

// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt::Debug;

use super::{DescriptorSet, DescriptorSetLayoutBinding};
use crate::buffer::Buffer;
use crate::device::Device;
use crate::enums::DescriptorType;
use crate::enums::ImageLayout;
use crate::error::{Error, Result};
use crate::exclusive::Exclusive;
use crate::ffi::Array;
use crate::image::ImageView;
use crate::sampler::Sampler;
use crate::types::*;

use bumpalo::collections::Vec as BumpVec;

/// An object to build calls to vkUpdateDescriptorSets. It's best to re-use it
/// as much as possible, since it holds onto some memory to avoid allocating.
///
#[doc = crate::man_link!(vkUpdateDescriptorSets)]
///
/// ```rust
/// # use maia::vk;
/// # let inst = vk::Instance::new(&Default::default())?;
/// # let (device, _) = vk::Device::new(
/// #     &inst.enumerate_physical_devices()?[0],
/// #     &vk::DeviceCreateInfo {
/// #         enabled_features: Some(&vk::PhysicalDeviceFeatures {
/// #             robust_buffer_access: vk::True,
/// #             ..Default::default()
/// #         }),
/// #         ..Default::default()
/// #     },
/// # )?;
/// # let layout = vk::DescriptorSetLayout::new(
/// #     &device,
/// #     vec![vk::DescriptorSetLayoutBinding {
/// #         descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
/// #         descriptor_count: 1,
/// #         stage_flags: vk::ShaderStageFlags::VERTEX,
/// #         immutable_samplers: vec![],
/// #     },
/// #     vk::DescriptorSetLayoutBinding {
/// #         descriptor_type: vk::DescriptorType::SAMPLER,
/// #         descriptor_count: 1,
/// #         stage_flags: vk::ShaderStageFlags::FRAGMENT,
/// #         immutable_samplers: vec![],
/// #     }],
/// # )?;
/// # let mut pool = vk::DescriptorPool::new(
/// #     &device,
/// #     2,
/// #     &[vk::DescriptorPoolSize {
/// #         descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
/// #         descriptor_count: 2,
/// #     },
/// #     vk::DescriptorPoolSize {
/// #         descriptor_type: vk::DescriptorType::SAMPLER,
/// #         descriptor_count: 2,
/// #     }],
/// # )?;
/// let mut desc_set1 = vk::DescriptorSet::new(&mut pool, &layout)?;
/// let mut desc_set2 = vk::DescriptorSet::new(&mut pool, &layout)?;
/// # let buffer = vk::BufferWithoutMemory::new(&device, &vk::BufferCreateInfo {
/// #         size: 256,
/// #         usage: vk::BufferUsageFlags::UNIFORM_BUFFER,
/// #         ..Default::default()
/// #     })?.allocate_memory(0)?;
/// # let sampler = vk::Sampler::new(&device, &Default::default())?;
/// let mut update = vk::DescriptorSetUpdateBuilder::new(&device);
/// update
///     .begin()
///     .dst_set(&mut desc_set1)
///         .uniform_buffers(
///             0,
///             0,
///             &[vk::DescriptorBufferInfo {
///                 buffer: &buffer,
///                 offset: 0,
///                 range: Some(128),
///             }],
///         )?
///         .samplers(1, 0, &[&sampler])?
///     .dst_set(&mut desc_set2)
///         .uniform_buffers(
///             0,
///             0,
///             &[vk::DescriptorBufferInfo {
///                 buffer: &buffer,
///                 offset: 128,
///                 range: None,
///             }],
///         )?
///         .samplers(1, 0, &[&sampler])?
///     .end();
/// # Ok::<_, vk::Error>(())
/// ```
pub struct DescriptorSetUpdateBuilder {
    pub(crate) device: Arc<Device>,
    pub(crate) scratch: Exclusive<bumpalo::Bump>,
}

impl DescriptorSetUpdateBuilder {
    /// Create an object that builds calls to vkUpdateDescriptorSets.
    pub fn new(device: &Arc<Device>) -> Self {
        DescriptorSetUpdateBuilder {
            scratch: Exclusive::new(bumpalo::Bump::new()),
            device: device.clone(),
        }
    }
}

struct Resource {
    set: usize,
    binding: usize,
    element: usize,
    resource: Arc<dyn Send + Sync + Debug>,
}

/// A builder for a call to vkUpdateDescriptorSets.
#[must_use = "This object does nothing until end() is called."]
pub struct DescriptorSetUpdates<'a> {
    device: &'a Device,
    bump: &'a bumpalo::Bump,
    writes: BumpVec<'a, VkWriteDescriptorSet<'a>>,
    copies: BumpVec<'a, VkCopyDescriptorSet<'a>>,
    dst_sets: BumpVec<'a, &'a mut DescriptorSet>,
    resources: BumpVec<'a, Resource>,
}

impl DescriptorSetUpdateBuilder {
    /// Begin creating a call to vkUpdateDescriptorSets. Since these calls are
    /// expensive, try to combine them as much as possible.
    pub fn begin(&mut self) -> DescriptorSetUpdates<'_> {
        let bump = &*self.scratch.get_mut();
        DescriptorSetUpdates {
            device: &self.device,
            bump,
            writes: bumpalo::vec![in bump],
            copies: bumpalo::vec![in bump],
            dst_sets: bumpalo::vec![in bump],
            resources: bumpalo::vec![in bump],
        }
    }
}

/// A builder to update a single descriptor set.
#[must_use = "This object does nothing until end() is called."]
pub struct DescriptorSetUpdate<'a> {
    pub(crate) updates: DescriptorSetUpdates<'a>,
    pub(crate) set: &'a mut DescriptorSet,
}

impl<'a> DescriptorSetUpdates<'a> {
    /// Add updates to the given set to the builder.
    pub fn dst_set(
        self, set: &'a mut DescriptorSet,
    ) -> DescriptorSetUpdate<'a> {
        assert_eq!(&*set.layout.device, self.device);
        DescriptorSetUpdate { updates: self, set }
    }
    pub(crate) fn end(mut self) {
        for res in self.resources {
            self.dst_sets[res.set].resources[res.binding][res.element] =
                Some(res.resource);
        }
        unsafe {
            (self.device.fun.update_descriptor_sets)(
                self.device.handle(),
                self.writes.len() as u32,
                Array::from_slice(&self.writes),
                self.copies.len() as u32,
                Array::from_slice(&self.copies),
            )
        }
    }
}

#[doc = crate::man_link!(VkDescriptorBufferInfo)]
pub struct DescriptorBufferInfo<'a> {
    pub buffer: &'a Arc<Buffer>,
    pub offset: u64,
    pub range: Option<u64>,
}

macro_rules! buffer_checks {
    () => {
        "Returns [`Error::OutOfBounds`] if there are not enough bindings, and
        [`Error::InvalidArgument`] if some of the bindings in the destination
        range are of a different type, or some of the buffers don't have the
        required [`BufferUsageFlags`][crate::vk::BufferUsageFlags] set."
    };
}

macro_rules! image_checks {
    () => {
        "Returns [`Error::OutOfBounds`] if there are not enough bindings, and
        [`Error::InvalidArgument`] if some of the bindings in the destination
        range are of a different type, or some of the images don't have the
        required [`ImageUsageFlags`][crate::vk::ImageUsageFlags] set."
    };
}

impl<'a> DescriptorSetUpdate<'a> {
    /// Finish the builder and call vkUpdateDescriptorSets.
    #[doc = crate::man_link!(vkUpdateDescriptorSets)]
    pub fn end(mut self) {
        self.updates.dst_sets.push(self.set);
        self.updates.end()
    }

    pub(crate) fn set_ref(&mut self) -> Mut<'a, VkDescriptorSet> {
        // Safety: The set is kept mutably borrowed while the builder
        // is alive, and one call to vkUpdateDescriptorSets counts as
        // a single use as far as external synchronization is concerned
        unsafe { self.set.handle.borrow_mut().reborrow_mut_unchecked() }
    }

    /// Add updates to the given set to the builder.
    pub fn dst_set(
        mut self, set: &'a mut DescriptorSet,
    ) -> DescriptorSetUpdate<'a> {
        self.updates.dst_sets.push(self.set);
        self.updates.dst_set(set)
    }

    pub(crate) fn buffers_impl(
        mut self, dst_binding: u32, dst_array_element: u32,
        buffers: &'_ [DescriptorBufferInfo<'a>], max_range: u32,
        descriptor_type: DescriptorType,
    ) -> Result<Self> {
        let iter = BindingIter::new(
            &self.set.layout.bindings,
            dst_binding as usize,
            dst_array_element,
            descriptor_type,
        );
        for (b, be) in buffers.iter().zip(iter) {
            let (binding, element) = be?;
            if b.range.map_or(false, |r| r > max_range as u64) {
                return Err(Error::LimitExceeded);
            }
            if !descriptor_type.supports_buffer_usage(b.buffer.usage()) {
                return Err(Error::InvalidArgument);
            }
            assert!(std::ptr::eq(&**b.buffer.device(), self.updates.device));
            self.updates.resources.push(Resource {
                set: self.updates.dst_sets.len(),
                binding,
                element,
                resource: b.buffer.clone(),
            });
        }

        let buffer_infos =
            self.updates.bump.alloc_slice_fill_iter(buffers.iter().map(|b| {
                VkDescriptorBufferInfo {
                    buffer: b.buffer.handle(),
                    offset: b.offset,
                    range: b.range.unwrap_or(u64::MAX),
                }
            }));
        let dst_set = self.set_ref();
        self.updates.writes.push(VkWriteDescriptorSet {
            stype: Default::default(),
            next: Default::default(),
            dst_set,
            dst_binding,
            dst_array_element,
            descriptor_count: buffer_infos.len() as u32,
            descriptor_type,
            image_info: None,
            buffer_info: Array::from_slice(buffer_infos),
            texel_buffer_view: None,
        });
        Ok(self)
    }
    /// Update uniform buffer bindings.
    #[doc = buffer_checks!()]
    pub fn uniform_buffers(
        self, dst_binding: u32, dst_array_element: u32,
        buffers: &'_ [DescriptorBufferInfo<'a>],
    ) -> Result<Self> {
        let max_range = self.updates.device.limits().max_uniform_buffer_range;
        self.buffers_impl(
            dst_binding,
            dst_array_element,
            buffers,
            max_range,
            DescriptorType::UNIFORM_BUFFER,
        )
    }
    /// Update storage buffer bindings.
    #[doc = buffer_checks!()]
    pub fn storage_buffers(
        self, dst_binding: u32, dst_array_element: u32,
        buffers: &'_ [DescriptorBufferInfo<'a>],
    ) -> Result<Self> {
        let max_range = self.updates.device.limits().max_storage_buffer_range;
        self.buffers_impl(
            dst_binding,
            dst_array_element,
            buffers,
            max_range,
            DescriptorType::STORAGE_BUFFER,
        )
    }
    /// Update dynamic uniform buffer bindings.
    #[doc = buffer_checks!()]
    pub fn uniform_buffers_dynamic(
        self, dst_binding: u32, dst_array_element: u32,
        buffers: &'_ [DescriptorBufferInfo<'a>],
    ) -> Result<Self> {
        let max_range = self.updates.device.limits().max_uniform_buffer_range;
        self.buffers_impl(
            dst_binding,
            dst_array_element,
            buffers,
            max_range,
            DescriptorType::UNIFORM_BUFFER_DYNAMIC,
        )
    }
    /// Update dynamic storage buffer bindings.
    #[doc = buffer_checks!()]
    pub fn storage_buffers_dynamic(
        self, dst_binding: u32, dst_array_element: u32,
        buffers: &'_ [DescriptorBufferInfo<'a>],
    ) -> Result<Self> {
        let max_range = self.updates.device.limits().max_storage_buffer_range;
        self.buffers_impl(
            dst_binding,
            dst_array_element,
            buffers,
            max_range,
            DescriptorType::STORAGE_BUFFER_DYNAMIC,
        )
    }

    /// Update sampler bindings. Returns [Error::OutOfBounds] if
    /// there are not enough bindings, and [Error::InvalidArgument] if some of
    /// the bindings in the destination range are of a different type or already
    /// have immutable samplers.
    pub fn samplers(
        mut self, dst_binding: u32, dst_array_element: u32,
        samplers: &[&'a Arc<Sampler>],
    ) -> Result<Self> {
        let iter = BindingIter::new(
            &self.set.layout.bindings,
            dst_binding as usize,
            dst_array_element,
            DescriptorType::SAMPLER,
        );
        for (&s, be) in samplers.iter().zip(iter) {
            let (binding, element) = be?;
            if !self.set.layout.bindings[binding].immutable_samplers.is_empty()
            {
                return Err(Error::InvalidArgument);
            }
            assert!(std::ptr::eq(&**s.device(), self.updates.device));
            self.updates.resources.push(Resource {
                set: self.updates.dst_sets.len(),
                binding,
                element,
                resource: s.clone(),
            });
        }
        let image_info =
            self.updates.bump.alloc_slice_fill_iter(samplers.iter().map(|s| {
                VkDescriptorImageInfo {
                    sampler: Some(s.handle()),
                    ..Default::default()
                }
            }));
        let dst_set = self.set_ref();
        self.updates.writes.push(VkWriteDescriptorSet {
            stype: Default::default(),
            next: Default::default(),
            dst_set,
            dst_binding,
            dst_array_element,
            descriptor_count: image_info.len() as u32,
            descriptor_type: DescriptorType::SAMPLER,
            image_info: Array::from_slice(image_info),
            buffer_info: None,
            texel_buffer_view: None,
        });
        Ok(self)
    }

    pub(crate) fn images_impl(
        mut self, dst_binding: u32, dst_array_element: u32,
        images: &[(&'a Arc<ImageView>, ImageLayout)],
        descriptor_type: DescriptorType,
    ) -> Result<Self> {
        let iter = BindingIter::new(
            &self.set.layout.bindings,
            dst_binding as usize,
            dst_array_element,
            descriptor_type,
        );
        for (&(i, _), be) in images.iter().zip(iter) {
            let (binding, element) = be?;
            if !descriptor_type.supports_image_usage(i.image().usage()) {
                return Err(Error::InvalidArgument);
            }
            assert!(std::ptr::eq(&**i.device(), self.updates.device));
            self.updates.resources.push(Resource {
                set: self.updates.dst_sets.len(),
                binding,
                element,
                resource: i.clone(),
            });
        }
        let image_info = self.updates.bump.alloc_slice_fill_iter(
            images.iter().map(|&(i, image_layout)| VkDescriptorImageInfo {
                image_view: Some(i.handle()),
                image_layout,
                ..Default::default()
            }),
        );
        let dst_set = self.set_ref();
        self.updates.writes.push(VkWriteDescriptorSet {
            stype: Default::default(),
            next: Default::default(),
            dst_set,
            dst_binding,
            dst_array_element,
            descriptor_count: image_info.len() as u32,
            descriptor_type,
            image_info: Array::from_slice(image_info),
            buffer_info: None,
            texel_buffer_view: None,
        });
        Ok(self)
    }
    /// Update sampled image bindings.
    #[doc = image_checks!()]
    pub fn sampled_images(
        self, dst_binding: u32, dst_array_element: u32,
        images: &[(&'a Arc<ImageView>, ImageLayout)],
    ) -> Result<Self> {
        self.images_impl(
            dst_binding,
            dst_array_element,
            images,
            DescriptorType::SAMPLED_IMAGE,
        )
    }
    /// Update storage image bindings.
    #[doc = image_checks!()]
    pub fn storage_images(
        self, dst_binding: u32, dst_array_element: u32,
        images: &[(&'a Arc<ImageView>, ImageLayout)],
    ) -> Result<Self> {
        self.images_impl(
            dst_binding,
            dst_array_element,
            images,
            DescriptorType::STORAGE_IMAGE,
        )
    }
    /// Update input attachment bindings.
    #[doc = image_checks!()]
    pub fn input_attachments(
        self, dst_binding: u32, dst_array_element: u32,
        images: &[(&'a Arc<ImageView>, ImageLayout)],
    ) -> Result<Self> {
        self.images_impl(
            dst_binding,
            dst_array_element,
            images,
            DescriptorType::INPUT_ATTACHMENT,
        )
    }

    /// Update combined image-sampler bindings.
    #[doc = image_checks!()]
    pub fn combined_image_samplers(
        mut self, dst_binding: u32, dst_array_element: u32,
        images: &[(&'a Arc<ImageView>, ImageLayout)],
    ) -> Result<Self> {
        let iter = BindingIter::new(
            &self.set.layout.bindings,
            dst_binding as usize,
            dst_array_element,
            DescriptorType::COMBINED_IMAGE_SAMPLER,
        );
        for (&(i, _), be) in images.iter().zip(iter) {
            let (binding, element) = be?;
            assert!(std::ptr::eq(&**i.device(), self.updates.device));
            self.updates.resources.push(Resource {
                set: self.updates.dst_sets.len(),
                binding,
                element,
                resource: i.clone(),
            });
        }
        let image_info = self.updates.bump.alloc_slice_fill_iter(
            images.iter().map(|&(i, image_layout)| VkDescriptorImageInfo {
                image_view: Some(i.handle()),
                image_layout,
                ..Default::default()
            }),
        );
        let dst_set = self.set_ref();
        self.updates.writes.push(VkWriteDescriptorSet {
            stype: Default::default(),
            next: Default::default(),
            dst_set,
            dst_binding,
            dst_array_element,
            descriptor_count: image_info.len() as u32,
            descriptor_type: DescriptorType::COMBINED_IMAGE_SAMPLER,
            image_info: Array::from_slice(image_info),
            buffer_info: None,
            texel_buffer_view: None,
        });
        Ok(self)
    }
}

/// Gets the binding and element number for a series of consecutive bindings,
/// doing bounds and type checking on each.
struct BindingIter<'a> {
    bindings: &'a [DescriptorSetLayoutBinding],
    binding: usize,
    element: u32,
    descriptor_type: DescriptorType,
}

impl<'a> BindingIter<'a> {
    fn new(
        bindings: &'a [DescriptorSetLayoutBinding], binding: usize,
        element: u32, descriptor_type: DescriptorType,
    ) -> Self {
        Self { bindings, binding, element, descriptor_type }
    }
}

impl<'a> Iterator for BindingIter<'a> {
    type Item = Result<(usize, usize)>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.binding >= self.bindings.len() {
            return Some(Err(Error::OutOfBounds));
        }
        while self.element >= self.bindings[self.binding].descriptor_count {
            self.element -= self.bindings[self.binding].descriptor_count;
            self.binding += 1;
            if self.binding >= self.bindings.len() {
                return Some(Err(Error::OutOfBounds));
            }
        }
        if self.bindings[self.binding].descriptor_type != self.descriptor_type {
            return Some(Err(Error::InvalidArgument));
        }
        self.element += 1;

        Some(Ok((self.binding, self.element as usize - 1)))
    }
}