Skip to content

Commit

Permalink
Sync input source data every frame if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed May 3, 2019
1 parent 5c8132c commit b693af6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 20 additions & 2 deletions components/script/dom/vrdisplay.rs
Expand Up @@ -121,6 +121,8 @@ struct VRRAFUpdate {
/// Number uniquely identifying the WebGL context
/// so that we may setup/tear down VR compositors as things change
context_id: usize,
/// Do we need input data?
needs_inputs: bool,
}

type VRRAFUpdateSender = Sender<Result<VRRAFUpdate, ()>>;
Expand Down Expand Up @@ -635,6 +637,7 @@ impl VRDisplay {
depth_far: self.depth_far.get(),
api_sender: self.api_sender(),
context_id: self.context_id(),
needs_inputs: self.initialized_inputs.get(),
}
}

Expand Down Expand Up @@ -698,6 +701,7 @@ impl VRDisplay {
let (raf_sender, raf_receiver) = unbounded();
let (wakeup_sender, wakeup_receiver) = unbounded();
*self.raf_wakeup_sender.borrow_mut() = Some(wakeup_sender);
let mut needs_inputs = false;

// The render loop at native headset frame rate is implemented using a dedicated thread.
// Every loop iteration syncs pose data with the HMD, submits the pixels to the display and waits for Vsync.
Expand Down Expand Up @@ -738,7 +742,7 @@ impl VRDisplay {
display_id,
near,
far,
false,
needs_inputs,
sync_sender.clone(),
);
api_sender.send_vr(msg).unwrap();
Expand All @@ -765,6 +769,7 @@ impl VRDisplay {
if let Ok(update) = raf_receiver.recv().unwrap() {
near = update.depth_near;
far = update.depth_far;
needs_inputs = update.needs_inputs;
if update.context_id != context_id {
if let Some(ref api_sender) = update.api_sender {
api_sender
Expand Down Expand Up @@ -823,6 +828,14 @@ impl VRDisplay {
match receiver.recv().unwrap() {
Ok(pose) => {
*self.frame_data.borrow_mut() = pose.frame.block();
if self.initialized_inputs.get() {
let inputs = self.input_sources.borrow();
for (id, state) in pose.gamepads {
if let Some(input) = inputs.get(&id) {
input.update_state(state);
}
}
}
VRFrameDataStatus::Synced
},
Err(()) => VRFrameDataStatus::Exit,
Expand Down Expand Up @@ -944,7 +957,12 @@ impl VRDisplay {
.expect("initialize_inputs called on a VR session");
let roots: Vec<_> = gamepads
.into_iter()
.map(|g| (g.1.gamepad_id, XRInputSource::new(&global, &session, g.0, g.1)))
.map(|g| {
(
g.1.gamepad_id,
XRInputSource::new(&global, &session, g.0, g.1),
)
})
.collect();

let mut inputs = self.input_sources.borrow_mut();
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/xrinputsource.rs
Expand Up @@ -47,4 +47,8 @@ impl XRInputSource {
XRInputSourceBinding::Wrap,
)
}

pub fn update_state(&self, state: WebVRGamepadState) {
*self.state.borrow_mut() = state;
}
}

0 comments on commit b693af6

Please sign in to comment.