Skip to content

Commit

Permalink
Update transforms of modified parts and fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlansneff committed Oct 4, 2020
1 parent 919ab37 commit 9ca98a2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 13 deletions.
15 changes: 8 additions & 7 deletions crates/periodic-table/src/lib.rs
Expand Up @@ -146,10 +146,13 @@ pub struct PeriodicTable {

impl PeriodicTable {
pub fn new() -> Self {
let mut element_reprs = vec![ElementRepr {
color: Vec3::new(0.0, 0.0, 0.0), // Black
radius: 1.0,
}; 118];
let mut element_reprs = vec![
ElementRepr {
color: Vec3::new(0.0, 0.0, 0.0), // Black
radius: 1.0,
};
118
];

element_reprs[Element::Hydrogen as usize - 1] = ElementRepr {
color: Vec3::new(1.0, 1.0, 1.0), // white
Expand Down Expand Up @@ -184,9 +187,7 @@ impl PeriodicTable {
repr.radius *= 0.85;
}

Self {
element_reprs,
}
Self { element_reprs }
}
}

Expand Down
19 changes: 17 additions & 2 deletions crates/render/src/buffer_vec.rs
Expand Up @@ -151,6 +151,21 @@ impl BufferVec {
// #[must_use = "user must be aware if the vector re-allocated or not"]
// pub fn push_large -> BufferVecOp

// #[must_use = "user must be aware if the vector re-allocated or not"]
// pub fn write_partial(&mut self, gpu_resources: &GlobalGpuResources, encoder: &mut wgpu::CommandEncoder) -> BufferVecOp
pub fn write_partial_small(
&mut self,
gpu_resources: &GlobalGpuResources,
offset: u64,
data: &[u8],
) {
let inner = self.inner.as_ref().expect(
"you must have already instantiated this buffer vec to call `write_partial_small`",
);
if offset + (data.len() as u64) <= inner.len {
gpu_resources
.queue
.write_buffer(&inner.buffer, offset, data);
} else {
panic!("attempting to partially write beyond buffer bounds")
}
}
}
42 changes: 40 additions & 2 deletions crates/render/src/lib.rs
Expand Up @@ -8,7 +8,7 @@ use crate::{
buffer_vec::BufferVec,
};
use common::AsBytes as _;
use periodic_table::{PeriodicTable, Element};
use periodic_table::{Element, PeriodicTable};
use std::{
collections::{HashMap, HashSet},
convert::TryInto as _,
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Renderer {
}

self.upload_new_transforms(&mut encoder, world);
// self.update_transforms(&mut encoder, world);
self.update_transforms(&mut encoder, world);

let frame = self
.swap_chain
Expand Down Expand Up @@ -356,13 +356,51 @@ impl Renderer {
.collect();

// This doesn't use a bind group.
// Eventually switch this to `push_large`, once it's written.
let _ = self.fragment_transforms.push_small(
&self.gpu_resources,
encoder,
transforms[..].as_bytes(),
);
}

fn update_transforms(&mut self, _encoder: &mut wgpu::CommandEncoder, world: &mut World) {
if world.modified_parts.len() + world.modified_fragments.len() == 0 {
return;
}

let (parts, fragments) = (&world.parts, &world.fragments);

let modified_fragments = world.modified_fragments.drain(..).chain(
world
.modified_parts
.drain(..)
.map(|part_id| parts[&part_id].fragments().iter().copied())
.flatten(),
);

for fragment_id in modified_fragments {
let (part_id, buffer_offset) = self.per_fragment[&fragment_id];

let part = &parts[&part_id];
let fragment = &fragments[&fragment_id];

let offset = part.offset() + fragment.offset();
let rotation = part.rotation() * fragment.rotation();

let transform = rotation
.into_matrix()
.into_homogeneous()
.translated(&offset);

self.fragment_transforms.write_partial_small(
&self.gpu_resources,
buffer_offset,
transform.as_bytes(),
);
}
}

/// TODO: Re-upload any transforms that have changed
// fn update_transforms(&mut self, encoder: &mut wgpu::CommandEncoder, world: &mut World) {
// if world.added_fragments.len() + world.added_parts.len() == 0
Expand Down
8 changes: 7 additions & 1 deletion crates/render/src/world.rs
Expand Up @@ -196,7 +196,7 @@ pub struct World {
pub(crate) added_parts: Vec<PartId>,
pub(crate) added_fragments: Vec<(PartId, FragmentId)>,
pub(crate) modified_parts: Vec<PartId>,
pub(crate) modified_fragments: Vec<(PartId, FragmentId)>,
pub(crate) modified_fragments: Vec<FragmentId>,
}

impl World {
Expand Down Expand Up @@ -240,6 +240,12 @@ impl World {
self.modified_fragments.extend(other.modified_fragments);
}

pub fn part_mut(&mut self, id: PartId) -> &mut Part {
let part = &mut self.parts[&id];
self.modified_parts.push(id);
part
}

pub fn parts(&self) -> impl ExactSizeIterator<Item = &Part> {
self.parts.values()
}
Expand Down
2 changes: 1 addition & 1 deletion src/camera.rs
Expand Up @@ -72,7 +72,7 @@ impl Camera for ArcballCamera {
true
}
WindowEvent::MouseInput { state, button, .. } => {
if button == MouseButton::Left {
if button == MouseButton::Right {
if state == ElementState::Pressed {
self.mouse_button_pressed = true;
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/main.rs
Expand Up @@ -34,6 +34,8 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
// part.move_to(ultraviolet::Vec3::new(0.0, 0.0, 10.0));
// }

let some_part = loaded_pdb.parts().next().unwrap().id();

world.merge(loaded_pdb);

let interations = Interactions::default();
Expand All @@ -55,6 +57,20 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
// test to show that modifying parts is working correctly.
Event::WindowEvent {
event:
winit::event::WindowEvent::MouseInput {
state: winit::event::ElementState::Pressed,
button: winit::event::MouseButton::Left,
..
},
..
} => {
world
.part_mut(some_part)
.offset_by(ultraviolet::Vec3::new(0.0, 0.0, 2.0));
}
Event::WindowEvent { event, .. } => {
renderer.camera().update(InputEvent::Window(event));
}
Expand Down

0 comments on commit 9ca98a2

Please sign in to comment.