vk-graph 0.14.1+beta

A high-performance Vulkan driver with automatic resource management and execution.
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
//! Resource pooling, requesting, and caching types.
//!
//! Resource pools provide caching for buffer, image, and acceleration structure resources. Pooled
//! resources may be requested from a pool using their corresponding information structure.
//!
//! Leased resources may be bound directly to a [`Graph`](crate::Graph) and used in the same manner
//! as regular resources. After execution has completed pooled resources are automatically returned
//! to their pool for reuse.
//!
//! # Buckets
//!
//! The provided [`Pool`] implementations store resources in buckets, with each implementation
//! offering a different strategy which balances performance (_more buckets_) with memory efficiency
//! (_fewer buckets_).
//!
//! _vk-graph_'s pools can be grouped into two major categories:
//!
//! * Single-bucket: [`FifoPool`](self::fifo::FifoPool)
//! * Multi-bucket: [`LazyPool`](self::lazy::LazyPool), [`HashPool`](self::hash::HashPool)
//!
//! # Examples
//!
//! Leasing an image:
//!
//! ```no_run
//! # use std::sync::Arc;
//! # use ash::vk;
//! # use vk_graph::driver::DriverError;
//! # use vk_graph::driver::device::{Device, DeviceInfo};
//! # use vk_graph::driver::image::{ImageInfo};
//! # use vk_graph::pool::{Pool};
//! # use vk_graph::pool::lazy::{LazyPool};
//! # fn main() -> Result<(), DriverError> {
//! # let device = Device::new(DeviceInfo::default())?;
//! let mut pool = LazyPool::new(&device);
//!
//! let info = ImageInfo::image_2d(8, 8, vk::Format::R8G8B8A8_UNORM, vk::ImageUsageFlags::STORAGE);
//! let my_image = pool.resource(info)?;
//!
//! assert!(my_image.info.usage.contains(vk::ImageUsageFlags::STORAGE));
//! # Ok(()) }
//! ```
//!
//! # When Should You Use Which Pool?
//!
//! These are fairly high-level break-downs of when each pool should be considered. You may need
//! to investigate each type of pool individually to provide the absolute best fit for your purpose.
//!
//! ### Use a [`FifoPool`](self::fifo::FifoPool) when:
//! * Low memory usage is most important
//! * Automatic bucket management is desired
//!
//! ### Use a [`LazyPool`](self::lazy::LazyPool) when:
//! * Resources have different attributes each frame
//!
//! ### Use a [`HashPool`](self::hash::HashPool) when:
//! * High performance is most important
//! * Resources have consistent attributes each frame
//!
//! # When Should You Use Resource Caching?
//!
//! Wrapping any pool using [`cache::Cache::new`] enables resource caching, which prevents excess
//! resources from being created even when different parts of your code request compatible
//! resources.
//!
//! **_NOTE:_** Graph submission will automatically attempt to re-order submitted commands to
//! reduce contention between individual resources.
//!
//! **_NOTE:_** In cases where multiple cached resources using identical request information are
//! used in the same graph command, ensure they come from different cache tags or different pool
//! wrappers. Otherwise, two requests may resolve to the same underlying resource and trigger
//! Vulkan validation warnings when reading from and writing to the same images.
//!
//! ### Pros:
//!
//! * Fewer resources are created overall
//! * Wrapped pools behave like and retain all functionality of unwrapped pools
//! * Easy to experiment with and benchmark in your existing code
//!
//! ### Cons:
//!
//! * Non-zero cost: atomic load and compatibility check per active cached resource
//! * May cause GPU stalling if there is not enough work being submitted
//! * Cached resources are typed `Arc<Lease<T>>` and are not guaranteed to be mutable or unique

pub mod cache;
pub mod fifo;
pub mod hash;
pub mod lazy;

use {
    crate::driver::{
        DriverError,
        accel_struct::{
            AccelerationStructure, AccelerationStructureInfo, AccelerationStructureInfoBuilder,
        },
        buffer::{Buffer, BufferInfo, BufferInfoBuilder},
        cmd_buf::CommandBuffer,
        image::{Image, ImageInfo, ImageInfoBuilder},
    },
    derive_builder::{Builder, UninitializedFieldError},
    std::{
        fmt::Debug,
        mem::ManuallyDrop,
        ops::{Deref, DerefMut},
        sync::{Arc, Weak},
        thread::panicking,
    },
};

#[cfg(feature = "parking_lot")]
use parking_lot::Mutex;

#[cfg(not(feature = "parking_lot"))]
use std::sync::Mutex;

type Cache<T> = Arc<Mutex<Vec<T>>>;
type CacheRef<T> = Weak<Mutex<Vec<T>>>;

