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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! Items related to Vulkan and the Rust API used by Nannou called Vulkano.
//!
//! This module re-exports the entire `vulkano` crate along with all of its documentation while
//! also adding some additional helper types.
//!
//! Individual items from throughout the vulkano crate have been re-exported within this module for
//! ease of access via the `vk::` prefix, removing the need for a lot of boilerplate when coding.
//! However, as a result, the documentation for this module is quite noisey! You can find cleaner
//! information about how the different areas of Vulkan interoperate by checking out the
//! module-level documentation for that area. For example, read about Framebuffers and RenderPasses
//! in the `nannou::vk::framebuffer` module, or read about the CommandBuffer within the
//! `nannou::vk::command_buffer` module.
//!
//! For more information on extensions to the vulkano crate added by nannou, scroll past the
//! "Re-exports" items below.

// Re-export `vulkano` along with its docs under this short-hand `vk` module.
#[doc(inline)]
pub use vulkano::*;

// Re-export type and trait names whose meaning are still obvious outside of their module.
pub use vulkano::{
    buffer::cpu_pool::{CpuBufferPoolChunk, CpuBufferPoolSubbuffer},
    buffer::{
        BufferAccess, BufferCreationError, BufferInner, BufferSlice, BufferUsage, BufferView,
        BufferViewRef, CpuAccessibleBuffer, CpuBufferPool, DeviceLocalBuffer, ImmutableBuffer,
        TypedBufferAccess,
    },
    command_buffer::{
        AutoCommandBuffer, AutoCommandBufferBuilder, AutoCommandBufferBuilderContextError,
        CommandBuffer, CommandBufferExecFuture, DispatchIndirectCommand, DrawIndirectCommand,
        DynamicState, ExecuteCommandsError, StateCacherOutcome, UpdateBufferError,
    },
    descriptor::descriptor::{
        DescriptorBufferDesc, DescriptorDesc, DescriptorDescSupersetError, DescriptorDescTy,
        DescriptorImageDesc, DescriptorImageDescArray, DescriptorImageDescDimensions,
        DescriptorType, ShaderStages, ShaderStagesSupersetError,
    },
    descriptor::descriptor_set::{
        DescriptorPool, DescriptorPoolAlloc, DescriptorPoolAllocError, DescriptorSetDesc,
        DescriptorSetsCollection, DescriptorWrite, DescriptorsCount, FixedSizeDescriptorSet,
        FixedSizeDescriptorSetBuilder, FixedSizeDescriptorSetBuilderArray,
        FixedSizeDescriptorSetsPool, PersistentDescriptorSet, PersistentDescriptorSetBuf,
        PersistentDescriptorSetBufView, PersistentDescriptorSetBuildError,
        PersistentDescriptorSetBuilder, PersistentDescriptorSetBuilderArray,
        PersistentDescriptorSetError, PersistentDescriptorSetImg, PersistentDescriptorSetSampler,
        StdDescriptorPool, StdDescriptorPoolAlloc, UnsafeDescriptorPool,
        UnsafeDescriptorPoolAllocIter, UnsafeDescriptorSet, UnsafeDescriptorSetLayout,
    },
    descriptor::pipeline_layout::{
        EmptyPipelineDesc, PipelineLayout, PipelineLayoutCreationError, PipelineLayoutDesc,
        PipelineLayoutDescPcRange, PipelineLayoutDescUnion, PipelineLayoutLimitsError,
        PipelineLayoutNotSupersetError, PipelineLayoutPushConstantsCompatible,
        PipelineLayoutSetsCompatible, PipelineLayoutSuperset, PipelineLayoutSys,
        RuntimePipelineDesc, RuntimePipelineDescError,
    },
    descriptor::{DescriptorSet, PipelineLayoutAbstract},
    device::{
        Device, DeviceCreationError, DeviceExtensions, DeviceOwned, Queue, QueuesIter,
        RawDeviceExtensions,
    },
    format::{
        AcceptsPixels, ClearValue, ClearValuesTuple, Format, FormatDesc, FormatTy,
        PossibleCompressedFormatDesc, PossibleDepthFormatDesc, PossibleDepthStencilFormatDesc,
        PossibleFloatFormatDesc, PossibleFloatOrCompressedFormatDesc, PossibleSintFormatDesc,
        PossibleStencilFormatDesc, PossibleUintFormatDesc, StrongStorage,
    },
    framebuffer::{
        AttachmentDescription, AttachmentsList, Framebuffer, FramebufferAbstract,
        FramebufferBuilder, FramebufferCreationError, FramebufferSys,
        IncompatibleRenderPassAttachmentError, LoadOp, PassDependencyDescription, PassDescription,
        RenderPass, RenderPassAbstract, RenderPassCompatible, RenderPassCreationError,
        RenderPassDesc, RenderPassDescAttachments, RenderPassDescClearValues,
        RenderPassDescDependencies, RenderPassDescSubpasses, RenderPassSubpassInterface,
        RenderPassSys, StoreOp, Subpass, SubpassContents,
    },
    image::immutable::ImmutableImageInitialization,
    image::traits::{
        AttachmentImageView, ImageAccessFromUndefinedLayout, ImageClearValue, ImageContent,
    },
    image::{
        AttachmentImage, ImageAccess, ImageCreationError, ImageDimensions, ImageInner, ImageLayout,
        ImageUsage, ImageViewAccess, ImmutableImage, MipmapsCount, StorageImage, SwapchainImage,
    },
    instance::{
        ApplicationInfo, Instance, InstanceCreationError, InstanceExtensions, Limits,
        PhysicalDevice, PhysicalDeviceType, PhysicalDevicesIter, QueueFamiliesIter, QueueFamily,
        RawInstanceExtensions, Version,
    },
    pipeline::blend::{AttachmentBlend, AttachmentsBlend, Blend, BlendFactor, BlendOp, LogicOp},
    pipeline::depth_stencil::{DepthBounds, DepthStencil, Stencil, StencilOp},
    pipeline::vertex::{
        AttributeInfo, BufferlessDefinition, BufferlessVertices, IncompatibleVertexDefinitionError,
        OneVertexOneInstanceDefinition, SingleBufferDefinition, SingleInstanceBufferDefinition,
        TwoBuffersDefinition, Vertex, VertexDefinition, VertexMember, VertexMemberInfo,
        VertexMemberTy, VertexSource,
    },
    pipeline::viewport::{Scissor, Viewport, ViewportsState},
    pipeline::{
        ComputePipeline, ComputePipelineAbstract, ComputePipelineCreationError, ComputePipelineSys,
        GraphicsPipeline, GraphicsPipelineAbstract, GraphicsPipelineBuilder,
        GraphicsPipelineCreationError, GraphicsPipelineSys,
    },
    query::{
        OcclusionQueriesPool, QueryPipelineStatisticFlags, QueryPoolCreationError, QueryType,
        UnsafeQueriesRange, UnsafeQuery, UnsafeQueryPool,
    },
    sampler::{
        Compare as DepthStencilCompare, Sampler, SamplerAddressMode, SamplerCreationError,
        UnnormalizedSamplerAddressMode,
    },
    swapchain::{Surface, Swapchain, SwapchainAcquireFuture, SwapchainCreationError},
    sync::{
        Fence, FenceSignalFuture, GpuFuture, JoinFuture, NowFuture, Semaphore,
        SemaphoreSignalFuture,
    },
};
pub use vulkano_shaders as shaders;
pub use vulkano_win as win;

