tesselator: produce fewer paint jobs with PaintCmd::Triangles

This commit is contained in:
Emil Ernerfeldt 2020-10-21 11:09:42 +02:00
parent 7fbb11481b
commit 9b01c75e16
2 changed files with 21 additions and 23 deletions

View file

@ -113,6 +113,14 @@ impl PaintCmd {
debug_assert!(triangles.is_valid()); debug_assert!(triangles.is_valid());
Self::Triangles(triangles) Self::Triangles(triangles)
} }
pub fn texture_id(&self) -> super::TextureId {
if let PaintCmd::Triangles(triangles) = self {
triangles.texture_id
} else {
super::TextureId::Egui
}
}
} }
#[derive(Clone, Copy, Debug, Default, PartialEq)] #[derive(Clone, Copy, Debug, Default, PartialEq)]

View file

@ -104,23 +104,23 @@ impl Triangles {
} }
/// Append all the indices and vertices of `other` to `self`. /// Append all the indices and vertices of `other` to `self`.
pub fn append(&mut self, other: &Triangles) { pub fn append(&mut self, other: Triangles) {
debug_assert!(other.is_valid()); debug_assert!(other.is_valid());
if self.is_empty() { if self.is_empty() {
self.texture_id = other.texture_id; *self = other;
} else { } else {
assert_eq!( assert_eq!(
self.texture_id, other.texture_id, self.texture_id, other.texture_id,
"Can't merge Triangles using different textures" "Can't merge Triangles using different textures"
); );
}
let index_offset = self.vertices.len() as u32; let index_offset = self.vertices.len() as u32;
for index in &other.indices { for index in &other.indices {
self.indices.push(index_offset + index); self.indices.push(index_offset + index);
}
self.vertices.extend(other.vertices.iter());
} }
self.vertices.extend(other.vertices.iter());
} }
pub fn colored_vertex(&mut self, pos: Pos2, color: Srgba) { pub fn colored_vertex(&mut self, pos: Pos2, color: Srgba) {
@ -695,7 +695,7 @@ fn tessellate_paint_command(
} }
PaintCmd::Triangles(triangles) => { PaintCmd::Triangles(triangles) => {
if triangles.is_valid() { if triangles.is_valid() {
out.append(&triangles); out.append(triangles);
} else { } else {
debug_assert!(false, "Ivalid Triangles in PaintCmd::Traingles"); debug_assert!(false, "Ivalid Triangles in PaintCmd::Traingles");
} }
@ -834,22 +834,12 @@ pub fn tessellate_paint_commands(
let mut jobs = PaintJobs::default(); let mut jobs = PaintJobs::default();
for (clip_rect, cmd) in commands { for (clip_rect, cmd) in commands {
// TODO: cull(clip_rect, cmd) let start_new_job = match jobs.last() {
None => true,
Some(job) => job.0 != clip_rect || job.1.texture_id != cmd.texture_id(),
};
if let PaintCmd::Triangles(triangles) = cmd { if start_new_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;
}
if jobs.is_empty()
|| jobs.last().unwrap().0 != clip_rect
|| jobs.last().unwrap().1.texture_id != TextureId::Egui
{
jobs.push((clip_rect, Triangles::default())); jobs.push((clip_rect, Triangles::default()));
} }