Add Surface::from_window[_ref], deprecate vulkano-win (#2204)

* Add `Surface::from_window[_ref]`

* Add `Surface::required_extensions`

* Deprecate vulkano-win

* Fix SPIR-V word alignment in tests

* Fix oopsie

* Remove pointer transmute
This commit is contained in:
marc0246 2023-05-12 09:52:20 +02:00 committed by GitHub
parent 535ae1abec
commit 77fa2bbfcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 405 additions and 382 deletions

View File

@ -45,22 +45,23 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -71,10 +72,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -137,7 +136,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -294,7 +292,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -21,25 +21,26 @@ use vulkano::{
instance::{Instance, InstanceCreateInfo},
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass},
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
// The start of this example is exactly the same as `triangle`. You should read the `triangle`
// example if you haven't done so yet.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -50,10 +51,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -115,7 +114,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -176,7 +174,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -41,17 +41,16 @@ use vulkano::{
instance::{Instance, InstanceCreateInfo},
memory::allocator::StandardMemoryAllocator,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
mod frame;
@ -60,8 +59,10 @@ mod triangle_draw_system;
fn main() {
// Basic initialization. See the triangle example if you want more details about this.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -72,10 +73,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -137,7 +136,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let (swapchain, images) = Swapchain::new(
device.clone(),
@ -200,7 +198,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -51,7 +51,7 @@ mod linux {
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
AcquireError, Surface, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
},
sync::{
@ -64,7 +64,6 @@ mod linux {
},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
@ -98,7 +97,7 @@ mod linux {
device,
_instance,
mut swapchain,
surface,
window,
mut viewport,
queue,
render_pass,
@ -290,7 +289,6 @@ mod linux {
previous_frame_end.as_mut().unwrap().cleanup_finished();
if recreate_swapchain {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let (new_swapchain, new_images) =
match swapchain.recreate(SwapchainCreateInfo {
image_extent: window.inner_size().into(),
@ -405,21 +403,21 @@ mod linux {
display: glium::HeadlessRenderer,
event_loop: &EventLoop<()>,
) -> (
Arc<vulkano::device::Device>,
Arc<vulkano::instance::Instance>,
Arc<Device>,
Arc<Instance>,
Arc<Swapchain>,
Arc<vulkano::swapchain::Surface>,
vulkano::pipeline::graphics::viewport::Viewport,
Arc<Window>,
Viewport,
Arc<Queue>,
Arc<RenderPass>,
Vec<Arc<Framebuffer>>,
Arc<vulkano::sampler::Sampler>,
Arc<Sampler>,
Arc<GraphicsPipeline>,
StandardMemoryAllocator,
Subbuffer<[MyVertex]>,
) {
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -429,13 +427,9 @@ mod linux {
khr_external_semaphore_capabilities: true,
khr_external_fence_capabilities: true,
ext_debug_utils: true,
..InstanceExtensions::empty()
}
.union(&required_extensions),
..required_extensions
},
enumerate_portability: true,
..Default::default()
},
)
@ -457,9 +451,8 @@ mod linux {
.unwrap()
};
let surface = WindowBuilder::new()
.build_vk_surface(event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_external_semaphore: true,
@ -536,7 +529,6 @@ mod linux {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -674,7 +666,7 @@ mod linux {
device,
instance,
swapchain,
surface,
window,
viewport,
queue,
render_pass,
@ -715,7 +707,7 @@ mod linux {
images
.iter()
.map(|image| -> Arc<Framebuffer> {
.map(|image| {
let view = ImageView::new_default(image.clone()).unwrap();
Framebuffer::new(

View File

@ -47,25 +47,26 @@ use vulkano::{
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
// The start of this example is exactly the same as `triangle`. You should read the `triangle`
// example if you haven't done so yet.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -76,10 +77,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -141,7 +140,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -421,7 +419,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -45,25 +45,26 @@ use vulkano::{
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
// The start of this example is exactly the same as `triangle`. You should read the `triangle`
// example if you haven't done so yet.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -74,10 +75,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -139,7 +138,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -347,7 +345,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -54,22 +54,23 @@ use vulkano::{
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -80,10 +81,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -145,7 +144,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -368,7 +366,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -61,22 +61,23 @@ use vulkano::{
shader::PipelineShaderStageCreateInfo,
single_pass_renderpass,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -87,10 +88,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -154,7 +153,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -385,7 +383,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -43,17 +43,16 @@ use vulkano::{
shader::PipelineShaderStageCreateInfo,
single_pass_renderpass,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
/// The vertex type that we will be used to describe the triangle's geometry.
@ -75,8 +74,10 @@ struct InstanceData {
}
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -87,10 +88,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -153,7 +152,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -366,7 +364,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -99,11 +99,9 @@ use vulkano::{
fn main() {
// The usual Vulkan initialization.
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let instance = Instance::new(
library,
InstanceCreateInfo {
enabled_extensions: required_extensions,
enumerate_portability: true,
..Default::default()
},
@ -111,7 +109,6 @@ fn main() {
.unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
..DeviceExtensions::empty()
};
let (physical_device, queue_family_index) = instance

View File

@ -52,7 +52,6 @@ use vulkano::{
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
@ -61,7 +60,7 @@ use winit::{
/// A struct to contain resources related to a window.
struct WindowSurface {
surface: Arc<Surface>,
window: Arc<Window>,
swapchain: Arc<Swapchain>,
framebuffers: Vec<Arc<Framebuffer>>,
recreate_swapchain: bool,
@ -69,8 +68,10 @@ struct WindowSurface {
}
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -80,17 +81,14 @@ fn main() {
},
)
.unwrap();
let event_loop = EventLoop::new();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
// A hashmap that contains all of our created windows and their resources.
let mut window_surfaces = HashMap::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
// Use the window's id as a means to access it from the hashmap.
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let window_id = window.id();
// Find the device and a queue.
@ -318,7 +316,7 @@ fn main() {
window_surfaces.insert(
window_id,
WindowSurface {
surface,
window,
swapchain,
recreate_swapchain: false,
framebuffers: window_size_dependent_setup(&images, render_pass.clone(), &mut viewport),
@ -355,10 +353,8 @@ fn main() {
},
..
} => {
let surface = WindowBuilder::new()
.build_vk_surface(event_loop, instance.clone())
.unwrap();
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let window = Arc::new(WindowBuilder::new().build(event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let window_id = window.id();
let (swapchain, images) = {
let composite_alpha = surface_caps
@ -392,7 +388,7 @@ fn main() {
window_surfaces.insert(
window_id,
WindowSurface {
surface,
window,
swapchain,
recreate_swapchain: false,
framebuffers: window_size_dependent_setup(
@ -405,26 +401,19 @@ fn main() {
);
}
Event::RedrawEventsCleared => {
window_surfaces.values().for_each(|s| {
let window = s
.surface
.object()
.unwrap()
.downcast_ref::<Window>()
.unwrap();
window.request_redraw()
});
window_surfaces
.values()
.for_each(|s| s.window.request_redraw());
}
Event::RedrawRequested(window_id) => {
let WindowSurface {
surface,
window,
swapchain,
recreate_swapchain,
framebuffers,
previous_frame_end,
} = window_surfaces.get_mut(&window_id).unwrap();
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -44,22 +44,23 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -70,10 +71,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -135,7 +134,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -384,7 +382,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -43,22 +43,23 @@ use vulkano::{
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -69,10 +70,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -135,7 +134,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -342,7 +340,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -51,22 +51,23 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::{PipelineShaderStageCreateInfo, ShaderModule},
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -77,10 +78,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -142,7 +141,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -313,7 +311,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -45,25 +45,26 @@ use vulkano::{
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
// The start of this example is exactly the same as `triangle`. You should read the `triangle`
// example if you haven't done so yet.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -74,10 +75,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -146,7 +145,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -454,7 +452,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -46,16 +46,16 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, Subpass},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, PresentMode, Swapchain, SwapchainCreateInfo, SwapchainPresentInfo,
acquire_next_image, PresentMode, Surface, Swapchain, SwapchainCreateInfo,
SwapchainPresentInfo,
},
sync::{self, future::FenceSignalFuture, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
const WINDOW_WIDTH: u32 = 800;
@ -66,8 +66,10 @@ const PARTICLE_COUNT: usize = 100_000;
fn main() {
// The usual Vulkan initialization. Largely the same as example `triangle.rs` until further
// commentation is provided.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -78,14 +80,16 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
// For simplicity, we are going to assert that the window size is static.
.with_resizable(false)
.with_title("simple particles")
.with_inner_size(winit::dpi::PhysicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT))
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(
WindowBuilder::new()
// For simplicity, we are going to assert that the window size is static.
.with_resizable(false)
.with_title("simple particles")
.with_inner_size(winit::dpi::PhysicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT))
.build(&event_loop)
.unwrap(),
);
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -518,7 +522,6 @@ fn main() {
*control_flow = ControlFlow::Exit;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -47,25 +47,26 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::{EntryPoint, PipelineShaderStageCreateInfo},
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
// The start of this example is exactly the same as `triangle`. You should read the `triangle`
// example if you haven't done so yet.
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -76,10 +77,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -142,7 +141,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -276,7 +274,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -52,17 +52,16 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
mod vs {
@ -163,8 +162,10 @@ mod fs {
}
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -175,10 +176,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -247,7 +246,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -426,7 +424,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -45,17 +45,16 @@ use vulkano::{
sampler::{Sampler, SamplerCreateInfo},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
@ -64,8 +63,10 @@ fn main() {
// And not this:
// uniform sampler2D array_of_textures[42];
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
let required_extensions = vulkano_win::required_extensions(&library);
let required_extensions = Surface::required_extensions(&event_loop);
let instance = Instance::new(
library,
InstanceCreateInfo {
@ -76,10 +77,8 @@ fn main() {
)
.unwrap();
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
let device_extensions = DeviceExtensions {
khr_swapchain: true,
@ -141,7 +140,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
Swapchain::new(
device.clone(),
@ -352,7 +350,6 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -52,20 +52,21 @@ use vulkano::{
render_pass::{LoadOp, StoreOp},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
Version, VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
// The first step of any Vulkan program is to create an instance.
@ -73,9 +74,9 @@ fn main() {
// When we create an instance, we have to pass a list of extensions that we want to enable.
//
// All the window-drawing functionalities are part of non-core extensions that we need to
// enable manually. To do so, we ask the `vulkano_win` crate for the list of extensions
// required to draw to a window.
let required_extensions = vulkano_win::required_extensions(&library);
// enable manually. To do so, we ask `Surface` for the list of extensions required to draw to
// a window.
let required_extensions = Surface::required_extensions(&event_loop);
// Now creating the instance.
let instance = Instance::new(
@ -91,19 +92,13 @@ fn main() {
.unwrap();
// The objective of this example is to draw a triangle on a window. To do so, we first need to
// create the window.
// create the window. We use the `WindowBuilder` from the `winit` crate to do that here.
//
// This is done by creating a `WindowBuilder` from the `winit` crate, then calling the
// `build_vk_surface` method provided by the `VkSurfaceBuild` trait from `vulkano_win`. If you
// ever get an error about `build_vk_surface` being undefined in one of your projects, this
// probably means that you forgot to import this trait.
//
// This returns a `vulkano::swapchain::Surface` object that contains both a cross-platform
// winit window and a cross-platform Vulkan surface that represents the surface of the window.
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
// Before we can render to a window, we must first create a `vulkano::swapchain::Surface`
// object from it, which represents the drawable surface of a window. For that we must wrap the
// `winit::window::Window` in an `Arc`.
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
// Choose device extensions that we're going to use. In order to present images to a surface,
// we need a `Swapchain`, which is provided by the `khr_swapchain` extension.
@ -252,7 +247,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
// Please take a look at the docs for the meaning of the parameters we didn't mention.
Swapchain::new(
@ -531,6 +525,13 @@ fn main() {
recreate_swapchain = true;
}
Event::RedrawEventsCleared => {
// Do not draw the frame when the screen dimensions are zero. On Windows, this can
// occur when minimizing the application.
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;
}
// It is important to call this function from time to time, otherwise resources
// will keep accumulating and you will eventually reach an out of memory error.
// Calling this function polls various fences in order to determine what the GPU
@ -541,12 +542,9 @@ fn main() {
// window size. 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::<Window>().unwrap();
let (new_swapchain, new_images) =
match swapchain.recreate(SwapchainCreateInfo {
image_extent: window.inner_size().into(),
image_extent: dimensions.into(),
..swapchain.create_info()
}) {
Ok(r) => r,

View File

@ -46,20 +46,21 @@ use vulkano::{
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::PipelineShaderStageCreateInfo,
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
SwapchainPresentInfo,
acquire_next_image, AcquireError, Surface, Swapchain, SwapchainCreateInfo,
SwapchainCreationError, SwapchainPresentInfo,
},
sync::{self, FlushError, GpuFuture},
VulkanLibrary,
};
use vulkano_win::VkSurfaceBuild;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let library = VulkanLibrary::new().unwrap();
// The first step of any Vulkan program is to create an instance.
@ -67,9 +68,9 @@ fn main() {
// When we create an instance, we have to pass a list of extensions that we want to enable.
//
// All the window-drawing functionalities are part of non-core extensions that we need to
// enable manually. To do so, we ask the `vulkano_win` crate for the list of extensions
// required to draw to a window.
let required_extensions = vulkano_win::required_extensions(&library);
// enable manually. To do so, we ask `Surface` for the list of extensions required to draw to
// a window.
let required_extensions = Surface::required_extensions(&event_loop);
// Now creating the instance.
let instance = Instance::new(
@ -85,19 +86,13 @@ fn main() {
.unwrap();
// The objective of this example is to draw a triangle on a window. To do so, we first need to
// create the window.
// create the window. We use the `WindowBuilder` from the `winit` crate to do that here.
//
// This is done by creating a `WindowBuilder` from the `winit` crate, then calling the
// `build_vk_surface` method provided by the `VkSurfaceBuild` trait from `vulkano_win`. If you
// ever get an error about `build_vk_surface` being undefined in one of your projects, this
// probably means that you forgot to import this trait.
//
// This returns a `vulkano::swapchain::Surface` object that contains both a cross-platform
// winit window and a cross-platform Vulkan surface that represents the surface of the window.
let event_loop = EventLoop::new();
let surface = WindowBuilder::new()
.build_vk_surface(&event_loop, instance.clone())
.unwrap();
// Before we can render to a window, we must first create a `vulkano::swapchain::Surface`
// object from it, which represents the drawable surface of a window. For that we must wrap the
// `winit::window::Window` in an `Arc`.
let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
let surface = Surface::from_window(instance.clone(), window.clone()).unwrap();
// Choose device extensions that we're going to use. In order to present images to a surface,
// we need a `Swapchain`, which is provided by the `khr_swapchain` extension.
@ -221,7 +216,6 @@ fn main() {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
// Please take a look at the docs for the meaning of the parameters we didn't mention.
Swapchain::new(
@ -528,7 +522,6 @@ fn main() {
Event::RedrawEventsCleared => {
// Do not draw the frame when the screen dimensions are zero. On Windows, this can
// occur when minimizing the application.
let window = surface.object().unwrap().downcast_ref::<Window>().unwrap();
let dimensions = window.inner_size();
if dimensions.width == 0 || dimensions.height == 0 {
return;

View File

@ -122,16 +122,17 @@ impl VulkanoContext {
/// # Panics
///
/// - Panics where the underlying Vulkano struct creations fail
// FIXME:
#[allow(deprecated)]
pub fn new(mut config: VulkanoConfig) -> Self {
let library = match VulkanLibrary::new() {
Ok(x) => x,
#[cfg(target_os = "macos")]
Err(vulkano::library::LoadingError::LibraryLoadFailure(err)) => {
panic!("Failed to load Vulkan library: {}. Did you install vulkanSDK from https://vulkan.lunarg.com/sdk/home ?", err);
}
Err(err) => {
panic!("Failed to load Vulkan library: {}.", err);
}
Err(vulkano::library::LoadingError::LibraryLoadFailure(err)) => panic!(
"failed to load Vulkan library: {err}; did you install VulkanSDK from \
https://vulkan.lunarg.com/sdk/home?",
),
Err(err) => panic!("failed to load Vulkan library: {err}"),
};
// Append required extensions
@ -140,7 +141,7 @@ impl VulkanoContext {
// Create instance
let instance =
Instance::new(library, config.instance_create_info).expect("Failed to create instance");
Instance::new(library, config.instance_create_info).expect("failed to create instance");
// Create debug callback
let _debug_utils_messenger =
@ -149,16 +150,16 @@ impl VulkanoContext {
.take()
.map(|dbg_create_info| unsafe {
DebugUtilsMessenger::new(instance.clone(), dbg_create_info)
.expect("Failed to create debug callback")
.expect("failed to create debug callback")
});
// Get prioritized device
let physical_device = instance
.enumerate_physical_devices()
.expect("Failed to enumerate physical devices")
.expect("failed to enumerate physical devices")
.filter(|p| (config.device_filter_fn)(p))
.min_by_key(|p| (config.device_priority_fn)(p))
.expect("Failed to create physical device");
.expect("failed to create physical device");
// Print used device
if config.print_device_name {
println!(
@ -201,7 +202,7 @@ impl VulkanoContext {
.map(|(i, q)| (i as u32, q))
.find(|(_i, q)| q.queue_flags.intersects(QueueFlags::GRAPHICS))
.map(|(i, _)| i)
.expect("Could not find a queue that supports graphics");
.expect("could not find a queue that supports graphics");
// Try finding a separate queue for compute
let queue_family_compute = physical_device
.queue_family_properties()
@ -242,7 +243,7 @@ impl VulkanoContext {
..Default::default()
},
)
.expect("Failed to create device")
.expect("failed to create device")
};
let gfx_queue = queues.next().unwrap();
let compute_queue = if is_separate_compute_queue {

View File

@ -23,7 +23,6 @@ use vulkano::{
},
sync::{self, FlushError, GpuFuture},
};
use vulkano_win::create_surface_from_winit;
use winit::window::Window;
/// Swapchain Image View. Your final render target typically.
@ -42,7 +41,7 @@ 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>,
window: Arc<Window>,
graphics_queue: Arc<Queue>,
compute_queue: Arc<Queue>,
swapchain: Arc<Swapchain>,
@ -67,15 +66,12 @@ impl VulkanoWindowRenderer {
descriptor: &WindowDescriptor,
swapchain_create_info_modify: fn(&mut SwapchainCreateInfo),
) -> VulkanoWindowRenderer {
// Create rendering surface from window
let surface =
create_surface_from_winit(Arc::new(window), vulkano_context.instance().clone())
.unwrap();
let window = Arc::new(window);
// Create swap chain & frame(s) to which we'll render
let (swap_chain, final_views) = Self::create_swapchain(
vulkano_context.device().clone(),
surface.clone(),
&window,
descriptor,
swapchain_create_info_modify,
);
@ -83,7 +79,7 @@ impl VulkanoWindowRenderer {
let previous_frame_end = Some(sync::now(vulkano_context.device().clone()).boxed());
VulkanoWindowRenderer {
surface,
window,
graphics_queue: vulkano_context.graphics_queue().clone(),
compute_queue: vulkano_context.compute_queue().clone(),
swapchain: swap_chain,
@ -101,10 +97,11 @@ impl VulkanoWindowRenderer {
/// can be modified with the `swapchain_create_info_modify` function passed as an input.
fn create_swapchain(
device: Arc<Device>,
surface: Arc<Surface>,
window: &Arc<Window>,
window_descriptor: &WindowDescriptor,
swapchain_create_info_modify: fn(&mut SwapchainCreateInfo),
) -> (Arc<Swapchain>, Vec<SwapchainImageView>) {
let surface = Surface::from_window(device.instance().clone(), window.clone()).unwrap();
let surface_capabilities = device
.physical_device()
.surface_capabilities(&surface, Default::default())
@ -116,13 +113,11 @@ impl VulkanoWindowRenderer {
.unwrap()[0]
.0,
);
let window = surface.object().unwrap().downcast_ref::<Window>().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,
image_format,
image_extent,
image_extent: window.inner_size().into(),
image_usage: ImageUsage::COLOR_ATTACHMENT,
composite_alpha: surface_capabilities
.supported_composite_alpha
@ -141,6 +136,7 @@ impl VulkanoWindowRenderer {
.into_iter()
.map(|image| ImageView::new_default(image).unwrap())
.collect::<Vec<_>>();
(swapchain, images)
}
@ -182,13 +178,13 @@ impl VulkanoWindowRenderer {
/// Render target surface.
#[inline]
pub fn surface(&self) -> Arc<Surface> {
self.surface.clone()
self.swapchain.surface().clone()
}
/// Winit window (you can manipulate window through this).
/// Winit window (you can manipulate the window through this).
#[inline]
pub fn window(&self) -> &Window {
self.surface.object().unwrap().downcast_ref().unwrap()
&self.window
}
/// Size of the physical window.
@ -282,7 +278,7 @@ impl VulkanoWindowRenderer {
self.recreate_swapchain = true;
return Err(AcquireError::OutOfDate);
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("failed to acquire next image: {e}"),
};
if suboptimal {
self.recreate_swapchain = true;
@ -317,7 +313,7 @@ impl VulkanoWindowRenderer {
if wait_future {
match future.wait(None) {
Ok(x) => x,
Err(err) => println!("{:?}", err),
Err(e) => println!("{e}"),
}
// wait allows you to organize resource waiting yourself.
} else {
@ -332,7 +328,7 @@ impl VulkanoWindowRenderer {
Some(sync::now(self.graphics_queue.device().clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("failed to flush future: {e}");
self.previous_frame_end =
Some(sync::now(self.graphics_queue.device().clone()).boxed());
}
@ -350,7 +346,7 @@ impl VulkanoWindowRenderer {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("failed to recreate swapchain: {e}"),
};
self.swapchain = new_swapchain;

View File

@ -8,6 +8,11 @@
//! [`raw_window_handle`]: https://crates.io/crates/raw_window_handle
//! [`winit`]: https://crates.io/crates/winit
#![deprecated(
since = "0.34.0",
note = "vulkano-win is deprecated, use `Surface::required_extensions` and \
`Surface::from_window` instead"
)]
#![doc(html_logo_url = "https://raw.githubusercontent.com/vulkano-rs/vulkano/master/logo.png")]
#![allow(clippy::missing_safety_doc)]
#![warn(rust_2018_idioms, rust_2021_compatibility)]

View File

@ -27,12 +27,13 @@ half = { version = "2", features = ["bytemuck"] }
libloading = "0.7"
once_cell = "1.17"
parking_lot = { version = "0.12", features = ["send_guard"] }
raw-window-handle = "0.5"
serde = { version = "1.0", optional = true }
smallvec = "1.8"
thread_local = "1.1"
vulkano-macros = { path = "../vulkano-macros", version = "0.33.0", optional = true }
[target.'cfg(target_os = "ios")'.dependencies]
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
objc = "0.2.5"
core-graphics-types = "0.1"

View File

@ -300,17 +300,13 @@ mod tests {
* void main() {
* }
*/
const MODULE: [u8; 192] = [
3, 2, 35, 7, 0, 0, 1, 0, 10, 0, 8, 0, 6, 0, 0, 0, 0, 0, 0, 0, 17, 0, 2, 0, 1, 0, 0,
0, 11, 0, 6, 0, 1, 0, 0, 0, 71, 76, 83, 76, 46, 115, 116, 100, 46, 52, 53, 48, 0,
0, 0, 0, 14, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 15, 0, 5, 0, 5, 0, 0, 0, 4, 0, 0, 0,
109, 97, 105, 110, 0, 0, 0, 0, 16, 0, 6, 0, 4, 0, 0, 0, 17, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 3, 0, 3, 0, 2, 0, 0, 0, 194, 1, 0, 0, 5, 0, 4, 0, 4, 0, 0, 0,
109, 97, 105, 110, 0, 0, 0, 0, 19, 0, 2, 0, 2, 0, 0, 0, 33, 0, 3, 0, 3, 0, 0, 0, 2,
0, 0, 0, 54, 0, 5, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 248, 0, 2, 0,
5, 0, 0, 0, 253, 0, 1, 0, 56, 0, 1, 0,
const MODULE: [u32; 48] = [
119734787, 65536, 524298, 6, 0, 131089, 1, 393227, 1, 1280527431, 1685353262,
808793134, 0, 196622, 0, 1, 327695, 5, 4, 1852399981, 0, 393232, 4, 17, 1, 1, 1,
196611, 2, 450, 262149, 4, 1852399981, 0, 131091, 2, 196641, 3, 2, 327734, 2, 4, 0,
3, 131320, 5, 65789, 65592,
];
let module = ShaderModule::from_bytes(device.clone(), &MODULE).unwrap();
let module = ShaderModule::from_words(device.clone(), &MODULE).unwrap();
module.entry_point("main").unwrap()
};
@ -350,17 +346,13 @@ mod tests {
* void main() {
* }
*/
const MODULE: [u8; 192] = [
3, 2, 35, 7, 0, 0, 1, 0, 10, 0, 8, 0, 6, 0, 0, 0, 0, 0, 0, 0, 17, 0, 2, 0, 1,
0, 0, 0, 11, 0, 6, 0, 1, 0, 0, 0, 71, 76, 83, 76, 46, 115, 116, 100, 46, 52,
53, 48, 0, 0, 0, 0, 14, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 15, 0, 5, 0, 5, 0, 0,
0, 4, 0, 0, 0, 109, 97, 105, 110, 0, 0, 0, 0, 16, 0, 6, 0, 4, 0, 0, 0, 17, 0,
0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 3, 0, 2, 0, 0, 0, 194, 1, 0, 0,
5, 0, 4, 0, 4, 0, 0, 0, 109, 97, 105, 110, 0, 0, 0, 0, 19, 0, 2, 0, 2, 0, 0, 0,
33, 0, 3, 0, 3, 0, 0, 0, 2, 0, 0, 0, 54, 0, 5, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0,
0, 0, 3, 0, 0, 0, 248, 0, 2, 0, 5, 0, 0, 0, 253, 0, 1, 0, 56, 0, 1, 0,
const MODULE: [u32; 48] = [
119734787, 65536, 524298, 6, 0, 131089, 1, 393227, 1, 1280527431, 1685353262,
808793134, 0, 196622, 0, 1, 327695, 5, 4, 1852399981, 0, 393232, 4, 17, 1, 1,
1, 196611, 2, 450, 262149, 4, 1852399981, 0, 131091, 2, 196641, 3, 2, 327734,
2, 4, 0, 3, 131320, 5, 65789, 65592,
];
let module = ShaderModule::from_bytes(device.clone(), &MODULE).unwrap();
let module = ShaderModule::from_words(device.clone(), &MODULE).unwrap();
module.entry_point("main").unwrap()
};
@ -391,27 +383,17 @@ mod tests {
* uint idx = gl_GlobalInvocationID.x;
* }
*/
const MODULE: [u8; 432] = [
3, 2, 35, 7, 0, 0, 1, 0, 10, 0, 8, 0, 16, 0, 0, 0, 0, 0, 0, 0, 17, 0, 2, 0, 1,
0, 0, 0, 11, 0, 6, 0, 1, 0, 0, 0, 71, 76, 83, 76, 46, 115, 116, 100, 46, 52,
53, 48, 0, 0, 0, 0, 14, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 15, 0, 6, 0, 5, 0, 0,
0, 4, 0, 0, 0, 109, 97, 105, 110, 0, 0, 0, 0, 11, 0, 0, 0, 16, 0, 6, 0, 4, 0,
0, 0, 17, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 3, 0, 2, 0, 0, 0,
194, 1, 0, 0, 5, 0, 4, 0, 4, 0, 0, 0, 109, 97, 105, 110, 0, 0, 0, 0, 5, 0, 3,
0, 8, 0, 0, 0, 105, 100, 120, 0, 5, 0, 8, 0, 11, 0, 0, 0, 103, 108, 95, 71,
108, 111, 98, 97, 108, 73, 110, 118, 111, 99, 97, 116, 105, 111, 110, 73, 68,
0, 0, 0, 71, 0, 4, 0, 11, 0, 0, 0, 11, 0, 0, 0, 28, 0, 0, 0, 19, 0, 2, 0, 2, 0,
0, 0, 33, 0, 3, 0, 3, 0, 0, 0, 2, 0, 0, 0, 21, 0, 4, 0, 6, 0, 0, 0, 32, 0, 0,
0, 0, 0, 0, 0, 32, 0, 4, 0, 7, 0, 0, 0, 7, 0, 0, 0, 6, 0, 0, 0, 23, 0, 4, 0, 9,
0, 0, 0, 6, 0, 0, 0, 3, 0, 0, 0, 32, 0, 4, 0, 10, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0,
0, 59, 0, 4, 0, 10, 0, 0, 0, 11, 0, 0, 0, 1, 0, 0, 0, 43, 0, 4, 0, 6, 0, 0, 0,
12, 0, 0, 0, 0, 0, 0, 0, 32, 0, 4, 0, 13, 0, 0, 0, 1, 0, 0, 0, 6, 0, 0, 0, 54,
0, 5, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 248, 0, 2, 0, 5, 0, 0,
0, 59, 0, 4, 0, 7, 0, 0, 0, 8, 0, 0, 0, 7, 0, 0, 0, 65, 0, 5, 0, 13, 0, 0, 0,
14, 0, 0, 0, 11, 0, 0, 0, 12, 0, 0, 0, 61, 0, 4, 0, 6, 0, 0, 0, 15, 0, 0, 0,
14, 0, 0, 0, 62, 0, 3, 0, 8, 0, 0, 0, 15, 0, 0, 0, 253, 0, 1, 0, 56, 0, 1, 0,
const MODULE: [u32; 108] = [
119734787, 65536, 524298, 16, 0, 131089, 1, 393227, 1, 1280527431, 1685353262,
808793134, 0, 196622, 0, 1, 393231, 5, 4, 1852399981, 0, 11, 393232, 4, 17, 1,
1, 1, 196611, 2, 450, 262149, 4, 1852399981, 0, 196613, 8, 7890025, 524293, 11,
1197436007, 1633841004, 1986939244, 1952539503, 1231974249, 68, 262215, 11, 11,
28, 131091, 2, 196641, 3, 2, 262165, 6, 32, 0, 262176, 7, 7, 6, 262167, 9, 6,
3, 262176, 10, 1, 9, 262203, 10, 11, 1, 262187, 6, 12, 0, 262176, 13, 1, 6,
327734, 2, 4, 0, 3, 131320, 5, 262203, 7, 8, 7, 327745, 13, 14, 11, 12, 262205,
6, 15, 14, 196670, 8, 15, 65789, 65592,
];
let module = ShaderModule::from_bytes(device.clone(), &MODULE).unwrap();
let module = ShaderModule::from_words(device.clone(), &MODULE).unwrap();
module.entry_point("main").unwrap()
};
@ -452,17 +434,13 @@ mod tests {
* void main() {
* }
*/
const MODULE: [u8; 192] = [
3, 2, 35, 7, 0, 0, 1, 0, 10, 0, 8, 0, 6, 0, 0, 0, 0, 0, 0, 0, 17, 0, 2, 0, 1, 0, 0,
0, 11, 0, 6, 0, 1, 0, 0, 0, 71, 76, 83, 76, 46, 115, 116, 100, 46, 52, 53, 48, 0,
0, 0, 0, 14, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 15, 0, 5, 0, 5, 0, 0, 0, 4, 0, 0, 0,
109, 97, 105, 110, 0, 0, 0, 0, 16, 0, 6, 0, 4, 0, 0, 0, 17, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 3, 0, 3, 0, 2, 0, 0, 0, 194, 1, 0, 0, 5, 0, 4, 0, 4, 0, 0, 0,
109, 97, 105, 110, 0, 0, 0, 0, 19, 0, 2, 0, 2, 0, 0, 0, 33, 0, 3, 0, 3, 0, 0, 0, 2,
0, 0, 0, 54, 0, 5, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 248, 0, 2, 0,
5, 0, 0, 0, 253, 0, 1, 0, 56, 0, 1, 0,
const MODULE: [u32; 48] = [
119734787, 65536, 524298, 6, 0, 131089, 1, 393227, 1, 1280527431, 1685353262,
808793134, 0, 196622, 0, 1, 327695, 5, 4, 1852399981, 0, 393232, 4, 17, 1, 1, 1,
196611, 2, 450, 262149, 4, 1852399981, 0, 131091, 2, 196641, 3, 2, 327734, 2, 4, 0,
3, 131320, 5, 65789, 65592,
];
let module = ShaderModule::from_bytes(device.clone(), &MODULE).unwrap();
let module = ShaderModule::from_words(device.clone(), &MODULE).unwrap();
module.entry_point("main").unwrap()
};

View File

@ -548,28 +548,17 @@ mod tests {
write.write = VALUE;
}
*/
const MODULE: [u8; 480] = [
3, 2, 35, 7, 0, 0, 1, 0, 1, 0, 8, 0, 14, 0, 0, 0, 0, 0, 0, 0, 17, 0, 2, 0, 1, 0, 0,
0, 11, 0, 6, 0, 1, 0, 0, 0, 71, 76, 83, 76, 46, 115, 116, 100, 46, 52, 53, 48, 0,
0, 0, 0, 14, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 15, 0, 5, 0, 5, 0, 0, 0, 4, 0, 0, 0,
109, 97, 105, 110, 0, 0, 0, 0, 16, 0, 6, 0, 4, 0, 0, 0, 17, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 3, 0, 3, 0, 2, 0, 0, 0, 194, 1, 0, 0, 5, 0, 4, 0, 4, 0, 0, 0,
109, 97, 105, 110, 0, 0, 0, 0, 5, 0, 4, 0, 7, 0, 0, 0, 79, 117, 116, 112, 117, 116,
0, 0, 6, 0, 5, 0, 7, 0, 0, 0, 0, 0, 0, 0, 119, 114, 105, 116, 101, 0, 0, 0, 5, 0,
4, 0, 9, 0, 0, 0, 119, 114, 105, 116, 101, 0, 0, 0, 5, 0, 4, 0, 11, 0, 0, 0, 86,
65, 76, 85, 69, 0, 0, 0, 72, 0, 5, 0, 7, 0, 0, 0, 0, 0, 0, 0, 35, 0, 0, 0, 0, 0, 0,
0, 71, 0, 3, 0, 7, 0, 0, 0, 3, 0, 0, 0, 71, 0, 4, 0, 9, 0, 0, 0, 34, 0, 0, 0, 0, 0,
0, 0, 71, 0, 4, 0, 9, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 71, 0, 4, 0, 11, 0, 0, 0,
1, 0, 0, 0, 83, 0, 0, 0, 19, 0, 2, 0, 2, 0, 0, 0, 33, 0, 3, 0, 3, 0, 0, 0, 2, 0, 0,
0, 21, 0, 4, 0, 6, 0, 0, 0, 32, 0, 0, 0, 1, 0, 0, 0, 30, 0, 3, 0, 7, 0, 0, 0, 6, 0,
0, 0, 32, 0, 4, 0, 8, 0, 0, 0, 2, 0, 0, 0, 7, 0, 0, 0, 59, 0, 4, 0, 8, 0, 0, 0, 9,
0, 0, 0, 2, 0, 0, 0, 43, 0, 4, 0, 6, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 50, 0, 4, 0,
6, 0, 0, 0, 11, 0, 0, 0, 239, 190, 173, 222, 32, 0, 4, 0, 12, 0, 0, 0, 2, 0, 0, 0,
6, 0, 0, 0, 54, 0, 5, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 248, 0, 2,
0, 5, 0, 0, 0, 65, 0, 5, 0, 12, 0, 0, 0, 13, 0, 0, 0, 9, 0, 0, 0, 10, 0, 0, 0, 62,
0, 3, 0, 13, 0, 0, 0, 11, 0, 0, 0, 253, 0, 1, 0, 56, 0, 1, 0,
const MODULE: [u32; 120] = [
119734787, 65536, 524289, 14, 0, 131089, 1, 393227, 1, 1280527431, 1685353262,
808793134, 0, 196622, 0, 1, 327695, 5, 4, 1852399981, 0, 393232, 4, 17, 1, 1, 1,
196611, 2, 450, 262149, 4, 1852399981, 0, 262149, 7, 1886680399, 29813, 327686, 7,
0, 1953067639, 101, 262149, 9, 1953067639, 101, 262149, 11, 1431060822, 69, 327752,
7, 0, 35, 0, 196679, 7, 3, 262215, 9, 34, 0, 262215, 9, 33, 0, 262215, 11, 1, 83,
131091, 2, 196641, 3, 2, 262165, 6, 32, 1, 196638, 7, 6, 262176, 8, 2, 7, 262203,
8, 9, 2, 262187, 6, 10, 0, 262194, 6, 11, 3735928559, 262176, 12, 2, 6, 327734, 2,
4, 0, 3, 131320, 5, 327745, 12, 13, 9, 10, 196670, 13, 11, 65789, 65592,
];
let module = ShaderModule::from_bytes(device.clone(), &MODULE).unwrap();
let module = ShaderModule::from_words(device.clone(), &MODULE).unwrap();
module.entry_point("main").unwrap()
};

View File

@ -12,7 +12,7 @@ use crate::{
cache::OnceCache,
format::Format,
image::ImageUsage,
instance::Instance,
instance::{Instance, InstanceExtensions},
macros::{impl_id_counter, vulkan_bitflags_enum, vulkan_enum},
swapchain::{
display::{DisplayMode, DisplayPlane},
@ -20,10 +20,11 @@ use crate::{
},
OomError, RequiresOneOf, RuntimeError, VulkanObject,
};
#[cfg(target_os = "ios")]
#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc::{class, msg_send, runtime::Object, sel, sel_impl};
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use std::{
any::Any,
error::Error,
@ -46,6 +47,7 @@ pub struct Surface {
// If true, a swapchain has been associated to this surface, and that any new swapchain
// creation should be forbidden.
has_swapchain: AtomicBool,
// FIXME: This field is never set.
#[cfg(target_os = "ios")]
metal_layer: IOSMetalLayer,
@ -59,6 +61,83 @@ pub struct Surface {
}
impl Surface {
/// Returns the instance extensions required to create a surface from a window of the given
/// event loop.
pub fn required_extensions(event_loop: &impl HasRawDisplayHandle) -> InstanceExtensions {
let mut extensions = InstanceExtensions {
khr_surface: true,
..InstanceExtensions::empty()
};
match event_loop.raw_display_handle() {
RawDisplayHandle::Android(_) => extensions.khr_android_surface = true,
// FIXME: `mvk_macos_surface` and `mvk_ios_surface` are deprecated.
RawDisplayHandle::AppKit(_) => extensions.mvk_macos_surface = true,
RawDisplayHandle::UiKit(_) => extensions.mvk_ios_surface = true,
RawDisplayHandle::Windows(_) => extensions.khr_win32_surface = true,
RawDisplayHandle::Wayland(_) => extensions.khr_wayland_surface = true,
RawDisplayHandle::Xcb(_) => extensions.khr_xcb_surface = true,
RawDisplayHandle::Xlib(_) => extensions.khr_xlib_surface = true,
_ => unimplemented!(),
}
extensions
}
/// Creates a new `Surface` from the given `window`.
pub fn from_window(
instance: Arc<Instance>,
window: Arc<impl HasRawWindowHandle + HasRawDisplayHandle + Any + Send + Sync>,
) -> Result<Arc<Self>, SurfaceCreationError> {
let mut surface = unsafe { Self::from_window_ref(instance, &*window) }?;
Arc::get_mut(&mut surface).unwrap().object = Some(window);
Ok(surface)
}
/// Creates a new `Surface` from the given `window` without ensuring that the window outlives
/// the surface.
///
/// # Safety
///
/// - The given `window` must outlive the created surface.
pub unsafe fn from_window_ref(
instance: Arc<Instance>,
window: &(impl HasRawWindowHandle + HasRawDisplayHandle),
) -> Result<Arc<Self>, SurfaceCreationError> {
match (window.raw_window_handle(), window.raw_display_handle()) {
(RawWindowHandle::AndroidNdk(window), RawDisplayHandle::Android(_display)) => {
Self::from_android(instance, window.a_native_window, None)
}
#[cfg(target_os = "macos")]
(RawWindowHandle::AppKit(window), RawDisplayHandle::AppKit(_display)) => {
// Ensure the layer is `CAMetalLayer`.
let layer = get_metal_layer_macos(window.ns_view);
Self::from_mac_os(instance, layer as *const (), None)
}
#[cfg(target_os = "ios")]
(RawWindowHandle::UiKit(window), RawDisplayHandle::UiKit(_display)) => {
// Ensure the layer is `CAMetalLayer`.
let layer = get_metal_layer_ios(window.ui_view);
Self::from_ios(instance, layer, None)
}
(RawWindowHandle::Wayland(window), RawDisplayHandle::Wayland(display)) => {
Self::from_wayland(instance, display.display, window.surface, None)
}
(RawWindowHandle::Win32(window), RawDisplayHandle::Windows(_display)) => {
Self::from_win32(instance, window.hinstance, window.hwnd, None)
}
(RawWindowHandle::Xcb(window), RawDisplayHandle::Xcb(display)) => {
Self::from_xcb(instance, display.connection, window.window, None)
}
(RawWindowHandle::Xlib(window), RawDisplayHandle::Xlib(display)) => {
Self::from_xlib(instance, display.display, window.window, None)
}
_ => unimplemented!(),
}
}
/// Creates a `Surface` from a raw handle.
///
/// # Safety
@ -1335,7 +1414,54 @@ unsafe impl SurfaceSwapchainLock for Surface {
}
}
/// Error that can happen when creating a debug callback.
/// Get sublayer from iOS main view (ui_view). The sublayer is created as `CAMetalLayer`.
#[cfg(target_os = "ios")]
unsafe fn get_metal_layer_ios(ui_view: *mut std::ffi::c_void) -> IOSMetalLayer {
use core_graphics_types::{base::CGFloat, geometry::CGRect};
let view: *mut Object = ui_view.cast();
let main_layer: *mut Object = msg_send![view, layer];
let class = class!(CAMetalLayer);
let new_layer: *mut Object = msg_send![class, new];
let frame: CGRect = msg_send![main_layer, bounds];
let () = msg_send![new_layer, setFrame: frame];
let () = msg_send![main_layer, addSublayer: new_layer];
let screen: *mut Object = msg_send![class!(UIScreen), mainScreen];
let scale_factor: CGFloat = msg_send![screen, nativeScale];
let () = msg_send![view, setContentScaleFactor: scale_factor];
IOSMetalLayer::new(view, new_layer)
}
/// Get (and set) `CAMetalLayer` to `ns_view`. This is necessary to be able to render on Mac.
#[cfg(target_os = "macos")]
unsafe fn get_metal_layer_macos(ns_view: *mut std::ffi::c_void) -> *mut Object {
use core_graphics_types::base::CGFloat;
use objc::runtime::YES;
use objc::runtime::{BOOL, NO};
let view: *mut Object = ns_view.cast();
let main_layer: *mut Object = msg_send![view, layer];
let class = class!(CAMetalLayer);
let is_valid_layer: BOOL = msg_send![main_layer, isKindOfClass: class];
if is_valid_layer == NO {
let new_layer: *mut Object = msg_send![class, new];
let () = msg_send![new_layer, setEdgeAntialiasingMask: 0];
let () = msg_send![new_layer, setPresentsWithTransaction: false];
let () = msg_send![new_layer, removeAllAnimations];
let () = msg_send![view, setLayer: new_layer];
let () = msg_send![view, setWantsLayer: YES];
let window: *mut Object = msg_send![view, window];
if !window.is_null() {
let scale_factor: CGFloat = msg_send![window, backingScaleFactor];
let () = msg_send![new_layer, setContentsScale: scale_factor];
}
new_layer
} else {
main_layer
}
}
/// Error that can happen when creating a surface.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SurfaceCreationError {
/// Not enough memory.