Skip to content

Commit 807df63

Browse files
committed
performance optimisation
1 parent e30801c commit 807df63

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

crates/processing_glfw/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use processing_core::error::Result;
1313
use processing_input::{
1414
input_cursor_grab_mode, input_cursor_visible, input_flush, input_set_char,
1515
input_set_cursor_enter, input_set_cursor_leave, input_set_focus, input_set_key,
16-
input_set_mouse_button, input_set_mouse_move, input_set_scroll,
16+
input_set_mouse_button, input_set_mouse_move, input_set_scroll, input_window_resize,
1717
};
1818
use processing_render::surface::{MonitorWorkarea, WindowControls};
1919

@@ -256,6 +256,7 @@ impl GlfwContext {
256256
}
257257
};
258258

259+
let mut pending_resize: Option<(i32, i32)> = None;
259260
for (_, event) in glfw::flush_messages(&self.events) {
260261
match event {
261262
WindowEvent::Close => {
@@ -301,8 +302,8 @@ impl GlfwContext {
301302
input_set_focus(surface, focused).unwrap();
302303
}
303304
WindowEvent::Size(width, height) => {
304-
processing_render::surface_resize(surface, width as u32, height as u32)
305-
.unwrap();
305+
// A drag delivers many Size events per poll; keep only the last and apply it once after the loop.
306+
pending_resize = Some((width, height));
306307
}
307308
_ => {}
308309
}
@@ -313,6 +314,12 @@ impl GlfwContext {
313314
return false;
314315
}
315316

317+
// Apply the coalesced resize once, the WindowResized message and Window change are processed this poll.
318+
if let Some((width, height)) = pending_resize {
319+
input_window_resize(surface, width as f32, height as f32).unwrap();
320+
processing_render::surface_resize(surface, width as u32, height as u32).unwrap();
321+
}
322+
316323
let Ok(_) = input_flush() else {
317324
return false;
318325
};

crates/processing_input/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy::input::mouse::{
88
};
99
use bevy::input::touch::TouchPhase;
1010
use bevy::prelude::*;
11-
use bevy::window::CursorMoved;
11+
use bevy::window::{CursorMoved, WindowResized};
1212

1313
use processing_core::app_mut;
1414
use processing_core::error;
@@ -342,3 +342,14 @@ pub fn input_mouse_scrolled() -> error::Result<bool> {
342342
Ok(d.x != 0.0 || d.y != 0.0)
343343
})
344344
}
345+
346+
pub fn input_window_resize(surface: Entity, width: f32, height: f32) -> error::Result<()> {
347+
app_mut(|app| {
348+
app.world_mut().write_message(WindowResized {
349+
window: surface,
350+
width,
351+
height,
352+
});
353+
Ok(())
354+
})
355+
}

crates/processing_render/src/graphics.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bevy::{
2222
sync_world::MainEntity,
2323
view::ViewTarget,
2424
},
25-
window::{WindowRef, WindowResized},
25+
window::WindowRef,
2626
};
2727

2828
use crate::{
@@ -260,7 +260,6 @@ pub fn sync_to_surface(
260260
mut graphics_query: Query<(&mut Graphics, &RenderTarget)>,
261261
windows: Query<&Window, (With<Surface>, Changed<Window>)>,
262262
render_device: Res<RenderDevice>,
263-
mut resize_messages: MessageWriter<WindowResized>,
264263
) {
265264
for (mut graphics, target) in graphics_query.iter_mut() {
266265
let RenderTarget::Window(WindowRef::Entity(surface_entity)) = *target else {
@@ -274,12 +273,6 @@ pub fn sync_to_surface(
274273
if graphics.size.width == physical_w && graphics.size.height == physical_h {
275274
continue;
276275
}
277-
//winit plugin disabled, so WindowResized is never triggered automatically
278-
resize_messages.write(WindowResized {
279-
window: surface_entity,
280-
width: window.width(),
281-
height: window.height(),
282-
});
283276
graphics.size = Extent3d {
284277
width: physical_w,
285278
height: physical_h,

0 commit comments

Comments
 (0)