logo
   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
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
// Copyright (c) 2021 The vulkano developers
// 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. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

//! Image views.
//!
//! This module contains types related to image views. An image view wraps around
//! an image and describes how the GPU should interpret the data. It is needed when an image is
//! to be used in a shader descriptor or as a framebuffer attachment.

use crate::device::physical::FormatFeatures;
use crate::device::{Device, DeviceOwned};
use crate::format::{ChromaSampling, Format};
use crate::image::{
    ImageAccess, ImageAspects, ImageDimensions, ImageTiling, ImageType, ImageUsage, SampleCount,
};
use crate::sampler::ycbcr::SamplerYcbcrConversion;
use crate::sampler::ComponentMapping;
use crate::OomError;
use crate::VulkanObject;
use crate::{check_errors, Error};
use std::error;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem::MaybeUninit;
use std::ops::Range;
use std::ptr;
use std::sync::Arc;

/// A wrapper around an image that makes it available to shaders or framebuffers.
pub struct ImageView<I>
where
    I: ImageAccess,
{
    handle: ash::vk::ImageView,
    image: Arc<I>,

    array_layers: Range<u32>,
    aspects: ImageAspects,
    component_mapping: ComponentMapping,
    format: Format,
    format_features: FormatFeatures,
    mip_levels: Range<u32>,
    sampler_ycbcr_conversion: Option<Arc<SamplerYcbcrConversion>>,
    ty: ImageViewType,
    usage: ImageUsage,

    filter_cubic: bool,
    filter_cubic_minmax: bool,
}

impl<I> ImageView<I>
where
    I: ImageAccess,
{
    /// Creates a default `ImageView`. Equivalent to `ImageView::start(image).build()`.
    #[inline]
    pub fn new(image: Arc<I>) -> Result<Arc<ImageView<I>>, ImageViewCreationError> {
        Self::start(image).build()
    }

    /// Begins building an `ImageView`.
    pub fn start(image: Arc<I>) -> ImageViewBuilder<I> {
        let array_layers = 0..image.dimensions().array_layers();
        let aspects = {
            let aspects = image.format().aspects();
            if aspects.depth || aspects.stencil {
                debug_assert!(!aspects.color);
                ImageAspects {
                    depth: aspects.depth,
                    stencil: aspects.stencil,
                    ..Default::default()
                }
            } else {
                debug_assert!(aspects.color);
                ImageAspects {
                    color: true,
                    ..Default::default()
                }
            }
        };
        let format = image.format();
        let mip_levels = 0..image.mip_levels();
        let ty = match image.dimensions() {
            ImageDimensions::Dim1d {
                array_layers: 1, ..
            } => ImageViewType::Dim1d,
            ImageDimensions::Dim1d { .. } => ImageViewType::Dim1dArray,
            ImageDimensions::Dim2d {
                array_layers: 1, ..
            } => ImageViewType::Dim2d,
            ImageDimensions::Dim2d { .. } => ImageViewType::Dim2dArray,
            ImageDimensions::Dim3d { .. } => ImageViewType::Dim3d,
        };

        ImageViewBuilder {
            image,

            array_layers,
            aspects,
            component_mapping: ComponentMapping::default(),
            format,
            mip_levels,
            sampler_ycbcr_conversion: None,
            ty,
        }
    }

    /// Returns the wrapped image that this image view was created from.
    pub fn image(&self) -> &Arc<I> {
        &self.image
    }
}

unsafe impl<I> VulkanObject for ImageView<I>
where
    I: ImageAccess,
{
    type Object = ash::vk::ImageView;

    #[inline]
    fn internal_object(&self) -> ash::vk::ImageView {
        self.handle
    }
}

unsafe impl<I> DeviceOwned for ImageView<I>
where
    I: ImageAccess,
{
    fn device(&self) -> &Arc<Device> {
        self.image.inner().image.device()
    }
}

impl<I> fmt::Debug for ImageView<I>
where
    I: ImageAccess,
{
    #[inline]
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "<Vulkan image view {:?}>", self.handle)
    }
}

impl<I> Drop for ImageView<I>
where
    I: ImageAccess,
{
    #[inline]
    fn drop(&mut self) {
        unsafe {
            let device = self.device();
            let fns = device.fns();
            fns.v1_0
                .destroy_image_view(device.internal_object(), self.handle, ptr::null());
        }
    }
}

impl<I> PartialEq for ImageView<I>
where
    I: ImageAccess,
{
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.handle == other.handle && self.device() == other.device()
    }
}

