Skip to content

Commit 797e16f

Browse files
authored
Upgrade to winit 0.27, raw-window-handle 0.5, ash-window 0.12 (#142)
* Upgrade to `winit 0.27`, `raw-window-handle 0.5`, `ash-window 0.12` * vulkan-visualization: Replace `imgui-winit-support` with manual `handle_imgui_event()` We already use our own implementation in `d3d12`, and `imgui` is still stuck on `winit 0.26` while a release that fixes this (and more!) is constantly and consistently being delayed. * Run `cargo clippy --fix`
1 parent 05f66de commit 797e16f

File tree

5 files changed

+142
-37
lines changed

5 files changed

+142
-37
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ optional = true
4747
[dev-dependencies]
4848
# Enable the "loaded" feature to be able to access the Vulkan entrypoint.
4949
ash = { version = "0.37", default-features = false, features = ["debug", "loaded"] }
50-
ash-window = "0.10.0"
51-
raw-window-handle = "0.4"
52-
winit = "0.26"
53-
imgui-winit-support = { version = "0.8", default-features = false, features = ["winit-26"] }
50+
ash-window = "0.12"
51+
raw-window-handle = "0.5"
52+
winit = { version = "0.27", features = ["x11", "wayland"] }
5453

5554
[target.'cfg(windows)'.dev-dependencies]
5655
winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] }

examples/vulkan-visualization/imgui_renderer.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,117 @@ impl ImGuiRenderer {
832832
}
833833
}
834834
}
835+
836+
pub(crate) fn handle_imgui_event(
837+
io: &mut imgui::Io,
838+
window: &winit::window::Window,
839+
event: &winit::event::Event<'_, ()>,
840+
) -> bool {
841+
use winit::event::{
842+
DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase,
843+
VirtualKeyCode, WindowEvent,
844+
};
845+
846+
match event {
847+
Event::WindowEvent { event, window_id } if *window_id == window.id() => match *event {
848+
WindowEvent::Resized(physical_size) => {
849+
io.display_size = [physical_size.width as f32, physical_size.height as f32];
850+
false
851+
}
852+
WindowEvent::KeyboardInput {
853+
input:
854+
KeyboardInput {
855+
virtual_keycode: Some(key),
856+
state,
857+
..
858+
},
859+
..
860+
} => {
861+
let pressed = state == ElementState::Pressed;
862+
io.keys_down[key as usize] = pressed;
863+
match key {
864+
VirtualKeyCode::LShift | VirtualKeyCode::RShift => io.key_shift = pressed,
865+
VirtualKeyCode::LControl | VirtualKeyCode::RControl => io.key_ctrl = pressed,
866+
VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => io.key_alt = pressed,
867+
VirtualKeyCode::LWin | VirtualKeyCode::RWin => io.key_super = pressed,
868+
_ => (),
869+
}
870+
871+
io.want_capture_keyboard
872+
}
873+
WindowEvent::ReceivedCharacter(ch) => {
874+
io.add_input_character(ch);
875+
876+
io.want_capture_keyboard
877+
}
878+
879+
WindowEvent::CursorMoved { position, .. } => {
880+
io.mouse_pos = [position.x as f32, position.y as f32];
881+
882+
io.want_capture_mouse
883+
}
884+
WindowEvent::MouseWheel {
885+
delta,
886+
phase: TouchPhase::Moved,
887+
..
888+
} => {
889+
match delta {
890+
MouseScrollDelta::LineDelta(h, v) => {
891+
io.mouse_wheel_h = h;
892+
io.mouse_wheel = v;
893+
}
894+
MouseScrollDelta::PixelDelta(pos) => {
895+
match pos.x.partial_cmp(&0.0) {
896+
Some(std::cmp::Ordering::Greater) => io.mouse_wheel_h += 1.0,
897+
Some(std::cmp::Ordering::Less) => io.mouse_wheel_h -= 1.0,
898+
_ => (),
899+
}
900+
match pos.y.partial_cmp(&0.0) {
901+
Some(std::cmp::Ordering::Greater) => io.mouse_wheel += 1.0,
902+
Some(std::cmp::Ordering::Less) => io.mouse_wheel -= 1.0,
903+
_ => (),
904+
}
905+
}
906+
}
907+
908+
io.want_capture_mouse
909+
}
910+
WindowEvent::MouseInput { state, button, .. } => {
911+
let pressed = state == ElementState::Pressed;
912+
match button {
913+
MouseButton::Left => io.mouse_down[0] = pressed,
914+
MouseButton::Right => io.mouse_down[1] = pressed,
915+
MouseButton::Middle => io.mouse_down[2] = pressed,
916+
MouseButton::Other(idx @ 0..=4) => io.mouse_down[idx as usize] = pressed,
917+
MouseButton::Other(_) => (),
918+
}
919+
920+
io.want_capture_mouse
921+
}
922+
_ => false,
923+
},
924+
// Track key release events outside our window. If we don't do this,
925+
// we might never see the release event if some other window gets focus.
926+
Event::DeviceEvent {
927+
event:
928+
DeviceEvent::Key(KeyboardInput {
929+
state: ElementState::Released,
930+
virtual_keycode: Some(key),
931+
..
932+
}),
933+
..
934+
} => {
935+
io.keys_down[*key as usize] = false;
936+
match *key {
937+
VirtualKeyCode::LShift | VirtualKeyCode::RShift => io.key_shift = false,
938+
VirtualKeyCode::LControl | VirtualKeyCode::RControl => io.key_ctrl = false,
939+
VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => io.key_alt = false,
940+
VirtualKeyCode::LWin | VirtualKeyCode::RWin => io.key_super = false,
941+
_ => (),
942+
}
943+
944+
io.want_capture_keyboard
945+
}
946+
_ => false,
947+
}
948+
}

