Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to winit 0.29 #67

Merged
merged 10 commits into from
Jan 10, 2024
Merged
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Ability to select from available swapchain surface formats when creating an `EventLoop`
- Driver `surface` and `swapchain` modules (_and their types_) are now public API

### Changed

- Changed `KeyBuf` implementation functions to take values instead of borrows
- Updated `winit` to v0.29
- Updated `egui` to v0.25
- Updated `imgui-rs` to latest
[`main`](https://github.com/imgui-rs/imgui-rs/tree/ca05418cb449dadaabf014487c5c965908dfcbdd)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ profiling = "1.0"
raw-window-handle = "0.5"
spirq = "=1.1.1"
vk-sync = { version = "0.4.0", package = "vk-sync-fork" } # // SEE: https://github.com/gwihlidal/vk-sync-rs/pull/4 -> https://github.com/expenses/vk-sync-rs
winit = { version = "0.28" }
winit = { version = "0.29", features = ["rwh_05"] }

[target.'cfg(target_os = "macos")'.dependencies]
ash-molten = "0.15"
Expand Down
4 changes: 2 additions & 2 deletions contrib/screen-13-egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ readme = "README.md"

[dependencies]
bytemuck = "1.13"
egui = { version = "0.22", features = [
egui = { version = "0.25", features = [
"bytemuck"
] }
egui-winit = "0.22"
egui-winit = "0.25"
inline-spirv = "0.1"
screen-13 = { path = "../.." }
screen-13-fx = { path = "../screen-13-fx" }
29 changes: 24 additions & 5 deletions contrib/screen-13-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,29 @@ impl Egui {
.unwrap(),
);

let ctx = egui::Context::default();
let native_pixels_per_point = event_loop
.primary_monitor()
.map(|monitor| monitor.scale_factor() as f32);
let max_texture_side = Some(
device
.physical_device
.properties_v1_0
.limits
.max_image_dimension2_d as usize,
);
let egui_winit = egui_winit::State::new(
ctx.clone(),
egui::ViewportId::ROOT,
event_loop,
native_pixels_per_point,
max_texture_side,
);

Self {
ppl,
ctx: egui::Context::default(),
egui_winit: egui_winit::State::new(event_loop),
ctx,
egui_winit,
textures: HashMap::default(),
cache: HashPool::new(device),
next_tex_id: 0,
Expand Down Expand Up @@ -201,7 +220,7 @@ impl Egui {
for egui::ClippedPrimitive {
clip_rect,
primitive,
} in self.ctx.tessellate(shapes)
} in self.ctx.tessellate(shapes, self.ctx.pixels_per_point())
{
match primitive {
egui::epaint::Primitive::Mesh(mesh) => {
Expand Down Expand Up @@ -298,15 +317,15 @@ impl Egui {
if let Event::WindowEvent { event, .. } = event {
#[allow(unused_must_use)]
{
self.egui_winit.on_event(&self.ctx, event);
self.egui_winit.on_window_event(window, event);
}
}
}
let raw_input = self.egui_winit.take_egui_input(window);
let full_output = self.ctx.run(raw_input, ui_fn);

self.egui_winit
.handle_platform_output(window, &self.ctx, full_output.platform_output);
.handle_platform_output(window, full_output.platform_output);

let deltas = full_output.textures_delta;

Expand Down
2 changes: 1 addition & 1 deletion contrib/screen-13-hot/examples/glsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> Result<(), DisplayError> {
pretty_env_logger::init();

let event_loop = EventLoop::new()
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.desired_surface_format(Surface::linear_or_default)
.build()?;

// Create a compute pipeline - the same as normal except for "Hot" prefixes and we provide the
Expand Down
5 changes: 2 additions & 3 deletions contrib/screen-13-imgui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ readme = "README.md"
[dependencies]
bytemuck = "1.13"

# HACK: https://github.com/imgui-rs/imgui-rs/pull/716
imgui = { git = "https://github.com/imgui-rs/imgui-rs", rev = "4d0c9ebc0faaea421e59e0a58f712abbb18c3093" }
imgui-winit-support = { git = "https://github.com/imgui-rs/imgui-rs", rev = "4d0c9ebc0faaea421e59e0a58f712abbb18c3093" }
imgui = { git = "https://github.com/imgui-rs/imgui-rs", rev = "ca05418cb449dadaabf014487c5c965908dfcbdd" }
imgui-winit-support = { git = "https://github.com/imgui-rs/imgui-rs", rev = "ca05418cb449dadaabf014487c5c965908dfcbdd" }

inline-spirv = "0.1"
screen-13 = { path = "../.." }
Expand Down
2 changes: 1 addition & 1 deletion contrib/screen-13-imgui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ImGui {
pub fn draw(
&mut self,
dt: f32,
events: &[Event<'_, ()>],
events: &[Event<()>],
window: &Window,
render_graph: &mut RenderGraph,
ui_func: impl FnOnce(&mut Ui),
Expand Down
3 changes: 2 additions & 1 deletion examples/egui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fn main() -> Result<(), DisplayError> {

let event_loop = EventLoop::new()
.desired_swapchain_image_count(2)
.desired_surface_format(Surface::linear_or_default)
.window(|window| window.with_transparent(false))
.build()?;
let mut egui = Egui::new(&event_loop.device, event_loop.as_ref());
Expand Down Expand Up @@ -44,7 +45,7 @@ fn main() -> Result<(), DisplayError> {
.show(ui, |ui| {
ui.add(egui::Button::new("Test"));
ui.add(egui::Link::new("Test"));
ui.add(egui::Image::new(id, [50., 50.]));
ui.add(egui::Image::new((id, egui::Vec2::new(50., 50.))));
});
},
);
Expand Down
2 changes: 1 addition & 1 deletion examples/imgui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() -> Result<(), DisplayError> {

// Screen 13 things we need for this demo
let event_loop = EventLoop::new()
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.desired_surface_format(Surface::linear_or_default)
.desired_swapchain_image_count(2)
.build()?;
let display = ComputePresenter::new(&event_loop.device)?;
Expand Down
12 changes: 6 additions & 6 deletions examples/ray_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,17 +788,17 @@ fn main() -> anyhow::Result<()> {

const SPEED: f32 = 0.01f32;

if keyboard.is_pressed(&VirtualKeyCode::Left) {
if keyboard.is_pressed(KeyCode::ArrowLeft) {
position[0] -= SPEED;
} else if keyboard.is_pressed(&VirtualKeyCode::Right) {
} else if keyboard.is_pressed(KeyCode::ArrowRight) {
position[0] += SPEED;
} else if keyboard.is_pressed(&VirtualKeyCode::Up) {
} else if keyboard.is_pressed(KeyCode::ArrowUp) {
position[2] -= SPEED;
} else if keyboard.is_pressed(&VirtualKeyCode::Down) {
} else if keyboard.is_pressed(KeyCode::ArrowDown) {
position[2] += SPEED;
} else if keyboard.is_pressed(&VirtualKeyCode::Space) {
} else if keyboard.is_pressed(KeyCode::Space) {
position[1] -= SPEED;
} else if keyboard.is_pressed(&VirtualKeyCode::LAlt) {
} else if keyboard.is_pressed(KeyCode::AltLeft) {
position[1] += SPEED;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/rt_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn main() -> anyhow::Result<()> {
pretty_env_logger::init();

let event_loop = EventLoop::new()
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.desired_surface_format(Surface::linear_or_default)
.build()?;
let mut pool = HashPool::new(&event_loop.device);

Expand Down
2 changes: 1 addition & 1 deletion examples/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() -> anyhow::Result<()> {
// Create Screen 13 things any similar program might need
let event_loop = EventLoop::new()
.window(|builder| builder.with_inner_size(LogicalSize::new(1024.0f64, 768.0f64)))
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.desired_surface_format(Surface::linear_or_default)
.build()?;
let display = ComputePresenter::new(&event_loop.device)?;
let mut imgui = ImGui::new(&event_loop.device);
Expand Down
10 changes: 5 additions & 5 deletions examples/vsm_omni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ fn main() -> anyhow::Result<()> {
update_keyboard(&mut keyboard, frame.events);

// Hold spacebar to stop the light
if !keyboard.is_held(&VirtualKeyCode::Space) {
if !keyboard.is_held(KeyCode::Space) {
elapsed += frame.dt;
}

// Hit F11 to enable borderless fullscreen
if keyboard.is_pressed(&VirtualKeyCode::F11) {
if keyboard.is_pressed(KeyCode::F11) {
frame
.window
.set_fullscreen(Some(Fullscreen::Borderless(None)));
}

// Hit F12 to enable exclusive fullscreen
if keyboard.is_pressed(&VirtualKeyCode::F12) {
if keyboard.is_pressed(KeyCode::F12) {
if let Some(monitor) = frame.window.current_monitor() {
if let Some(video_mode) = monitor.video_modes().next() {
frame
Expand All @@ -119,7 +119,7 @@ fn main() -> anyhow::Result<()> {
}

// Hit Escape to cancel fullscreen or exit
if keyboard.is_pressed(&VirtualKeyCode::Escape) {
if keyboard.is_pressed(KeyCode::Escape) {
if frame.window.fullscreen().is_some() {
frame.window.set_fullscreen(None);
} else {
Expand Down Expand Up @@ -213,7 +213,7 @@ fn main() -> anyhow::Result<()> {
);

// Hold tab to view a debug mode
if keyboard.is_held(&VirtualKeyCode::Tab) {
if keyboard.is_held(KeyCode::Tab) {
frame
.render_graph
.begin_pass("DEBUG")
Expand Down
4 changes: 3 additions & 1 deletion src/driver/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ impl Device {
) {
Ok(properties) => Ok(properties),
Err(err) if err == vk::Result::ERROR_FORMAT_NOT_SUPPORTED => {
error!("Format not supported");
// We don't log this condition because it is normal for unsupported
// formats to be checked - we use the result to inform callers they
// cannot use those formats. TODO: This may be better as an Option...

Err(DriverError::Unsupported)
}
Expand Down
5 changes: 2 additions & 3 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ pub mod image;
pub mod physical_device;
pub mod ray_trace;
pub mod shader;
pub mod surface;
pub mod swapchain;

mod cmd_buf;
mod descriptor_set;
mod descriptor_set_layout;
mod instance;
mod render_pass;
mod surface;

pub(crate) mod swapchain;

pub use {
self::{cmd_buf::CommandBuffer, instance::Instance},
Expand Down
64 changes: 61 additions & 3 deletions src/driver/surface.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Native platform window surface types.

use {
super::{device::Device, DriverError, Instance},
ash::vk,
Expand All @@ -12,13 +14,18 @@ use {
},
};

/// Smart pointer handle to a [`vk::SurfaceKHR`] object.
pub struct Surface {
device: Arc<Device>,
surface: vk::SurfaceKHR,
}

impl Surface {
pub fn new(
/// Create a surface from a raw window display handle.
///
/// `device` must have been created with platform specific surface extensions enabled, acquired
/// through [`Device::create_display_window`].
pub fn create(
device: &Arc<Device>,
display_window: &(impl HasRawDisplayHandle + HasRawWindowHandle),
) -> Result<Self, DriverError> {
Expand All @@ -34,14 +41,15 @@ impl Surface {
)
}
.map_err(|err| {
error!("unable to create surface: {err}");
error!("Unable to create surface: {err}");

DriverError::Unsupported
})?;

Ok(Self { device, surface })
}

/// Lists the supported surface formats.
pub fn formats(this: &Self) -> Result<Vec<vk::SurfaceFormatKHR>, DriverError> {
unsafe {
this.device
Expand All @@ -50,12 +58,62 @@ impl Surface {
.unwrap()
.get_physical_device_surface_formats(*this.device.physical_device, this.surface)
.map_err(|err| {
error!("unable to get surface formats: {err}");
error!("Unable to get surface formats: {err}");

DriverError::Unsupported
})
}
}

/// Helper function to automatically select the best UNORM format, if one is available.
pub fn linear(formats: &[vk::SurfaceFormatKHR]) -> Option<vk::SurfaceFormatKHR> {
formats
.iter()
.find(|&&vk::SurfaceFormatKHR { format, .. }| {
matches!(
format,
vk::Format::R8G8B8A8_UNORM | vk::Format::B8G8R8A8_UNORM
)
})
.copied()
}

/// Helper function to automatically select the best UNORM format.
///
/// **_NOTE:_** The default surface format is undefined, and although legal the results _may_
/// not support presentation. You should prefer to use [`Surface::linear`] and fall back to
/// supported values manually.
pub fn linear_or_default(formats: &[vk::SurfaceFormatKHR]) -> vk::SurfaceFormatKHR {
Self::linear(formats).unwrap_or_else(|| formats.first().copied().unwrap_or_default())
}

/// Helper function to automatically select the best sRGB format, if one is available.
pub fn srgb(formats: &[vk::SurfaceFormatKHR]) -> Option<vk::SurfaceFormatKHR> {
formats
.iter()
.find(
|&&vk::SurfaceFormatKHR {
color_space,
format,
}| {
matches!(color_space, vk::ColorSpaceKHR::SRGB_NONLINEAR)
&& matches!(
format,
vk::Format::R8G8B8A8_SRGB | vk::Format::B8G8R8A8_SRGB
)
},
)
.copied()
}

/// Helper function to automatically select the best sRGB format.
///
/// **_NOTE:_** The default surface format is undefined, and although legal the results _may_
/// not support presentation. You should prefer to use [`Surface::srgb`] and fall back to
/// supported values manually.
pub fn srgb_or_default(formats: &[vk::SurfaceFormatKHR]) -> vk::SurfaceFormatKHR {
Self::srgb(formats).unwrap_or_else(|| formats.first().copied().unwrap_or_default())
}
}

impl Debug for Surface {
Expand Down
Loading
Loading