impl<I> Eq for ImageView<I> where I: ImageAccess {}

impl<I> Hash for ImageView<I>
where
    I: ImageAccess,
{
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.handle.hash(state);
        self.device().hash(state);
    }
}

#[derive(Debug)]
pub struct ImageViewBuilder<I> {
    image: Arc<I>,

    array_layers: Range<u32>,
    aspects: ImageAspects,
    component_mapping: ComponentMapping,
    format: Format,
    mip_levels: Range<u32>,
    sampler_ycbcr_conversion: Option<Arc<SamplerYcbcrConversion>>,
    ty: ImageViewType,
}

impl<I> ImageViewBuilder<I>
where
    I: ImageAccess,
{
    /// Builds the `ImageView`.
    pub fn build(self) -> Result<Arc<ImageView<I>>, ImageViewCreationError> {
        let Self {
            array_layers,
            aspects,
            component_mapping,
            format,
            mip_levels,
            ty,
            sampler_ycbcr_conversion,
            image,
        } = self;

        let image_inner = image.inner().image;
        let level_count = mip_levels.end - mip_levels.start;
        let layer_count = array_layers.end - array_layers.start;

        // Get format features
        let format_features = {
            let format_features = if format != image_inner.format() {
                let format_properties = image_inner
                    .device()
                    .physical_device()
                    .format_properties(self.format);

                match image_inner.tiling() {
                    ImageTiling::Optimal => format_properties.optimal_tiling_features,
                    ImageTiling::Linear => format_properties.linear_tiling_features,
                }
            } else {
                *image_inner.format_features()
            };

            // Per https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap12.html#resources-image-view-format-features
            if image_inner
                .device()
                .enabled_extensions()
                .khr_format_feature_flags2
            {
                format_features
            } else {
                let is_without_format = format.shader_storage_image_without_format();

                FormatFeatures {
                    sampled_image_depth_comparison: format.type_color().is_none()
                        && format_features.sampled_image,
                    storage_read_without_format: is_without_format
                        && image_inner
                            .device()
                            .enabled_features()
                            .shader_storage_image_read_without_format,
                    storage_write_without_format: is_without_format
                        && image_inner
                            .device()
                            .enabled_features()
                            .shader_storage_image_write_without_format,
                    ..format_features
                }
            }
        };

        // No VUID apparently, but this seems like something we want to check?
        if !image_inner.format().aspects().contains(&aspects) {
            return Err(ImageViewCreationError::ImageAspectsNotCompatible {
                aspects,
                image_aspects: image_inner.format().aspects(),
            });
        }

        // VUID-VkImageViewCreateInfo-None-02273
        if format_features == FormatFeatures::default() {
            return Err(ImageViewCreationError::FormatNotSupported);
        }

        // Get usage
        // Can be different from image usage, see
        // https://khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkImageViewCreateInfo.html#_description
        let usage = *image_inner.usage();

        // Check for compatibility with the image
        let image_type = image.dimensions().image_type();

        // VUID-VkImageViewCreateInfo-subResourceRange-01021
        if !ty.is_compatible_with(image_type) {
            return Err(ImageViewCreationError::ImageTypeNotCompatible);
        }

        // VUID-VkImageViewCreateInfo-image-01003
        if (ty == ImageViewType::Cube || ty == ImageViewType::CubeArray)
            && !image_inner.flags().cube_compatible
        {
            return Err(ImageViewCreationError::ImageNotCubeCompatible);
        }

        // VUID-VkImageViewCreateInfo-viewType-01004
        if ty == ImageViewType::CubeArray
            && !image_inner.device().enabled_features().image_cube_array
        {
            return Err(ImageViewCreationError::FeatureNotEnabled {
                feature: "image_cube_array",
                reason: "the `CubeArray` view type was requested",
            });
        }

        // VUID-VkImageViewCreateInfo-subresourceRange-01718
        if mip_levels.end > image_inner.mip_levels() {
            return Err(ImageViewCreationError::MipLevelsOutOfRange {
                range_end: mip_levels.end,
                max: image_inner.mip_levels(),
            });
        }

        if image_type == ImageType::Dim3d
            && (ty == ImageViewType::Dim2d || ty == ImageViewType::Dim2dArray)
        {
            // VUID-VkImageViewCreateInfo-image-01005
            if !image_inner.flags().array_2d_compatible {
                return Err(ImageViewCreationError::ImageNotArray2dCompatible);
            }

            // VUID-VkImageViewCreateInfo-image-04970
            if level_count != 1 {
                return Err(ImageViewCreationError::Array2dCompatibleMultipleMipLevels);
            }

            // VUID-VkImageViewCreateInfo-image-02724
            // VUID-VkImageViewCreateInfo-subresourceRange-02725
            // We're using the depth dimension as array layers, but because of mip scaling, the
            // depth, and therefore number of layers available, shrinks as the mip level gets
            // higher.
            let max = image_inner
                .dimensions()
                .mip_level_dimensions(mip_levels.start)
                .unwrap()
                .depth();
            if array_layers.end > max {
                return Err(ImageViewCreationError::ArrayLayersOutOfRange {
                    range_end: array_layers.end,
                    max,
                });
            }
        } else {
            // VUID-VkImageViewCreateInfo-image-01482
            // VUID-VkImageViewCreateInfo-subresourceRange-01483
            if array_layers.end > image_inner.dimensions().array_layers() {
                return Err(ImageViewCreationError::ArrayLayersOutOfRange {
                    range_end: array_layers.end,
                    max: image_inner.dimensions().array_layers(),
                });
            }
        }

        // VUID-VkImageViewCreateInfo-image-04972
        if image_inner.samples() != SampleCount::Sample1
            && !(ty == ImageViewType::Dim2d || ty == ImageViewType::Dim2dArray)
        {
            return Err(ImageViewCreationError::MultisamplingNot2d);
        }

        /* Check usage requirements */

        // VUID-VkImageViewCreateInfo-image-04441
        if !(image_inner.usage().sampled
            || image_inner.usage().storage
            || image_inner.usage().color_attachment
            || image_inner.usage().depth_stencil_attachment
            || image_inner.usage().input_attachment
            || image_inner.usage().transient_attachment)
        {
            return Err(ImageViewCreationError::InvalidImageUsage);
        }

        // VUID-VkImageViewCreateInfo-usage-02274
        if usage.sampled && !format_features.sampled_image {
            return Err(ImageViewCreationError::FormatUsageNotSupported { usage: "sampled" });
        }

        // VUID-VkImageViewCreateInfo-usage-02275
        if usage.storage && !format_features.storage_image {
            return Err(ImageViewCreationError::FormatUsageNotSupported { usage: "storage" });
        }

        // VUID-VkImageViewCreateInfo-usage-02276
        if usage.color_attachment && !format_features.color_attachment {
            return Err(ImageViewCreationError::FormatUsageNotSupported {
                usage: "color_attachment",
            });
        }

        // VUID-VkImageViewCreateInfo-usage-02277
        if usage.depth_stencil_attachment && !format_features.depth_stencil_attachment {
            return Err(ImageViewCreationError::FormatUsageNotSupported {
                usage: "depth_stencil_attachment",
            });
        }

        // VUID-VkImageViewCreateInfo-usage-02652
        if usage.input_attachment
            && !(format_features.color_attachment || format_features.depth_stencil_attachment)
        {
            return Err(ImageViewCreationError::FormatUsageNotSupported {
                usage: "input_attachment",
            });
        }

        /* Check flags requirements */

        if image_inner.flags().block_texel_view_compatible {
            // VUID-VkImageViewCreateInfo-image-01583
            if !(format.compatibility() == image_inner.format().compatibility()
                || format.block_size() == image_inner.format().block_size())
            {
                return Err(ImageViewCreationError::FormatNotCompatible);
            }

            // VUID-VkImageViewCreateInfo-image-01584
            if layer_count != 1 {
                return Err(ImageViewCreationError::BlockTexelViewCompatibleMultipleArrayLayers);
            }

            // VUID-VkImageViewCreateInfo-image-01584
            if level_count != 1 {
                return Err(ImageViewCreationError::BlockTexelViewCompatibleMultipleMipLevels);
            }

            // VUID-VkImageViewCreateInfo-image-04739
            if format.compression().is_none() && ty == ImageViewType::Dim3d {
                return Err(ImageViewCreationError::BlockTexelViewCompatibleUncompressedIs3d);
            }
        }
        // VUID-VkImageViewCreateInfo-image-01761
        else if image_inner.flags().mutable_format
            && image_inner.format().planes().is_empty()
            && format.compatibility() != image_inner.format().compatibility()
        {
            return Err(ImageViewCreationError::FormatNotCompatible);
        }

        if image_inner.flags().mutable_format
            && !image_inner.format().planes().is_empty()
            && !aspects.color
        {
            let plane = if aspects.plane0 {
                0
            } else if aspects.plane1 {
                1
            } else if aspects.plane2 {
                2
            } else {
                unreachable!()
            };
            let plane_format = image_inner.format().planes()[plane];

            // VUID-VkImageViewCreateInfo-image-01586
            if format.compatibility() != plane_format.compatibility() {
                return Err(ImageViewCreationError::FormatNotCompatible);
            }
        }
        // VUID-VkImageViewCreateInfo-image-01762
        else if format != image_inner.format() {
            return Err(ImageViewCreationError::FormatNotCompatible);
        }

        // VUID-VkImageViewCreateInfo-imageViewType-04973
        if (ty == ImageViewType::Dim1d || ty == ImageViewType::Dim2d || ty == ImageViewType::Dim3d)
            && layer_count != 1
        {
            return Err(ImageViewCreationError::TypeNonArrayedMultipleArrayLayers);
        }
        // VUID-VkImageViewCreateInfo-viewType-02960
        else if ty == ImageViewType::Cube && layer_count != 6 {
            return Err(ImageViewCreationError::TypeCubeNot6ArrayLayers);
        }
        // VUID-VkImageViewCreateInfo-viewType-02961
        else if ty == ImageViewType::CubeArray && layer_count % 6 != 0 {
            return Err(ImageViewCreationError::TypeCubeArrayNotMultipleOf6ArrayLayers);
        }

        // VUID-VkImageViewCreateInfo-format-04714
        // VUID-VkImageViewCreateInfo-format-04715
        match format.ycbcr_chroma_sampling() {
            Some(ChromaSampling::Mode422) => {
                if image_inner.dimensions().width() % 2 != 0 {
                    return Err(
                        ImageViewCreationError::FormatChromaSubsamplingInvalidImageDimensions,
                    );
                }
            }
            Some(ChromaSampling::Mode420) => {
                if image_inner.dimensions().width() % 2 != 0
                    || image_inner.dimensions().height() % 2 != 0
                {
                    return Err(
                        ImageViewCreationError::FormatChromaSubsamplingInvalidImageDimensions,
                    );
                }
            }
            _ => (),
        }

        // Don't need to check features because you can't create a conversion object without the
        // feature anyway.
        let mut sampler_ycbcr_conversion_info = if let Some(conversion) = &sampler_ycbcr_conversion
        {
            assert_eq!(image_inner.device(), conversion.device());

            // VUID-VkImageViewCreateInfo-pNext-01970
            if !component_mapping.is_identity() {
                return Err(
                    ImageViewCreationError::SamplerYcbcrConversionComponentMappingNotIdentity {
                        component_mapping,
                    },
                );
            }

            Some(ash::vk::SamplerYcbcrConversionInfo {
                conversion: conversion.internal_object(),
                ..Default::default()
            })
        } else {
            // VUID-VkImageViewCreateInfo-format-06415
            if format.ycbcr_chroma_sampling().is_some() {
                return Err(
                    ImageViewCreationError::FormatRequiresSamplerYcbcrConversion { format },
                );
            }

            None
        };

        let mut create_info = ash::vk::ImageViewCreateInfo {
            flags: ash::vk::ImageViewCreateFlags::empty(),
            image: image_inner.internal_object(),
            view_type: ty.into(),
            format: format.into(),
            components: component_mapping.into(),
            subresource_range: ash::vk::ImageSubresourceRange {
                aspect_mask: aspects.into(),
                base_mip_level: mip_levels.start,
                level_count,
                base_array_layer: array_layers.start,
                layer_count,
            },
            ..Default::default()
        };

        if let Some(sampler_ycbcr_conversion_info) = sampler_ycbcr_conversion_info.as_mut() {
            sampler_ycbcr_conversion_info.p_next = create_info.p_next;
            create_info.p_next = sampler_ycbcr_conversion_info as *const _ as *const _;
        }

        let handle = unsafe {
            let fns = image_inner.device().fns();
            let mut output = MaybeUninit::uninit();
            check_errors(fns.v1_0.create_image_view(
                image_inner.device().internal_object(),
                &create_info,
                ptr::null(),
                output.as_mut_ptr(),
            ))?;
            output.assume_init()
        };

        let (filter_cubic, filter_cubic_minmax) = if let Some(properties) = image_inner
            .device()
            .physical_device()
            .image_format_properties(
                image_inner.format(),
                image_type,
                image_inner.tiling(),
                *image_inner.usage(),
                image_inner.flags(),
                None,
                Some(ty),
            )? {
            (properties.filter_cubic, properties.filter_cubic_minmax)
        } else {
            (false, false)
        };

        Ok(Arc::new(ImageView {
            handle,
            image,

            array_layers,
            aspects,
            component_mapping,
            format,
            format_features,
            mip_levels,
            sampler_ycbcr_conversion,
            ty,
            usage,

            filter_cubic,
            filter_cubic_minmax,
        }))
    }

    /// The range of array layers of the image that the view should cover.
    ///
    /// The default value is the full range of array layers present in the image.
    ///
    /// # Panics
    ///
    /// - Panics if `array_layers` is empty.
    #[inline]
    pub fn array_layers(mut self, array_layers: Range<u32>) -> Self {
        assert!(!array_layers.is_empty());
        self.array_layers = array_layers;
        self
    }

    /// The aspects of the image that the view should cover.
    ///
    /// The default value is `color` if the image is a color format, `depth` and/or `stencil` if
    /// the image is a depth/stencil format.
    ///
    /// # Panics
    ///
    /// - Panics if aspects other than `color`, `depth`, `stencil`, `plane0`, `plane1` or `plane2`
    ///   are selected.
    /// - Panics if more than one aspect is selected, unless `depth` and `stencil` are the only
    ///   aspects selected.
    #[inline]
    pub fn aspects(mut self, aspects: ImageAspects) -> Self {
        let ImageAspects {
            color,
            depth,
            stencil,
            metadata,
            plane0,
            plane1,
            plane2,
            memory_plane0,
            memory_plane1,
            memory_plane2,
        } = aspects;

        assert!(!(metadata || memory_plane0 || memory_plane1 || memory_plane2));
        assert!({
            let num_bits = color as u8
                + depth as u8
                + stencil as u8
                + plane0 as u8
                + plane1 as u8
                + plane2 as u8;
            num_bits == 1 || depth && stencil && !(color || plane0 || plane1 || plane2)
        });

        self.aspects = aspects;
        self
    }

    /// How to map components of each pixel.
    ///
    /// The default value is [`ComponentMapping::identity()`].
    #[inline]
    pub fn component_mapping(mut self, component_mapping: ComponentMapping) -> Self {
        self.component_mapping = component_mapping;
        self
    }

    /// The format of the image view.
    ///
    /// If this is set to a format that is different from the image, the image must be created with
    /// the `mutable_format` flag.
    ///
    /// The default value is the format of the image.
    #[inline]
    pub fn format(mut self, format: Format) -> Self {
        self.format = format;
        self
    }

    /// The range of mipmap levels of the image that the view should cover.
    ///
    /// The default value is the full range of mipmaps present in the image.
    ///
    /// # Panics
    ///
    /// - Panics if `mip_levels` is empty.
    #[inline]
    pub fn mip_levels(mut self, mip_levels: Range<u32>) -> Self {
        assert!(!mip_levels.is_empty());
        self.mip_levels = mip_levels;
        self
    }

    /// The sampler YCbCr conversion to be used with the image view.
    ///
    /// If set to `Some`, several restrictions apply:
    /// - The `component_mapping` must be the identity swizzle for all components.
    /// - If the image view is to be used in a shader, it must be in a combined image sampler
    ///   descriptor, a separate sampled image descriptor is not allowed.
    /// - The corresponding sampler must have the same sampler YCbCr object or an identically
    ///   created one, and must be used as an immutable sampler within a descriptor set layout.
    ///
    /// The default value is `None`.
    #[inline]
    pub fn sampler_ycbcr_conversion(
        mut self,
        conversion: Option<Arc<SamplerYcbcrConversion>>,
    ) -> Self {
        self.sampler_ycbcr_conversion = conversion;
        self
    }

    /// The image view type.
    ///
    /// The view type must be compatible with the dimensions of the image and the selected array
    /// layers.
    ///
    /// The default value is determined from the image, based on its dimensions and number of
    /// layers.
    #[inline]
    pub fn ty(mut self, ty: ImageViewType) -> Self {
        self.ty = ty;
        self
    }
}

