Skip to content

Commit

Permalink
Optimizations in monet #14
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Nov 20, 2016
1 parent 32f749b commit a9ddee9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
1 change: 1 addition & 0 deletions lib/monet/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ authors = ["Anselm Eickhoff <anselm.eickhoff@gmail.com>"]
glium = "*"
glium_text = "*"
clippy = "*"
fnv = "1.0.3"

[dependencies.kay]
path = "../kay"
Expand Down
41 changes: 21 additions & 20 deletions lib/monet/src/lib.rs
Expand Up @@ -9,23 +9,24 @@ extern crate kay;
#[macro_use]
extern crate kay_macros;
extern crate glium_text;
extern crate fnv;

pub use ::descartes::{N, P3, P2, V3, V4, M4, Iso3, Persp3, ToHomogeneous, Norm, Into2d, Into3d, WithUniqueOrthogonal, Inverse, Rotate};
use ::kay::{ID, Recipient, CVec, ActorSystem, Individual, Fate};
use std::collections::HashMap;
use fnv::FnvHashMap;

use glium::{index, Surface};
pub use glium::backend::glutin_backend::GlutinFacade;

pub struct Renderer {
pub scenes: HashMap<usize, Scene>,
pub scenes: Vec<Scene>,
pub render_context: RenderContext
}

