From e7bda0997835a7162a863914f9a9ca7b43fe99a4 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 22 Nov 2019 14:06:44 -0800 Subject: [PATCH] simple perf boosts: avoiding some unnecessary GPU uploads every frame --- ezgui/Cargo.toml | 1 + ezgui/src/drawing.rs | 3 +++ ezgui/src/widgets/slider.rs | 27 +++++++++++--------- game/src/common/info.rs | 6 +---- game/src/sandbox/gameplay/spawner.rs | 37 ++++++++++++++++++++++------ 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/ezgui/Cargo.toml b/ezgui/Cargo.toml index 81898051b6..d3c5fd8583 100644 --- a/ezgui/Cargo.toml +++ b/ezgui/Cargo.toml @@ -10,6 +10,7 @@ profiler = ["cpuprofiler"] [dependencies] abstutil = { path = "../abstutil" } +# backtrace = "0.3.40" cpuprofiler = { version = "0.0.3", optional = true } geom = { path = "../geom" } glium = "0.25.1" diff --git a/ezgui/src/drawing.rs b/ezgui/src/drawing.rs index 8d49d48cac..538a6355b4 100644 --- a/ezgui/src/drawing.rs +++ b/ezgui/src/drawing.rs @@ -417,6 +417,9 @@ impl<'a> Prerender<'a> { } fn actually_upload(&self, permanent: bool, list: Vec<(Color, &Polygon)>) -> Drawable { + //let bt = format!("{:?}", backtrace::Backtrace::new()); + //println!("{}", bt); + self.num_uploads.set(self.num_uploads.get() + 1); let mut vertices: Vec = Vec::new(); diff --git a/ezgui/src/widgets/slider.rs b/ezgui/src/widgets/slider.rs index a362e492af..f722cb4367 100644 --- a/ezgui/src/widgets/slider.rs +++ b/ezgui/src/widgets/slider.rs @@ -1,8 +1,8 @@ use crate::layout::{stack_vertically, ContainerOrientation, Widget}; use crate::widgets::text_box::TextBox; use crate::{ - hotkey, Canvas, Color, EventCtx, EventLoopMode, GfxCtx, InputResult, Key, Line, ModalMenu, - MultiKey, ScreenDims, ScreenPt, ScreenRectangle, Text, Warper, + hotkey, Canvas, Color, EventCtx, EventLoopMode, GeomBatch, GfxCtx, InputResult, Key, Line, + ModalMenu, MultiKey, ScreenDims, ScreenPt, ScreenRectangle, Text, Warper, }; use geom::{Distance, Duration, Polygon, Pt2D}; @@ -98,12 +98,13 @@ impl Slider { } pub fn draw(&self, g: &mut GfxCtx) { - g.fork_screenspace(); + // TODO Cache the batch + let mut batch = GeomBatch::new(); // A nice background for the entire thing - g.draw_polygon( + batch.push( Color::grey(0.3), - &Polygon::rectangle_topleft( + Polygon::rectangle_topleft( Pt2D::new(self.top_left.x, self.top_left.y), Distance::meters(self.dims.total_width), Distance::meters(self.dims.bar_height + 2.0 * self.dims.vert_padding), @@ -117,9 +118,9 @@ impl Slider { }); // The bar - g.draw_polygon( + batch.push( Color::WHITE, - &Polygon::rectangle_topleft( + Polygon::rectangle_topleft( Pt2D::new( self.top_left.x + self.dims.horiz_padding, self.top_left.y + self.dims.vert_padding, @@ -131,9 +132,9 @@ impl Slider { // Show the progress if self.current_percent != 0.0 { - g.draw_polygon( + batch.push( Color::GREEN, - &Polygon::rectangle_topleft( + Polygon::rectangle_topleft( Pt2D::new( self.top_left.x + self.dims.horiz_padding, self.top_left.y + self.dims.vert_padding, @@ -145,14 +146,18 @@ impl Slider { } // The actual slider - g.draw_polygon( + batch.push( if self.mouse_on_slider { Color::YELLOW } else { Color::grey(0.7) }, - &self.slider_geom(), + self.slider_geom(), ); + + g.fork_screenspace(); + batch.draw(g); + g.unfork(); } fn slider_geom(&self) -> Polygon { diff --git a/game/src/common/info.rs b/game/src/common/info.rs index 99337a8c5c..5fff071704 100644 --- a/game/src/common/info.rs +++ b/game/src/common/info.rs @@ -182,11 +182,7 @@ fn info_for(id: ID, ui: &UI, ctx: &EventCtx) -> Text { ) ))); } - // TODO No way to trigger the info panel for this yet. - ID::Turn(id) => { - let t = map.get_t(id); - txt.add(Line(format!("{:?}", t.turn_type))); - } + ID::Turn(_) => unreachable!(), ID::Building(id) => { let b = map.get_b(id); txt.add(Line(format!( diff --git a/game/src/sandbox/gameplay/spawner.rs b/game/src/sandbox/gameplay/spawner.rs index 9680351320..ed975f494a 100644 --- a/game/src/sandbox/gameplay/spawner.rs +++ b/game/src/sandbox/gameplay/spawner.rs @@ -43,7 +43,6 @@ enum Goal { impl AgentSpawner { pub fn new(ctx: &mut EventCtx, ui: &mut UI) -> Option> { - let menu = ModalMenu::new("Agent Spawner", vec![(hotkey(Key::Escape), "quit")], ctx); let map = &ui.primary.map; match ui.primary.current_selection { Some(ID::Building(id)) => { @@ -66,7 +65,11 @@ impl AgentSpawner { .contextual_action(Key::F3, "spawn a pedestrian starting here just walking") { return Some(Box::new(AgentSpawner { - menu, + menu: ModalMenu::new( + "Agent Spawner", + vec![(hotkey(Key::Escape), "quit")], + ctx, + ), from: Source::WalkFromBldg(id), maybe_goal: None, })); @@ -80,7 +83,11 @@ impl AgentSpawner { ) { return Some(Box::new(AgentSpawner { - menu, + menu: ModalMenu::new( + "Agent Spawner", + vec![(hotkey(Key::Escape), "quit")], + ctx, + ), from: Source::WalkFromBldgThenMaybeUseCar(id), maybe_goal: None, })); @@ -91,7 +98,11 @@ impl AgentSpawner { .contextual_action(Key::F4, "spawn a car starting here") { return Some(Box::new(AgentSpawner { - menu, + menu: ModalMenu::new( + "Agent Spawner", + vec![(hotkey(Key::Escape), "quit")], + ctx, + ), from: Source::Drive(pos), maybe_goal: None, })); @@ -103,7 +114,11 @@ impl AgentSpawner { .contextual_action(Key::F7, "spawn a bike starting here") { return Some(Box::new(AgentSpawner { - menu, + menu: ModalMenu::new( + "Agent Spawner", + vec![(hotkey(Key::Escape), "quit")], + ctx, + ), from: Source::BikeFromBldg(id, pos), maybe_goal: None, })); @@ -117,7 +132,11 @@ impl AgentSpawner { .contextual_action(Key::F3, "spawn a car starting here") { return Some(Box::new(AgentSpawner { - menu, + menu: ModalMenu::new( + "Agent Spawner", + vec![(hotkey(Key::Escape), "quit")], + ctx, + ), from: Source::Drive(Position::new(id, map.get_l(id).length() / 2.0)), maybe_goal: None, })); @@ -127,7 +146,11 @@ impl AgentSpawner { .contextual_action(Key::F3, "spawn a pedestrian starting here") { return Some(Box::new(AgentSpawner { - menu, + menu: ModalMenu::new( + "Agent Spawner", + vec![(hotkey(Key::Escape), "quit")], + ctx, + ), from: Source::WalkFromSidewalk(Position::new( id, map.get_l(id).length() / 2.0,