/// Error that can happen when creating an image view.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ImageViewCreationError {
    /// Allocating memory failed.
    OomError(OomError),

    FeatureNotEnabled {
        feature: &'static str,
        reason: &'static str,
    },

    /// A 2D image view was requested from a 3D image, but a range of multiple mip levels was
    /// specified.
    Array2dCompatibleMultipleMipLevels,

    /// The specified range of array layers was not a subset of those in the image.
    ArrayLayersOutOfRange { range_end: u32, max: u32 },

    /// The image has the `block_texel_view_compatible` flag, but a range of multiple array layers
    /// was specified.
    BlockTexelViewCompatibleMultipleArrayLayers,

    /// The image has the `block_texel_view_compatible` flag, but a range of multiple mip levels
    /// was specified.
    BlockTexelViewCompatibleMultipleMipLevels,

    /// The image has the `block_texel_view_compatible` flag, and an uncompressed format was
    /// requested, and the image view type was `Dim3d`.
    BlockTexelViewCompatibleUncompressedIs3d,

    /// The requested format has chroma subsampling, but the width and/or height of the image was
    /// not a multiple of 2.
    FormatChromaSubsamplingInvalidImageDimensions,

    /// The requested format was not compatible with the image.
    FormatNotCompatible,

    /// The given format was not supported by the device.
    FormatNotSupported,

    /// The format requires a sampler YCbCr conversion, but none was provided.
    FormatRequiresSamplerYcbcrConversion { format: Format },

    /// A requested usage flag was not supported by the given format.
    FormatUsageNotSupported { usage: &'static str },

    /// An aspect was selected that was not present in the image.
    ImageAspectsNotCompatible {
        aspects: ImageAspects,
        image_aspects: ImageAspects,
    },

    /// A 2D image view was requested from a 3D image, but the image was not created with the
    /// `array_2d_compatible` flag.
    ImageNotArray2dCompatible,

    /// A cube image view type was requested, but the image was not created with the
    /// `cube_compatible` flag.
    ImageNotCubeCompatible,

    /// The given image view type was not compatible with the type of the image.
    ImageTypeNotCompatible,

    /// The requested [`ImageViewType`] was not compatible with the image, or with the specified
    /// ranges of array layers and mipmap levels.
    IncompatibleType,

    /// The image was not created with
    /// [one of the required usages](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#valid-imageview-imageusage)
    /// for image views.
    InvalidImageUsage,

    /// The specified range of mip levels was not a subset of those in the image.
    MipLevelsOutOfRange { range_end: u32, max: u32 },

    /// The image has multisampling enabled, but the image view type was not `Dim2d` or
    /// `Dim2dArray`.
    MultisamplingNot2d,

    /// Sampler YCbCr conversion was enabled, but `component_mapping` was not the identity mapping.
    SamplerYcbcrConversionComponentMappingNotIdentity { component_mapping: ComponentMapping },

    /// The `CubeArray` image view type was specified, but the range of array layers did not have a
    /// size that is a multiple 6.
    TypeCubeArrayNotMultipleOf6ArrayLayers,

    /// The `Cube` image view type was specified, but the range of array layers did not have a size
    /// of 6.
    TypeCubeNot6ArrayLayers,

    /// A non-arrayed image view type was specified, but a range of multiple array layers was
    /// specified.
    TypeNonArrayedMultipleArrayLayers,
}

