Try to protect against invalid Triangles

This commit is contained in:
Emil Ernerfeldt 2020-10-21 11:04:22 +02:00
parent bc5dbd718e
commit 7fbb11481b
6 changed files with 34 additions and 12 deletions

View file

@ -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)
}

View file

@ -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));
});
}
}

View file

@ -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)]

View file

@ -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) => {
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:
// 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
}

View file

@ -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<Srgba>, 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

View file

@ -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)