use crate::vk;
use crate::vk::instance::debug::{
    DebugCallback, DebugCallbackCreationError, Message, MessageTypes,
};
use crate::vk::instance::loader::{FunctionPointers, Loader};
use std::borrow::Cow;
use std::ops::{self, Range};
use std::panic::RefUnwindSafe;
use std::sync::Arc;

#[cfg(all(target_os = "macos", not(test)))]
use moltenvk_deps;
#[cfg(all(target_os = "macos", not(test)))]
use vulkano::instance::loader::DynamicLibraryLoader;

/// The default application name used with the default `ApplicationInfo`.
pub const DEFAULT_APPLICATION_NAME: &'static str = "nannou-app";

/// The default application info
pub const DEFAULT_APPLICATION_INFO: ApplicationInfo<'static> = ApplicationInfo {
    application_name: Some(Cow::Borrowed(DEFAULT_APPLICATION_NAME)),
    application_version: None,
    engine_name: None,
    engine_version: None,
};

/// The **FramebufferObject** or **Fbo** type for easy management of a framebuffer.
///
/// Creating and maintaining a framebuffer and ensuring it is up to date with the given renderpass
/// and images can be a tedious task that requires a lot of boilerplate code. This type simplifies
/// the process with a single `update` method that creates or recreates the framebuffer if any of
/// the following conditions are met:
/// - The `update` method is called for the first time.
/// - The given render pass is different to that which was used to create the existing framebuffer.
/// - The dimensions of the framebuffer don't match the dimensions of the images.
#[derive(Default)]
pub struct FramebufferObject {
    framebuffer: Option<Arc<FramebufferAbstract + Send + Sync>>,
}