impl error::Error for ImageViewCreationError {
    #[inline]
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            ImageViewCreationError::OomError(ref err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for ImageViewCreationError {
    #[inline]
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            Self::OomError(err) => write!(
                fmt,
                "allocating memory failed",
            ),
            Self::FeatureNotEnabled { feature, reason } => {
                write!(fmt, "the feature {} must be enabled: {}", feature, reason)
            }
            Self::Array2dCompatibleMultipleMipLevels => write!(
                fmt,
                "a 2D image view was requested from a 3D image, but a range of multiple mip levels was specified",
            ),
            Self::ArrayLayersOutOfRange { .. } => write!(
                fmt,
                "the specified range of array layers was not a subset of those in the image",
            ),
            Self::BlockTexelViewCompatibleMultipleArrayLayers => write!(
                fmt,
                "the image has the `block_texel_view_compatible` flag, but a range of multiple array layers was specified",
            ),
            Self::BlockTexelViewCompatibleMultipleMipLevels => write!(
                fmt,
                "the image has the `block_texel_view_compatible` flag, but a range of multiple mip levels was specified",
            ),
            Self::BlockTexelViewCompatibleUncompressedIs3d => write!(
                fmt,
                "the image has the `block_texel_view_compatible` flag, and an uncompressed format was requested, and the image view type was `Dim3d`",
            ),
            Self::FormatChromaSubsamplingInvalidImageDimensions => write!(
                fmt,
                "the requested format has chroma subsampling, but the width and/or height of the image was not a multiple of 2",
            ),
            Self::FormatNotCompatible => write!(
                fmt,
                "the requested format was not compatible with the image",
            ),
            Self::FormatNotSupported => write!(
                fmt,
                "the given format was not supported by the device"
            ),
            Self::FormatRequiresSamplerYcbcrConversion { .. } => write!(
                fmt,
                "the format requires a sampler YCbCr conversion, but none was provided",
            ),
            Self::FormatUsageNotSupported { usage } => write!(
                fmt,
                "a requested usage flag was not supported by the given format"
            ),
            Self::ImageAspectsNotCompatible { .. } => write!(
                fmt,
                "an aspect was selected that was not present in the image",
            ),
            Self::ImageNotArray2dCompatible => write!(
                fmt,
                "a 2D image view was requested from a 3D image, but the image was not created with the `array_2d_compatible` flag",
            ),
            Self::ImageNotCubeCompatible => write!(
                fmt,
                "a cube image view type was requested, but the image was not created with the `cube_compatible` flag",
            ),
            Self::ImageTypeNotCompatible => write!(
                fmt,
                "the given image view type was not compatible with the type of the image",
            ),
            Self::IncompatibleType => write!(
                fmt,
                "image view type is not compatible with image, array layers or mipmap levels",
            ),
            Self::InvalidImageUsage => write!(
                fmt,
                "the usage of the image is not compatible with image views",
            ),
            Self::MipLevelsOutOfRange { .. } => write!(
                fmt,
                "the specified range of mip levels was not a subset of those in the image",
            ),
            Self::MultisamplingNot2d => write!(
                fmt,
                "the image has multisampling enabled, but the image view type was not `Dim2d` or `Dim2dArray`",
            ),
            Self::SamplerYcbcrConversionComponentMappingNotIdentity { .. } => write!(
                fmt,
                "sampler YCbCr conversion was enabled, but `component_mapping` was not the identity mapping",
            ),
            Self::TypeCubeArrayNotMultipleOf6ArrayLayers => write!(
                fmt,
                "the `CubeArray` image view type was specified, but the range of array layers did not have a size that is a multiple 6"
            ),
            Self::TypeCubeNot6ArrayLayers => write!(
                fmt,
                "the `Cube` image view type was specified, but the range of array layers did not have a size of 6"
            ),
            Self::TypeNonArrayedMultipleArrayLayers => write!(
                fmt,
                "a non-arrayed image view type was specified, but a range of multiple array layers was specified"
            )
        }
    }
}

