use std::sync::Arc;
use device::Queue;
use format::ClearValue;
use format::Format;
use format::FormatDesc;
use image::ImageDimensions;
use image::Dimensions;
use image::ViewType;
use image::traits::ImageAccess;
use image::traits::ImageClearValue;
use image::traits::ImageContent;
use image::traits::ImageViewAccess;
use image::traits::Image;
use image::traits::ImageView;
use image::ImageLayout;
use image::sys::UnsafeImage;
use image::sys::UnsafeImageView;
use swapchain::Swapchain;
use sync::AccessError;
use OomError;
pub struct SwapchainImage {
swapchain: Arc<Swapchain>,
image_offset: usize,
view: UnsafeImageView,
}
impl SwapchainImage {
pub unsafe fn from_raw(swapchain: Arc<Swapchain>, id: usize)
-> Result<Arc<SwapchainImage>, OomError>
{
let image = swapchain.raw_image(id).unwrap();
let view = try!(UnsafeImageView::raw(&image, ViewType::Dim2d, 0 .. 1, 0 .. 1));
Ok(Arc::new(SwapchainImage {
swapchain: swapchain.clone(),
image_offset: id,
view: view,
}))
}
#[inline]
pub fn dimensions(&self) -> [u32; 2] {
let dims = self.my_image().dimensions();
[dims.width(), dims.height()]
}
#[inline]
pub fn swapchain(&self) -> &Arc<Swapchain> {
&self.swapchain
}
#[inline]
fn my_image(&self) -> &UnsafeImage {
self.swapchain.raw_image(self.image_offset).unwrap()
}
}
unsafe impl ImageAccess for SwapchainImage {
#[inline]
fn inner(&self) -> &UnsafeImage {
self.my_image()
}
#[inline]
fn initial_layout_requirement(&self) -> ImageLayout {
ImageLayout::PresentSrc
}
#[inline]
fn final_layout_requirement(&self) -> ImageLayout {
ImageLayout::PresentSrc
}
#[inline]
fn conflict_key(&self, _: u32, _: u32, _: u32, _: u32) -> u64 {
self.my_image().key()
}
#[inline]
fn try_gpu_lock(&self, _: bool, _: &Queue) -> Result<(), AccessError> {
Err(AccessError::SwapchainImageAcquireOnly)
}
#[inline]
unsafe fn increase_gpu_lock(&self) {
}
}
unsafe impl ImageClearValue<<Format as FormatDesc>::ClearValue> for SwapchainImage
{
#[inline]
fn decode(&self, value: <Format as FormatDesc>::ClearValue) -> Option<ClearValue> {
Some(self.swapchain.format().decode_clear_value(value))
}
}
unsafe impl<P> ImageContent<P> for SwapchainImage {
#[inline]
fn matches_format(&self) -> bool {
true }
}
unsafe impl ImageViewAccess for SwapchainImage {
#[inline]
fn parent(&self) -> &ImageAccess {
self
}
#[inline]
fn dimensions(&self) -> Dimensions {
let dims = self.swapchain.dimensions();
Dimensions::Dim2d { width: dims[0], height: dims[1] }
}
#[inline]
fn inner(&self) -> &UnsafeImageView {
&self.view
}
#[inline]
fn descriptor_set_storage_image_layout(&self) -> ImageLayout {
ImageLayout::ShaderReadOnlyOptimal
}
#[inline]
fn descriptor_set_combined_image_sampler_layout(&self) -> ImageLayout {
ImageLayout::ShaderReadOnlyOptimal
}
#[inline]
fn descriptor_set_sampled_image_layout(&self) -> ImageLayout {
ImageLayout::ShaderReadOnlyOptimal
}
#[inline]
fn descriptor_set_input_attachment_layout(&self) -> ImageLayout {
ImageLayout::ShaderReadOnlyOptimal
}
#[inline]
fn identity_swizzle(&self) -> bool {
true
}
}
unsafe impl Image for SwapchainImage {
type Access = SwapchainImage;
#[inline]
fn access(self) -> Self::Access {
self
}
#[inline]
fn format(&self) -> Format {
self.my_image().format()
}
#[inline]
fn samples(&self) -> u32 {
self.my_image().samples()
}
#[inline]
fn dimensions(&self) -> ImageDimensions {
self.my_image().dimensions()
}
}
unsafe impl ImageView for SwapchainImage {
type Access = SwapchainImage;
fn access(self) -> Self::Access {
self
}
}
unsafe impl Image for Arc<SwapchainImage> {
type Access = Arc<SwapchainImage>;
#[inline]
fn access(self) -> Self::Access {
self
}
#[inline]
fn format(&self) -> Format {
self.my_image().format()
}
#[inline]
fn samples(&self) -> u32 {
self.my_image().samples()
}
#[inline]
fn dimensions(&self) -> ImageDimensions {
self.my_image().dimensions()
}
}
unsafe impl ImageView for Arc<SwapchainImage> {
type Access = Arc<SwapchainImage>;
fn access(self) -> Self::Access {
self
}
}