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

Fix crashes related to invalid scissor or window size #18

Merged
merged 4 commits into from
Apr 24, 2021
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
27 changes: 20 additions & 7 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl EguiNode {
struct DrawCommand {
vertices_count: usize,
texture_handle: Option<Handle<Texture>>,
clipping_zone: egui::Rect,
clipping_zone: (u32, u32, u32, u32), // x, y, w, h
}

impl Node for EguiNode {
Expand Down Expand Up @@ -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)
Expand All @@ -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),
});
}

Expand Down Expand Up @@ -309,12 +323,11 @@ impl Node for EguiNode {
None,
);

let scale_factor = window_size.scale_factor * egui_settings.scale_factor as f32;
render_pass.set_scissor_rect(
(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.0,
draw_command.clipping_zone.1,
draw_command.clipping_zone.2,
draw_command.clipping_zone.3,
);
render_pass.draw_indexed(
vertex_offset..(vertex_offset + draw_command.vertices_count as u32),
Expand Down
19 changes: 11 additions & 8 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down