impl From<OomError> for ImageViewCreationError {
    #[inline]
    fn from(err: OomError) -> ImageViewCreationError {
        ImageViewCreationError::OomError(err)
    }
}

impl From<Error> for ImageViewCreationError {
    #[inline]
    fn from(err: Error) -> ImageViewCreationError {
        match err {
            err @ Error::OutOfHostMemory => OomError::from(err).into(),
            err @ Error::OutOfDeviceMemory => OomError::from(err).into(),
            _ => panic!("unexpected error: {:?}", err),
        }
    }
}

/// The geometry type of an image view.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(i32)]
pub enum ImageViewType {
    Dim1d = ash::vk::ImageViewType::TYPE_1D.as_raw(),
    Dim1dArray = ash::vk::ImageViewType::TYPE_1D_ARRAY.as_raw(),
    Dim2d = ash::vk::ImageViewType::TYPE_2D.as_raw(),
    Dim2dArray = ash::vk::ImageViewType::TYPE_2D_ARRAY.as_raw(),
    Dim3d = ash::vk::ImageViewType::TYPE_3D.as_raw(),
    Cube = ash::vk::ImageViewType::CUBE.as_raw(),
    CubeArray = ash::vk::ImageViewType::CUBE_ARRAY.as_raw(),
}

impl ImageViewType {
    /// Returns whether the type is arrayed.
    #[inline]
    pub fn is_arrayed(&self) -> bool {
        match self {
            Self::Dim1d | Self::Dim2d | Self::Dim3d | Self::Cube => false,
            Self::Dim1dArray | Self::Dim2dArray | Self::CubeArray => true,
        }
    }