/// Shorthand for the **FramebufferObject** type.
pub type Fbo = FramebufferObject;

/// Shorthand for the builder result type expected by the function given to `Fbo::update`.
pub type FramebufferBuilderResult<R, A> =
    Result<FramebufferBuilder<R, A>, FramebufferCreationError>;

/// A builder struct that makes the process of building an instance more modular.
#[derive(Default)]
pub struct InstanceBuilder {
    pub app_info: Option<ApplicationInfo<'static>>,
    pub extensions: Option<InstanceExtensions>,
    pub layers: Vec<String>,
    pub loader: Option<FunctionPointers<Box<dyn Loader + Send + Sync>>>,
}

/// A builder struct that makes the process of building a debug callback more modular.
#[derive(Default)]
pub struct DebugCallbackBuilder {
    pub message_types: Option<MessageTypes>,
    pub user_callback: Option<BoxedUserCallback>,
}

/// A builder struct that makes the process of building a **Sampler** more modular.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SamplerBuilder {
    pub mag_filter: Option<vk::sampler::Filter>,
    pub min_filter: Option<vk::sampler::Filter>,
    pub mipmap_mode: Option<vk::sampler::MipmapMode>,
    pub address_u: Option<SamplerAddressMode>,
    pub address_v: Option<SamplerAddressMode>,
    pub address_w: Option<SamplerAddressMode>,
    pub mip_lod_bias: Option<f32>,
    pub max_anisotropy: Option<f32>,
    pub min_lod: Option<f32>,
    pub max_lod: Option<f32>,
}

/// A builder struct that makes the process of building a **Viewport** more modular.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ViewportBuilder {
    pub origin: Option<[f32; 2]>,
    pub depth_range: Option<Range<f32>>,
}

/// A simple trait that extends the `DynamicState` type with builder methods.
///
/// This fills a similar role to the `SamplerBuilder` and `ViewportBuilder` structs, but seeing as
/// `DynamicState` already uses `Option`s for all its fields, a trait allows for a simpler
/// approach.
///
/// This trait is included within the `nannou::prelude` for ease of access.
pub trait DynamicStateBuilder {
    fn line_width(self, line_width: f32) -> Self;
    fn viewports(self, viewports: Vec<Viewport>) -> Self;
    fn scissors(self, scissors: Vec<Scissor>) -> Self;
}

impl DynamicStateBuilder for DynamicState {
    fn line_width(mut self, line_width: f32) -> Self {
        self.line_width = Some(line_width);
        self
    }
    fn viewports(mut self, viewports: Vec<Viewport>) -> Self {
        self.viewports = Some(viewports);
        self
    }
    fn scissors(mut self, scissors: Vec<Scissor>) -> Self {
        self.scissors = Some(scissors);
        self
    }
}

// The user vulkan debug callback allocated on the heap to avoid complicated type params.
type BoxedUserCallback = Box<Fn(&Message) + 'static + Send + RefUnwindSafe>;

impl FramebufferObject {
    /// Access the inner framebuffer trait object.
    pub fn inner(&self) -> &Option<Arc<FramebufferAbstract + Send + Sync>> {
        &self.framebuffer
    }

    /// Ensure the framebuffer is up to date with the given dimensions and render pass.
    pub fn update<R, F, A>(
        &mut self,
        render_pass: R,
        dimensions: [u32; 3],
        builder: F,
    ) -> Result<(), FramebufferCreationError>
    where
        R: 'static + vk::framebuffer::RenderPassAbstract + Send + Sync,
        F: FnOnce(FramebufferBuilder<R, ()>) -> FramebufferBuilderResult<R, A>,
        A: 'static + vk::framebuffer::AttachmentsList + Send + Sync,
    {
        let needs_creation = self.framebuffer.is_none()
            || !self.dimensions_match(dimensions)
            || !self.render_passes_match(&render_pass);
        if needs_creation {
            let builder = builder(Framebuffer::start(render_pass))?;
            let fb = builder.build()?;
            self.framebuffer = Some(Arc::new(fb));
        }
        Ok(())
    }

