Skip to content

Commit

Permalink
Fix linux segfault (#2090)
Browse files Browse the repository at this point in the history
* fix too long message in messagebox

* fix segfault due to concurrency

---------

Co-authored-by: Nikita Morozov <mornickmyl@gmail.com>
  • Loading branch information
SniperJoe and Nikita Morozov authored May 1, 2024
1 parent f6a2f83 commit de235ea
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ node_modules
.flatpak*

ALVR-Launcher

#allows adding CMake files for some IDEs to parse C++ files correctly
CMakeLists.txt
cmake-build-debug/
14 changes: 10 additions & 4 deletions alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Logger.h"
#include "bindings.h"
#include <mutex>
#include <memory>

#ifndef __APPLE__
// Workaround symbol clash in openvr.h / openvr_driver.h
Expand Down Expand Up @@ -84,14 +85,19 @@ void _SetChaperoneArea(float areaWidth, float areaHeight) {
}

#ifdef __linux__
vr::HmdMatrix34_t GetInvZeroPose() {
vr::HmdMatrix34_t mat;
std::unique_ptr<vr::HmdMatrix34_t> GetInvZeroPose() {
std::unique_lock<std::mutex> lock(chaperone_mutex);
if (!isOpenvrInit)
{
return nullptr;
}
std::unique_ptr<vr::HmdMatrix34_t> mat;
// revert pulls live into working copy
vr::VRChaperoneSetup()->RevertWorkingCopy();
if (vr::VRCompositor()->GetTrackingSpace() == vr::TrackingUniverseStanding) {
vr::VRChaperoneSetup()->GetWorkingStandingZeroPoseToRawTrackingPose(&mat);
vr::VRChaperoneSetup()->GetWorkingStandingZeroPoseToRawTrackingPose(mat.get());
} else {
vr::VRChaperoneSetup()->GetWorkingSeatedZeroPoseToRawTrackingPose(&mat);
vr::VRChaperoneSetup()->GetWorkingSeatedZeroPoseToRawTrackingPose(mat.get());
}
return mat;
}
Expand Down
18 changes: 13 additions & 5 deletions alvr/server/cpp/alvr_server/alvr_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@

#ifdef __linux__
#include "include/openvr_math.h"
vr::HmdMatrix34_t GetInvZeroPose();
std::unique_ptr<vr::HmdMatrix34_t> GetInvZeroPose();

vr::HmdMatrix34_t GetRawZeroPose() {
return vrmath::matInv33(GetInvZeroPose());
std::unique_ptr<vr::HmdMatrix34_t> GetRawZeroPose() {
auto invZeroPose = GetInvZeroPose();
if (invZeroPose == nullptr)
{
return nullptr;
}
return vrmath::matInv33(std::move(invZeroPose));
}

bool IsOpenvrClientReady();
Expand Down Expand Up @@ -227,8 +232,11 @@ class DriverProvider : public vr::IServerTrackedDeviceProvider {
|| event.eventType == vr::VREvent_StandingZeroPoseReset
|| event.eventType == vr::VREvent_SceneApplicationChanged
|| event.eventType == VendorEvent_ALVRDriverResync) {
if (hmd && hmd->m_poseHistory && IsOpenvrClientReady()) {
hmd->m_poseHistory->SetTransform(GetRawZeroPose());
if (hmd && hmd->m_poseHistory) {
auto rawZeroPose = GetRawZeroPose();
if (rawZeroPose != nullptr) {
hmd->m_poseHistory->SetTransform(*rawZeroPose);
}
}
}
#endif
Expand Down
58 changes: 29 additions & 29 deletions alvr/server/cpp/alvr_server/include/openvr_math.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <cmath>

#include <memory>

inline vr::HmdQuaternion_t operator+(const vr::HmdQuaternion_t& lhs, const vr::HmdQuaternion_t& rhs) {
return {
Expand Down Expand Up @@ -298,42 +298,42 @@ namespace vrmath {
return result;
}

inline vr::HmdMatrix34_t matInv33(const vr::HmdMatrix34_t &matrix) {
vr::HmdMatrix34_t result;
float cofac00 = matrix.m[1][1] * matrix.m[2][2] - matrix.m[1][2] * matrix.m[2][1];
float cofac10 = matrix.m[1][2] * matrix.m[2][0] - matrix.m[1][0] * matrix.m[2][2];
float cofac20 = matrix.m[1][0] * matrix.m[2][1] - matrix.m[1][1] * matrix.m[2][0];
inline std::unique_ptr<vr::HmdMatrix34_t> matInv33(std::unique_ptr<vr::HmdMatrix34_t> matrix) {
std::unique_ptr<vr::HmdMatrix34_t> result;
float cofac00 = matrix->m[1][1] * matrix->m[2][2] - matrix->m[1][2] * matrix->m[2][1];
float cofac10 = matrix->m[1][2] * matrix->m[2][0] - matrix->m[1][0] * matrix->m[2][2];
float cofac20 = matrix->m[1][0] * matrix->m[2][1] - matrix->m[1][1] * matrix->m[2][0];

float det = matrix.m[0][0] * cofac00 + matrix.m[0][1] * cofac10 + matrix.m[0][2] * cofac20;
float det = matrix->m[0][0] * cofac00 + matrix->m[0][1] * cofac10 + matrix->m[0][2] * cofac20;

if (det == 0) {
vr::HmdMatrix34_t result = { { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 } } };
return result;
return std::make_unique<vr::HmdMatrix34_t>(result);
}

float invDet = 1.0f / det;

float cofac01 = matrix.m[0][2] * matrix.m[2][1] - matrix.m[0][1] * matrix.m[2][2];
float cofac02 = matrix.m[0][1] * matrix.m[1][2] - matrix.m[0][2] * matrix.m[1][1];
float cofac11 = matrix.m[0][0] * matrix.m[2][2] - matrix.m[0][2] * matrix.m[2][0];
float cofac12 = matrix.m[0][2] * matrix.m[1][0] - matrix.m[0][0] * matrix.m[1][2];
float cofac21 = matrix.m[0][1] * matrix.m[2][0] - matrix.m[0][0] * matrix.m[2][1];
float cofac22 = matrix.m[0][0] * matrix.m[1][1] - matrix.m[0][1] * matrix.m[1][0];

result.m[0][0] = invDet * cofac00;
result.m[0][1] = invDet * cofac01;
result.m[0][2] = invDet * cofac02;
result.m[0][3] = 0.0f;

result.m[1][0] = invDet * cofac10;
result.m[1][1] = invDet * cofac11;
result.m[1][2] = invDet * cofac12;
result.m[1][3] = 0.0f;

result.m[2][0] = invDet * cofac20;
result.m[2][1] = invDet * cofac21;
result.m[2][2] = invDet * cofac22;
result.m[2][3] = 0.0f;
float cofac01 = matrix->m[0][2] * matrix->m[2][1] - matrix->m[0][1] * matrix->m[2][2];
float cofac02 = matrix->m[0][1] * matrix->m[1][2] - matrix->m[0][2] * matrix->m[1][1];
float cofac11 = matrix->m[0][0] * matrix->m[2][2] - matrix->m[0][2] * matrix->m[2][0];
float cofac12 = matrix->m[0][2] * matrix->m[1][0] - matrix->m[0][0] * matrix->m[1][2];
float cofac21 = matrix->m[0][1] * matrix->m[2][0] - matrix->m[0][0] * matrix->m[2][1];
float cofac22 = matrix->m[0][0] * matrix->m[1][1] - matrix->m[0][1] * matrix->m[1][0];

result->m[0][0] = invDet * cofac00;
result->m[0][1] = invDet * cofac01;
result->m[0][2] = invDet * cofac02;
result->m[0][3] = 0.0f;

result->m[1][0] = invDet * cofac10;
result->m[1][1] = invDet * cofac11;
result->m[1][2] = invDet * cofac12;
result->m[1][3] = 0.0f;

result->m[2][0] = invDet * cofac20;
result->m[2][1] = invDet * cofac21;
result->m[2][2] = invDet * cofac22;
result->m[2][3] = 0.0f;

return result;
}
Expand Down

0 comments on commit de235ea

Please sign in to comment.