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
use {
    super::{DescriptorBindingFlags, ImageViewDescriptor, TypedDescriptorBinding},
    crate::{
        image::{Image, Layout},
        view::{ImageView, ImageViewInfo},
        Device, OutOfMemory,
    },
};

impl TypedDescriptorBinding for Image {
    const COUNT: u32 = 1;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::empty();
    type Descriptors = [ImageViewDescriptor; 1];

    fn eq(&self, descriptors: &[ImageViewDescriptor; 1]) -> bool {
        *self == descriptors[0].view.info().image
    }

    fn get_descriptors(&self, device: &Device) -> Result<[ImageViewDescriptor; 1], OutOfMemory> {
        let view = device.create_image_view(ImageViewInfo::new(self.clone()))?;
        Ok([ImageViewDescriptor {
            view,
            layout: Layout::ShaderReadOnlyOptimal,
        }])
    }
}

impl TypedDescriptorBinding for ImageView {
    const COUNT: u32 = 1;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::empty();
    type Descriptors = [ImageViewDescriptor; 1];

    fn eq(&self, descriptors: &[ImageViewDescriptor; 1]) -> bool {
        *self == descriptors[0].view
    }

    fn get_descriptors(&self, _device: &Device) -> Result<[ImageViewDescriptor; 1], OutOfMemory> {
        Ok([ImageViewDescriptor {
            view: self.clone(),
            layout: Layout::ShaderReadOnlyOptimal,
        }])
    }
}

impl TypedDescriptorBinding for ImageViewDescriptor {
    const COUNT: u32 = 1;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::empty();
    type Descriptors = [ImageViewDescriptor; 1];

    fn eq(&self, descriptors: &[ImageViewDescriptor; 1]) -> bool {
        *self == descriptors[0]
    }

    fn get_descriptors(&self, _device: &Device) -> Result<[ImageViewDescriptor; 1], OutOfMemory> {
        Ok([self.clone()])
    }
}

impl<const N: usize> TypedDescriptorBinding for [ImageView; N] {
    const COUNT: u32 = N as u32;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::empty();
    type Descriptors = [ImageViewDescriptor; N];

    fn eq(&self, descriptors: &[ImageViewDescriptor; N]) -> bool {
        self.iter()
            .zip(descriptors)
            .all(|(me, descriptor)| *me == descriptor.view)
    }

    fn get_descriptors(&self, _device: &Device) -> Result<[ImageViewDescriptor; N], OutOfMemory> {
        let mut result = arrayvec::ArrayVec::new();

        for me in self {
            unsafe {
                result.push_unchecked(ImageViewDescriptor {
                    view: me.clone(),
                    layout: Layout::ShaderReadOnlyOptimal,
                });
            }
        }

        Ok(result.into_inner().unwrap())
    }
}

impl<const N: usize> TypedDescriptorBinding for [ImageViewDescriptor; N] {
    const COUNT: u32 = N as u32;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::empty();
    type Descriptors = [ImageViewDescriptor; N];

    fn eq(&self, descriptors: &[ImageViewDescriptor; N]) -> bool {
        *self == *descriptors
    }

    fn get_descriptors(&self, _device: &Device) -> Result<[ImageViewDescriptor; N], OutOfMemory> {
        Ok(self.clone())
    }
}

impl<const N: usize> TypedDescriptorBinding for arrayvec::ArrayVec<ImageView, N> {
    const COUNT: u32 = N as u32;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::PARTIALLY_BOUND;
    type Descriptors = arrayvec::ArrayVec<ImageViewDescriptor, N>;

    fn eq(&self, descriptors: &arrayvec::ArrayVec<ImageViewDescriptor, N>) -> bool {
        self.len() == descriptors.len()
            && self
                .iter()
                .zip(descriptors)
                .all(|(me, descriptor)| *me == descriptor.view)
    }

    fn get_descriptors(
        &self,
        _device: &Device,
    ) -> Result<arrayvec::ArrayVec<ImageViewDescriptor, N>, OutOfMemory> {
        let mut result = arrayvec::ArrayVec::new();

        for me in self {
            unsafe {
                result.push_unchecked(ImageViewDescriptor {
                    view: me.clone(),
                    layout: Layout::ShaderReadOnlyOptimal,
                });
            }
        }

        Ok(result)
    }
}

impl<const N: usize> TypedDescriptorBinding for arrayvec::ArrayVec<ImageViewDescriptor, N> {
    const COUNT: u32 = N as u32;
    const FLAGS: DescriptorBindingFlags = DescriptorBindingFlags::PARTIALLY_BOUND;
    type Descriptors = arrayvec::ArrayVec<ImageViewDescriptor, N>;

    fn eq(&self, descriptors: &arrayvec::ArrayVec<ImageViewDescriptor, N>) -> bool {
        *self == *descriptors
    }

    fn get_descriptors(
        &self,
        _device: &Device,
    ) -> Result<arrayvec::ArrayVec<ImageViewDescriptor, N>, OutOfMemory> {
        Ok(self.clone())
    }
}