Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add support for one frame ahead WebXR in Wave #3058

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions app/src/wavevr/cpp/DeviceDelegateWaveVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <array>
#include <vector>
#include <deque>

#include <wvr/wvr.h>
#include <wvr/wvr_render.h>
Expand All @@ -34,6 +35,7 @@ namespace crow {
static const vrb::Vector kAverageHeight(0.0f, 1.7f, 0.0f);
static const int32_t kMaxControllerCount = 2;
static const int32_t kRecenterDelay = 72;
static const int32_t kMaxTimeSamples = 10;

struct DeviceDelegateWaveVR::State {
struct Controller {
Expand Down Expand Up @@ -84,6 +86,11 @@ struct DeviceDelegateWaveVR::State {
uint32_t renderHeight;

WVR_DevicePosePair_t devicePairs[WVR_DEVICE_COUNT_LEVEL_1];
WVR_PoseState_t predictedPose;
DeviceDelegate::FramePrediction framePrediction;
std::deque<double> timeSamples;
double currentStartTime = 0;
double prevStartTime = 0;
ElbowModelPtr elbow;
ControllerDelegatePtr delegate;
GestureDelegatePtr gestures;
Expand All @@ -95,6 +102,7 @@ struct DeviceDelegateWaveVR::State {
bool ignoreNextRecenter;
int32_t sixDoFControllerCount;
bool handsCalculated;
uint32_t delta = 0;
State()
: isRunning(true)
, near(0.1f)
Expand Down Expand Up @@ -223,6 +231,31 @@ struct DeviceDelegateWaveVR::State {
ReleaseTextureQueues();
}

void AddTimeSample(double sample) {
timeSamples.push_back(sample);
if (timeSamples.size() > kMaxTimeSamples) {
timeSamples.pop_front();
}
}

double GetTimestamp() {
vrb::RenderContextPtr render = context.lock();
return render->GetTimestamp();
}

uint32_t GetFrameAheadPredictedTimeMs() {
if (timeSamples.size() == 0) {
vrb::RenderContextPtr render = context.lock();
return (uint32_t) (render->GetFrameDelta() * 1000.0f);
}
double t = 0;
for (double delta: timeSamples) {
t += delta;
}
uint32_t predictedFrameTime = (uint32_t) (1000.0f * t / timeSamples.size());
return predictedFrameTime;
}

void CreateController(Controller& aController) {
if (!delegate) {
VRB_ERROR("Failed to create controller. No ControllerDelegate has been set.");
Expand Down Expand Up @@ -284,6 +317,19 @@ struct DeviceDelegateWaveVR::State {
const bool touchpadPressed = WVR_GetInputButtonState(controller.type, WVR_InputId_Alias1_Touchpad);
const bool touchpadTouched = WVR_GetInputTouchState(controller.type, WVR_InputId_Alias1_Touchpad);
const bool menuPressed = WVR_GetInputButtonState(controller.type, WVR_InputId_Alias1_Menu);
const bool grip = WVR_GetInputButtonState(controller.type, WVR_InputId_Alias1_Grip);
static bool sTouchpadPressed = false;
static bool sBumperPressed = false;
if (renderMode == device::RenderMode::Immersive) {
if (grip && !sTouchpadPressed) {
delta += 1;
}
if (bumperPressed && !sBumperPressed) {
delta -= 1;
}
Comment on lines +324 to +329
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this value being used anywhere.

}
sTouchpadPressed = grip;
sBumperPressed = bumperPressed;

delegate->SetButtonCount(controller.index, controller.is6DoF ? 3 : 2); // For immersive mode
delegate->SetButtonState(controller.index, ControllerDelegate::BUTTON_TOUCHPAD, 0, touchpadPressed, touchpadTouched);
Expand Down Expand Up @@ -677,6 +723,11 @@ DeviceDelegateWaveVR::ProcessEvents() {
m.UpdateControllers();
}

bool
DeviceDelegateWaveVR::SupportsFramePrediction(FramePrediction aPrediction) const {
return true;
}

static inline vrb::Vector
GetDirection(const vrb::Vector& location, const vrb::Vector& head) {
vrb::Vector result = location - head;
Expand All @@ -693,15 +744,33 @@ HandToString(ElbowModel::HandEnum hand) {
return "Left";
}

static float old = 0;

void
DeviceDelegateWaveVR::StartFrame(const FramePrediction aPrediction) {
VRB_GL_CHECK(glClearColor(m.clearColor.Red(), m.clearColor.Green(), m.clearColor.Blue(), m.clearColor.Alpha()));
if (!m.lastSubmitDiscarded) {
m.leftFBOIndex = WVR_GetAvailableTextureIndex(m.leftTextureQueue);
m.rightFBOIndex = WVR_GetAvailableTextureIndex(m.rightTextureQueue);
}

m.framePrediction = aPrediction;
if (aPrediction == FramePrediction::ONE_FRAME_AHEAD) {
m.prevStartTime = m.currentStartTime;
m.currentStartTime = m.GetTimestamp();
uint32_t predictedTime = m.GetFrameAheadPredictedTimeMs();
m.predictedPose = m.devicePairs[WVR_DEVICE_HMD].pose;
WVR_GetPoseState(WVR_DeviceType_HMD, WVR_PoseOriginModel_OriginOnHead, predictedTime, &m.devicePairs[WVR_DEVICE_HMD].pose);
m.devicePairs[WVR_DEVICE_HMD + 1].type = WVR_DeviceType_Controller_Left;
m.devicePairs[WVR_DEVICE_HMD + 2].type = WVR_DeviceType_Controller_Right;
WVR_GetPoseState(WVR_DeviceType_Controller_Left, WVR_PoseOriginModel_OriginOnTrackingObserver, predictedTime, &m.devicePairs[WVR_DEVICE_HMD + 1].pose);
WVR_GetPoseState(WVR_DeviceType_Controller_Right, WVR_PoseOriginModel_OriginOnTrackingObserver, predictedTime, &m.devicePairs[WVR_DEVICE_HMD + 2].pose);
} else {
WVR_GetSyncPose(WVR_PoseOriginModel_OriginOnHead, m.devicePairs, WVR_DEVICE_COUNT_LEVEL_1);
m.predictedPose = m.devicePairs[WVR_DEVICE_HMD].pose;
}
// Update cameras
WVR_GetSyncPose(WVR_PoseOriginModel_OriginOnHead, m.devicePairs, WVR_DEVICE_COUNT_LEVEL_1);
//
vrb::Matrix hmd = vrb::Matrix::Identity();
if (m.devicePairs[WVR_DEVICE_HMD].pose.isValidPose) {
hmd = vrb::Matrix::FromRowMajor(m.devicePairs[WVR_DEVICE_HMD].pose.poseMatrix.m);
Expand Down Expand Up @@ -821,7 +890,11 @@ DeviceDelegateWaveVR::EndFrame(const FrameEndMode aMode) {

// Right eye
WVR_TextureParams_t rightEyeTexture = WVR_GetTexture(m.rightTextureQueue, m.rightFBOIndex);
result = WVR_SubmitFrame(WVR_Eye_Right, &rightEyeTexture);
result = WVR_SubmitFrame(WVR_Eye_Right, &rightEyeTexture, &m.predictedPose);
if (m.framePrediction == DeviceDelegate::FramePrediction::ONE_FRAME_AHEAD
&& m.prevStartTime > 0) {
m.AddTimeSample(m.GetTimestamp() - m.prevStartTime);
}
if (result != WVR_SubmitError_None) {
VRB_ERROR("Failed to submit right eye frame");
}
Expand Down
1 change: 1 addition & 0 deletions app/src/wavevr/cpp/DeviceDelegateWaveVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DeviceDelegateWaveVR : public DeviceDelegate {
int32_t GetControllerModelCount() const override;
const std::string GetControllerModelName(const int32_t aModelIndex) const override;
void ProcessEvents() override;
bool SupportsFramePrediction(FramePrediction aPrediction) const override;
void StartFrame(const FramePrediction aPrediction) override;
void BindEye(const device::Eye aWhich) override;
void EndFrame(const FrameEndMode aMode) override;
Expand Down