examples/vulkan-visualization/main.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
use ash::vk;
2-
31
use std::default::Default;
42
use std::ffi::CString;
53

4+
use ash::vk;
65
use gpu_allocator::vulkan::{Allocator, AllocatorCreateDesc};
7-
8-
mod helper;
9-
use helper::record_and_submit_command_buffer;
6+
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
107

118
mod imgui_renderer;
12-
use imgui_renderer::ImGuiRenderer;
9+
use imgui_renderer::{handle_imgui_event, ImGuiRenderer};
1310

14-
use imgui_winit_support::{HiDpiMode, WinitPlatform};
11+
mod helper;
12+
use helper::record_and_submit_command_buffer;
1513

1614
fn main() -> ash::prelude::VkResult<()> {
1715
let entry = unsafe { ash::Entry::load() }.unwrap();
@@ -21,7 +19,7 @@ fn main() -> ash::prelude::VkResult<()> {
2119
let window_width = 1920;
2220
let window_height = 1080;
2321
let window = winit::window::WindowBuilder::new()
24-
.with_title("gpu-allocator vulkan visualization")
22+
.with_title("gpu-allocator Vulkan visualization")
2523
.with_inner_size(winit::dpi::PhysicalSize::new(
2624
window_width as f64,
2725
window_height as f64,
@@ -30,7 +28,7 @@ fn main() -> ash::prelude::VkResult<()> {
3028
.build(&event_loop)
3129
.unwrap();
3230

33-
// Create vulkan instance
31+
// Create Vulkan instance
3432
let instance = {
3533
let app_name = CString::new("gpu-allocator examples vulkan-visualization").unwrap();
3634

@@ -47,7 +45,8 @@ fn main() -> ash::prelude::VkResult<()> {
4745
.map(|raw_name| raw_name.as_ptr())
4846
.collect();
4947

50-
let surface_extensions = ash_window::enumerate_required_extensions(&window).unwrap();
48+
let surface_extensions =
49+
ash_window::enumerate_required_extensions(event_loop.raw_display_handle()).unwrap();
5150

5251
let create_info = vk::InstanceCreateInfo::builder()
5352
.application_info(&appinfo)
@@ -61,10 +60,19 @@ fn main() -> ash::prelude::VkResult<()> {
6160
}
6261
};
6362

64-
let surface = unsafe { ash_window::create_surface(&entry, &instance, &window, None) }.unwrap();
63+
let surface = unsafe {
64+
ash_window::create_surface(
65+
&entry,
66+
&instance,
67+
window.raw_display_handle(),
68+
window.raw_window_handle(),
69+
None,
70+
)
71+
}
72+
.unwrap();
6573
let surface_loader = ash::extensions::khr::Surface::new(&entry, &instance);
6674

67-
// Look for vulkan physical device
75+
// Look for Vulkan physical device
6876
let (pdevice, queue_family_index) = {
6977
let pdevices = unsafe {
7078
instance
@@ -97,7 +105,7 @@ fn main() -> ash::prelude::VkResult<()> {
97105
.expect("Couldn't find suitable device.")
98106
};
99107

100-
// Create vulkan device
108+
// Create Vulkan device
101109
let device = {
102110
let device_extension_names_raw = [ash::extensions::khr::Swapchain::name().as_ptr()];
103111
let features = vk::PhysicalDeviceFeatures {
@@ -239,13 +247,7 @@ fn main() -> ash::prelude::VkResult<()> {
239247
unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap();
240248

241249
let mut imgui = imgui::Context::create();
242-
let mut platform = WinitPlatform::init(&mut imgui);
243-
// imgui.io_mut().display_size = [window_width as f32, window_height as f32];
244-
platform.attach_window(
245-
imgui.io_mut(),
246-
&window,
247-
HiDpiMode::Rounded, /* Default is blurry! */
248-
);
250+
imgui.io_mut().display_size = [window_width as f32, window_height as f32];
249251

250252
let descriptor_pool = {
251253
let pool_sizes = [
@@ -294,12 +296,11 @@ fn main() -> ash::prelude::VkResult<()> {
294296
.collect::<Vec<_>>();
295297

296298
let mut visualizer = Some(gpu_allocator::vulkan::AllocatorVisualizer::new());
297-
let mut last_frame = std::time::Instant::now();
298299

299300
event_loop.run(move |event, _, control_flow| {
300301
*control_flow = winit::event_loop::ControlFlow::Wait;
301302

302-
platform.handle_event(imgui.io_mut(), &window, &event);
303+
handle_imgui_event(imgui.io_mut(), &window, &event);
303304

304305
let mut ready_for_rendering = false;
305306
match event {
@@ -318,11 +319,6 @@ fn main() -> ash::prelude::VkResult<()> {
318319
_ => {}
319320
},
320321
winit::event::Event::MainEventsCleared => ready_for_rendering = true,
321-
winit::event::Event::NewEvents(_) => {
322-
let now = std::time::Instant::now();
323-
imgui.io_mut().update_delta_time(now - last_frame);
324-
last_frame = now;
325-
}
326322
_ => {}
327323
}
328324

@@ -338,9 +334,6 @@ fn main() -> ash::prelude::VkResult<()> {
338334
.unwrap();
339335

340336
// Start ImGui frame
341-
platform
342-
.prepare_frame(imgui.io_mut(), &window)
343-
.expect("Failed to prepare frame");
344337
let ui = imgui.frame();
345338

346339
// Submit visualizer ImGui commands
@@ -350,7 +343,6 @@ fn main() -> ash::prelude::VkResult<()> {
350343
.render(allocator.as_ref().unwrap(), &ui, None);
351344

352345
// Finish ImGui Frame
353-
platform.prepare_render(&ui, &window);
354346
let imgui_draw_data = ui.render();
355347

356348
record_and_submit_command_buffer(

src/d3d12/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl MemoryType {
452452
Err(AllocationError::OutOfMemory) => {} // Block is full, continue search.
453453
Err(err) => return Err(err), // Unhandled error, return.
454454
}
455-
} else if empty_block_index == None {
455+
} else if empty_block_index.is_none() {
456456
empty_block_index = Some(mem_block_i);
457457
}
458458
}

src/vulkan/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ impl MemoryType {
342342
_ => return Err(err), // Unhandled error, return.
343343
},
344344
}
345-
} else if empty_block_index == None {
345+
} else if empty_block_index.is_none() {
346346
empty_block_index = Some(mem_block_i);
347347
}
348348
}

0 commit comments

Comments
 (0)