    /// Returns whether `self` is compatible with the given `image_type`.
    #[inline]
    pub fn is_compatible_with(&self, image_type: ImageType) -> bool {
        matches!(
            (*self, image_type,),
            (
                ImageViewType::Dim1d | ImageViewType::Dim1dArray,
                ImageType::Dim1d
            ) | (
                ImageViewType::Dim2d | ImageViewType::Dim2dArray,
                ImageType::Dim2d | ImageType::Dim3d
            ) | (
                ImageViewType::Cube | ImageViewType::CubeArray,
                ImageType::Dim2d
            ) | (ImageViewType::Dim3d, ImageType::Dim3d)
        )
    }
}

impl From<ImageViewType> for ash::vk::ImageViewType {
    fn from(val: ImageViewType) -> Self {
        Self::from_raw(val as i32)
    }
}

/// Trait for types that represent the GPU can access an image view.
pub unsafe trait ImageViewAbstract:
    VulkanObject<Object = ash::vk::ImageView> + DeviceOwned + Send + Sync
{
    /// Returns the wrapped image that this image view was created from.
    fn image(&self) -> Arc<dyn ImageAccess>;

    /// Returns the range of array layers of the wrapped image that this view exposes.
    fn array_layers(&self) -> Range<u32>;

    /// Returns the aspects of the wrapped image that this view exposes.
    fn aspects(&self) -> &ImageAspects;

    /// Returns the component mapping of this view.
    fn component_mapping(&self) -> ComponentMapping;

    /// Returns whether the image view supports sampling with a
    /// [`Cubic`](crate::sampler::Filter::Cubic) `mag_filter` or `min_filter`.
    fn filter_cubic(&self) -> bool;

    /// Returns whether the image view supports sampling with a
    /// [`Cubic`](crate::sampler::Filter::Cubic) `mag_filter` or `min_filter`, and with a
    /// [`Min`](crate::sampler::SamplerReductionMode::Min) or
    /// [`Max`](crate::sampler::SamplerReductionMode::Max) `reduction_mode`.
    fn filter_cubic_minmax(&self) -> bool;

    /// Returns the format of this view. This can be different from the parent's format.
    fn format(&self) -> Format;

    /// Returns the features supported by the image view's format.
    fn format_features(&self) -> &FormatFeatures;

    /// Returns the range of mip levels of the wrapped image that this view exposes.
    fn mip_levels(&self) -> Range<u32>;

    /// Returns the sampler YCbCr conversion that this image view was created with, if any.
    fn sampler_ycbcr_conversion(&self) -> Option<&Arc<SamplerYcbcrConversion>>;

    /// Returns the [`ImageViewType`] of this image view.
    fn ty(&self) -> ImageViewType;

    /// Returns the usage of the image view.
    fn usage(&self) -> &ImageUsage;
}

