From 7fbb11481b0f5868ca43189a73a22b5e9fcd49b5 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 21 Oct 2020 11:04:22 +0200 Subject: [PATCH] Try to protect against invalid Triangles --- egui/src/demos/color_test.rs | 4 ++-- egui/src/introspection.rs | 4 ++-- egui/src/paint/command.rs | 5 +++++ egui/src/paint/tessellator.rs | 23 ++++++++++++++++++++--- egui/src/widgets/color_picker.rs | 6 +++--- egui/src/widgets/image.rs | 4 ++-- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/egui/src/demos/color_test.rs b/egui/src/demos/color_test.rs index 5f614002..b8fe423c 100644 --- a/egui/src/demos/color_test.rs +++ b/egui/src/demos/color_test.rs @@ -271,7 +271,7 @@ fn vertex_gradient(ui: &mut Ui, bg_fill: Srgba, gradient: &Gradient) -> Response if bg_fill != Default::default() { let mut triangles = Triangles::default(); triangles.add_colored_rect(rect, bg_fill); - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); } { let n = gradient.0.len(); @@ -288,7 +288,7 @@ fn vertex_gradient(ui: &mut Ui, bg_fill: Srgba, gradient: &Gradient) -> Response triangles.add_triangle(2 * i + 1, 2 * i + 2, 2 * i + 3); } } - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); } ui.interact_hover(rect) } diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index e864d19f..7e303afb 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -22,7 +22,7 @@ impl Texture { let rect = ui.allocate_space(size); let mut triangles = Triangles::default(); triangles.add_rect_with_uv(rect, [pos2(0.0, 0.0), pos2(1.0, 1.0)].into(), WHITE); - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); let tex_w = self.width as f32; let tex_h = self.height as f32; @@ -48,7 +48,7 @@ impl Texture { ); let mut triangles = Triangles::default(); triangles.add_rect_with_uv(zoom_rect, uv_rect, WHITE); - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); }); } } diff --git a/egui/src/paint/command.rs b/egui/src/paint/command.rs index 968b7bc3..d2fd1067 100644 --- a/egui/src/paint/command.rs +++ b/egui/src/paint/command.rs @@ -108,6 +108,11 @@ impl PaintCmd { color, } } + + pub fn triangles(triangles: Triangles) -> Self { + debug_assert!(triangles.is_valid()); + Self::Triangles(triangles) + } } #[derive(Clone, Copy, Debug, Default, PartialEq)] diff --git a/egui/src/paint/tessellator.rs b/egui/src/paint/tessellator.rs index d47f2a19..8809e43e 100644 --- a/egui/src/paint/tessellator.rs +++ b/egui/src/paint/tessellator.rs @@ -105,6 +105,8 @@ impl Triangles { /// Append all the indices and vertices of `other` to `self`. pub fn append(&mut self, other: &Triangles) { + debug_assert!(other.is_valid()); + if self.is_empty() { self.texture_id = other.texture_id; } else { @@ -692,7 +694,11 @@ fn tessellate_paint_command( stroke_path(&path.0, Closed, stroke, options, out); } PaintCmd::Triangles(triangles) => { - out.append(&triangles); + if triangles.is_valid() { + out.append(&triangles); + } else { + debug_assert!(false, "Ivalid Triangles in PaintCmd::Traingles"); + } } PaintCmd::LineSegment { points, stroke } => { path.add_line_segment(points); @@ -831,8 +837,12 @@ pub fn tessellate_paint_commands( // TODO: cull(clip_rect, cmd) if let PaintCmd::Triangles(triangles) = cmd { - // Assume non-Egui texture, which means own paint job: - jobs.push((clip_rect, triangles)); + // Assume non-Egui texture, which means own paint job. + if triangles.is_valid() { + jobs.push((clip_rect, triangles)); + } else { + debug_assert!(false, "Ivalid Triangles in PaintCmd::Traingles"); + } continue; } @@ -880,5 +890,12 @@ pub fn tessellate_paint_commands( } } + for (_, triangles) in &jobs { + debug_assert!( + triangles.is_valid(), + "Tesselator generated invalid Triangles" + ); + } + jobs } diff --git a/egui/src/widgets/color_picker.rs b/egui/src/widgets/color_picker.rs index 90fae80e..234428d4 100644 --- a/egui/src/widgets/color_picker.rs +++ b/egui/src/widgets/color_picker.rs @@ -35,7 +35,7 @@ fn background_checkers(painter: &Painter, rect: Rect) { ); std::mem::swap(&mut top_color, &mut bottom_color); } - painter.add(PaintCmd::Triangles(triangles)); + painter.add(PaintCmd::triangles(triangles)); } pub fn show_color(ui: &mut Ui, color: impl Into, desired_size: Vec2) -> Response { @@ -105,7 +105,7 @@ fn color_slider_1d(ui: &mut Ui, value: &mut f32, color_at: impl Fn(f32) -> Srgba triangles.add_triangle(2 * i + 1, 2 * i + 2, 2 * i + 3); } } - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); } ui.painter().rect_stroke(rect, 0.0, visuals.bg_stroke); // outline @@ -169,7 +169,7 @@ fn color_slider_2d( } } } - ui.painter().add(PaintCmd::Triangles(triangles)); // fill + ui.painter().add(PaintCmd::triangles(triangles)); // fill ui.painter().rect_stroke(rect, 0.0, visuals.bg_stroke); // outline diff --git a/egui/src/widgets/image.rs b/egui/src/widgets/image.rs index a78e194e..cd7a9826 100644 --- a/egui/src/widgets/image.rs +++ b/egui/src/widgets/image.rs @@ -53,13 +53,13 @@ impl Widget for Image { if bg_fill != Default::default() { let mut triangles = Triangles::default(); triangles.add_colored_rect(rect, bg_fill); - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); } { // TODO: builder pattern for Triangles let mut triangles = Triangles::with_texture(texture_id); triangles.add_rect_with_uv(rect, uv, tint); - ui.painter().add(PaintCmd::Triangles(triangles)); + ui.painter().add(PaintCmd::triangles(triangles)); } ui.interact_hover(rect)