    /// Expects that there is a inner framebuffer object instantiated and returns it.
    ///
    /// **panic!**s if the `update` method has not yet been called.
    ///
    /// This method is shorthand for `fbo.as_ref().expect("inner framebuffer was None").clone()`.
    pub fn expect_inner(&self) -> Arc<FramebufferAbstract + Send + Sync> {
        self.framebuffer
            .as_ref()
            .expect("inner framebuffer was `None` - you must call the `update` method first")
            .clone()
    }

    /// Whether or not the given renderpass matches the framebuffer's render pass.
    pub fn render_passes_match<R>(&self, render_pass: R) -> bool
    where
        R: vk::framebuffer::RenderPassAbstract,
    {
        self.framebuffer
            .as_ref()
            .map(|fb| vk::framebuffer::RenderPassAbstract::inner(fb).internal_object())
            .map(|obj| obj == render_pass.inner().internal_object())
            .unwrap_or(false)
    }

    /// Whether or not the given dimensions match the current dimensions.
    pub fn dimensions_match(&self, dimensions: [u32; 3]) -> bool {
        self.framebuffer
            .as_ref()
            .map(|fb| fb.dimensions() == dimensions)
            .unwrap_or(false)
    }
}

impl InstanceBuilder {
    /// Begin building a vulkano instance.
    pub fn new() -> Self {
        Default::default()
    }

    /// Specify the application info with which the instance should be created.
    pub fn app_info(mut self, app_info: ApplicationInfo<'static>) -> Self {
        self.app_info = Some(app_info);
        self
    }

    /// Specify the exact extensions to enable for the instance.
    pub fn extensions(mut self, extensions: InstanceExtensions) -> Self {
        self.extensions = Some(extensions);
        self
    }

    /// Add the given extensions to the set of existing extensions within the builder.
    ///
    /// Unlike the `extensions` method, this does not disable pre-existing extensions.
    pub fn add_extensions(mut self, ext: InstanceExtensions) -> Self {
        self.extensions = self
            .extensions
            .take()
            .map(|mut e| {
                // TODO: Remove this when `InstanceExtensions::union` gets merged.
                e.khr_surface |= ext.khr_surface;
                e.khr_display |= ext.khr_display;
                e.khr_xlib_surface |= ext.khr_xlib_surface;
                e.khr_xcb_surface |= ext.khr_xcb_surface;
                e.khr_wayland_surface |= ext.khr_wayland_surface;
                e.khr_android_surface |= ext.khr_android_surface;
                e.khr_win32_surface |= ext.khr_win32_surface;
                e.ext_debug_report |= ext.ext_debug_report;
                e.mvk_ios_surface |= ext.mvk_ios_surface;
                e.mvk_macos_surface |= ext.mvk_macos_surface;
                e.mvk_moltenvk |= ext.mvk_moltenvk;
                e.nn_vi_surface |= ext.nn_vi_surface;
                e.ext_swapchain_colorspace |= ext.ext_swapchain_colorspace;
                e.khr_get_physical_device_properties2 |= ext.khr_get_physical_device_properties2;
                e
            })
            .or(Some(ext));
        self
    }

    /// Specify the exact layers to enable for the instance.
    pub fn layers<L>(mut self, layers: L) -> Self
    where
        L: IntoIterator,
        L::Item: Into<String>,
    {
        self.layers = layers.into_iter().map(Into::into).collect();
        self
    }

    /// Extend the existing list of layers with the given layers.
    pub fn add_layers<L>(mut self, layers: L) -> Self
    where
        L: IntoIterator,
        L::Item: Into<String>,
    {
        self.layers.extend(layers.into_iter().map(Into::into));
        self
    }

    /// Add custom vulkan loader
    pub fn add_loader(mut self, loader: FunctionPointers<Box<dyn Loader + Send + Sync>>) -> Self {
        self.loader = Some(loader);
        self
    }

    /// Build the vulkan instance with the existing parameters.
    pub fn build(self) -> Result<Arc<Instance>, InstanceCreationError> {
        let InstanceBuilder {
            app_info,
            extensions,
            layers,
            loader,
        } = self;

        let app_info = app_info.unwrap_or(DEFAULT_APPLICATION_INFO);
        let extensions = extensions.unwrap_or_else(required_windowing_extensions);
        let layers = layers.iter().map(|s| &s[..]);
        match loader {
            None => Instance::new(Some(&app_info), &extensions, layers),
            Some(loader) => Instance::with_loader(loader, Some(&app_info), &extensions, layers),
        }
    }
}