unsafe impl<I> ImageViewAbstract for ImageView<I>
where
    I: ImageAccess + 'static,
{
    #[inline]
    fn image(&self) -> Arc<dyn ImageAccess> {
        self.image.clone() as Arc<_>
    }

    #[inline]
    fn array_layers(&self) -> Range<u32> {
        self.array_layers.clone()
    }

    #[inline]
    fn aspects(&self) -> &ImageAspects {
        &self.aspects
    }

    #[inline]
    fn component_mapping(&self) -> ComponentMapping {
        self.component_mapping
    }

    #[inline]
    fn filter_cubic(&self) -> bool {
        self.filter_cubic
    }

    #[inline]
    fn filter_cubic_minmax(&self) -> bool {
        self.filter_cubic_minmax
    }

    #[inline]
    fn format(&self) -> Format {
        self.format
    }

    #[inline]
    fn format_features(&self) -> &FormatFeatures {
        &self.format_features
    }

    #[inline]
    fn mip_levels(&self) -> Range<u32> {
        self.mip_levels.clone()
    }

    #[inline]
    fn sampler_ycbcr_conversion(&self) -> Option<&Arc<SamplerYcbcrConversion>> {
        self.sampler_ycbcr_conversion.as_ref()
    }

    #[inline]
    fn ty(&self) -> ImageViewType {
        self.ty
    }

    #[inline]
    fn usage(&self) -> &ImageUsage {
        &self.usage
    }
}

impl PartialEq for dyn ImageViewAbstract {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.internal_object() == other.internal_object() && self.device() == other.device()
    }
}

impl Eq for dyn ImageViewAbstract {}

impl Hash for dyn ImageViewAbstract {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.internal_object().hash(state);
        self.device().hash(state);
    }
}