Skip to content

Commit

Permalink
Merge pull request #8 from SlimeVR/ipc-upgrade
Browse files Browse the repository at this point in the history
IPC upgrade
  • Loading branch information
Eirenliel authored Oct 5, 2021
2 parents 119e47e + b76185b commit 21a5857
Show file tree
Hide file tree
Showing 17 changed files with 799 additions and 209 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: SlimeVR OpenVR Driver

on: [ push, pull_request ]

jobs:
build:

name: Build for ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-latest] # Others are disabled, this only targets Windows
include:
- os: windows-latest
triplet: x64-windows
env:
# Indicates the CMake build directory where project files and binaries are being produced.
CMAKE_BUILD_DIR: ${{ github.workspace }}/build
# Indicates the location of the vcpkg as a Git submodule of the project repository.
VCPKG_ROOT: ${{ github.workspace }}/vcpkg

steps:
- uses: actions/checkout@v2
with:
submodules: true

- uses: lukka/get-cmake@latest

- name: Clone vcpkg
uses: actions/checkout@v2
with:
repository: microsoft/vcpkg
path: ${{ env.VCPKG_ROOT }}
submodules: true

- name: Restore vcpkg and its artifacts
uses: actions/cache@v2
with:
# The first path is where vcpkg generates artifacts while consuming the vcpkg.json manifest file.
# The second path is the location of vcpkg (it contains the vcpkg executable and data files).
# The other paths starting with "!" are exclusions: they contain termporary files generated during the build of the installed packages.
path: |
${{ env.CMAKE_BUILD_DIR }}/vcpkg_installed/
${{ env.VCPKG_ROOT }}
!${{ env.VCPKG_ROOT }}/buildtrees
!${{ env.VCPKG_ROOT }}/packages
!${{ env.VCPKG_ROOT }}/downloads
# The key is composed in a way that it gets properly invalidated: this must happen whenever vcpkg's Git commit id changes, or the list of packages changes. In this case a cache miss must happen and a new entry with a new key with be pushed to GitHub the cache service.
# The key includes: hash of the vcpkg.json file, the hash of the vcpkg Git commit id, and the used vcpkg's triplet. The vcpkg's commit id would suffice, but computing an hash out it does not harm.
# Note: given a key, the cache content is immutable. If a cache entry has been created improperly, in order the recreate the right content the key must be changed as well, and it must be brand new (i.e. not existing already).
key: |
${{ hashFiles( 'vcpkg_manifest/vcpkg.json' ) }}-${{ hashFiles( '.git/modules/vcpkg/HEAD' )}}-${{ hashFiles( '${{ env.VCPKG_ROOT }}/.git/HEAD' )}}-${{ matrix.triplet }}-invalidate
- if: matrix.os == 'windows-latest'
name: Set up vcpkg for Windows
run: ${{ env.VCPKG_ROOT }}/bootstrap-vcpkg.bat

- if: matrix.os != 'windows-latest'
name: Set up vcpkg for Unix
run: ${{ env.VCPKG_ROOT }}/bootstrap-vcpkg.sh

- name: Install dependencies
run: ${{ env.VCPKG_ROOT }}/vcpkg install protobuf protobuf:${{ matrix.triplet }}

- name: Configure CMake
run: cmake -S "${{github.workspace}}" -B "${{env.CMAKE_BUILD_DIR}}"

- name: Build
run: cmake --build "${{env.CMAKE_BUILD_DIR}}" --config Release --target ALL_BUILD -j 6 --