impl DebugCallbackBuilder {
    /// Begin building a vulkan debug callback.
    pub fn new() -> Self {
        Default::default()
    }

    /// The message types to be emitted to the debug callback.
    ///
    /// If unspecified, nannou will use `MessageTypes::errors_and_warnings`.
    pub fn message_types(mut self, msg_tys: MessageTypes) -> Self {
        self.message_types = Some(msg_tys);
        self
    }

    /// The function that will be called for handling messages.
    ///
    /// If unspecified, nannou will use a function that prints to `stdout` and `stderr`.
    pub fn user_callback<F>(mut self, cb: F) -> Self
    where
        F: Fn(&Message) + 'static + Send + RefUnwindSafe,
    {
        self.user_callback = Some(Box::new(cb) as Box<_>);
        self
    }

    /// Build the debug callback builder for the given vulkan instance.
    pub fn build(
        self,
        instance: &Arc<Instance>,
    ) -> Result<DebugCallback, DebugCallbackCreationError> {
        let DebugCallbackBuilder {
            message_types,
            user_callback,
        } = self;
        let message_types = message_types.unwrap_or_else(|| MessageTypes {
            error: true,
            warning: true,
            performance_warning: true,
            information: true,
            debug: true,
        });
        let user_callback = move |msg: &Message| {
            match user_callback {
                Some(ref cb) => (**cb)(msg),
                None => {
                    let ty = if msg.ty.error {
                        "error"
                    } else if msg.ty.warning {
                        "warning"
                    } else if msg.ty.performance_warning {
                        "performance_warning"
                    } else if msg.ty.information {
                        "information"
                    } else if msg.ty.debug {
                        "debug"
                    } else {
                        println!("[vulkan] <unknown message type>");
                        return;
                    };
                    println!("[vulkan] {} {}: {}", msg.layer_prefix, ty, msg.description);
                }
            };
        };
        DebugCallback::new(instance, message_types, user_callback)
    }
}

impl SamplerBuilder {
    pub const DEFAULT_MAG_FILTER: vk::sampler::Filter = vk::sampler::Filter::Linear;
    pub const DEFAULT_MIN_FILTER: vk::sampler::Filter = vk::sampler::Filter::Linear;
    pub const DEFAULT_MIPMAP_MODE: vk::sampler::MipmapMode = vk::sampler::MipmapMode::Nearest;
    pub const DEFAULT_ADDRESS_U: SamplerAddressMode = SamplerAddressMode::ClampToEdge;
    pub const DEFAULT_ADDRESS_V: SamplerAddressMode = SamplerAddressMode::ClampToEdge;
    pub const DEFAULT_ADDRESS_W: SamplerAddressMode = SamplerAddressMode::ClampToEdge;
    pub const DEFAULT_MIP_LOD_BIAS: f32 = 0.0;
    pub const DEFAULT_MAX_ANISOTROPY: f32 = 1.0;
    pub const DEFAULT_MIN_LOD: f32 = 0.0;
    pub const DEFAULT_MAX_LOD: f32 = 1.0;

    /// Begin building a new vulkan **Sampler**.
    pub fn new() -> Self {
        Self::default()
    }

    /// How the implementation should sample from the image when it is respectively larger than the
    /// original.
    pub fn mag_filter(mut self, filter: vk::sampler::Filter) -> Self {
        self.mag_filter = Some(filter);
        self
    }

    /// How the implementation should sample from the image when it is respectively smaller than
    /// the original.
    pub fn min_filter(mut self, filter: vk::sampler::Filter) -> Self {
        self.min_filter = Some(filter);
        self
    }

    /// How the implementation should choose which mipmap to use.
    pub fn mipmap_mode(mut self, mode: vk::sampler::MipmapMode) -> Self {
        self.mipmap_mode = Some(mode);
        self
    }

    /// How the implementation should behave when sampling outside of the texture coordinates range
    /// [0.0, 1.0].
    pub fn address_u(mut self, mode: SamplerAddressMode) -> Self {
        self.address_u = Some(mode);
        self
    }

