From e271ee78a069ab855346f8489a55b913450eb1ac Mon Sep 17 00:00:00 2001 From: Stephan Buys Date: Wed, 14 Apr 2021 13:11:11 +0200 Subject: [PATCH 1/4] Do not attempt to render widgets with no height or width. --- src/egui_node.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/egui_node.rs b/src/egui_node.rs index 960762f33..94341d43a 100644 --- a/src/egui_node.rs +++ b/src/egui_node.rs @@ -310,18 +310,25 @@ impl Node for EguiNode { ); let scale_factor = window_size.scale_factor * egui_settings.scale_factor as f32; - render_pass.set_scissor_rect( + + let (x, y, w, mut h) = ( (draw_command.clipping_zone.min.x * scale_factor) as u32, (draw_command.clipping_zone.min.y * scale_factor) as u32, (draw_command.clipping_zone.width() * scale_factor) as u32, - (draw_command.clipping_zone.height() * scale_factor) as u32, - ); - render_pass.draw_indexed( - vertex_offset..(vertex_offset + draw_command.vertices_count as u32), - 0, - 0..1, + (draw_command.clipping_zone.height() * scale_factor) as u32 ); - vertex_offset += draw_command.vertices_count as u32; + + if h != 0 && w != 0 { + render_pass.set_scissor_rect( + x, y, w, h, + ); + render_pass.draw_indexed( + vertex_offset..(vertex_offset + draw_command.vertices_count as u32), + 0, + 0..1, + ); + vertex_offset += draw_command.vertices_count as u32; + } } }, ); From 62d71c68c3b1867303746c66de0814bb82a0954f Mon Sep 17 00:00:00 2001 From: Stephan Buys Date: Wed, 14 Apr 2021 13:15:51 +0200 Subject: [PATCH 2/4] Remove stray mutability modifier --- src/egui_node.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/egui_node.rs b/src/egui_node.rs index 94341d43a..ae1d022f7 100644 --- a/src/egui_node.rs +++ b/src/egui_node.rs @@ -311,7 +311,7 @@ impl Node for EguiNode { let scale_factor = window_size.scale_factor * egui_settings.scale_factor as f32; - let (x, y, w, mut h) = ( + let (x, y, w, h) = ( (draw_command.clipping_zone.min.x * scale_factor) as u32, (draw_command.clipping_zone.min.y * scale_factor) as u32, (draw_command.clipping_zone.width() * scale_factor) as u32, From 488dc6dd47212e4e25f159de8b9c41e0acc42ee3 Mon Sep 17 00:00:00 2001 From: Stephan Buys Date: Wed, 14 Apr 2021 13:21:10 +0200 Subject: [PATCH 3/4] cargo fmt --- src/egui_node.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/egui_node.rs b/src/egui_node.rs index ae1d022f7..62bc20571 100644 --- a/src/egui_node.rs +++ b/src/egui_node.rs @@ -315,13 +315,11 @@ impl Node for EguiNode { (draw_command.clipping_zone.min.x * scale_factor) as u32, (draw_command.clipping_zone.min.y * scale_factor) as u32, (draw_command.clipping_zone.width() * scale_factor) as u32, - (draw_command.clipping_zone.height() * scale_factor) as u32 + (draw_command.clipping_zone.height() * scale_factor) as u32, ); if h != 0 && w != 0 { - render_pass.set_scissor_rect( - x, y, w, h, - ); + render_pass.set_scissor_rect(x, y, w, h); render_pass.draw_indexed( vertex_offset..(vertex_offset + draw_command.vertices_count as u32), 0, From a826f8c3a96fe309bf5a4914af4858bb8f734546 Mon Sep 17 00:00:00 2001 From: mvlabat Date: Sat, 24 Apr 2021 14:14:24 +0300 Subject: [PATCH 4/4] Fix crashes related to invalid scissor or window size --- src/egui_node.rs | 46 +++++++++++++++++++++++++++------------------- src/systems.rs | 19 +++++++++++-------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/egui_node.rs b/src/egui_node.rs index 62bc20571..11d49b342 100644 --- a/src/egui_node.rs +++ b/src/egui_node.rs @@ -152,7 +152,7 @@ impl EguiNode { struct DrawCommand { vertices_count: usize, texture_handle: Option>, - clipping_zone: egui::Rect, + clipping_zone: (u32, u32, u32, u32), // x, y, w, h } impl Node for EguiNode { @@ -227,7 +227,19 @@ impl Node for EguiNode { let mut draw_commands = Vec::new(); let mut index_offset = 0; + let scale_factor = window_size.scale_factor * egui_settings.scale_factor as f32; for egui::ClippedMesh(rect, triangles) in &egui_paint_jobs { + let (x, y, w, h) = ( + (rect.min.x * scale_factor).round() as u32, + (rect.min.y * scale_factor).round() as u32, + (rect.width() * scale_factor).round() as u32, + (rect.height() * scale_factor).round() as u32, + ); + + if w < 1 || h < 1 { + continue; + } + let texture_handle = egui_context .egui_textures .get(&triangles.texture_id) @@ -254,10 +266,12 @@ impl Node for EguiNode { index_buffer.extend_from_slice(indices_with_offset.as_slice().as_bytes()); index_offset += triangles.vertices.len() as u32; + let x_viewport_clamp = (x + w).saturating_sub(window_size.physical_width as u32); + let y_viewport_clamp = (y + h).saturating_sub(window_size.physical_height as u32); draw_commands.push(DrawCommand { vertices_count: triangles.indices.len(), texture_handle, - clipping_zone: *rect, + clipping_zone: (x, y, w - x_viewport_clamp, h - y_viewport_clamp), }); } @@ -309,24 +323,18 @@ impl Node for EguiNode { None, ); - let scale_factor = window_size.scale_factor * egui_settings.scale_factor as f32; - - let (x, y, w, h) = ( - (draw_command.clipping_zone.min.x * scale_factor) as u32, - (draw_command.clipping_zone.min.y * scale_factor) as u32, - (draw_command.clipping_zone.width() * scale_factor) as u32, - (draw_command.clipping_zone.height() * scale_factor) as u32, + render_pass.set_scissor_rect( + draw_command.clipping_zone.0, + draw_command.clipping_zone.1, + draw_command.clipping_zone.2, + draw_command.clipping_zone.3, ); - - if h != 0 && w != 0 { - render_pass.set_scissor_rect(x, y, w, h); - render_pass.draw_indexed( - vertex_offset..(vertex_offset + draw_command.vertices_count as u32), - 0, - 0..1, - ); - vertex_offset += draw_command.vertices_count as u32; - } + render_pass.draw_indexed( + vertex_offset..(vertex_offset + draw_command.vertices_count as u32), + 0, + 0..1, + ); + vertex_offset += draw_command.vertices_count as u32; } }, ); diff --git a/src/systems.rs b/src/systems.rs index a64d3620a..34819e0be 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -71,17 +71,20 @@ pub fn process_input( window.physical_height() as f32, window.scale_factor() as f32, ); + let width = window_size.physical_width + / window_size.scale_factor + / egui_settings.scale_factor as f32; + let height = window_size.physical_height + / window_size.scale_factor + / egui_settings.scale_factor as f32; + + if width < 1.0 || height < 1.0 { + continue; + } egui_input.raw_input.screen_rect = Some(egui::Rect::from_min_max( egui::pos2(0.0, 0.0), - egui::pos2( - window_size.physical_width - / window_size.scale_factor - / egui_settings.scale_factor as f32, - window_size.physical_height - / window_size.scale_factor - / egui_settings.scale_factor as f32, - ), + egui::pos2(width, height), )); egui_input.raw_input.pixels_per_point =