fn lease_command_buffer(cache: &mut Vec<CommandBuffer>) -> Option<CommandBuffer> {
    for idx in 0..cache.len() {
        if unsafe {
            let cmd = cache.get_unchecked(idx);

            // Don't lease this command buffer if it is unsignalled; we'll create a new one
            // and wait for this, and those behind it, to signal.
            cmd.device.get_fence_status(cmd.fence).unwrap_or_default()
        } {
            return Some(cache.swap_remove(idx));
        }
    }

    None
}

fn with_cache<T, R>(cache: &Cache<T>, f: impl FnOnce(&mut Vec<T>) -> R) -> R {
    let cache = cache.lock();

    #[cfg(not(feature = "parking_lot"))]
    let cache = cache.expect("poisoned cache lock");

    let mut cache = cache;

    f(&mut cache)
}

/// Holds a pooled resource and implements `Drop` in order to return the resource.
///
/// This simple wrapper type implements only the `AsRef`, `AsMut`, `Deref` and `DerefMut` traits
/// and provides no other functionality. A freshly obtained resource is guaranteed to have no other
/// owners and may be mutably accessed.
#[derive(Debug)]
pub struct Lease<T> {
    cache_ref: CacheRef<T>,
    item: ManuallyDrop<T>,
}

// The following debug_name functions take a self of Lease<T> and return Self.
// This allows pooled resources to have the same `.debug_name("bugs")` chaining

impl Lease<AccelerationStructure> {
    /// Sets the debugging name assigned to this acceleration structure.
    pub fn debug_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());

        self
    }
}

impl Lease<Buffer> {
    /// Sets the debugging name assigned to this buffer.
    pub fn debug_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());

        self
    }
}

impl Lease<Image> {
    /// Sets the debugging name assigned to this image.
    pub fn debug_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());

        self
    }
}

impl<T> Lease<T> {
    fn new(cache_ref: CacheRef<T>, item: T) -> Self {
        Self {
            cache_ref,
            item: ManuallyDrop::new(item),
        }
    }
}

impl<T> Deref for Lease<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.item
    }
}

impl<T> DerefMut for Lease<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.item
    }
}

impl<T> Drop for Lease<T> {
    #[profiling::function]
    fn drop(&mut self) {
        if panicking() {
            return;
        }

        // If the pool cache has been dropped we must manually drop the item, otherwise it goes back
        // into the pool.
        if let Some(cache) = self.cache_ref.upgrade() {
            with_cache(&cache, |cache| {
                if cache.len() >= cache.capacity() {
                    cache.pop();
                }

                cache.push(unsafe { ManuallyDrop::take(&mut self.item) });
            });
        } else {
            unsafe {
                ManuallyDrop::drop(&mut self.item);
            }
        }
    }
}

/// Allows requesting resources using driver information structures.
pub trait Pool<I, T> {
    #[deprecated = "use resource function"]
    #[doc(hidden)]
    fn lease(&mut self, info: I) -> Result<Lease<T>, DriverError> {
        self.resource(info)
    }

    /// Request a resource.
    fn resource(&mut self, info: I) -> Result<Lease<T>, DriverError>;
}

// Enable requesting items using their info builder type for convenience
macro_rules! lease_builder {
    ($info:ident => $item:ident) => {
        paste::paste! {
            impl<T> Pool<[<$info Builder>], $item> for T where T: Pool<$info, $item> {
                fn resource(
                    &mut self,
                    builder: [<$info Builder>],
                ) -> Result<Lease<$item>, DriverError> {
                    let info = builder.build();

                    self.resource(info)
                }
            }
        }
    };
}

lease_builder!(AccelerationStructureInfo => AccelerationStructure);
lease_builder!(BufferInfo => Buffer);
lease_builder!(ImageInfo => Image);

/// Information used to create a [`FifoPool`](self::fifo::FifoPool),
/// [`HashPool`](self::hash::HashPool) or [`LazyPool`](self::lazy::LazyPool) instance.
#[derive(Builder, Clone, Copy, Debug, Eq, PartialEq)]
#[builder(
    build_fn(private, name = "fallible_build", error = "UninitializedFieldError"),
    derive(Clone, Copy, Debug),
    pattern = "owned"
)]
pub struct PoolConfig {
    /// The maximum size of a single bucket of acceleration structure resource instances. The
    /// default value is [`PoolConfig::DEFAULT_RESOURCE_CAPACITY`].
    ///
    /// # Note
    ///
    /// Individual [`Pool`] implementations store varying numbers of buckets. Read the
    /// documentation of each implementation to understand how this affects total number of
    /// stored acceleration structure instances.
    #[builder(
        default = "PoolConfig::DEFAULT_RESOURCE_CAPACITY",
        setter(strip_option)
    )]
    pub accel_struct_capacity: usize,

    /// The maximum size of a single bucket of buffer resource instances. The default value is
    /// [`PoolConfig::DEFAULT_RESOURCE_CAPACITY`].
    ///
    /// # Note
    ///
    /// Individual [`Pool`] implementations store varying numbers of buckets. Read the
    /// documentation of each implementation to understand how this affects total number of
    /// stored buffer instances.
    #[builder(
        default = "PoolConfig::DEFAULT_RESOURCE_CAPACITY",
        setter(strip_option)
    )]
    pub buffer_capacity: usize,

    /// The maximum size of a single bucket of image resource instances. The default value is
    /// [`PoolConfig::DEFAULT_RESOURCE_CAPACITY`].
    ///
    /// # Note
    ///
    /// Individual [`Pool`] implementations store varying numbers of buckets. Read the
    /// documentation of each implementation to understand how this affects total number of
    /// stored image instances.
    #[builder(
        default = "PoolConfig::DEFAULT_RESOURCE_CAPACITY",
        setter(strip_option)
    )]
    pub image_capacity: usize,
}