- name: Upload a build artifact
uses: actions/upload-artifact@v2
with:
# Artifact name
name: slimevr-openvr-driver-${{ matrix.triplet }} # optional, default is artifact
# A file, directory or wildcard pattern that describes what to upload
# Using wildcards so that only the driver directory gets included (if you specify it, then it won't be included)
path: |
${{env.CMAKE_BUILD_DIR}}/Release/driver/*
50 changes: 47 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
cmake_minimum_required(VERSION 3.0.0)
project(SlimeVR-OpenVR-Driver VERSION 0.1.0)

# If the toolchain is already defined, do not attempt to find it
if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
# If the VCPKG_ROOT environment variable is not defined, try to automatically define it from AppData/home
if(NOT DEFINED ENV{VCPKG_ROOT})
if(WIN32)
file(READ "$ENV{LOCALAPPDATA}/vcpkg/vcpkg.path.txt" VCPKG_ROOT)
elseif(UNIX)
file(READ "$ENV{HOME}/.vcpkg/vcpkg.path.txt" VCPKG_ROOT)
endif()

set(VCPKG_PATH "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
else()
set(VCPKG_PATH "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
endif()

if(EXISTS "${VCPKG_PATH}")
message("vcpkg CMake toolchain was found at \"${VCPKG_PATH}\"")
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_PATH}")
else()
message(FATAL_ERROR "vcpkg could not be found")
endif()
endif()

project(SlimeVR-OpenVR-Driver VERSION 0.2.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(DRIVER_NAME "SlimeVR")

Expand All @@ -26,15 +50,26 @@ endif()

find_library(OPENVR_LIB openvr_api HINTS "${CMAKE_CURRENT_SOURCE_DIR}/libraries/openvr/lib/${PLATFORM_NAME}${PROCESSOR_ARCH}/" NO_DEFAULT_PATH )

# Protobuf
# Installation:
# Please refer to this readme to install protobuf in your system: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
# WARNING: CLang has an arror building protobuf messages, use MSVC 2019
INCLUDE(FindProtobuf)
find_package(protobuf CONFIG REQUIRED)
protobuf_generate_cpp(PROTO_SRC PROTO_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/src/bridge/ProtobufMessages.proto")
SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE)

# Project
file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp")
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_library("${PROJECT_NAME}" SHARED "${HEADERS}" "${SOURCES}")
add_library("${PROJECT_NAME}" SHARED "${HEADERS}" "${SOURCES}" ${PROTO_HEADER} ${PROTO_SRC})
target_include_directories("${PROJECT_NAME}" PUBLIC "${OPENVR_INCLUDE_DIR}")
target_include_directories("${PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/libraries/linalg")
target_include_directories("${PROJECT_NAME}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src/")
target_link_libraries("${PROJECT_NAME}" PUBLIC "${OPENVR_LIB}")
target_link_libraries("${PROJECT_NAME}" PUBLIC "${OPENVR_LIB}" protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 17)
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

# IDE Config
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Header Files" FILES ${HEADERS})
Expand Down Expand Up @@ -62,3 +97,12 @@ add_custom_command(
$<TARGET_FILE:${PROJECT_NAME}>
$<TARGET_FILE_DIR:${PROJECT_NAME}>/driver/${DRIVER_NAME}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/driver_${DRIVER_NAME}$<TARGET_FILE_SUFFIX:${PROJECT_NAME}>
)

# Copy libprotobuf dll to output folder
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE_DIR:${PROJECT_NAME}>/libprotobuf.dll
$<TARGET_FILE_DIR:${PROJECT_NAME}>/driver/${DRIVER_NAME}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/libprotobuf.dll
)
31 changes: 28 additions & 3 deletions src/ControllerDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "ControllerDevice.hpp"
#include <Windows.h>

SlimeVRDriver::ControllerDevice::ControllerDevice(std::string serial, ControllerDevice::Handedness handedness):
serial_(serial),
handedness_(handedness)
SlimeVRDriver::ControllerDevice::ControllerDevice(std::string serial, int deviceId, ControllerDevice::Handedness handedness):
serial_(serial), handedness_(handedness), deviceId_(deviceId)
{
}

Expand Down Expand Up @@ -186,6 +185,27 @@ vr::EVRInitError SlimeVRDriver::ControllerDevice::Activate(uint32_t unObjectId)
return vr::EVRInitError::VRInitError_None;
}

void SlimeVRDriver::ControllerDevice::PositionMessage(messages::Position &position)
{
// Setup pose for this frame
auto pose = this->last_pose_;
//send the new position and rotation from the pipe to the tracker object
if(position.has_x()) {
pose.vecPosition[0] = position.x();
pose.vecPosition[1] = position.y();
pose.vecPosition[2] = position.z();
}

pose.qRotation.w = position.qw();
pose.qRotation.x = position.qx();
pose.qRotation.y = position.qy();
pose.qRotation.z = position.qz();

// Post pose
GetDriver()->GetDriverHost()->TrackedDevicePoseUpdated(this->device_index_, pose, sizeof(vr::DriverPose_t));
this->last_pose_ = pose;
}

void SlimeVRDriver::ControllerDevice::Deactivate()
{
this->device_index_ = vr::k_unTrackedDeviceIndexInvalid;
Expand All @@ -210,3 +230,8 @@ vr::DriverPose_t SlimeVRDriver::ControllerDevice::GetPose()
{
return last_pose_;
}

int SlimeVRDriver::ControllerDevice::getDeviceId()
{
return deviceId_;
}
5 changes: 4 additions & 1 deletion src/ControllerDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace SlimeVRDriver {
ANY
};

ControllerDevice(std::string serial, Handedness handedness = Handedness::ANY);
ControllerDevice(std::string serial, int deviceId, Handedness handedness = Handedness::ANY);
~ControllerDevice() = default;

// Inherited via IVRDevice
Expand All @@ -34,6 +34,8 @@ namespace SlimeVRDriver {
virtual void* GetComponent(const char* pchComponentNameAndVersion) override;
virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) override;
virtual vr::DriverPose_t GetPose() override;
virtual int getDeviceId() override;
virtual void PositionMessage(messages::Position &position) override;

private:
vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid;
Expand All @@ -43,6 +45,7 @@ namespace SlimeVRDriver {
vr::DriverPose_t last_pose_;

bool did_vibrate_ = false;
int deviceId_;
float vibrate_anim_state_ = 0.f;

vr::VRInputComponentHandle_t haptic_component_ = 0;
Expand Down
1 change: 1 addition & 0 deletions src/DeviceType.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

enum class DeviceType {
HMD,
CONTROLLER,
Expand Down
29 changes: 28 additions & 1 deletion src/HMDDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "HMDDevice.hpp"
#include <Windows.h>

SlimeVRDriver::HMDDevice::HMDDevice(std::string serial):serial_(serial)
SlimeVRDriver::HMDDevice::HMDDevice(std::string serial, int deviceId):
serial_(serial), deviceId_(deviceId)
{
}

Expand Down Expand Up @@ -58,6 +59,27 @@ void SlimeVRDriver::HMDDevice::Update()
this->last_pose_ = pose;
}

void SlimeVRDriver::HMDDevice::PositionMessage(messages::Position &position)
{
// Setup pose for this frame
auto pose = this->last_pose_;
//send the new position and rotation from the pipe to the tracker object
if(position.has_x()) {
pose.vecPosition[0] = position.x();
pose.vecPosition[1] = position.y();
pose.vecPosition[2] = position.z();
}

pose.qRotation.w = position.qw();
pose.qRotation.x = position.qx();
pose.qRotation.y = position.qy();
pose.qRotation.z = position.qz();

// Post pose
GetDriver()->GetDriverHost()->TrackedDevicePoseUpdated(this->device_index_, pose, sizeof(vr::DriverPose_t));
this->last_pose_ = pose;
}

DeviceType SlimeVRDriver::HMDDevice::GetDeviceType()
{
return DeviceType::HMD;
Expand Down Expand Up @@ -220,4 +242,9 @@ vr::DistortionCoordinates_t SlimeVRDriver::HMDDevice::ComputeDistortion(vr::EVRE
coordinates.rfRed[0] = fU;
coordinates.rfRed[1] = fV;
return coordinates;
}

int SlimeVRDriver::HMDDevice::getDeviceId()
{
return deviceId_;
}
5 changes: 4 additions & 1 deletion src/HMDDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace SlimeVRDriver {
class HMDDevice : public IVRDevice, public vr::IVRDisplayComponent {
public:
HMDDevice(std::string serial);
HMDDevice(std::string serial, int deviceId);
~HMDDevice() = default;

// Inherited via IVRDevice
Expand All @@ -35,9 +35,12 @@ namespace SlimeVRDriver {
virtual void GetEyeOutputViewport(vr::EVREye eEye, uint32_t* pnX, uint32_t* pnY, uint32_t* pnWidth, uint32_t* pnHeight) override;
virtual void GetProjectionRaw(vr::EVREye eEye, float* pfLeft, float* pfRight, float* pfTop, float* pfBottom) override;
virtual vr::DistortionCoordinates_t ComputeDistortion(vr::EVREye eEye, float fU, float fV) override;
virtual int getDeviceId() override;
virtual void PositionMessage(messages::Position &position) override;
private:
vr::TrackedDeviceIndex_t device_index_ = vr::k_unTrackedDeviceIndexInvalid;
std::string serial_;
int deviceId_;

vr::DriverPose_t last_pose_ = IVRDevice::MakeDefaultPose();

Expand Down
4 changes: 4 additions & 0 deletions src/IVRDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <variant>
#include <openvr_driver.h>
#include <DeviceType.hpp>
#include "ProtobufMessages.pb.h"

namespace SlimeVRDriver {

Expand Down Expand Up @@ -58,6 +59,9 @@ namespace SlimeVRDriver {
virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) = 0;
virtual vr::DriverPose_t GetPose() = 0;

virtual int getDeviceId() = 0;
virtual void PositionMessage(messages::Position& position) = 0;

~IVRDevice() = default;
};
};
Loading

0 comments on commit 21a5857

Please sign in to comment.