Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ash-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
image = { version = "0.25", default-features = false, features = ["png"] }
winit = { version = "0.29", features = ["rwh_06"] }
winit = { version = "0.30", features = ["rwh_06"] }
# The examples require the validation layers, which means the SDK or
# equivalent development packages should be present, so we can link
# directly and benefit from the infallible `Entry` constructor.
Expand Down
9 changes: 4 additions & 5 deletions ash-examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use winit::{
keyboard::{Key, NamedKey},
platform::run_on_demand::EventLoopExtRunOnDemand,
raw_window_handle::{HasDisplayHandle, HasWindowHandle},
window::WindowBuilder,
window::WindowAttributes,
};

// Simple offset_of macro akin to C++ offsetof
Expand Down Expand Up @@ -203,14 +203,13 @@ impl ExampleBase {
pub fn new(window_width: u32, window_height: u32) -> Result<Self, Box<dyn Error>> {
unsafe {
let event_loop = EventLoop::new()?;
let window = WindowBuilder::new()
let window_attributes = WindowAttributes::default()
.with_title("Ash - Example")
.with_inner_size(winit::dpi::LogicalSize::new(
f64::from(window_width),
f64::from(window_height),
))
.build(&event_loop)
.unwrap();
));
let window = event_loop.create_window(window_attributes)?;
let entry = Entry::linked();
let app_name = c"VulkanTriangle";

Expand Down
2 changes: 1 addition & 1 deletion ash-window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ raw-window-handle = "0.6"
raw-window-metal = "0.4"

[dev-dependencies]
winit = { version = "0.29", features = ["rwh_06"] }
winit = { version = "0.30", features = ["rwh_06"] }
ash = { path = "../ash", version = "0.38", default-features = false, features = ["linked"] }

[[example]]
Expand Down
142 changes: 87 additions & 55 deletions ash-window/examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,90 @@
use ash::vk;
use std::error::Error;
use winit::{
application::ApplicationHandler,
dpi::PhysicalSize,
event::{Event, KeyEvent, WindowEvent},
event::{KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::{Key, NamedKey},
raw_window_handle::{HasDisplayHandle, HasWindowHandle},
window::WindowBuilder,
window::WindowAttributes,
};

struct App {
entry: ash::Entry,
instance: ash::Instance,
surface_fn: ash::khr::surface::Instance,
window: Option<winit::window::Window>,
surface: Option<vk::SurfaceKHR>,
}

impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
let window_attributes =
WindowAttributes::default().with_inner_size(PhysicalSize::<u32>::from((800, 600)));
let window = event_loop.create_window(window_attributes).unwrap();

// Create a surface from winit window.
unsafe {
let s = ash_window::create_surface(
&self.entry,
&self.instance,
window.display_handle().unwrap().as_raw(),
window.window_handle().unwrap().as_raw(),
None,
)
.unwrap();
println!("surface: {s:?}");
assert!(
self.surface.replace(s).is_none(),
"Surface must not yet exist when Resumed is called"
);
}
assert!(self.window.replace(window).is_none());
}

fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
_window_id: winit::window::WindowId,
event: WindowEvent,
) {
match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
} => event_loop.exit(),
_ => {}
}
}

fn exiting(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {
// This will be the last event before the loop terminates.
// TODO: How does this play with Suspended?
// https://github.com/rust-windowing/winit/issues/3206
if let Some(surface) = self.surface.take() {
unsafe {
self.surface_fn.destroy_surface(surface, None);
}
}
}

fn suspended(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {
let surface = self
.surface
.take()
.expect("Surface must have been created in Resumed");
unsafe {
self.surface_fn.destroy_surface(surface, None);
}
}
}

fn main() -> Result<(), Box<dyn Error>> {
let event_loop = EventLoop::new()?;

Expand All @@ -30,62 +106,18 @@ fn main() -> Result<(), Box<dyn Error>> {

let instance = entry.create_instance(&instance_desc, None)?;

let window = WindowBuilder::new()
.with_inner_size(PhysicalSize::<u32>::from((800, 600)))
.build(&event_loop)?;

// Load the surface extensions
let surface_fn = ash::khr::surface::Instance::new(&entry, &instance);
let mut surface = None;

let _ = event_loop.run(move |event, elwp| match event {
winit::event::Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id: _,
} => {
elwp.exit();
}
Event::LoopExiting => {
// This will be the last event before the loop terminates.
// TODO: How does this play with Suspended?
// https://github.com/rust-windowing/winit/issues/3206
if let Some(surface) = surface.take() {
surface_fn.destroy_surface(surface, None);
}
}
Event::Resumed => {
// Create a surface from winit window.
let s = ash_window::create_surface(
&entry,
&instance,
window.display_handle().unwrap().as_raw(),
window.window_handle().unwrap().as_raw(),
None,
)
.unwrap();
println!("surface: {s:?}");
assert!(
surface.replace(s).is_none(),
"Surface must not yet exist when Resumed is called"
);
}
Event::Suspended => {
let surface = surface
.take()
.expect("Surface must have been created in Resumed");
surface_fn.destroy_surface(surface, None);
}
_ => {}
});
let mut app = App {
entry: entry,
instance: instance,
surface_fn: surface_fn,
window: None,
surface: None,
};

let _ = event_loop.run_app(&mut app);
Ok(())
}
}