impl Renderer {
pub fn new (window: GlutinFacade) -> Renderer {
Renderer {
scenes: HashMap::new(),
scenes: Vec::new(),
render_context: RenderContext::new(window)
}
}
Expand All @@ -45,28 +46,28 @@ pub struct RenderToScene {pub renderer_id: ID, pub scene_id: usize}
impl Recipient<Control> for Renderer {
fn receive(&mut self, msg: &Control) -> Fate {match *msg {
Control::Setup => {
for (scene_id, scene) in &self.scenes {
for (scene_id, scene) in self.scenes.iter().enumerate() {
for renderable in &scene.renderables {
*renderable << SetupInScene{renderer_id: Self::id(), scene_id: *scene_id};
*renderable << SetupInScene{renderer_id: Self::id(), scene_id: scene_id};
}
}
Fate::Live
},

Control::Render => {
for (scene_id, mut scene) in &mut self.scenes {
for (scene_id, mut scene) in self.scenes.iter_mut().enumerate() {
for batch in (&mut scene).batches.values_mut() {
batch.instances.clear();
}
for renderable in &scene.renderables {
*renderable << RenderToScene{renderer_id: Self::id(), scene_id: *scene_id};
*renderable << RenderToScene{renderer_id: Self::id(), scene_id: scene_id};
}
}
Fate::Live
}

Control::Submit => {
for scene in self.scenes.values() {
for scene in &self.scenes {
self.render_context.submit(scene);
}
Fate::Live
Expand All @@ -80,7 +81,7 @@ pub struct MoveEye {pub scene_id: usize, pub delta: V3}
impl Recipient<MoveEye> for Renderer {
fn receive(&mut self, msg: &MoveEye) -> Fate {match *msg{
MoveEye{scene_id, delta} => {
let eye = &mut self.scenes.get_mut(&scene_id).unwrap().eye;
let eye = &mut self.scenes[scene_id].eye;
let eye_direction_2d = (eye.target - eye.position).into_2d().normalize();
let absolute_delta = delta.x * eye_direction_2d.into_3d()
+ delta.y * eye_direction_2d.orthogonal().into_3d()
Expand All @@ -93,36 +94,36 @@ impl Recipient<MoveEye> for Renderer {
}

#[derive(Compact, Clone)]
pub struct AddBatch {pub scene_id: usize, pub batch_id: usize, pub thing: Thing}
pub struct AddBatch {pub scene_id: usize, pub batch_id: u16, pub thing: Thing}

impl Recipient<AddBatch> for Renderer {
fn receive(&mut self, msg: &AddBatch) -> Fate {match *msg {
AddBatch{scene_id, batch_id, ref thing} => {
self.scenes.get_mut(&scene_id).unwrap().batches.insert(batch_id, Batch::new(thing.clone(), Vec::new()));
self.scenes[scene_id].batches.insert(batch_id, Batch::new(thing.clone(), Vec::new()));
Fate::Live
}
}}
}

#[derive(Compact, Clone)]
pub struct UpdateThing {pub scene_id: usize, pub thing_id: usize, pub thing: Thing, pub instance: Instance}
pub struct UpdateThing {pub scene_id: usize, pub thing_id: u16, pub thing: Thing, pub instance: Instance}

impl Recipient<UpdateThing> for Renderer {
fn receive(&mut self, msg: &UpdateThing) -> Fate {match *msg {
UpdateThing{scene_id, thing_id, ref thing, instance} => {
self.scenes.get_mut(&scene_id).unwrap().things.insert(thing_id, (thing.clone(), instance));
self.scenes[scene_id].things.insert(thing_id, (thing.clone(), instance));
Fate::Live
}
}}
}

#[derive(Copy, Clone)]
pub struct AddInstance {pub scene_id: usize, pub batch_id: usize, pub position: Instance}
pub struct AddInstance {pub scene_id: usize, pub batch_id: u16, pub position: Instance}

impl Recipient<AddInstance> for Renderer {
fn receive(&mut self, msg: &AddInstance) -> Fate {match *msg {
AddInstance{scene_id, batch_id, position} => {
self.scenes.get_mut(&scene_id).unwrap().batches.get_mut(&batch_id).unwrap().instances.push(position);
self.scenes[scene_id].batches.get_mut(&batch_id).unwrap().instances.push(position);
Fate::Live
}
}}
Expand All @@ -137,7 +138,7 @@ pub struct Projected3d{pub position_3d: P3}
impl Recipient<Project2dTo3d> for Renderer {
fn receive(&mut self, msg: &Project2dTo3d) -> Fate {match *msg{
Project2dTo3d{scene_id, position_2d, requester} => {
let eye = &self.scenes.get_mut(&scene_id).unwrap().eye;
let eye = &self.scenes[scene_id].eye;
let frame_size = self.render_context.window.get_framebuffer_dimensions();

// mouse is on the close plane of the frustum
Expand Down Expand Up @@ -192,8 +193,8 @@ pub fn setup(system: &mut ActorSystem, renderer: Renderer) {

pub struct Scene {
pub eye: Eye,
pub batches: HashMap<usize, Batch>,
pub things: HashMap<usize, (Thing, Instance)>,
pub batches: FnvHashMap<u16, Batch>,
pub things: FnvHashMap<u16, (Thing, Instance)>,
pub renderables: Vec<ID>,
pub debug_text: String
}
Expand All @@ -207,8 +208,8 @@ impl Scene {
up: V3::new(0.0, 0.0, 1.0),
field_of_view: 0.3 * ::std::f32::consts::PI
},
batches: HashMap::new(),
things: HashMap::new(),
batches: FnvHashMap::default(),
things: FnvHashMap::default(),
renderables: Vec::new(),
debug_text: String::new()
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry.rs
Expand Up @@ -112,12 +112,12 @@ pub fn band_to_thing<P: Path>(band: &Band<P>, z: N) -> Thing {
Thing::new(vertices, indices)
}

static mut LAST_DEBUG_THING: usize = 0;
static mut LAST_DEBUG_THING: u16 = 0;

pub fn add_debug_path(path: CPath, color: [f32; 3], z: f32) {
Renderer::id() << UpdateThing{
scene_id: 0,
thing_id: 3498539847 + unsafe{LAST_DEBUG_THING},
thing_id: 4000 + unsafe{LAST_DEBUG_THING},
thing: band_to_thing(&Band::new(path, 0.2), z),
instance: Instance::with_color(color)
};
Expand All @@ -127,7 +127,7 @@ pub fn add_debug_path(path: CPath, color: [f32; 3], z: f32) {
pub fn add_debug_point(point: P2, color: [f32; 3], z: f32) {
Renderer::id() << UpdateThing{
scene_id: 0,
thing_id: 3498539847 + unsafe{LAST_DEBUG_THING},
thing_id: 4000 + unsafe{LAST_DEBUG_THING},
thing: Thing::new(
vec![
Vertex{position: [point.x + -0.5, point.y + -0.5, z]},
Expand Down
8 changes: 4 additions & 4 deletions src/game/lanes_and_cars/lane_rendering.rs
Expand Up @@ -17,7 +17,7 @@ impl RecipientAsSwarm<SetupInScene> for Lane {
SetupInScene{renderer_id, scene_id} => {
renderer_id << AddBatch{scene_id: scene_id, batch_id: 0, thing: car::create()};

renderer_id << AddBatch{scene_id: scene_id, batch_id: 222666222662, thing: Thing::new(
renderer_id << AddBatch{scene_id: scene_id, batch_id: 1333, thing: Thing::new(
vec![
Vertex{position: [-1.0, -1.0, 0.0]},
Vertex{position: [1.0, -1.0, 0.0]},
Expand Down Expand Up @@ -92,15 +92,15 @@ impl Recipient<RenderToScene> for Lane {
}

if ! self.interactions.iter().any(|inter| match inter.kind {InteractionKind::Next{..} => true, _ => false}) {
renderer_id << AddInstance{scene_id: scene_id, batch_id: 222666222662, position: Instance{
renderer_id << AddInstance{scene_id: scene_id, batch_id: 1333, position: Instance{
instance_position: [self.path.end().x, self.path.end().y, 0.0],
instance_direction: [1.0, 0.0],
instance_color: [1.0, 0.0, 0.0]
}};
}

if ! self.interactions.iter().any(|inter| match inter.kind {InteractionKind::Previous{..} => true, _ => false}) {
renderer_id << AddInstance{scene_id: scene_id, batch_id: 222666222662, position: Instance{
renderer_id << AddInstance{scene_id: scene_id, batch_id: 1333, position: Instance{
instance_position: [self.path.start().x, self.path.start().y, 0.0],
instance_direction: [1.0, 0.0],
instance_color: [0.0, 1.0, 0.0]
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Recipient<RenderToScene> for TransferLane {

renderer_id << UpdateThing{
scene_id: scene_id,
thing_id: 200 + self.id().instance_id as usize,
thing_id: 200 + self.id().instance_id as u16,
thing: band_to_thing(&Band::new(self.path.clone(), 3.0), 0.1),
instance: Instance::with_color([1.0, 1.0, 0.5])
};
Expand Down
4 changes: 2 additions & 2 deletions src/game/lanes_and_cars/lane_thing_collector.rs
Expand Up @@ -86,7 +86,7 @@ impl Recipient<RenderToScene> for LaneThingCollector{

renderer_id << UpdateThing{
scene_id: scene_id,
thing_id: 3498547908345,
thing_id: 2000,
thing: living_thing,
instance: Instance{
instance_position: [0.0, 0.0, -0.1],
Expand All @@ -101,7 +101,7 @@ impl Recipient<RenderToScene> for LaneThingCollector{

renderer_id << UpdateThing{
scene_id: scene_id,
thing_id: 9384598345983,
thing_id: 2001,
thing: self.cached_frozen_thing.clone(),
instance: Instance{
instance_position: [0.0, 0.0, -0.1],
Expand Down
6 changes: 3 additions & 3 deletions src/game/lanes_and_cars/planning/current_plan_rendering.rs
Expand Up @@ -26,7 +26,7 @@ impl Recipient<RenderToScene> for CurrentPlan {
.sum();
renderer_id << UpdateThing{
scene_id: scene_id,
thing_id: 3834747834,
thing_id: 500,
thing: thing,
instance: Instance::with_color([0.3, 0.3, 0.5])
};
Expand All @@ -36,7 +36,7 @@ impl Recipient<RenderToScene> for CurrentPlan {
.sum();
renderer_id << UpdateThing{
scene_id: scene_id,
thing_id: 3834747835,
thing_id: 501,
thing: intersections_thing,
instance: Instance::with_color([0.0, 0.0, 1.0])
};
Expand All @@ -46,7 +46,7 @@ impl Recipient<RenderToScene> for CurrentPlan {
.sum();
renderer_id << UpdateThing{
scene_id: scene_id,
thing_id: 3834747836,
thing_id: 502,
thing: connecting_strokes_thing,
instance: Instance::with_color([0.5, 0.5, 1.0])
};
Expand Down
Expand Up @@ -110,7 +110,7 @@ use monet::AddBatch;
impl RecipientAsSwarm<SetupInScene> for RoadStrokeNodeInteractable {
fn receive(_swarm: &mut Swarm<Self>, msg: &SetupInScene) -> Fate {match *msg{
SetupInScene{renderer_id, scene_id} => {
renderer_id << AddBatch{scene_id: scene_id, batch_id: 4982939, thing: Thing::new(
renderer_id << AddBatch{scene_id: scene_id, batch_id: 2400, thing: Thing::new(
vec![
Vertex{position: [-1.0, -1.0, 0.0]},
Vertex{position: [1.0, -1.0, 0.0]},
Expand All @@ -133,7 +133,7 @@ use monet::AddInstance;
impl Recipient<RenderToScene> for RoadStrokeNodeInteractable {
fn receive(&mut self, msg: &RenderToScene) -> Fate {match *msg {
RenderToScene{renderer_id, scene_id} => {
renderer_id << AddInstance{scene_id: scene_id, batch_id: 4982939, position: Instance{
renderer_id << AddInstance{scene_id: scene_id, batch_id: 2400, position: Instance{
instance_position: [self.position.x, self.position.y, 0.0],
instance_direction: [1.0, 0.0],
instance_color: if self.hovered {[1.0, 0.0, 0.0]} else {match self.parent {
Expand Down

0 comments on commit a9ddee9

Please sign in to comment.