    /// How the implementation should behave when sampling outside of the texture coordinates range
    /// [0.0, 1.0].
    pub fn address_v(mut self, mode: SamplerAddressMode) -> Self {
        self.address_v = Some(mode);
        self
    }

    /// How the implementation should behave when sampling outside of the texture coordinates range
    /// [0.0, 1.0].
    pub fn address_w(mut self, mode: SamplerAddressMode) -> Self {
        self.address_w = Some(mode);
        self
    }

    /// Level of detail bias.
    pub fn mip_lod_bias(mut self, bias: f32) -> Self {
        self.mip_lod_bias = Some(bias);
        self
    }

    /// Must be greater than oro equal to 1.0.
    ///
    /// If greater than 1.0, the implementation will use anisotropic filtering. Using a value
    /// greater than 1.0 requires the sampler_anisotropy feature to be enabled when creating the
    /// device.
    pub fn max_anisotropy(mut self, max: f32) -> Self {
        self.max_anisotropy = Some(max);
        self
    }

    /// The minimum mipmap level to use.
    pub fn min_lod(mut self, lod: f32) -> Self {
        self.min_lod = Some(lod);
        self
    }

    /// The maximum mipmap level to use.
    pub fn max_lod(mut self, lod: f32) -> Self {
        self.max_lod = Some(lod);
        self
    }

    /// Build the sampler with the givenn behaviour.
    pub fn build(self, device: Arc<Device>) -> Result<Arc<Sampler>, SamplerCreationError> {
        let SamplerBuilder {
            mag_filter,
            min_filter,
            mipmap_mode,
            address_u,
            address_v,
            address_w,
            mip_lod_bias,
            max_anisotropy,
            min_lod,
            max_lod,
        } = self;
        Sampler::new(
            device,
            mag_filter.unwrap_or(Self::DEFAULT_MAG_FILTER),
            min_filter.unwrap_or(Self::DEFAULT_MIN_FILTER),
            mipmap_mode.unwrap_or(Self::DEFAULT_MIPMAP_MODE),
            address_u.unwrap_or(Self::DEFAULT_ADDRESS_U),
            address_v.unwrap_or(Self::DEFAULT_ADDRESS_V),
            address_w.unwrap_or(Self::DEFAULT_ADDRESS_W),
            mip_lod_bias.unwrap_or(Self::DEFAULT_MIP_LOD_BIAS),
            max_anisotropy.unwrap_or(Self::DEFAULT_MAX_ANISOTROPY),
            min_lod.unwrap_or(Self::DEFAULT_MIN_LOD),
            max_lod.unwrap_or(Self::DEFAULT_MAX_LOD),
        )
    }
}

impl ViewportBuilder {
    pub const DEFAULT_ORIGIN: [f32; 2] = [0.0; 2];
    pub const DEFAULT_DEPTH_RANGE: Range<f32> = 0.0..1.0;

    /// Begin building a new **Viewport**.
    pub fn new() -> Self {
        Self::default()
    }

    /// Coordinates in pixels of the top-left hand corner of the viewport.
    ///
    /// By default this is `ViewportDefault::DEFAULT_ORIGIN`.
    pub fn origin(mut self, origin: [f32; 2]) -> Self {
        self.origin = Some(origin);
        self
    }

    /// Minimum and maximum values of the depth.
    ///
    /// The values `0.0` to `1.0` of each vertex's Z coordinate will be mapped to this
    /// `depth_range` before being compared to the existing depth value.
    ///
    /// This is equivalents to `glDepthRange` in OpenGL, except that OpenGL uses the Z coordinate
    /// range from `-1.0` to `1.0` instead.
    ///
    /// By default this is `ViewportDefault::DEFAULT_DEPTH_RANGE`.
    pub fn depth_range(mut self, range: Range<f32>) -> Self {
        self.depth_range = Some(range);
        self
    }

    /// Construct the viewport with its dimensions in pixels.
    pub fn build(self, dimensions: [f32; 2]) -> Viewport {
        let ViewportBuilder {
            origin,
            depth_range,
        } = self;
        Viewport {
            origin: origin.unwrap_or(Self::DEFAULT_ORIGIN),
            depth_range: depth_range.unwrap_or(Self::DEFAULT_DEPTH_RANGE),
            dimensions,
        }
    }
}

impl ops::Deref for FramebufferObject {
    type Target = Option<Arc<FramebufferAbstract + Send + Sync>>;
    fn deref(&self) -> &Self::Target {
        &self.framebuffer
    }
}

