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

use std::sync::Arc;
use vks;
use ::{VdResult, SwapchainKhr, Device, ImageHandle, Handle};


#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct ImageViewHandle(pub(crate) vks::VkImageView);

impl ImageViewHandle {
    #[inline(always)]
    pub fn to_raw(&self) -> vks::VkImageView {
        self.0
    }
}

unsafe impl Handle for ImageViewHandle {
    type Target = ImageViewHandle;

    /// Returns this object's handle.
    #[inline(always)]
    fn handle(&self) -> Self::Target {
        *self
    }
}


#[derive(Debug)]
pub struct Inner {
    handle: ImageViewHandle,
    device: Device,
    swapchain: Option<SwapchainKhr>,
}

#[derive(Debug, Clone)]
pub struct ImageView {
    inner: Arc<Inner>,
}

impl ImageView {
    /// Returns a new `ImageViewBuilder`.
    pub fn builder<'b>() -> ImageViewBuilder<'b> {
        ImageViewBuilder::new()
    }

    /// Returns this object's handle.
    pub fn handle(&self) -> ImageViewHandle {
        self.inner.handle
    }

    /// Returns a reference to the associated device.
    pub fn device(&self) -> &Device {
        &self.inner.device
    }
}

unsafe impl<'i> Handle for &'i ImageView {
    type Target = ImageViewHandle;

    #[inline(always)]
    fn handle(&self) -> Self::Target {
        self.inner.handle
    }
}

impl Drop for Inner {
    fn drop(&mut self) {
        unsafe {
            // self.device.proc_addr_loader().vk.vkDestroyImageView(self.device.handle().0,
            //     self.handle.0, ptr::null());
            self.device.destroy_image_view(self.handle, None);
        }
    }
}

/// A builder for an `ImageView`.
#[derive(Debug, Clone)]
pub struct ImageViewBuilder<'b> {
    create_info: ::ImageViewCreateInfo<'b>,
}

impl<'b> ImageViewBuilder<'b> {
    /// Returns a new `ImageViewBuilder`.
    pub fn new() -> ImageViewBuilder<'b> {
        ImageViewBuilder { create_info: ::ImageViewCreateInfo::default() }
    }

    /// Specifies the image on which the view will be created.
    pub fn image<'s, H>(&'s mut self, image: H) -> &'s mut ImageViewBuilder<'b>
            where H: Handle<Target=ImageHandle> {
        self.create_info.set_image(image);
        self
    }

    /// Specifies the type of the image view.
    pub fn view_type<'s>(&'s mut self, view_type: ::ImageViewType) -> &'s mut ImageViewBuilder<'b> {
        self.create_info.set_view_type(view_type);
        self
    }

    /// Specifies the format and type used to interpret data elements in the
    /// image.
    pub fn format<'s>(&'s mut self, format: ::Format) -> &'s mut ImageViewBuilder<'b> {
        self.create_info.set_format(format);
        self
    }

    /// Specifies a remapping of color components (or of depth or stencil
    /// components after they have been converted into color components).
    pub fn components<'s>(&'s mut self, components: ::ComponentMapping)
            -> &'s mut ImageViewBuilder<'b> {
        self.create_info.set_components(components);
        self
    }

    /// Specifies the range selecting the set of mipmap levels and array
    /// layers to be accessible to the view.
    pub fn subresource_range<'s>(&'s mut self, subresource_range: ::ImageSubresourceRange)
            -> &'s mut ImageViewBuilder<'b> {
        self.create_info.set_subresource_range(subresource_range);
        self
    }

    pub fn build(&self, device: Device, swapchain: Option<SwapchainKhr>) -> VdResult<ImageView> {
        // let mut handle = 0;

        // unsafe {
        //     ::check(device.proc_addr_loader().vk.vkCreateImageView(device.handle().0,
        //         self.create_info.as_raw(), ptr::null(), &mut handle));
        // }

        let handle = unsafe { device.create_image_view(&self.create_info, None)? };

        Ok(ImageView {
            inner: Arc::new(Inner {
                // handle: ImageViewHandle(handle),
                handle,
                device,
                swapchain,
            })
        })
    }
}