From cdab36a322fbe4254a0adf1d77619fccae924da3 Mon Sep 17 00:00:00 2001 From: Rua Date: Thu, 20 Oct 2022 09:26:34 +0200 Subject: [PATCH] Remove type parameter from `Surface` (#2036) --- examples/src/bin/buffer-pool.rs | 12 +- examples/src/bin/clear_attachments.rs | 12 +- examples/src/bin/deferred/main.rs | 12 +- examples/src/bin/gl-interop.rs | 16 +- examples/src/bin/image-self-copy-blit/main.rs | 12 +- examples/src/bin/image/main.rs | 12 +- examples/src/bin/immutable-sampler/main.rs | 12 +- examples/src/bin/indirect.rs | 12 +- examples/src/bin/instancing.rs | 12 +- examples/src/bin/multi-window.rs | 36 +- examples/src/bin/occlusion-query.rs | 12 +- examples/src/bin/push-descriptors/main.rs | 12 +- examples/src/bin/runtime-shader/main.rs | 12 +- examples/src/bin/runtime_array/main.rs | 12 +- examples/src/bin/simple-particles.rs | 9 +- examples/src/bin/teapot/main.rs | 12 +- examples/src/bin/tessellation.rs | 12 +- examples/src/bin/texture_array/main.rs | 12 +- examples/src/bin/triangle-v1_3.rs | 14 +- examples/src/bin/triangle.rs | 12 +- vulkano-util/src/renderer.rs | 20 +- vulkano-win/src/raw_window_handle.rs | 27 +- vulkano-win/src/winit.rs | 97 +++--- vulkano/src/device/physical.rs | 49 ++- vulkano/src/image/swapchain.rs | 41 +-- vulkano/src/swapchain/mod.rs | 47 ++- vulkano/src/swapchain/surface.rs | 315 ++++++++++-------- vulkano/src/swapchain/swapchain.rs | 212 +++++------- 28 files changed, 543 insertions(+), 532 deletions(-) diff --git a/examples/src/bin/buffer-pool.rs b/examples/src/bin/buffer-pool.rs index 04b7f7a4..64ed5829 100644 --- a/examples/src/bin/buffer-pool.rs +++ b/examples/src/bin/buffer-pool.rs @@ -46,8 +46,8 @@ use vulkano::{ }, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -145,6 +145,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -152,7 +153,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -257,7 +258,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -392,7 +394,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/clear_attachments.rs b/examples/src/bin/clear_attachments.rs index 1e535ec0..767983dc 100644 --- a/examples/src/bin/clear_attachments.rs +++ b/examples/src/bin/clear_attachments.rs @@ -20,8 +20,8 @@ use vulkano::{ instance::{Instance, InstanceCreateInfo}, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -114,6 +114,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -121,7 +122,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -176,7 +177,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -298,7 +300,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, ) -> Vec> { images diff --git a/examples/src/bin/deferred/main.rs b/examples/src/bin/deferred/main.rs index b0fa1804..6002b494 100644 --- a/examples/src/bin/deferred/main.rs +++ b/examples/src/bin/deferred/main.rs @@ -39,8 +39,8 @@ use vulkano::{ image::{view::ImageView, ImageUsage}, instance::{Instance, InstanceCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -49,7 +49,7 @@ use vulkano_win::VkSurfaceBuild; use winit::{ event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::WindowBuilder, + window::{Window, WindowBuilder}, }; mod frame; @@ -135,6 +135,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); let (swapchain, images) = Swapchain::new( device.clone(), @@ -142,7 +143,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -194,7 +195,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } diff --git a/examples/src/bin/gl-interop.rs b/examples/src/bin/gl-interop.rs index d0c69121..f1991c66 100644 --- a/examples/src/bin/gl-interop.rs +++ b/examples/src/bin/gl-interop.rs @@ -47,8 +47,8 @@ mod linux { render_pass::{Framebuffer, RenderPass, Subpass}, sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo}, swapchain::{ - AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{ now, ExternalSemaphoreHandleType, ExternalSemaphoreHandleTypes, FlushError, GpuFuture, @@ -291,9 +291,10 @@ mod linux { previous_frame_end.as_mut().unwrap().cleanup_finished(); if recreate_swapchain { + let window = surface.object().unwrap().downcast_ref::().unwrap(); let (new_swapchain, new_images) = match swapchain.recreate(SwapchainCreateInfo { - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), ..swapchain.create_info() }) { Ok(r) => r, @@ -407,8 +408,8 @@ mod linux { ) -> ( Arc, Arc, - Arc>, - Arc>, + Arc, + Arc, vulkano::pipeline::graphics::viewport::Viewport, Arc, Arc, @@ -536,6 +537,7 @@ mod linux { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -543,7 +545,7 @@ mod linux { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -673,7 +675,7 @@ mod linux { } fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/image-self-copy-blit/main.rs b/examples/src/bin/image-self-copy-blit/main.rs index ea332e1c..fe4ed8fb 100644 --- a/examples/src/bin/image-self-copy-blit/main.rs +++ b/examples/src/bin/image-self-copy-blit/main.rs @@ -42,8 +42,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -136,6 +136,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -143,7 +144,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -378,7 +379,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -479,7 +481,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/image/main.rs b/examples/src/bin/image/main.rs index 9cb73938..98c23170 100644 --- a/examples/src/bin/image/main.rs +++ b/examples/src/bin/image/main.rs @@ -40,8 +40,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -134,6 +134,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -141,7 +142,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -305,7 +306,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -406,7 +408,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/immutable-sampler/main.rs b/examples/src/bin/immutable-sampler/main.rs index f6534e03..fe6cab97 100644 --- a/examples/src/bin/immutable-sampler/main.rs +++ b/examples/src/bin/immutable-sampler/main.rs @@ -49,8 +49,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -140,6 +140,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -147,7 +148,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -318,7 +319,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -419,7 +421,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/indirect.rs b/examples/src/bin/indirect.rs index c70f8ee6..78e12369 100644 --- a/examples/src/bin/indirect.rs +++ b/examples/src/bin/indirect.rs @@ -52,8 +52,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, single_pass_renderpass, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -154,6 +154,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -161,7 +162,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -335,7 +336,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -477,7 +479,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/instancing.rs b/examples/src/bin/instancing.rs index ae507abf..69f00171 100644 --- a/examples/src/bin/instancing.rs +++ b/examples/src/bin/instancing.rs @@ -37,8 +37,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, single_pass_renderpass, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -151,6 +151,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -158,7 +159,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -332,7 +333,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -438,7 +440,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/multi-window.rs b/examples/src/bin/multi-window.rs index 3be759a0..4547581b 100644 --- a/examples/src/bin/multi-window.rs +++ b/examples/src/bin/multi-window.rs @@ -40,8 +40,8 @@ use vulkano::{ }, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, swapchain::{ - acquire_next_image, AcquireError, Surface, Swapchain, SwapchainAbstract, - SwapchainCreateInfo, SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo, + SwapchainCreationError, SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -55,8 +55,8 @@ use winit::{ // A struct to contain resources related to a window struct WindowSurface { - surface: Arc>, - swapchain: Arc>, + surface: Arc, + swapchain: Arc, framebuffers: Vec>, recreate_swapchain: bool, previous_frame_end: Option>, @@ -84,7 +84,8 @@ fn main() { .build_vk_surface(&event_loop, instance.clone()) .unwrap(); // Use the window's id as a means to access it from the hashmap - let window_id = surface.window().id(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let window_id = window.id(); // Find the device and a queue. // TODO: it is assumed the device, queue, and surface surface_capabilities are the same for all windows @@ -155,6 +156,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -162,7 +164,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_caps.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -318,7 +320,8 @@ fn main() { let surface = WindowBuilder::new() .build_vk_surface(event_loop, instance.clone()) .unwrap(); - let window_id = surface.window().id(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let window_id = window.id(); let (swapchain, images) = { let composite_alpha = surface_caps .supported_composite_alpha @@ -339,7 +342,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_caps.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -367,9 +370,15 @@ fn main() { ); } Event::RedrawEventsCleared => { - window_surfaces - .values() - .for_each(|s| s.surface.window().request_redraw()); + window_surfaces.values().for_each(|s| { + let window = s + .surface + .object() + .unwrap() + .downcast_ref::() + .unwrap(); + window.request_redraw() + }); } Event::RedrawRequested(window_id) => { let WindowSurface { @@ -380,7 +389,8 @@ fn main() { ref mut previous_frame_end, } = window_surfaces.get_mut(&window_id).unwrap(); - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -475,7 +485,7 @@ fn main() { } fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/occlusion-query.rs b/examples/src/bin/occlusion-query.rs index 8f699856..39eaab59 100644 --- a/examples/src/bin/occlusion-query.rs +++ b/examples/src/bin/occlusion-query.rs @@ -39,8 +39,8 @@ use vulkano::{ query::{QueryControlFlags, QueryPool, QueryPoolCreateInfo, QueryResultFlags, QueryType}, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -130,6 +130,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -137,7 +138,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -343,7 +344,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -537,7 +539,7 @@ fn main() { } fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/push-descriptors/main.rs b/examples/src/bin/push-descriptors/main.rs index ee8e8394..10588b90 100644 --- a/examples/src/bin/push-descriptors/main.rs +++ b/examples/src/bin/push-descriptors/main.rs @@ -38,8 +38,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -130,6 +130,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -137,7 +138,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -297,7 +298,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -398,7 +400,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/runtime-shader/main.rs b/examples/src/bin/runtime-shader/main.rs index 0e94c466..9e1f9119 100644 --- a/examples/src/bin/runtime-shader/main.rs +++ b/examples/src/bin/runtime-shader/main.rs @@ -45,8 +45,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, shader::ShaderModule, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -145,6 +145,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -152,7 +153,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -273,7 +274,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -368,7 +370,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/runtime_array/main.rs b/examples/src/bin/runtime_array/main.rs index 064422f4..6f2b2c97 100644 --- a/examples/src/bin/runtime_array/main.rs +++ b/examples/src/bin/runtime_array/main.rs @@ -45,8 +45,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -146,6 +146,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -153,7 +154,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -435,7 +436,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -536,7 +538,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/simple-particles.rs b/examples/src/bin/simple-particles.rs index 129252a7..6f3a49c5 100644 --- a/examples/src/bin/simple-particles.rs +++ b/examples/src/bin/simple-particles.rs @@ -39,9 +39,7 @@ use vulkano::{ GraphicsPipeline, PipelineBindPoint, }, render_pass::{Framebuffer, FramebufferCreateInfo, Subpass}, - swapchain::{ - PresentMode, Swapchain, SwapchainAbstract, SwapchainCreateInfo, SwapchainPresentInfo, - }, + swapchain::{PresentMode, Swapchain, SwapchainCreateInfo, SwapchainPresentInfo}, sync::{FenceSignalFuture, GpuFuture}, VulkanLibrary, }; @@ -49,7 +47,7 @@ use vulkano_win::VkSurfaceBuild; use winit::{ event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::WindowBuilder, + window::{Window, WindowBuilder}, }; const WINDOW_WIDTH: u32 = 800; @@ -453,7 +451,8 @@ fn main() { *control_flow = ControlFlow::Exit; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } diff --git a/examples/src/bin/teapot/main.rs b/examples/src/bin/teapot/main.rs index cf6d876b..7088609e 100644 --- a/examples/src/bin/teapot/main.rs +++ b/examples/src/bin/teapot/main.rs @@ -37,8 +37,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, shader::ShaderModule, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -132,6 +132,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -139,7 +140,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -244,7 +245,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -400,7 +402,7 @@ fn window_size_dependent_setup( device: Arc, vs: &ShaderModule, fs: &ShaderModule, - images: &[Arc>], + images: &[Arc], render_pass: Arc, ) -> (Arc, Vec>) { let dimensions = images[0].dimensions().width_height(); diff --git a/examples/src/bin/tessellation.rs b/examples/src/bin/tessellation.rs index 16f5286e..a6d750a6 100644 --- a/examples/src/bin/tessellation.rs +++ b/examples/src/bin/tessellation.rs @@ -45,8 +45,8 @@ use vulkano::{ }, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -238,6 +238,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -245,7 +246,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -380,7 +381,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -475,7 +477,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/texture_array/main.rs b/examples/src/bin/texture_array/main.rs index e3ff2049..fb6ead4b 100644 --- a/examples/src/bin/texture_array/main.rs +++ b/examples/src/bin/texture_array/main.rs @@ -40,8 +40,8 @@ use vulkano::{ render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, sampler::{Sampler, SamplerCreateInfo}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -136,6 +136,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); Swapchain::new( device.clone(), @@ -143,7 +144,7 @@ fn main() { SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, image_format, - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, ..ImageUsage::empty() @@ -306,7 +307,8 @@ fn main() { recreate_swapchain = true; } Event::RedrawEventsCleared => { - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -407,7 +409,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/examples/src/bin/triangle-v1_3.rs b/examples/src/bin/triangle-v1_3.rs index 5b18b266..7a48ee48 100644 --- a/examples/src/bin/triangle-v1_3.rs +++ b/examples/src/bin/triangle-v1_3.rs @@ -47,8 +47,8 @@ use vulkano::{ }, render_pass::{LoadOp, StoreOp}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, Version, VulkanLibrary, @@ -234,6 +234,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); // Please take a look at the docs for the meaning of the parameters we didn't mention. Swapchain::new( @@ -257,7 +258,7 @@ fn main() { // // Both of these cases need the swapchain to use the window dimensions, so we just // use that. - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, @@ -460,10 +461,11 @@ fn main() { // In this example that includes the swapchain, the framebuffers and the dynamic state viewport. if recreate_swapchain { // Get the new dimensions of the window. + let window = surface.object().unwrap().downcast_ref::().unwrap(); let (new_swapchain, new_images) = match swapchain.recreate(SwapchainCreateInfo { - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), ..swapchain.create_info() }) { Ok(r) => r, @@ -605,9 +607,9 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], viewport: &mut Viewport, -) -> Vec>>> { +) -> Vec>> { let dimensions = images[0].dimensions().width_height(); viewport.dimensions = [dimensions[0] as f32, dimensions[1] as f32]; diff --git a/examples/src/bin/triangle.rs b/examples/src/bin/triangle.rs index bf2e7b9e..cd99a85d 100644 --- a/examples/src/bin/triangle.rs +++ b/examples/src/bin/triangle.rs @@ -40,8 +40,8 @@ use vulkano::{ }, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, swapchain::{ - acquire_next_image, AcquireError, Swapchain, SwapchainAbstract, SwapchainCreateInfo, - SwapchainCreationError, SwapchainPresentInfo, + acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError, + SwapchainPresentInfo, }, sync::{self, FlushError, GpuFuture}, VulkanLibrary, @@ -214,6 +214,7 @@ fn main() { .unwrap()[0] .0, ); + let window = surface.object().unwrap().downcast_ref::().unwrap(); // Please take a look at the docs for the meaning of the parameters we didn't mention. Swapchain::new( @@ -237,7 +238,7 @@ fn main() { // // Both of these cases need the swapchain to use the window dimensions, so we just // use that. - image_extent: surface.window().inner_size().into(), + image_extent: window.inner_size().into(), image_usage: ImageUsage { color_attachment: true, @@ -459,7 +460,8 @@ fn main() { Event::RedrawEventsCleared => { // Do not draw frame when screen dimensions are zero. // On Windows, this can occur from minimizing the application. - let dimensions = surface.window().inner_size(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let dimensions = window.inner_size(); if dimensions.width == 0 || dimensions.height == 0 { return; } @@ -615,7 +617,7 @@ fn main() { /// This method is called once during initialization, then again whenever the window is resized fn window_size_dependent_setup( - images: &[Arc>], + images: &[Arc], render_pass: Arc, viewport: &mut Viewport, ) -> Vec> { diff --git a/vulkano-util/src/renderer.rs b/vulkano-util/src/renderer.rs index 1ee36355..690c6d3f 100644 --- a/vulkano-util/src/renderer.rs +++ b/vulkano-util/src/renderer.rs @@ -26,7 +26,7 @@ use vulkano_win::create_surface_from_winit; use winit::window::Window; /// Swapchain Image View. Your final render target typically. -pub type SwapchainImageView = Arc>>; +pub type SwapchainImageView = Arc>; /// Multipurpose image view pub type DeviceImageView = Arc>; @@ -41,10 +41,10 @@ pub const DEFAULT_IMAGE_FORMAT: Format = Format::R8G8B8A8_UNORM; /// /// The intended usage of this struct is through [`crate::window::VulkanoWindows`]. pub struct VulkanoWindowRenderer { - surface: Arc>, + surface: Arc, graphics_queue: Arc, compute_queue: Arc, - swapchain: Arc>, + swapchain: Arc, final_views: Vec, /// Additional image views that you can add which are resized with the window. /// Use associated functions to get access to these. @@ -67,7 +67,8 @@ impl VulkanoWindowRenderer { ) -> VulkanoWindowRenderer { // Create rendering surface from window let surface = - create_surface_from_winit(window, vulkano_context.instance().clone()).unwrap(); + create_surface_from_winit(Arc::new(window), vulkano_context.instance().clone()) + .unwrap(); // Create swap chain & frame(s) to which we'll render let (swap_chain, final_views) = Self::create_swapchain( @@ -97,10 +98,10 @@ impl VulkanoWindowRenderer { /// can be modified with the `swapchain_create_info_modify` function passed as an input. fn create_swapchain( device: Arc, - surface: Arc>, + surface: Arc, window_descriptor: &WindowDescriptor, swapchain_create_info_modify: fn(&mut SwapchainCreateInfo), - ) -> (Arc>, Vec) { + ) -> (Arc, Vec) { let surface_capabilities = device .physical_device() .surface_capabilities(&surface, Default::default()) @@ -112,7 +113,8 @@ impl VulkanoWindowRenderer { .unwrap()[0] .0, ); - let image_extent = surface.window().inner_size().into(); + let window = surface.object().unwrap().downcast_ref::().unwrap(); + let image_extent = window.inner_size().into(); let (swapchain, images) = Swapchain::new(device, surface, { let mut create_info = SwapchainCreateInfo { min_image_count: surface_capabilities.min_image_count, @@ -179,14 +181,14 @@ impl VulkanoWindowRenderer { /// Render target surface. #[inline] - pub fn surface(&self) -> Arc> { + pub fn surface(&self) -> Arc { self.surface.clone() } /// Winit window (you can manipulate window through this). #[inline] pub fn window(&self) -> &Window { - self.surface.window() + self.surface.object().unwrap().downcast_ref().unwrap() } /// Size of the physical window. diff --git a/vulkano-win/src/raw_window_handle.rs b/vulkano-win/src/raw_window_handle.rs index a30086ba..1b2fad81 100644 --- a/vulkano-win/src/raw_window_handle.rs +++ b/vulkano-win/src/raw_window_handle.rs @@ -5,7 +5,7 @@ use crate::get_metal_layer_macos; use raw_window_handle::{ HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, }; -use std::sync::Arc; +use std::{any::Any, sync::Arc}; use vulkano::{ instance::Instance, swapchain::{Surface, SurfaceCreationError}, @@ -13,24 +13,21 @@ use vulkano::{ /// Creates a vulkan surface from a generic window /// which implements HasRawWindowHandle and thus can reveal the os-dependent handle. -pub fn create_surface_from_handle( - window: W, +pub fn create_surface_from_handle( + window: Arc, instance: Arc, -) -> Result>, SurfaceCreationError> -where - W: HasRawWindowHandle + HasRawDisplayHandle, -{ +) -> Result, SurfaceCreationError> { unsafe { match window.raw_window_handle() { RawWindowHandle::AndroidNdk(h) => { - Surface::from_android(instance, h.a_native_window, window) + Surface::from_android(instance, h.a_native_window, Some(window)) } RawWindowHandle::UiKit(_h) => { #[cfg(target_os = "ios")] { // Ensure the layer is CAMetalLayer let layer = get_metal_layer_ios(_h.ui_view); - Surface::from_ios(instance, layer, window) + Surface::from_ios(instance, layer, Some(window)) } #[cfg(not(target_os = "ios"))] { @@ -42,7 +39,7 @@ where { // Ensure the layer is CAMetalLayer let layer = get_metal_layer_macos(_h.ns_view); - Surface::from_mac_os(instance, layer as *const (), window) + Surface::from_mac_os(instance, layer as *const (), Some(window)) } #[cfg(not(target_os = "macos"))] { @@ -54,22 +51,24 @@ where RawDisplayHandle::Wayland(d) => d, _ => panic!("Invalid RawDisplayHandle"), }; - Surface::from_wayland(instance, d.display, h.surface, window) + Surface::from_wayland(instance, d.display, h.surface, Some(window)) + } + RawWindowHandle::Win32(h) => { + Surface::from_win32(instance, h.hinstance, h.hwnd, Some(window)) } - RawWindowHandle::Win32(h) => Surface::from_win32(instance, h.hinstance, h.hwnd, window), RawWindowHandle::Xcb(h) => { let d = match window.raw_display_handle() { RawDisplayHandle::Xcb(d) => d, _ => panic!("Invalid RawDisplayHandle"), }; - Surface::from_xcb(instance, d.connection, h.window, window) + Surface::from_xcb(instance, d.connection, h.window, Some(window)) } RawWindowHandle::Xlib(h) => { let d = match window.raw_display_handle() { RawDisplayHandle::Xlib(d) => d, _ => panic!("Invalid RawDisplayHandle"), }; - Surface::from_xlib(instance, d.display, h.window, window) + Surface::from_xlib(instance, d.display, h.window, Some(window)) } RawWindowHandle::Web(_) => unimplemented!(), _ => unimplemented!(), diff --git a/vulkano-win/src/winit.rs b/vulkano-win/src/winit.rs index 05404c7d..653c3020 100644 --- a/vulkano-win/src/winit.rs +++ b/vulkano-win/src/winit.rs @@ -1,8 +1,6 @@ use std::{ - borrow::Borrow, error::Error, fmt::{Display, Error as FmtError, Formatter}, - rc::Rc, sync::Arc, }; use vulkano::{ @@ -36,13 +34,10 @@ pub fn required_extensions(library: &VulkanLibrary) -> InstanceExtensions { /// Create a surface from a Winit window or a reference to it. The surface takes `W` to prevent it /// from being dropped before the surface. -pub fn create_surface_from_winit( - window: W, +pub fn create_surface_from_winit( + window: Arc, instance: Arc, -) -> Result>, SurfaceCreationError> -where - W: SafeBorrow, -{ +) -> Result, SurfaceCreationError> { unsafe { winit_to_surface(instance, window) } } @@ -51,7 +46,7 @@ pub trait VkSurfaceBuild { self, event_loop: &EventLoopWindowTarget, instance: Arc, - ) -> Result>, CreationError>; + ) -> Result, CreationError>; } impl VkSurfaceBuild for WindowBuilder { @@ -59,8 +54,8 @@ impl VkSurfaceBuild for WindowBuilder { self, event_loop: &EventLoopWindowTarget, instance: Arc, - ) -> Result>, CreationError> { - let window = self.build(event_loop)?; + ) -> Result, CreationError> { + let window = Arc::new(self.build(event_loop)?); Ok(create_surface_from_winit(window, instance)?) } @@ -110,14 +105,14 @@ impl From for CreationError { } #[cfg(target_os = "android")] -unsafe fn winit_to_surface>( +unsafe fn winit_to_surface( instance: Arc, - win: W, -) -> Result>, SurfaceCreationError> { + window: Arc, +) -> Result, SurfaceCreationError> { use raw_window_handle::HasRawWindowHandle; use raw_window_handle::RawWindowHandle::AndroidNdk; - if let AndroidNdk(handle) = win.borrow().raw_window_handle() { - Surface::from_android(instance, handle.a_native_window, win) + if let AndroidNdk(handle) = window.raw_window_handle() { + Surface::from_android(instance, handle.a_native_window, Some(window)) } else { unreachable!("This should be unreachable if the target is android"); } @@ -129,33 +124,32 @@ unsafe fn winit_to_surface>( not(target_os = "macos"), not(target_os = "ios") ))] -unsafe fn winit_to_surface>( +unsafe fn winit_to_surface( instance: Arc, - win: W, -) -> Result>, SurfaceCreationError> { + window: Arc, +) -> Result, SurfaceCreationError> { use winit::platform::unix::WindowExtUnix; - match ( - win.borrow().wayland_display(), - win.borrow().wayland_surface(), - ) { - (Some(display), Some(surface)) => Surface::from_wayland(instance, display, surface, win), + match (window.wayland_display(), window.wayland_surface()) { + (Some(display), Some(surface)) => { + Surface::from_wayland(instance, display, surface, Some(window)) + } _ => { // No wayland display found, check if we can use xlib. // If not, we use xcb. if instance.enabled_extensions().khr_xlib_surface { Surface::from_xlib( instance, - win.borrow().xlib_display().unwrap(), - win.borrow().xlib_window().unwrap() as _, - win, + window.xlib_display().unwrap(), + window.xlib_window().unwrap() as _, + Some(window), ) } else { Surface::from_xcb( instance, - win.borrow().xcb_connection().unwrap(), - win.borrow().xlib_window().unwrap() as _, - win, + window.xcb_connection().unwrap(), + window.xlib_window().unwrap() as _, + Some(window), ) } } @@ -196,13 +190,13 @@ pub(crate) unsafe fn get_metal_layer_macos(view: *mut std::ffi::c_void) -> *mut } #[cfg(target_os = "macos")] -unsafe fn winit_to_surface>( +unsafe fn winit_to_surface( instance: Arc, - win: W, -) -> Result>, SurfaceCreationError> { + window: Arc, +) -> Result, SurfaceCreationError> { use winit::platform::macos::WindowExtMacOS; - let layer = get_metal_layer_macos(win.borrow().ns_view()); - Surface::from_mac_os(instance, layer as *const (), win) + let layer = get_metal_layer_macos(window.ns_view()); + Surface::from_mac_os(instance, layer as *const (), Some(window)) } #[cfg(target_os = "ios")] @@ -227,27 +221,27 @@ pub(crate) unsafe fn get_metal_layer_ios(view: *mut std::ffi::c_void) -> IOSMeta } #[cfg(target_os = "ios")] -unsafe fn winit_to_surface>( +unsafe fn winit_to_surface( instance: Arc, - win: W, -) -> Result>, SurfaceCreationError> { + window: Arc, +) -> Result, SurfaceCreationError> { use winit::platform::ios::WindowExtIOS; - let layer = get_metal_layer_ios(win.borrow().ui_view()); - Surface::from_ios(instance, layer, win) + let layer = get_metal_layer_ios(window.ui_view()); + Surface::from_ios(instance, layer, Some(window)) } #[cfg(target_os = "windows")] -unsafe fn winit_to_surface>( +unsafe fn winit_to_surface( instance: Arc, - win: W, -) -> Result>, SurfaceCreationError> { + window: Arc, +) -> Result, SurfaceCreationError> { use winit::platform::windows::WindowExtWindows; Surface::from_win32( instance, - win.borrow().hinstance() as *const (), - win.borrow().hwnd() as *const (), - win, + window.hinstance() as *const (), + window.hwnd() as *const (), + Some(window), ) } @@ -262,14 +256,3 @@ use winit::{monitor::MonitorHandle, platform::windows::MonitorHandleExtWindows}; pub fn create_win32_monitor_from_winit(monitor_handle: &MonitorHandle) -> Win32Monitor { unsafe { Win32Monitor::new(monitor_handle.hmonitor() as *const ()) } } - -/// An alternative to `Borrow` with the requirement that all calls to -/// `borrow` return the same object. -pub unsafe trait SafeBorrow: Borrow {} - -unsafe impl SafeBorrow for T {} -unsafe impl<'a, T> SafeBorrow for &'a T {} -unsafe impl<'a, T> SafeBorrow for &'a mut T {} -unsafe impl SafeBorrow for Rc {} -unsafe impl SafeBorrow for Arc {} -unsafe impl SafeBorrow for Box {} diff --git a/vulkano/src/device/physical.rs b/vulkano/src/device/physical.rs index 2a0cb09b..1ff745a6 100644 --- a/vulkano/src/device/physical.rs +++ b/vulkano/src/device/physical.rs @@ -1478,9 +1478,9 @@ impl PhysicalDevice { /// # Panics /// /// - Panics if the physical device and the surface don't belong to the same instance. - pub fn surface_capabilities( + pub fn surface_capabilities( &self, - surface: &Surface, + surface: &Surface, surface_info: SurfaceInfo, ) -> Result { self.validate_surface_capabilities(surface, &surface_info)?; @@ -1488,9 +1488,9 @@ impl PhysicalDevice { unsafe { Ok(self.surface_capabilities_unchecked(surface, surface_info)?) } } - fn validate_surface_capabilities( + fn validate_surface_capabilities( &self, - surface: &Surface, + surface: &Surface, surface_info: &SurfaceInfo, ) -> Result<(), PhysicalDeviceError> { if !(self @@ -1543,9 +1543,9 @@ impl PhysicalDevice { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn surface_capabilities_unchecked( + pub unsafe fn surface_capabilities_unchecked( &self, - surface: &Surface, + surface: &Surface, surface_info: SurfaceInfo, ) -> Result { /* Input */ @@ -1701,9 +1701,9 @@ impl PhysicalDevice { /// # Panics /// /// - Panics if the physical device and the surface don't belong to the same instance. - pub fn surface_formats( + pub fn surface_formats( &self, - surface: &Surface, + surface: &Surface, surface_info: SurfaceInfo, ) -> Result, PhysicalDeviceError> { self.validate_surface_formats(surface, &surface_info)?; @@ -1711,9 +1711,9 @@ impl PhysicalDevice { unsafe { Ok(self.surface_formats_unchecked(surface, surface_info)?) } } - fn validate_surface_formats( + fn validate_surface_formats( &self, - surface: &Surface, + surface: &Surface, surface_info: &SurfaceInfo, ) -> Result<(), PhysicalDeviceError> { if !(self @@ -1780,9 +1780,9 @@ impl PhysicalDevice { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn surface_formats_unchecked( + pub unsafe fn surface_formats_unchecked( &self, - surface: &Surface, + surface: &Surface, surface_info: SurfaceInfo, ) -> Result, VulkanError> { surface.surface_formats.get_or_try_insert( @@ -1928,19 +1928,16 @@ impl PhysicalDevice { /// # Panics /// /// - Panics if the physical device and the surface don't belong to the same instance. - pub fn surface_present_modes( + pub fn surface_present_modes( &self, - surface: &Surface, + surface: &Surface, ) -> Result, PhysicalDeviceError> { self.validate_surface_present_modes(surface)?; unsafe { Ok(self.surface_present_modes_unchecked(surface)?) } } - fn validate_surface_present_modes( - &self, - surface: &Surface, - ) -> Result<(), PhysicalDeviceError> { + fn validate_surface_present_modes(&self, surface: &Surface) -> Result<(), PhysicalDeviceError> { if !self.instance.enabled_extensions().khr_surface { return Err(PhysicalDeviceError::RequirementNotMet { required_for: "`surface_present_modes`", @@ -1966,9 +1963,9 @@ impl PhysicalDevice { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn surface_present_modes_unchecked( + pub unsafe fn surface_present_modes_unchecked( &self, - surface: &Surface, + surface: &Surface, ) -> Result, VulkanError> { surface .surface_present_modes @@ -2020,20 +2017,20 @@ impl PhysicalDevice { /// The results of this function are cached, so that future calls with the same arguments /// do not need to make a call to the Vulkan API again. #[inline] - pub fn surface_support( + pub fn surface_support( &self, queue_family_index: u32, - surface: &Surface, + surface: &Surface, ) -> Result { self.validate_surface_support(queue_family_index, surface)?; unsafe { Ok(self.surface_support_unchecked(queue_family_index, surface)?) } } - fn validate_surface_support( + fn validate_surface_support( &self, queue_family_index: u32, - _surface: &Surface, + _surface: &Surface, ) -> Result<(), PhysicalDeviceError> { if !self.instance.enabled_extensions().khr_surface { return Err(PhysicalDeviceError::RequirementNotMet { @@ -2057,10 +2054,10 @@ impl PhysicalDevice { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn surface_support_unchecked( + pub unsafe fn surface_support_unchecked( &self, queue_family_index: u32, - surface: &Surface, + surface: &Surface, ) -> Result { surface .surface_support diff --git a/vulkano/src/image/swapchain.rs b/vulkano/src/image/swapchain.rs index 5a20135e..98920401 100644 --- a/vulkano/src/image/swapchain.rs +++ b/vulkano/src/image/swapchain.rs @@ -10,7 +10,7 @@ use super::{traits::ImageContent, ImageAccess, ImageDescriptorLayouts, ImageInner, ImageLayout}; use crate::{ device::{Device, DeviceOwned}, - swapchain::{Swapchain, SwapchainAbstract}, + swapchain::Swapchain, OomError, }; use std::{ @@ -32,22 +32,19 @@ use std::{ /// the screen. Once an image has been presented, it can no longer be used unless it is acquired /// again. #[derive(Debug)] -pub struct SwapchainImage { - swapchain: Arc>, +pub struct SwapchainImage { + swapchain: Arc, image_index: u32, } -impl SwapchainImage -where - W: Send + Sync, -{ +impl SwapchainImage { /// Builds a `SwapchainImage` from raw components. /// /// This is an internal method that you shouldn't call. pub unsafe fn from_raw( - swapchain: Arc>, + swapchain: Arc, image_index: u32, - ) -> Result>, OomError> { + ) -> Result, OomError> { Ok(Arc::new(SwapchainImage { swapchain, image_index, @@ -55,7 +52,7 @@ where } /// Returns the swapchain this image belongs to. - pub fn swapchain(&self) -> &Arc> { + pub fn swapchain(&self) -> &Arc { &self.swapchain } @@ -72,16 +69,13 @@ where } } -unsafe impl DeviceOwned for SwapchainImage { +unsafe impl DeviceOwned for SwapchainImage { fn device(&self) -> &Arc { self.swapchain.device() } } -unsafe impl ImageAccess for SwapchainImage -where - W: Send + Sync, -{ +unsafe impl ImageAccess for SwapchainImage { fn inner(&self) -> ImageInner<'_> { self.my_image() } @@ -112,30 +106,21 @@ where } } -unsafe impl ImageContent

for SwapchainImage -where - W: Send + Sync, -{ +unsafe impl

ImageContent

for SwapchainImage { fn matches_format(&self) -> bool { true // FIXME: } } -impl PartialEq for SwapchainImage -where - W: Send + Sync, -{ +impl PartialEq for SwapchainImage { fn eq(&self, other: &Self) -> bool { self.inner() == other.inner() } } -impl Eq for SwapchainImage where W: Send + Sync {} +impl Eq for SwapchainImage {} -impl Hash for SwapchainImage -where - W: Send + Sync, -{ +impl Hash for SwapchainImage { fn hash(&self, state: &mut H) { self.inner().hash(state); } diff --git a/vulkano/src/swapchain/mod.rs b/vulkano/src/swapchain/mod.rs index d89d1dc4..1b4504b5 100644 --- a/vulkano/src/swapchain/mod.rs +++ b/vulkano/src/swapchain/mod.rs @@ -83,15 +83,18 @@ //! //! # use std::sync::Arc; //! # struct Window(*const u32); -//! # impl Window { -//! # fn hwnd(&self) -> *const u32 { self.0 } -//! # } -//! # +//! # impl Window { fn hwnd(&self) -> *const u32 { self.0 } } +//! # unsafe impl Send for Window {} +//! # unsafe impl Sync for Window {} //! # fn build_window() -> Arc { Arc::new(Window(ptr::null())) } //! let window = build_window(); // Third-party function, not provided by vulkano //! let _surface = unsafe { //! let hinstance: *const () = ptr::null(); // Windows-specific object -//! Surface::from_win32(instance.clone(), hinstance, window.hwnd(), Arc::clone(&window)).unwrap() +//! Surface::from_win32( +//! instance.clone(), +//! hinstance, window.hwnd(), +//! Some(window), +//! ).unwrap() //! }; //! ``` //! @@ -146,7 +149,7 @@ //! # use vulkano::device::Device; //! # use vulkano::swapchain::Surface; //! # use std::cmp::{max, min}; -//! # fn choose_caps(device: Arc, surface: Arc>) -> Result<(), Box> { +//! # fn choose_caps(device: Arc, surface: Arc) -> Result<(), Box> { //! let surface_capabilities = device //! .physical_device() //! .surface_capabilities(&surface, Default::default())?; @@ -181,7 +184,7 @@ //! # use vulkano::format::Format; //! # use vulkano::swapchain::{Surface, Swapchain, SurfaceTransform, PresentMode, CompositeAlpha, ColorSpace, FullScreenExclusive, SwapchainCreateInfo}; //! # fn create_swapchain( -//! # device: Arc, surface: Arc>, +//! # device: Arc, surface: Arc, //! # min_image_count: u32, image_format: Format, image_extent: [u32; 2], //! # pre_transform: SurfaceTransform, composite_alpha: CompositeAlpha, //! # present_mode: PresentMode, full_screen_exclusive: FullScreenExclusive @@ -246,7 +249,7 @@ //! use vulkano::swapchain::{self, SwapchainPresentInfo}; //! use vulkano::sync::GpuFuture; //! # let queue: ::std::sync::Arc<::vulkano::device::Queue> = return; -//! # let mut swapchain: ::std::sync::Arc> = return; +//! # let mut swapchain: ::std::sync::Arc = return; //! // let mut (swapchain, images) = Swapchain::new(...); //! loop { //! # let mut command_buffer: ::vulkano::command_buffer::PrimaryAutoCommandBuffer = return; @@ -284,8 +287,8 @@ //! use vulkano::sync::GpuFuture; //! //! // let (swapchain, images) = Swapchain::new(...); -//! # let mut swapchain: ::std::sync::Arc<::vulkano::swapchain::Swapchain<()>> = return; -//! # let mut images: Vec<::std::sync::Arc<::vulkano::image::SwapchainImage<()>>> = return; +//! # let mut swapchain: ::std::sync::Arc<::vulkano::swapchain::Swapchain> = return; +//! # let mut images: Vec<::std::sync::Arc<::vulkano::image::SwapchainImage>> = return; //! # let queue: ::std::sync::Arc<::vulkano::device::Queue> = return; //! let mut recreate_swapchain = false; //! @@ -333,8 +336,8 @@ pub use self::{ swapchain::{ acquire_next_image, acquire_next_image_raw, present, wait_for_present, AcquireError, AcquiredImage, FullScreenExclusive, FullScreenExclusiveError, PresentFuture, - PresentWaitError, Swapchain, SwapchainAbstract, SwapchainAcquireFuture, - SwapchainCreateInfo, SwapchainCreationError, Win32Monitor, + PresentWaitError, Swapchain, SwapchainAcquireFuture, SwapchainCreateInfo, + SwapchainCreationError, Win32Monitor, }, }; #[cfg(target_os = "ios")] @@ -383,7 +386,7 @@ pub struct SwapchainPresentInfo { /// The swapchain to present to. /// /// There is no default value. - pub swapchain: Arc, + pub swapchain: Arc, /// The index of the swapchain image to present to. /// @@ -423,7 +426,7 @@ pub struct SwapchainPresentInfo { impl SwapchainPresentInfo { /// Returns a `SwapchainPresentInfo` with the specified `swapchain` and `image_index`. #[inline] - pub fn swapchain_image_index(swapchain: Arc, image_index: u32) -> Self { + pub fn swapchain_image_index(swapchain: Arc, image_index: u32) -> Self { Self { swapchain, image_index, @@ -438,7 +441,7 @@ impl SwapchainPresentInfo { #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct RectangleLayer { /// Coordinates in pixels of the top-left hand corner of the rectangle. - pub offset: [i32; 2], + pub offset: [u32; 2], /// Dimensions in pixels of the rectangle. pub extent: [u32; 2], @@ -450,13 +453,9 @@ pub struct RectangleLayer { impl RectangleLayer { /// Returns true if this rectangle layer is compatible with swapchain. #[inline] - pub fn is_compatible_with(&self, swapchain: &dyn SwapchainAbstract) -> bool { - // FIXME negative offset is not disallowed by spec, but semantically should not be possible - debug_assert!(self.offset[0] >= 0); - debug_assert!(self.offset[1] >= 0); - - self.offset[0] as u32 + self.extent[0] <= swapchain.image_extent()[0] - && self.offset[1] as u32 + self.extent[1] <= swapchain.image_extent()[1] + pub fn is_compatible_with(&self, swapchain: &Swapchain) -> bool { + self.offset[0] + self.extent[0] <= swapchain.image_extent()[0] + && self.offset[1] + self.extent[1] <= swapchain.image_extent()[1] && self.layer < swapchain.image_array_layers() } } @@ -466,8 +465,8 @@ impl From<&RectangleLayer> for ash::vk::RectLayerKHR { fn from(val: &RectangleLayer) -> Self { ash::vk::RectLayerKHR { offset: ash::vk::Offset2D { - x: val.offset[0], - y: val.offset[1], + x: val.offset[0] as i32, + y: val.offset[1] as i32, }, extent: ash::vk::Extent2D { width: val.extent[0], diff --git a/vulkano/src/swapchain/surface.rs b/vulkano/src/swapchain/surface.rs index a711f098..a4eea19f 100644 --- a/vulkano/src/swapchain/surface.rs +++ b/vulkano/src/swapchain/surface.rs @@ -25,6 +25,7 @@ use crate::{ use objc::{class, msg_send, runtime::Object, sel, sel_impl}; use std::{ + any::Any, error::Error, fmt::{Debug, Display, Error as FmtError, Formatter}, hash::{Hash, Hasher}, @@ -36,11 +37,11 @@ use std::{ /// Represents a surface on the screen. /// /// Creating a `Surface` is platform-specific. -pub struct Surface { +pub struct Surface { handle: ash::vk::SurfaceKHR, instance: Arc, api: SurfaceApi, - window: W, + object: Option>, // If true, a swapchain has been associated to this surface, and that any new swapchain // creation should be forbidden. has_swapchain: AtomicBool, @@ -56,7 +57,7 @@ pub struct Surface { pub(crate) surface_support: OnceCache<(ash::vk::PhysicalDevice, u32), bool>, } -impl Surface { +impl Surface { /// Creates a `Surface` from a raw handle. /// /// # Safety @@ -64,18 +65,18 @@ impl Surface { /// - `handle` must be a valid Vulkan object handle created from `instance`. /// - `handle` must have been created from `api`. /// - The window object that `handle` was created from must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_handle( instance: Arc, handle: ash::vk::SurfaceKHR, api: SurfaceApi, - win: W, + object: Option>, ) -> Self { Surface { handle, instance, api, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -91,10 +92,13 @@ impl Surface { /// /// Presenting to a headless surface does nothing, so this is mostly useless in itself. However, /// it may be useful for testing, and it is available for future extensions to layer on top of. - pub fn headless(instance: Arc, win: W) -> Result, SurfaceCreationError> { + pub fn headless( + instance: Arc, + object: Option>, + ) -> Result, SurfaceCreationError> { Self::validate_headless(&instance)?; - unsafe { Ok(Self::headless_unchecked(instance, win)?) } + unsafe { Ok(Self::headless_unchecked(instance, object)?) } } fn validate_headless(instance: &Instance) -> Result<(), SurfaceCreationError> { @@ -114,7 +118,7 @@ impl Surface { #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] pub unsafe fn headless_unchecked( instance: Arc, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::HeadlessSurfaceCreateInfoEXT { flags: ash::vk::HeadlessSurfaceCreateFlagsEXT::empty(), @@ -139,7 +143,7 @@ impl Surface { handle, instance, api: SurfaceApi::Headless, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -160,7 +164,7 @@ impl Surface { pub fn from_display_plane( display_mode: &DisplayMode, plane: &DisplayPlane, - ) -> Result>, SurfaceCreationError> { + ) -> Result, SurfaceCreationError> { Self::validate_from_display_plane(display_mode, plane)?; unsafe { Ok(Self::from_display_plane_unchecked(display_mode, plane)?) } @@ -199,7 +203,7 @@ impl Surface { pub unsafe fn from_display_plane_unchecked( display_mode: &DisplayMode, plane: &DisplayPlane, - ) -> Result>, VulkanError> { + ) -> Result, VulkanError> { let instance = display_mode.display().physical_device().instance(); let create_info = ash::vk::DisplaySurfaceCreateInfoKHR { @@ -236,7 +240,7 @@ impl Surface { handle, instance: instance.clone(), api: SurfaceApi::DisplayPlane, - window: (), + object: None, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -254,20 +258,24 @@ impl Surface { /// /// - `window` must be a valid Android `ANativeWindow` handle. /// - The object referred to by `window` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. - pub unsafe fn from_android( + /// The `object` parameter can be used to ensure this. + pub unsafe fn from_android( instance: Arc, - window: *const T, - win: W, + window: *const W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_android(&instance, window)?; - Ok(Self::from_android_unchecked(instance, window, win)?) + Ok(Self::from_android_unchecked( + instance, + window, + object, + )?) } - fn validate_from_android( + fn validate_from_android( instance: &Instance, - _window: *const T, + _window: *const W, ) -> Result<(), SurfaceCreationError> { if !instance.enabled_extensions().khr_android_surface { return Err(SurfaceCreationError::RequirementNotMet { @@ -286,10 +294,10 @@ impl Surface { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn from_android_unchecked( + pub unsafe fn from_android_unchecked( instance: Arc, - window: *const T, - win: W, + window: *const W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::AndroidSurfaceCreateInfoKHR { flags: ash::vk::AndroidSurfaceCreateFlagsKHR::empty(), @@ -315,7 +323,7 @@ impl Surface { handle, instance, api: SurfaceApi::Android, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -334,16 +342,21 @@ impl Surface { /// - `dfb` must be a valid DirectFB `IDirectFB` handle. /// - `surface` must be a valid DirectFB `IDirectFBSurface` handle. /// - The object referred to by `dfb` and `surface` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_directfb( instance: Arc, dfb: *const D, surface: *const S, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_directfb(&instance, dfb, surface)?; - Ok(Self::from_directfb_unchecked(instance, dfb, surface, win)?) + Ok(Self::from_directfb_unchecked( + instance, + dfb, + surface, + object, + )?) } fn validate_from_directfb( @@ -375,7 +388,7 @@ impl Surface { instance: Arc, dfb: *const D, surface: *const S, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::DirectFBSurfaceCreateInfoEXT { flags: ash::vk::DirectFBSurfaceCreateFlagsEXT::empty(), @@ -402,7 +415,7 @@ impl Surface { handle, instance, api: SurfaceApi::DirectFB, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -420,18 +433,18 @@ impl Surface { /// /// - `image_pipe_handle` must be a valid Fuchsia `zx_handle_t` handle. /// - The object referred to by `image_pipe_handle` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_fuchsia_image_pipe( instance: Arc, image_pipe_handle: ash::vk::zx_handle_t, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_fuchsia_image_pipe(&instance, image_pipe_handle)?; Ok(Self::from_fuchsia_image_pipe_unchecked( instance, image_pipe_handle, - win, + object, )?) } @@ -459,7 +472,7 @@ impl Surface { pub unsafe fn from_fuchsia_image_pipe_unchecked( instance: Arc, image_pipe_handle: ash::vk::zx_handle_t, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::ImagePipeSurfaceCreateInfoFUCHSIA { flags: ash::vk::ImagePipeSurfaceCreateFlagsFUCHSIA::empty(), @@ -486,7 +499,7 @@ impl Surface { handle, instance, api: SurfaceApi::FuchsiaImagePipe, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -504,18 +517,18 @@ impl Surface { /// /// - `stream_descriptor` must be a valid Google Games Platform `GgpStreamDescriptor` handle. /// - The object referred to by `stream_descriptor` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_ggp_stream_descriptor( instance: Arc, stream_descriptor: ash::vk::GgpStreamDescriptor, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_ggp_stream_descriptor(&instance, stream_descriptor)?; Ok(Self::from_ggp_stream_descriptor_unchecked( instance, stream_descriptor, - win, + object, )?) } @@ -543,7 +556,7 @@ impl Surface { pub unsafe fn from_ggp_stream_descriptor_unchecked( instance: Arc, stream_descriptor: ash::vk::GgpStreamDescriptor, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::StreamDescriptorSurfaceCreateInfoGGP { flags: ash::vk::StreamDescriptorSurfaceCreateFlagsGGP::empty(), @@ -570,7 +583,7 @@ impl Surface { handle, instance, api: SurfaceApi::GgpStreamDescriptor, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -588,17 +601,21 @@ impl Surface { /// /// - `metal_layer` must be a valid `IOSMetalLayer` handle. /// - The object referred to by `metal_layer` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. /// - The `UIView` must be backed by a `CALayer` instance of type `CAMetalLayer`. #[cfg(target_os = "ios")] pub unsafe fn from_ios( instance: Arc, metal_layer: IOSMetalLayer, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_ios(&instance, &metal_layer)?; - Ok(Self::from_ios_unchecked(instance, metal_layer, win)?) + Ok(Self::from_ios_unchecked( + instance, + metal_layer, + object, + )?) } #[cfg(target_os = "ios")] @@ -630,7 +647,7 @@ impl Surface { pub unsafe fn from_ios_unchecked( instance: Arc, metal_layer: IOSMetalLayer, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::IOSSurfaceCreateInfoMVK { flags: ash::vk::IOSSurfaceCreateFlagsMVK::empty(), @@ -656,7 +673,7 @@ impl Surface { handle, instance, api: SurfaceApi::Ios, - window: win, + object, has_swapchain: AtomicBool::new(false), metal_layer, @@ -673,23 +690,23 @@ impl Surface { /// /// - `view` must be a valid `CAMetalLayer` or `NSView` handle. /// - The object referred to by `view` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. /// - The `NSView` must be backed by a `CALayer` instance of type `CAMetalLayer`. #[cfg(target_os = "macos")] - pub unsafe fn from_mac_os( + pub unsafe fn from_mac_os( instance: Arc, - view: *const T, - win: W, + view: *const V, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_mac_os(&instance, view)?; - Ok(Self::from_mac_os_unchecked(instance, view, win)?) + Ok(Self::from_mac_os_unchecked(instance, view, object)?) } #[cfg(target_os = "macos")] - fn validate_from_mac_os( + fn validate_from_mac_os( instance: &Instance, - _view: *const T, + _view: *const V, ) -> Result<(), SurfaceCreationError> { if !instance.enabled_extensions().mvk_macos_surface { return Err(SurfaceCreationError::RequirementNotMet { @@ -712,10 +729,10 @@ impl Surface { #[cfg(target_os = "macos")] #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn from_mac_os_unchecked( + pub unsafe fn from_mac_os_unchecked( instance: Arc, - view: *const T, - win: W, + view: *const V, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::MacOSSurfaceCreateInfoMVK { flags: ash::vk::MacOSSurfaceCreateFlagsMVK::empty(), @@ -741,7 +758,7 @@ impl Surface { handle, instance, api: SurfaceApi::MacOs, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -759,20 +776,20 @@ impl Surface { /// /// - `layer` must be a valid Metal `CAMetalLayer` handle. /// - The object referred to by `layer` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. - pub unsafe fn from_metal( + /// The `object` parameter can be used to ensure this. + pub unsafe fn from_metal( instance: Arc, - layer: *const T, - win: W, + layer: *const L, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_metal(&instance, layer)?; - Ok(Self::from_metal_unchecked(instance, layer, win)?) + Ok(Self::from_metal_unchecked(instance, layer, object)?) } - fn validate_from_metal( + fn validate_from_metal( instance: &Instance, - _layer: *const T, + _layer: *const L, ) -> Result<(), SurfaceCreationError> { if !instance.enabled_extensions().ext_metal_surface { return Err(SurfaceCreationError::RequirementNotMet { @@ -788,10 +805,10 @@ impl Surface { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn from_metal_unchecked( + pub unsafe fn from_metal_unchecked( instance: Arc, - layer: *const T, - win: W, + layer: *const L, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::MetalSurfaceCreateInfoEXT { flags: ash::vk::MetalSurfaceCreateFlagsEXT::empty(), @@ -817,7 +834,7 @@ impl Surface { handle, instance, api: SurfaceApi::Metal, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -836,24 +853,27 @@ impl Surface { /// - `context` must be a valid QNX Screen `_screen_context` handle. /// - `window` must be a valid QNX Screen `_screen_window` handle. /// - The object referred to by `window` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. - pub unsafe fn from_qnx_screen( + /// The `object` parameter can be used to ensure this. + pub unsafe fn from_qnx_screen( instance: Arc, - context: *const T, - window: *const U, - win: W, + context: *const C, + window: *const W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_qnx_screen(&instance, context, window)?; Ok(Self::from_qnx_screen_unchecked( - instance, context, window, win, + instance, + context, + window, + object, )?) } - fn validate_from_qnx_screen( + fn validate_from_qnx_screen( instance: &Instance, - _context: *const T, - _window: *const U, + _context: *const C, + _window: *const W, ) -> Result<(), SurfaceCreationError> { if !instance.enabled_extensions().qnx_screen_surface { return Err(SurfaceCreationError::RequirementNotMet { @@ -875,11 +895,11 @@ impl Surface { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn from_qnx_screen_unchecked( + pub unsafe fn from_qnx_screen_unchecked( instance: Arc, - context: *const T, - window: *const U, - win: W, + context: *const C, + window: *const W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::ScreenSurfaceCreateInfoQNX { flags: ash::vk::ScreenSurfaceCreateFlagsQNX::empty(), @@ -906,7 +926,7 @@ impl Surface { handle, instance, api: SurfaceApi::Qnx, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -924,20 +944,20 @@ impl Surface { /// /// - `window` must be a valid `nn::vi::NativeWindowHandle` handle. /// - The object referred to by `window` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. - pub unsafe fn from_vi( + /// The `object` parameter can be used to ensure this. + pub unsafe fn from_vi( instance: Arc, - window: *const T, - win: W, + window: *const W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_vi(&instance, window)?; - Ok(Self::from_vi_unchecked(instance, window, win)?) + Ok(Self::from_vi_unchecked(instance, window, object)?) } - fn validate_from_vi( + fn validate_from_vi( instance: &Instance, - _window: *const T, + _window: *const W, ) -> Result<(), SurfaceCreationError> { if !instance.enabled_extensions().nn_vi_surface { return Err(SurfaceCreationError::RequirementNotMet { @@ -956,10 +976,10 @@ impl Surface { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn from_vi_unchecked( + pub unsafe fn from_vi_unchecked( instance: Arc, - window: *const T, - win: W, + window: *const W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::ViSurfaceCreateInfoNN { flags: ash::vk::ViSurfaceCreateFlagsNN::empty(), @@ -985,7 +1005,7 @@ impl Surface { handle, instance, api: SurfaceApi::Vi, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -1006,17 +1026,20 @@ impl Surface { /// - `display` must be a valid Wayland `wl_display` handle. /// - `surface` must be a valid Wayland `wl_surface` handle. /// - The objects referred to by `display` and `surface` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_wayland( instance: Arc, display: *const D, surface: *const S, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_wayland(&instance, display, surface)?; Ok(Self::from_wayland_unchecked( - instance, display, surface, win, + instance, + display, + surface, + object, )?) } @@ -1049,7 +1072,7 @@ impl Surface { instance: Arc, display: *const D, surface: *const S, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::WaylandSurfaceCreateInfoKHR { flags: ash::vk::WaylandSurfaceCreateFlagsKHR::empty(), @@ -1076,7 +1099,7 @@ impl Surface { handle, instance, api: SurfaceApi::Wayland, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -1097,22 +1120,27 @@ impl Surface { /// - `hinstance` must be a valid Win32 `HINSTANCE` handle. /// - `hwnd` must be a valid Win32 `HWND` handle. /// - The objects referred to by `hwnd` and `hinstance` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. - pub unsafe fn from_win32( + /// The `object` parameter can be used to ensure this. + pub unsafe fn from_win32( instance: Arc, - hinstance: *const T, - hwnd: *const U, - win: W, + hinstance: *const I, + hwnd: *const W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_win32(&instance, hinstance, hwnd)?; - Ok(Self::from_win32_unchecked(instance, hinstance, hwnd, win)?) + Ok(Self::from_win32_unchecked( + instance, + hinstance, + hwnd, + object, + )?) } - fn validate_from_win32( + fn validate_from_win32( instance: &Instance, - _hinstance: *const T, - _hwnd: *const U, + _hinstance: *const I, + _hwnd: *const W, ) -> Result<(), SurfaceCreationError> { if !instance.enabled_extensions().khr_win32_surface { return Err(SurfaceCreationError::RequirementNotMet { @@ -1134,11 +1162,11 @@ impl Surface { } #[cfg_attr(not(feature = "document_unchecked"), doc(hidden))] - pub unsafe fn from_win32_unchecked( + pub unsafe fn from_win32_unchecked( instance: Arc, - hinstance: *const T, - hwnd: *const U, - win: W, + hinstance: *const I, + hwnd: *const W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::Win32SurfaceCreateInfoKHR { flags: ash::vk::Win32SurfaceCreateFlagsKHR::empty(), @@ -1165,7 +1193,7 @@ impl Surface { handle, instance, api: SurfaceApi::Win32, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -1186,16 +1214,21 @@ impl Surface { /// - `connection` must be a valid X11 `xcb_connection_t` handle. /// - `window` must be a valid X11 `xcb_window_t` handle. /// - The objects referred to by `connection` and `window` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_xcb( instance: Arc, connection: *const C, window: ash::vk::xcb_window_t, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_xcb(&instance, connection, window)?; - Ok(Self::from_xcb_unchecked(instance, connection, window, win)?) + Ok(Self::from_xcb_unchecked( + instance, + connection, + window, + object, + )?) } fn validate_from_xcb( @@ -1227,7 +1260,7 @@ impl Surface { instance: Arc, connection: *const C, window: ash::vk::xcb_window_t, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::XcbSurfaceCreateInfoKHR { flags: ash::vk::XcbSurfaceCreateFlagsKHR::empty(), @@ -1254,7 +1287,7 @@ impl Surface { handle, instance, api: SurfaceApi::Xcb, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -1275,16 +1308,21 @@ impl Surface { /// - `display` must be a valid Xlib `Display` handle. /// - `window` must be a valid Xlib `Window` handle. /// - The objects referred to by `display` and `window` must outlive the created `Surface`. - /// The `win` parameter can be used to ensure this. + /// The `object` parameter can be used to ensure this. pub unsafe fn from_xlib( instance: Arc, display: *const D, window: ash::vk::Window, - win: W, + object: Option>, ) -> Result, SurfaceCreationError> { Self::validate_from_xlib(&instance, display, window)?; - Ok(Self::from_xlib_unchecked(instance, display, window, win)?) + Ok(Self::from_xlib_unchecked( + instance, + display, + window, + object, + )?) } fn validate_from_xlib( @@ -1316,7 +1354,7 @@ impl Surface { instance: Arc, display: *const D, window: ash::vk::Window, - win: W, + object: Option>, ) -> Result, VulkanError> { let create_info = ash::vk::XlibSurfaceCreateInfoKHR { flags: ash::vk::XlibSurfaceCreateFlagsKHR::empty(), @@ -1343,7 +1381,7 @@ impl Surface { handle, instance, api: SurfaceApi::Xlib, - window: win, + object, has_swapchain: AtomicBool::new(false), #[cfg(target_os = "ios")] @@ -1356,18 +1394,22 @@ impl Surface { } /// Returns the instance this surface was created with. + #[inline] pub fn instance(&self) -> &Arc { &self.instance } /// Returns the windowing API that was used to construct the surface. + #[inline] pub fn api(&self) -> SurfaceApi { self.api } - /// Returns a reference to the `W` type parameter that was passed when creating the surface. - pub fn window(&self) -> &W { - &self.window + /// Returns a reference to the `object` parameter that was passed when creating the + /// surface. + #[inline] + pub fn object(&self) -> Option<&Arc> { + self.object.as_ref() } /// Resizes the sublayer bounds on iOS. @@ -1388,7 +1430,7 @@ impl Surface { } } -impl Drop for Surface { +impl Drop for Surface { fn drop(&mut self) { unsafe { let fns = self.instance.fns(); @@ -1401,7 +1443,7 @@ impl Drop for Surface { } } -unsafe impl VulkanObject for Surface { +unsafe impl VulkanObject for Surface { type Object = ash::vk::SurfaceKHR; fn internal_object(&self) -> ash::vk::SurfaceKHR { @@ -1409,13 +1451,13 @@ unsafe impl VulkanObject for Surface { } } -impl Debug for Surface { +impl Debug for Surface { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { let Self { handle, instance, api, - window: _, + object: _, has_swapchain, .. } = self; @@ -1430,22 +1472,22 @@ impl Debug for Surface { } } -impl PartialEq for Surface { +impl PartialEq for Surface { fn eq(&self, other: &Self) -> bool { self.handle == other.handle && self.instance() == other.instance() } } -impl Eq for Surface {} +impl Eq for Surface {} -impl Hash for Surface { +impl Hash for Surface { fn hash(&self, state: &mut H) { self.handle.hash(state); self.instance().hash(state); } } -unsafe impl SurfaceSwapchainLock for Surface { +unsafe impl SurfaceSwapchainLock for Surface { fn flag(&self) -> &AtomicBool { &self.has_swapchain } @@ -2032,7 +2074,7 @@ mod tests { #[test] fn khr_win32_surface_ext_missing() { let instance = instance!(); - match unsafe { Surface::from_win32(instance, ptr::null::(), ptr::null::(), ()) } { + match unsafe { Surface::from_win32(instance, ptr::null::(), ptr::null::(), None) } { Err(SurfaceCreationError::RequirementNotMet { requires_one_of: RequiresOneOf { @@ -2048,7 +2090,7 @@ mod tests { #[test] fn khr_xcb_surface_ext_missing() { let instance = instance!(); - match unsafe { Surface::from_xcb(instance, ptr::null::(), 0, ()) } { + match unsafe { Surface::from_xcb(instance, ptr::null::(), 0, None) } { Err(SurfaceCreationError::RequirementNotMet { requires_one_of: RequiresOneOf { @@ -2064,7 +2106,7 @@ mod tests { #[test] fn khr_xlib_surface_ext_missing() { let instance = instance!(); - match unsafe { Surface::from_xlib(instance, ptr::null::(), 0, ()) } { + match unsafe { Surface::from_xlib(instance, ptr::null::(), 0, None) } { Err(SurfaceCreationError::RequirementNotMet { requires_one_of: RequiresOneOf { @@ -2080,7 +2122,8 @@ mod tests { #[test] fn khr_wayland_surface_ext_missing() { let instance = instance!(); - match unsafe { Surface::from_wayland(instance, ptr::null::(), ptr::null::(), ()) } { + match unsafe { Surface::from_wayland(instance, ptr::null::(), ptr::null::(), None) } + { Err(SurfaceCreationError::RequirementNotMet { requires_one_of: RequiresOneOf { @@ -2096,7 +2139,7 @@ mod tests { #[test] fn khr_android_surface_ext_missing() { let instance = instance!(); - match unsafe { Surface::from_android(instance, ptr::null::(), ()) } { + match unsafe { Surface::from_android(instance, ptr::null::(), None) } { Err(SurfaceCreationError::RequirementNotMet { requires_one_of: RequiresOneOf { diff --git a/vulkano/src/swapchain/swapchain.rs b/vulkano/src/swapchain/swapchain.rs index 25b914c9..3dd8ecae 100644 --- a/vulkano/src/swapchain/swapchain.rs +++ b/vulkano/src/swapchain/swapchain.rs @@ -45,10 +45,10 @@ use std::{ }; /// Contains the swapping system and the images that can be shown on a surface. -pub struct Swapchain { +pub struct Swapchain { handle: ash::vk::SwapchainKHR, device: Arc, - surface: Arc>, + surface: Arc, min_image_count: u32, image_format: Format, @@ -88,10 +88,7 @@ struct ImageEntry { undefined_layout: AtomicBool, } -impl Swapchain -where - W: Send + Sync, -{ +impl Swapchain { /// Creates a new `Swapchain`. /// /// This function returns the swapchain plus a list of the images that belong to the @@ -106,9 +103,9 @@ where // TODO: isn't it unsafe to take the surface through an Arc when it comes to vulkano-win? pub fn new( device: Arc, - surface: Arc>, + surface: Arc, mut create_info: SwapchainCreateInfo, - ) -> Result<(Arc>, Vec>>), SwapchainCreationError> { + ) -> Result<(Arc, Vec>), SwapchainCreationError> { Self::validate(&device, &surface, &mut create_info)?; // Checking that the surface doesn't already have a swapchain. @@ -181,7 +178,7 @@ where pub fn recreate( self: &Arc, mut create_info: SwapchainCreateInfo, - ) -> Result<(Arc>, Vec>>), SwapchainCreationError> { + ) -> Result<(Arc, Vec>), SwapchainCreationError> { Self::validate(&self.device, &self.surface, &mut create_info)?; { @@ -267,7 +264,7 @@ where fn validate( device: &Device, - surface: &Surface, + surface: &Surface, create_info: &mut SwapchainCreateInfo, ) -> Result<(), SwapchainCreationError> { let &mut SwapchainCreateInfo { @@ -554,9 +551,9 @@ where unsafe fn create( device: &Device, - surface: &Surface, + surface: &Surface, create_info: &SwapchainCreateInfo, - old_swapchain: Option<&Swapchain>, + old_swapchain: Option<&Swapchain>, ) -> Result<(ash::vk::SwapchainKHR, Vec), SwapchainCreationError> { let &SwapchainCreateInfo { min_image_count, @@ -746,10 +743,63 @@ where } /// Returns the saved Surface, from the Swapchain creation. - pub fn surface(&self) -> &Arc> { + pub fn surface(&self) -> &Arc { &self.surface } + /// Returns one of the images that belongs to this swapchain. + #[inline] + pub fn raw_image(&self, image_index: u32) -> Option> { + self.images.get(image_index as usize).map(|i| ImageInner { + image: &i.image, + first_layer: 0, + num_layers: self.image_array_layers, + first_mipmap_level: 0, + num_mipmap_levels: 1, + }) + } + + /// Returns the number of images of the swapchain. + #[inline] + pub fn image_count(&self) -> u32 { + self.images.len() as u32 + } + + /// Returns the format of the images of the swapchain. + #[inline] + pub fn image_format(&self) -> Format { + self.image_format + } + + /// Returns the color space of the images of the swapchain. + #[inline] + pub fn image_color_space(&self) -> ColorSpace { + self.image_color_space + } + + /// Returns the extent of the images of the swapchain. + #[inline] + pub fn image_extent(&self) -> [u32; 2] { + self.image_extent + } + + /// Returns the number of array layers of the images of the swapchain. + #[inline] + pub fn image_array_layers(&self) -> u32 { + self.image_array_layers + } + + #[inline] + pub(crate) unsafe fn full_screen_exclusive_held(&self) -> &AtomicBool { + &self.full_screen_exclusive_held + } + + #[inline] + pub(crate) unsafe fn try_claim_present_id(&self, present_id: NonZeroU64) -> bool { + let present_id = u64::from(present_id); + self.prev_present_id.fetch_max(present_id, Ordering::SeqCst) < present_id + } + /// Returns the pre-transform that was passed when creating the swapchain. pub fn pre_transform(&self) -> SurfaceTransform { self.pre_transform @@ -867,7 +917,7 @@ where } } -impl Drop for Swapchain { +impl Drop for Swapchain { fn drop(&mut self) { unsafe { let fns = self.device.fns(); @@ -881,7 +931,7 @@ impl Drop for Swapchain { } } -unsafe impl VulkanObject for Swapchain { +unsafe impl VulkanObject for Swapchain { type Object = ash::vk::SwapchainKHR; fn internal_object(&self) -> ash::vk::SwapchainKHR { @@ -889,28 +939,28 @@ unsafe impl VulkanObject for Swapchain { } } -unsafe impl DeviceOwned for Swapchain { +unsafe impl DeviceOwned for Swapchain { fn device(&self) -> &Arc { &self.device } } -impl PartialEq for Swapchain { +impl PartialEq for Swapchain { fn eq(&self, other: &Self) -> bool { self.handle == other.handle && self.device() == other.device() } } -impl Eq for Swapchain {} +impl Eq for Swapchain {} -impl Hash for Swapchain { +impl Hash for Swapchain { fn hash(&self, state: &mut H) { self.handle.hash(state); self.device().hash(state); } } -impl Debug for Swapchain { +impl Debug for Swapchain { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { let Self { handle, @@ -960,95 +1010,6 @@ impl Debug for Swapchain { } } -/// Trait for types that represent the GPU can access an image view. -pub unsafe trait SwapchainAbstract: - VulkanObject + DeviceOwned + Debug + Send + Sync -{ - /// Returns one of the images that belongs to this swapchain. - fn raw_image(&self, index: u32) -> Option>; - - /// Returns the number of images of the swapchain. - fn image_count(&self) -> u32; - - /// Returns the format of the images of the swapchain. - fn image_format(&self) -> Format; - - /// Returns the color space of the images of the swapchain. - fn image_color_space(&self) -> ColorSpace; - - /// Returns the extent of the images of the swapchain. - fn image_extent(&self) -> [u32; 2]; - - /// Returns the number of array layers of the images of the swapchain. - fn image_array_layers(&self) -> u32; - - #[doc(hidden)] - unsafe fn try_claim_present_id(&self, present_id: NonZeroU64) -> bool; - - #[doc(hidden)] - unsafe fn full_screen_exclusive_held(&self) -> &AtomicBool; -} - -unsafe impl SwapchainAbstract for Swapchain -where - W: Send + Sync, -{ - fn raw_image(&self, image_index: u32) -> Option> { - self.images.get(image_index as usize).map(|i| ImageInner { - image: &i.image, - first_layer: 0, - num_layers: self.image_array_layers, - first_mipmap_level: 0, - num_mipmap_levels: 1, - }) - } - - fn image_count(&self) -> u32 { - self.images.len() as u32 - } - - fn image_format(&self) -> Format { - self.image_format - } - - fn image_color_space(&self) -> ColorSpace { - self.image_color_space - } - - fn image_extent(&self) -> [u32; 2] { - self.image_extent - } - - fn image_array_layers(&self) -> u32 { - self.image_array_layers - } - - unsafe fn full_screen_exclusive_held(&self) -> &AtomicBool { - &self.full_screen_exclusive_held - } - - unsafe fn try_claim_present_id(&self, present_id: NonZeroU64) -> bool { - let present_id = u64::from(present_id); - self.prev_present_id.fetch_max(present_id, Ordering::SeqCst) < present_id - } -} - -impl PartialEq for dyn SwapchainAbstract { - #[inline] - fn eq(&self, other: &Self) -> bool { - self.internal_object() == other.internal_object() && self.device() == other.device() - } -} - -impl Eq for dyn SwapchainAbstract {} - -impl Hash for dyn SwapchainAbstract { - fn hash(&self, state: &mut H) { - self.internal_object().hash(state); - self.device().hash(state); - } -} - /// Parameters to create a new `Swapchain`. /// /// Many of the values here must be supported by the physical device. @@ -1538,10 +1499,10 @@ impl From for FullScreenExclusiveError { /// The second field in the tuple in the Ok result is a bool represent if the acquisition was /// suboptimal. In this case the acquired image is still usable, but the swapchain should be /// recreated as the Surface's properties no longer match the swapchain. -pub fn acquire_next_image( - swapchain: Arc>, +pub fn acquire_next_image( + swapchain: Arc, timeout: Option, -) -> Result<(u32, bool, SwapchainAcquireFuture), AcquireError> { +) -> Result<(u32, bool, SwapchainAcquireFuture), AcquireError> { let semaphore = Arc::new(Semaphore::from_pool(swapchain.device.clone())?); let fence = Fence::from_pool(swapchain.device.clone())?; @@ -1617,8 +1578,8 @@ where /// Returns a bool to represent if the presentation was suboptimal. In this case the swapchain is /// still usable, but the swapchain should be recreated as the Surface's properties no longer match /// the swapchain. -pub fn wait_for_present( - swapchain: Arc>, +pub fn wait_for_present( + swapchain: Arc, present_id: u64, timeout: Option, ) -> Result { @@ -1675,8 +1636,8 @@ pub fn wait_for_present( /// Represents the moment when the GPU will have access to a swapchain image. #[must_use] -pub struct SwapchainAcquireFuture { - swapchain: Arc>, +pub struct SwapchainAcquireFuture { + swapchain: Arc, image_index: u32, // Semaphore that is signalled when the acquire is complete. Empty if the acquire has already // happened. @@ -1687,22 +1648,19 @@ pub struct SwapchainAcquireFuture { finished: AtomicBool, } -impl SwapchainAcquireFuture { +impl SwapchainAcquireFuture { /// Returns the index of the image in the list of images returned when creating the swapchain. pub fn image_index(&self) -> u32 { self.image_index } /// Returns the corresponding swapchain. - pub fn swapchain(&self) -> &Arc> { + pub fn swapchain(&self) -> &Arc { &self.swapchain } } -unsafe impl GpuFuture for SwapchainAcquireFuture -where - W: Send + Sync, -{ +unsafe impl GpuFuture for SwapchainAcquireFuture { fn cleanup_finished(&mut self) {} unsafe fn build_submission(&self) -> Result { @@ -1795,7 +1753,7 @@ where } } -impl Drop for SwapchainAcquireFuture { +impl Drop for SwapchainAcquireFuture { fn drop(&mut self) { if let Some(ref fence) = self.fence { fence.wait(None).unwrap(); // TODO: handle error? @@ -1807,7 +1765,7 @@ impl Drop for SwapchainAcquireFuture { } } -unsafe impl DeviceOwned for SwapchainAcquireFuture { +unsafe impl DeviceOwned for SwapchainAcquireFuture { fn device(&self) -> &Arc { &self.swapchain.device } @@ -2028,7 +1986,7 @@ where } /// Returns the corresponding swapchain. - pub fn swapchain(&self) -> &Arc { + pub fn swapchain(&self) -> &Arc { &self.swapchain_info.swapchain } } @@ -2274,8 +2232,8 @@ pub struct AcquiredImage { /// - The semaphore and/or the fence must be kept alive until it is signaled. /// - The swapchain must not have been replaced by being passed as the old swapchain when creating /// a new one. -pub unsafe fn acquire_next_image_raw( - swapchain: &Swapchain, +pub unsafe fn acquire_next_image_raw( + swapchain: &Swapchain, timeout: Option, semaphore: Option<&Semaphore>, fence: Option<&Fence>,