impl PoolConfig {
    /// The maximum size of a single bucket of resource instances.
    pub const DEFAULT_RESOURCE_CAPACITY: usize = 16;

    /// Creates a default `PoolConfigBuilder`.
    pub fn builder() -> PoolConfigBuilder {
        Default::default()
    }

    fn default_cache<T>() -> Cache<T> {
        Cache::new(Mutex::new(Vec::with_capacity(
            Self::DEFAULT_RESOURCE_CAPACITY,
        )))
    }

    fn explicit_cache<T>(capacity: usize) -> Cache<T> {
        Cache::new(Mutex::new(Vec::with_capacity(capacity)))
    }

    /// Converts a `PoolConfig` into a `PoolConfigBuilder`.
    pub fn into_builder(self) -> PoolConfigBuilder {
        PoolConfigBuilder {
            accel_struct_capacity: Some(self.accel_struct_capacity),
            buffer_capacity: Some(self.buffer_capacity),
            image_capacity: Some(self.image_capacity),
        }
    }

    #[deprecated = "use into_builder function"]
    #[doc(hidden)]
    pub fn to_builder(self) -> PoolConfigBuilder {
        self.into_builder()
    }

    /// Constructs a new `PoolConfig` with the given acceleration structure, buffer and image
    /// resource capacity for any single bucket.
    pub const fn with_capacity(resource_capacity: usize) -> Self {
        Self {
            accel_struct_capacity: resource_capacity,
            buffer_capacity: resource_capacity,
            image_capacity: resource_capacity,
        }
    }
}

impl Default for PoolConfig {
    fn default() -> Self {
        PoolConfigBuilder::default().into()
    }
}

impl From<PoolConfigBuilder> for PoolConfig {
    fn from(info: PoolConfigBuilder) -> Self {
        info.build()
    }
}

impl From<usize> for PoolConfig {
    fn from(value: usize) -> Self {
        Self {
            accel_struct_capacity: value,
            buffer_capacity: value,
            image_capacity: value,
        }
    }
}

// HACK: https://github.com/colin-kiegel/rust-derive-builder/issues/56
impl PoolConfigBuilder {
    /// Builds a new `PoolConfig`.
    pub fn build(self) -> PoolConfig {
        self.fallible_build().expect("invalid pool config")
    }
}

#[doc(hidden)]
#[deprecated = "use PoolConfig instead"]
pub type PoolInfo = PoolConfig;

#[doc(hidden)]
#[deprecated = "use PoolConfigBuilder instead"]
pub type PoolInfoBuilder = PoolConfigBuilder;

mod deprecated {
    use {
        crate::pool::Lease,
        std::convert::{AsMut, AsRef},
    };

    impl<T> Lease<T> {
        #[allow(clippy::should_implement_trait)]
        #[deprecated = "use Deref impl"]
        #[doc(hidden)]
        pub fn as_ref(&self) -> &T {
            &self.item
        }

        #[allow(clippy::should_implement_trait)]
        #[deprecated = "use DerefMut impl"]
        #[doc(hidden)]
        pub fn as_mut(&mut self) -> &mut T {
            &mut self.item
        }
    }

    impl<T> AsRef<T> for Lease<T> {
        fn as_ref(&self) -> &T {
            &self.item
        }
    }

    impl<T> AsMut<T> for Lease<T> {
        fn as_mut(&mut self) -> &mut T {
            &mut self.item
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    type Info = PoolConfig;
    type Builder = PoolConfigBuilder;

    #[test]
    pub fn pool_info() {
        let info = Info::default();
        let builder = info.into_builder().build();

        assert_eq!(info, builder);
    }

    #[test]
    pub fn pool_info_builder() {
        let info = Info {
            accel_struct_capacity: 1,
            buffer_capacity: 2,
            image_capacity: 3,
        };
        let builder = Builder::default()
            .accel_struct_capacity(1)
            .buffer_capacity(2)
            .image_capacity(3)
            .build();

        assert_eq!(info, builder);
    }
}