Skip to content

Commit

Permalink
fix dpi handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 6, 2023
1 parent 7a3c598 commit b24f387
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets_gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl engine::framework::State for State {
fc.draw(self.gui.shown.clone());
}

fn resized(&mut self, ctx: &mut Context, size: (u32, u32)) {
fn resized(&mut self, ctx: &mut Context, size: (u32, u32, f64)) {
self.camera.resize(ctx, size.0 as f32, size.1 as f32);
}

Expand Down
26 changes: 10 additions & 16 deletions engine/src/egui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ pub struct EguiWrapper {
pub last_mouse_captured: bool,
pub last_kb_captured: bool,
pub to_remove: Vec<TextureId>,
pub pixels_per_point: f32,
pub zoom_factor: f32,
}

impl EguiWrapper {
pub fn new(gfx: &GfxContext, el: &EventLoopWindowTarget<()>) -> Self {
let egui = egui::Context::default();

let platform = egui_winit::State::new(egui.viewport_id(), el, None, None);
let platform =
egui_winit::State::new(egui.viewport_id(), el, Some(gfx.size.2 as f32), None);

let renderer = renderer::Renderer::new(&gfx.device, gfx.fbos.format, None, 1);

Expand All @@ -32,7 +33,7 @@ impl EguiWrapper {
last_kb_captured: false,
platform,
to_remove: vec![],
pixels_per_point: 1.0,
zoom_factor: 1.0,
}
}

Expand All @@ -46,22 +47,15 @@ impl EguiWrapper {
self.renderer.free_texture(&id);
}

let mut rinput = self.platform.take_egui_input(window);
rinput.screen_rect = Some(egui::Rect::from_min_size(
Default::default(),
egui::vec2(
gfx.size.0 as f32 / self.pixels_per_point,
gfx.size.1 as f32 / self.pixels_per_point,
),
));
for v in rinput.viewports.values_mut() {
v.native_pixels_per_point = Some(self.pixels_per_point);
}
let rinput = self.platform.take_egui_input(window);
self.egui.set_zoom_factor(self.zoom_factor);

let output = self.egui.run(rinput, |ctx| {
ui_render(ctx);
});
let clipped_primitives = self.egui.tessellate(output.shapes, self.pixels_per_point);
let clipped_primitives = self
.egui
.tessellate(output.shapes, self.egui.pixels_per_point());

//let mut rpass = gfx.rpass.take().unwrap();
for (id, delta) in output.textures_delta.set {
Expand All @@ -70,7 +64,7 @@ impl EguiWrapper {
}
let desc = ScreenDescriptor {
size_in_pixels: [gfx.size.0, gfx.size.1],
pixels_per_point: self.pixels_per_point,
pixels_per_point: self.egui.pixels_per_point(),
};
self.renderer.update_buffers(
gfx.device,
Expand Down
36 changes: 24 additions & 12 deletions engine/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::egui::EguiWrapper;
use crate::{AudioContext, FrameContext, GfxContext, InputContext};
use std::mem::ManuallyDrop;
use std::time::Instant;
use winit::dpi::PhysicalSize;
use winit::window::Window;
use winit::{
dpi::PhysicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
};
Expand All @@ -20,7 +20,7 @@ pub trait State: 'static {
fn render(&mut self, fc: &mut FrameContext);

/// Called when the window is resized.
fn resized(&mut self, ctx: &mut Context, size: (u32, u32)) {}
fn resized(&mut self, ctx: &mut Context, size: (u32, u32, f64)) {}

/// Called when the window asks to exit (e.g ALT+F4) to be able to control the flow, for example to ask "save before exit?".
/// Return true to exit, false to cancel.
Expand All @@ -38,7 +38,9 @@ async fn run<S: State>(el: EventLoop<()>, window: Window) {
ctx.gfx.defines_changed = false;

let mut frame: Option<ManuallyDrop<_>> = None;
let mut new_size: Option<PhysicalSize<u32>> = None;
let mut scale_factor = ctx.window.scale_factor();
log::info!("initial scale factor: {:?}", scale_factor);
let mut new_size: Option<(PhysicalSize<u32>, f64)> = None;
let mut last_update = Instant::now();

el.run(move |event, _, control_flow| {
Expand All @@ -57,8 +59,17 @@ async fn run<S: State>(el: EventLoop<()>, window: Window) {
match event {
WindowEvent::Resized(physical_size) => {
log::info!("resized: {:?}", physical_size);
new_size = Some(physical_size);
frame.take();
new_size = Some((physical_size, scale_factor));
frame.take().map(|v| ManuallyDrop::into_inner(v));
}
WindowEvent::ScaleFactorChanged {
scale_factor: sf,
new_inner_size
} => {
log::info!("scale_factor: {:?}", scale_factor);
scale_factor = sf;
new_size = Some((*new_inner_size, scale_factor));
frame.take().map(|v| ManuallyDrop::into_inner(v));
}
WindowEvent::CloseRequested => {
if state.exit() {
Expand All @@ -70,18 +81,18 @@ async fn run<S: State>(el: EventLoop<()>, window: Window) {
}
Event::MainEventsCleared => match frame.take() {
None => {
if let Some(new_size) = new_size.take() {
if new_size.height != 0 ||new_size.width != 0 {
ctx.gfx.resize(new_size.width, new_size.height);
state.resized(&mut ctx, (new_size.width, new_size.height));
if let Some((new_size, sf)) = new_size.take() {
if new_size.height != 0 || new_size.width != 0 {
ctx.gfx.resize(new_size.width, new_size.height, sf);
state.resized(&mut ctx, (new_size.width, new_size.height, sf));
ctx.gfx.update_sc = false;
}
}

let size = ctx.gfx.size;
if ctx.gfx.update_sc {
ctx.gfx.update_sc = false;
ctx.gfx.resize(size.0, size.1);
ctx.gfx.resize(size.0, size.1, size.2);
state.resized(
&mut ctx,
size,
Expand All @@ -95,7 +106,7 @@ async fn run<S: State>(el: EventLoop<()>, window: Window) {
Err(wgpu::SurfaceError::Outdated)
| Err(wgpu::SurfaceError::Lost)
| Err(wgpu::SurfaceError::Timeout) => {
ctx.gfx.resize(size.0, size.1);
ctx.gfx.resize(size.0, size.1, size.2);
state.resized(&mut ctx, size);
log::error!("swapchain has been lost or is outdated, recreating before retrying");
}
Expand Down Expand Up @@ -186,7 +197,7 @@ pub fn start<S: State>() {
window = wb;
}
let window = window
.with_inner_size(winit::dpi::PhysicalSize::new(
.with_inner_size(PhysicalSize::new(
size.width as f32 * 0.8,
size.height as f32 * 0.8,
))
Expand Down Expand Up @@ -214,6 +225,7 @@ impl Context {
&window,
window.inner_size().width,
window.inner_size().height,
window.scale_factor(),
)
.await;
let input = InputContext::default();
Expand Down
11 changes: 6 additions & 5 deletions engine/src/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct GfxContext {
pub device: Device,
pub queue: Queue,
pub fbos: FBOs,
pub size: (u32, u32),
pub size: (u32, u32, f64),
pub(crate) sc_desc: SurfaceConfiguration,
pub update_sc: bool,

Expand Down Expand Up @@ -127,7 +127,7 @@ u8slice_impl!(RenderParams);
pub struct GuiRenderContext<'a, 'b> {
pub encoder: &'a mut CommandEncoder,
pub view: &'a TextureView,
pub size: (u32, u32),
pub size: (u32, u32, f64),
pub device: &'a Device,
pub queue: &'a Queue,
pub rpass: Option<wgpu::RenderPass<'b>>,
Expand All @@ -149,6 +149,7 @@ impl GfxContext {
window: &W,
win_width: u32,
win_height: u32,
win_scale_factor: f64,
) -> Self {
let mut backends = backend_bits_from_env().unwrap_or_else(Backends::all);
if std::env::var("RENDERDOC").is_ok() {
Expand Down Expand Up @@ -243,7 +244,7 @@ impl GfxContext {
.build(&device, &queue);

let mut me = Self {
size: (win_width, win_height),
size: (win_width, win_height, win_scale_factor),
sc_desc,
update_sc: false,
adapter,
Expand Down Expand Up @@ -720,8 +721,8 @@ impl GfxContext {
}
}

pub fn resize(&mut self, width: u32, height: u32) {
self.size = (width, height);
pub fn resize(&mut self, width: u32, height: u32, scale_factor: f64) {
self.size = (width, height, scale_factor);
self.sc_desc.width = self.size.0;
self.sc_desc.height = self.size.1;

Expand Down
3 changes: 2 additions & 1 deletion engine_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
common = { path = "../common" }
geom = { path = "../geom" }
engine = { path = "../engine" }
engine = { path = "../engine" }
log = { version = "0.4.11", features=["max_level_info", "release_max_level_info"] }
inline_tweak = "1.0.8"
egui = { workspace = true }
8 changes: 7 additions & 1 deletion engine_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ impl engine::framework::State for State {
fc.draw(self.meshes.clone());
}

fn resized(&mut self, _: &mut Context, size: (u32, u32)) {
fn render_gui(&mut self, ui: &egui::Context) {
egui::Window::new("Hello world!").show(ui, |ui| {
ui.label("Hello world!");
});
}

fn resized(&mut self, _: &mut Context, size: (u32, u32, f64)) {
self.camera.set_viewport(size.0 as f32, size.1 as f32);
}
}
Expand Down
4 changes: 2 additions & 2 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct State {

impl engine::framework::State for State {
fn new(ctx: &mut Context) -> Self {
let camera = OrbitCamera::load(ctx.gfx.size);
let camera = OrbitCamera::load((ctx.gfx.size.0, ctx.gfx.size.1));

Gui::set_style(&ctx.egui.egui);
log::info!("loaded egui_render");
Expand Down Expand Up @@ -196,7 +196,7 @@ impl engine::framework::State for State {
.add_value(start.elapsed().as_secs_f32());
}

fn resized(&mut self, ctx: &mut Context, size: (u32, u32)) {
fn resized(&mut self, ctx: &mut Context, size: (u32, u32, f64)) {
self.uiw
.write::<OrbitCamera>()
.resize(ctx, size.0 as f32, size.1 as f32);
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/gui/windows/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ pub fn manage_settings(ctx: &mut engine::Context, settings: &Settings) {
ctx.gfx
.set_define_flag("TERRAIN_GRID", settings.terrain_grid);

ctx.egui.pixels_per_point = settings.gui_scale;
ctx.egui.zoom_factor = settings.gui_scale;

ctx.audio.set_settings(
settings.master_volume_percent,
Expand Down

0 comments on commit b24f387

Please sign in to comment.