Skip to content

Commit

Permalink
Fix synced updating action states more than necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 17, 2023
1 parent 0926bf6 commit fa5bd68
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
13 changes: 5 additions & 8 deletions src/mods/VR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,17 +1188,14 @@ void VR::on_pre_engine_tick(sdk::UGameEngine* engine, float delta) {
return;
}

static bool once = true;

if (once) {
spdlog::info("VR: Pre-engine tick");
once = false;
}
SPDLOG_INFO_ONCE("VR: Pre-engine tick");

m_render_target_pool_hook->on_pre_engine_tick(engine, delta);

//update_hmd_state();
update_action_states();
// Dont update action states on AFR frames
if (m_fake_stereo_hook != nullptr && !m_fake_stereo_hook->is_ignoring_next_viewport_draw()) {
update_action_states();
}
}

void VR::on_pre_viewport_client_draw(void* viewport_client, void* viewport, void* canvas){
Expand Down
4 changes: 4 additions & 0 deletions src/mods/vr/FFakeStereoRenderingHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ class FFakeStereoRenderingHook : public ModComponent {
return m_in_viewport_client_draw;
}

bool is_ignoring_next_viewport_draw() const {
return m_ignore_next_viewport_draw;
}

// Do not call these directly
static void setup_viewpoint(ISceneViewExtension* extension, void* player_controller, void* view_info);
static void localplayer_setup_viewpoint(void* localplayer, void* view_info, void* pass);
Expand Down
62 changes: 28 additions & 34 deletions src/mods/vr/runtimes/OpenXR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,12 +1179,17 @@ std::string OpenXR::translate_openvr_action_name(std::string action_name) const
return action_name;
}

Vector2f OpenXR::get_left_stick_axis() const {
Vector2f OpenXR::get_stick_axis(VRRuntime::Hand hand_idx) const {
if (hand_idx != VRRuntime::Hand::LEFT && hand_idx != VRRuntime::Hand::RIGHT) {
return Vector2f{};
}

if (!this->action_set.action_map.contains("joystick")) {
return Vector2f{};
}

const auto& hand = this->hands[VRRuntime::Hand::LEFT];
const auto& hand = this->hands[hand_idx];

auto profile_it = hand.profiles.find(this->get_current_interaction_profile());

if (profile_it == hand.profiles.end()) {
Expand All @@ -1193,45 +1198,26 @@ Vector2f OpenXR::get_left_stick_axis() const {

const auto& hand_profile = profile_it->second;

auto joystick_action = this->action_set.action_map.find("joystick")->second;
auto touchpad_action = this->action_set.action_map.find("touchpad")->second;

auto action = hand_profile.path_map.contains("joystick") ? joystick_action : touchpad_action;

XrActionStateGetInfo get_info{XR_TYPE_ACTION_STATE_GET_INFO};
get_info.action = action;
get_info.subactionPath = hand.path;

XrActionStateVector2f axis{XR_TYPE_ACTION_STATE_VECTOR2F};
auto result = xrGetActionStateVector2f(this->session, &get_info, &axis);
XrAction action{XR_NULL_HANDLE};

if (result != XR_SUCCESS) {
spdlog::error("[VR] Failed to get stick action state: {}", this->get_result_string(result));
return Vector2f{};
}
if (hand_profile.path_map.contains("joystick")) {
auto it = this->action_set.action_map.find("joystick");

return *(Vector2f*)&axis.currentState;
}
if (it == this->action_set.action_map.end()) {
return Vector2f{};
}

Vector2f OpenXR::get_right_stick_axis() const {
if (!this->action_set.action_map.contains("joystick")) {
return Vector2f{};
}
action = it->second;
} else {
auto it = this->action_set.action_map.find("touchpad");

const auto& hand = this->hands[VRRuntime::Hand::RIGHT];
auto profile_it = hand.profiles.find(this->get_current_interaction_profile());
if (it == this->action_set.action_map.end()) {
return Vector2f{};
}

if (profile_it == hand.profiles.end()) {
return Vector2f{};
action = it->second;
}

const auto& hand_profile = profile_it->second;

auto joystick_action = this->action_set.action_map.find("joystick")->second;
auto touchpad_action = this->action_set.action_map.find("touchpad")->second;

auto action = hand_profile.path_map.contains("joystick") ? joystick_action : touchpad_action;

XrActionStateGetInfo get_info{XR_TYPE_ACTION_STATE_GET_INFO};
get_info.action = action;
get_info.subactionPath = hand.path;
Expand All @@ -1247,6 +1233,14 @@ Vector2f OpenXR::get_right_stick_axis() const {
return *(Vector2f*)&axis.currentState;
}

Vector2f OpenXR::get_left_stick_axis() const {
return this->get_stick_axis(VRRuntime::Hand::LEFT);
}

Vector2f OpenXR::get_right_stick_axis() const {
return this->get_stick_axis(VRRuntime::Hand::RIGHT);
}

void OpenXR::trigger_haptic_vibration(float duration, float frequency, float amplitude, VRRuntime::Hand source) const {
if (!this->action_set.action_map.contains("haptic")) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/mods/vr/runtimes/OpenXR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct OpenXR final : public VRRuntime {
Vector2f get_action_axis(XrAction action, VRRuntime::Hand hand) const;
std::string translate_openvr_action_name(std::string action_name) const;

Vector2f get_stick_axis(VRRuntime::Hand hand) const;
Vector2f get_left_stick_axis() const;
Vector2f get_right_stick_axis() const;

Expand Down

0 comments on commit fa5bd68

Please sign in to comment.