/// The default set of required extensions used by Nannou.
///
/// This is the same as calling `vk::win::required_extensions()`.
pub fn required_windowing_extensions() -> InstanceExtensions {
    vulkano_win::required_extensions()
}

/// Whether or not the format is sRGB.
pub fn format_is_srgb(format: Format) -> bool {
    use crate::vk::format::Format::*;
    match format {
        R8Srgb
        | R8G8Srgb
        | R8G8B8Srgb
        | B8G8R8Srgb
        | R8G8B8A8Srgb
        | B8G8R8A8Srgb
        | A8B8G8R8SrgbPack32
        | BC1_RGBSrgbBlock
        | BC1_RGBASrgbBlock
        | BC2SrgbBlock
        | BC3SrgbBlock
        | BC7SrgbBlock
        | ETC2_R8G8B8SrgbBlock
        | ETC2_R8G8B8A1SrgbBlock
        | ETC2_R8G8B8A8SrgbBlock
        | ASTC_4x4SrgbBlock
        | ASTC_5x4SrgbBlock
        | ASTC_5x5SrgbBlock
        | ASTC_6x5SrgbBlock
        | ASTC_6x6SrgbBlock
        | ASTC_8x5SrgbBlock
        | ASTC_8x6SrgbBlock
        | ASTC_8x8SrgbBlock
        | ASTC_10x5SrgbBlock
        | ASTC_10x6SrgbBlock
        | ASTC_10x8SrgbBlock
        | ASTC_10x10SrgbBlock
        | ASTC_12x10SrgbBlock
        | ASTC_12x12SrgbBlock => true,
        _ => false,
    }
}

/// Given some target MSAA samples, limit it by the capabilities of the given `physical_device`.
///
/// This is useful for attempting a specific multisampling sample count but falling back to a
/// supported count in the case that the desired count is unsupported.
///
/// Specifically, this function limits the given `target_msaa_samples` to the minimum of the color
/// and depth sample count limits.
pub fn msaa_samples_limited(physical_device: &PhysicalDevice, target_msaa_samples: u32) -> u32 {
    let color_limit = physical_device.limits().framebuffer_color_sample_counts();
    let depth_limit = physical_device.limits().framebuffer_depth_sample_counts();
    let msaa_limit = std::cmp::min(color_limit, depth_limit);
    std::cmp::min(msaa_limit, target_msaa_samples)
}

#[cfg(all(target_os = "macos", not(test)))]
pub fn check_moltenvk(
    vulkan_builder: InstanceBuilder,
    settings: Option<moltenvk_deps::Install>,
) -> InstanceBuilder {
    let settings = match settings {
        Some(s) => s,
        None => Default::default(),
    };
    let path = match moltenvk_deps::check_or_install(settings) {
        Err(moltenvk_deps::Error::ResetEnvVars(p)) => Some(p),
        Err(moltenvk_deps::Error::NonDefaultDir) => None,
        Err(moltenvk_deps::Error::ChoseNotToInstall) => {
            panic!("Moltenvk is required for Nannou on MacOS")
        }
        Err(e) => panic!("Moltenvk installation failed {:?}", e),
        Ok(p) => Some(p),
    };
    let loader = path.map(|p| unsafe { DynamicLibraryLoader::new(p) });
    match loader {
        Some(Ok(l)) => {
            let loader: FunctionPointers<Box<(dyn Loader + Send + Sync + 'static)>> =
                FunctionPointers::new(Box::new(l));
            let required_extensions = required_extensions_with_loader(&loader);
            vulkan_builder
                .extensions(required_extensions)
                .add_loader(loader)
        }
        _ => vulkan_builder.extensions(required_windowing_extensions()),
    }
}

pub fn required_extensions_with_loader<L>(ptrs: &FunctionPointers<L>) -> InstanceExtensions
where
    L: Loader,
{
    let ideal = InstanceExtensions {
        khr_surface: true,
        khr_xlib_surface: true,
        khr_xcb_surface: true,
        khr_wayland_surface: true,
        khr_android_surface: true,
        khr_win32_surface: true,
        mvk_ios_surface: true,
        mvk_macos_surface: true,
        ..InstanceExtensions::none()
    };

    match InstanceExtensions::supported_by_core_with_loader(ptrs) {
        Ok(supported) => supported.intersection(&ideal),
        Err(_) => InstanceExtensions::none(),
    }
}