This commit is contained in:
Vindicus999
2025-08-24 09:54:57 -04:00
135 changed files with 12233 additions and 2792 deletions
+135 -105
View File
@@ -383,9 +383,18 @@ void scenes::lobby::connect(const configuration::server_data & data)
data.manual);
}
std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints)
std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(xr::spaces space, const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & joints)
{
const auto & palm = joints[XR_HAND_JOINT_PALM_EXT].first;
if (recentering_context and std::get<0>(*recentering_context) != space)
return std::nullopt;
if (not joints)
{
recentering_context.reset();
return std::nullopt;
}
const auto & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first;
const auto & o = palm.pose.orientation;
const auto & p = palm.pose.position;
glm::quat q{o.w, o.x, o.y, o.z};
@@ -393,102 +402,130 @@ std::optional<glm::vec3> scenes::lobby::check_recenter_gesture(const std::array<
if (glm::dot(q * glm::vec3(0, 1, 0), glm::vec3(0, -1, 0)) > constants::lobby::recenter_cosangle_min)
{
recentering_context.emplace(space, glm::vec3{}, 0);
return v + glm::vec3(0, constants::lobby::recenter_distance_up, 0) + q * glm::vec3(0, 0, -constants::lobby::recenter_distance_front);
}
recentering_context.reset();
return std::nullopt;
}
std::optional<glm::vec3> scenes::lobby::check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position)
{
std::optional<std::pair<glm::vec3, glm::quat>> aim;
glm::vec3 offset_position;
glm::quat offset_orientation;
xr::spaces controller;
glm::vec3 recenter_position;
float recenter_distance;
if (application::read_action_bool(recenter_left_action).value_or(std::pair{0, false}).second)
if (recentering_context)
{
aim = application::locate_controller(application::space(xr::spaces::aim_left), application::space(xr::spaces::world), predicted_display_time);
std::tie(offset_position, offset_orientation) = input->offset[xr::spaces::aim_left];
}
if (application::read_action_bool(recenter_right_action).value_or(std::pair{0, false}).second)
{
aim = application::locate_controller(application::space(xr::spaces::aim_right), application::space(xr::spaces::world), predicted_display_time);
std::tie(offset_position, offset_orientation) = input->offset[xr::spaces::aim_right];
}
if (aim)
{
// Handle controller offset
aim->first = aim->first + glm::mat3_cast(aim->second * offset_orientation) * offset_position;
aim->second = aim->second * offset_orientation;
if (not gui_recenter_position or not gui_recenter_distance)
controller = std::get<0>(*recentering_context);
bool state;
switch (controller)
{
// First frame where the recenter action is active
imgui_context::controller_state state{
.active = true,
.aim_position = aim->first,
.aim_orientation = aim->second,
};
case xr::spaces::aim_left:
state = application::read_action_bool(recenter_left_action).value_or(std::pair{0, false}).second;
break;
imgui_ctx->compute_pointer_position(state);
case xr::spaces::aim_right:
state = application::read_action_bool(recenter_right_action).value_or(std::pair{0, false}).second;
break;
if (state.pointer_position) // TODO: check that the pointer is inside an imgui window
{
auto M = glm::mat3_cast(imgui_ctx->layers()[0].orientation);
default:
return std::nullopt;
}
// Pointer position in world
glm::vec3 world_pointer_position = imgui_ctx->rw_from_vp(*state.pointer_position);
if (not state)
{
recentering_context.reset();
return std::nullopt;
}
}
else if (auto state = application::read_action_bool(recenter_left_action); state and state->second)
controller = xr::spaces::aim_left;
else if (auto state = application::read_action_bool(recenter_right_action); state and state->second)
controller = xr::spaces::aim_right;
else
return std::nullopt;
// Pointer position in GUI
gui_recenter_position = glm::transpose(M) * (world_pointer_position - imgui_ctx->layers()[0].position);
gui_recenter_distance = glm::length(state.aim_position - world_pointer_position);
}
else
{
gui_recenter_position = glm::vec3(0, 0, 0);
gui_recenter_distance = constants::lobby::recenter_action_distance;
}
auto aim = application::locate_controller(application::space(controller), application::space(xr::spaces::world), predicted_display_time);
if (not aim)
{
// The controller cannot be located
recentering_context.reset();
return std::nullopt;
}
// Handle controller offset
auto [offset_position, offset_orientation] = input->offset[controller];
aim->first = aim->first + glm::mat3_cast(aim->second * offset_orientation) * offset_position;
aim->second = aim->second * offset_orientation;
if (not recentering_context)
{
// First frame of recentering
imgui_context::controller_state state{
.active = true,
.aim_position = aim->first,
.aim_orientation = aim->second,
};
imgui_ctx->compute_pointer_position(state);
if (state.pointer_position) // TODO: check that the pointer is inside an imgui window
{
auto M = glm::mat3_cast(imgui_ctx->layers()[0].orientation);
// Pointer position in world
glm::vec3 world_pointer_position = imgui_ctx->rw_from_vp(*state.pointer_position);
// Pointer position in GUI
recenter_position = glm::transpose(M) * (world_pointer_position - imgui_ctx->layers()[0].position);
recenter_distance = glm::length(state.aim_position - world_pointer_position);
}
else
{
// Subsequent frames: find the GUI position that gives the correct world pointer position
glm::vec3 controller_direction = -glm::column(glm::mat3_cast(aim->second), 2);
glm::vec3 wanted_world_pointer_position = aim->first + controller_direction * *gui_recenter_distance;
// I'm sure there's an analytical solution but I can't be bothered to write it so
// let's use a gradient descent instead
auto f = [&](glm::vec3 new_gui_position) {
auto q = compute_gui_orientation(head_position, new_gui_position);
auto M = glm::mat3_cast(q); // plane-to-world transform
glm::vec3 world_pointer_position = new_gui_position + M * *gui_recenter_position;
return world_pointer_position - wanted_world_pointer_position;
};
// One step is usually enough, the solution will continuously improve in the next frames
glm::vec3 gui_position = imgui_ctx->layers()[0].position;
float eps = 0.01;
glm::vec3 obj = f(gui_position);
glm::vec3 obj_dx = (f(gui_position + glm::vec3(eps, 0, 0)) - obj) / eps;
glm::vec3 obj_dy = (f(gui_position + glm::vec3(0, eps, 0)) - obj) / eps;
glm::vec3 obj_dz = (f(gui_position + glm::vec3(0, 0, eps)) - obj) / eps;
glm::mat3 jacobian{obj_dx, obj_dy, obj_dz};
gui_position -= glm::inverse(jacobian) * obj;
return gui_position;
// Use the center of the GUI if the controller points outside of the GUI
recenter_position = glm::vec3(0, 0, 0);
recenter_distance = constants::lobby::recenter_action_distance;
}
recentering_context.emplace(controller, recenter_position, recenter_distance);
return std::nullopt;
}
else
{
gui_recenter_position.reset();
}
// Subsequent frames: find the GUI position that gives the correct world pointer position
std::tie(controller, recenter_position, recenter_distance) = *recentering_context;
return std::nullopt;
glm::vec3 controller_direction = -glm::column(glm::mat3_cast(aim->second), 2);
glm::vec3 wanted_world_pointer_position = aim->first + controller_direction * recenter_distance;
// I'm sure there's an analytical solution but I can't be bothered to write it so
// let's use a gradient descent instead
auto f = [&](glm::vec3 new_gui_position) {
auto q = compute_gui_orientation(head_position, new_gui_position);
auto M = glm::mat3_cast(q); // plane-to-world transform
glm::vec3 world_pointer_position = new_gui_position + M * recenter_position;
return world_pointer_position - wanted_world_pointer_position;
};
// One step is usually enough, the solution will continuously improve in the next frames
glm::vec3 gui_position = imgui_ctx->layers()[0].position;
float eps = 0.01;
glm::vec3 obj = f(gui_position);
glm::vec3 obj_dx = (f(gui_position + glm::vec3(eps, 0, 0)) - obj) / eps;
glm::vec3 obj_dy = (f(gui_position + glm::vec3(0, eps, 0)) - obj) / eps;
glm::vec3 obj_dz = (f(gui_position + glm::vec3(0, 0, eps)) - obj) / eps;
glm::mat3 jacobian{obj_dx, obj_dy, obj_dz};
gui_position -= glm::inverse(jacobian) * obj;
return gui_position;
}
}
std::optional<glm::vec3> scenes::lobby::check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation)
@@ -612,26 +649,24 @@ void scenes::lobby::render(const XrFrameState & frame_state)
if (!new_gui_position and head_position)
new_gui_position = check_recenter_action(frame_state.predictedDisplayTime, head_position->first);
if (application::get_hand_tracking_supported())
if (left_hand and right_hand)
{
auto left = application::get_left_hand().locate(world_space, frame_state.predictedDisplayTime);
auto right = application::get_right_hand().locate(world_space, frame_state.predictedDisplayTime);
auto left = left_hand->locate(world_space, frame_state.predictedDisplayTime);
auto right = right_hand->locate(world_space, frame_state.predictedDisplayTime);
hand_model::apply(world, left, right);
if (left and xr::hand_tracker::check_flags(*left, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
{
hide_left_controller = true;
if (!new_gui_position)
new_gui_position = check_recenter_gesture(*left);
}
if (right and xr::hand_tracker::check_flags(*right, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
{
hide_right_controller = true;
if (!new_gui_position)
new_gui_position = check_recenter_gesture(*right);
}
if (not new_gui_position)
new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left);
if (not new_gui_position)
new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right);
}
if (head_position && new_gui_position)
@@ -763,7 +798,7 @@ void scenes::lobby::on_focused()
// assert(std::ranges::all_of(views, [height](const XrViewConfigurationView & view) { return view.recommendedImageRectHeight == height; }));
renderer.emplace(device, physical_device, queue, commandpool);
loader.emplace(device, physical_device, queue, application::queue_family_index(), renderer->get_default_material());
loader.emplace(device, physical_device, queue, queue_family_index, renderer->get_default_material());
lobby_entity = load_gltf("ground.gltf", layer_lobby).first;
@@ -800,12 +835,6 @@ void scenes::lobby::on_focused()
xyz_axes_right_controller = load_gltf("xyz-arrows.glb", layer_controllers).first;
#endif
if (application::get_hand_tracking_supported())
{
hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "left-hand.glb", layer_controllers);
hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "right-hand.glb", layer_controllers);
}
recenter_left_action = get_action("recenter_left").first;
recenter_right_action = get_action("recenter_right").first;
@@ -827,21 +856,19 @@ void scenes::lobby::on_focused()
.haptic_output = get_action("right_haptic").first,
},
};
if (auto & hand = application::get_left_hand())
if (system.hand_tracking_supported())
{
imgui_inputs.push_back(
{
.hand = &hand,
});
}
if (auto & hand = application::get_right_hand())
{
imgui_inputs.push_back(
{
.hand = &hand,
});
left_hand = session.create_hand_tracker(XR_HAND_LEFT_EXT);
right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT);
hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "left-hand.glb", layer_controllers);
hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "right-hand.glb", layer_controllers);
imgui_inputs.push_back({.hand = &*left_hand});
imgui_inputs.push_back({.hand = &*right_hand});
}
face_tracker = xr::make_face_tracker(instance, system, session);
// 0.4mm / pixel
std::vector<imgui_context::viewport> vps{
{
@@ -942,6 +969,9 @@ void scenes::lobby::on_unfocused()
world.clear(); // Must be cleared before the renderer so that the descriptor sets are freed before their pools
input.reset();
left_hand.reset();
right_hand.reset();
face_tracker.emplace<std::monostate>();
loader.reset();
renderer.reset();
+14 -4
View File
@@ -29,7 +29,9 @@
#include "wivrn_config.h"
#include "wivrn_discover.h"
#include "wivrn_packets.h"
#include "xr/face_tracker.h"
#include <vulkan/vulkan_raii.hpp>
#include <openxr/openxr.h>
#include <optional>
#include <vector>
@@ -65,6 +67,11 @@ class lobby : public scene_impl<lobby>
std::optional<imgui_context> imgui_ctx;
std::optional<xr::hand_tracker> left_hand;
std::optional<xr::hand_tracker> right_hand;
xr::face_tracker face_tracker;
std::string selected_item;
std::unique_ptr<asset> license;
@@ -94,8 +101,10 @@ class lobby : public scene_impl<lobby>
XrAction recenter_left_action = XR_NULL_HANDLE;
XrAction recenter_right_action = XR_NULL_HANDLE;
std::optional<glm::vec3> gui_recenter_position;
std::optional<float> gui_recenter_distance;
// std::optional<glm::vec3> gui_recenter_position;
// std::optional<float> gui_recenter_distance;
// Which controller is used for recentering, position of the pointed point in the GUI, in GUI axes, and distance between the controller and the pointed point during recentering
std::optional<std::tuple<xr::spaces, glm::vec3, float>> recentering_context;
bool recenter_gui = true;
void move_gui(glm::vec3 head_position, glm::vec3 new_gui_position);
@@ -120,6 +129,7 @@ class lobby : public scene_impl<lobby>
ImTextureID about_picture;
ImTextureID default_icon;
std::unordered_map<std::string, ImTextureID> app_icons;
std::optional<XrTime> timestamp_start_application;
virtual_keyboard keyboard;
@@ -136,7 +146,7 @@ class lobby : public scene_impl<lobby>
void draw_features_status(XrTime predicted_display_time);
void gui_connecting(locked_notifiable<pin_request_data> & request);
void gui_enter_pin(locked_notifiable<pin_request_data> & request);
void gui_connected();
void gui_connected(XrTime predicted_display_time);
void gui_server_list();
void gui_new_server();
void gui_settings();
@@ -152,7 +162,7 @@ class lobby : public scene_impl<lobby>
void connect(const configuration::server_data & data);
std::unique_ptr<wivrn_session> connect_to_session(wivrn_discover::service service, bool manual_connection);
std::optional<glm::vec3> check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints);
std::optional<glm::vec3> check_recenter_gesture(xr::spaces space, const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & joints);
std::optional<glm::vec3> check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position);
std::optional<glm::vec3> check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation);
+68 -34
View File
@@ -34,11 +34,13 @@
#include "utils/overloaded.h"
#include "utils/ranges.h"
#include "version.h"
#include "xr/body_tracker.h"
#include <algorithm>
#include <cassert>
#include <chrono>
#include <filesystem>
#include <glm/gtc/quaternion.hpp>
#include <imspinner.h>
#include <ranges>
#include <spdlog/fmt/fmt.h>
#include <string>
@@ -245,6 +247,7 @@ void scenes::lobby::gui_connecting(locked_notifiable<pin_request_data> & pin_req
if (next_scene)
{
current_tab = tab::connected;
timestamp_start_application.reset();
ImGui::CloseCurrentPopup();
return;
}
@@ -375,7 +378,7 @@ void scenes::lobby::gui_enter_pin(locked_notifiable<pin_request_data> & pin_requ
imgui_ctx->vibrate_on_hover();
}
void scenes::lobby::gui_connected()
void scenes::lobby::gui_connected(XrTime predicted_display_time)
{
if (not next_scene)
{
@@ -400,14 +403,17 @@ void scenes::lobby::gui_connected()
auto apps = next_scene->get_applications();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20});
if (apps->applications.empty())
if (apps->empty())
{
CenterTextHV(_("Start an application on the server to start streaming."));
}
else
{
bool app_starting = timestamp_start_application and (predicted_display_time - *timestamp_start_application < 10'000'000'000);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {20, 0});
ImGui::BeginChild("Main", ImGui::GetWindowSize() - ImGui::GetCursorPos() - ImVec2(0, disconnect_size.y + 80), 0);
ImGui::BeginDisabled(app_starting);
ImGui::BeginChild("Main", ImGui::GetWindowSize() - ImGui::GetCursorPos() - ImVec2(0, disconnect_size.y + 80), 0, app_starting ? ImGuiWindowFlags_NoScrollWithMouse : 0);
ImGui::Indent(20);
if (server_name.empty())
@@ -426,7 +432,7 @@ void scenes::lobby::gui_connected()
ImGui::Indent((usable_window_width - icon_line_width) / 2);
auto t0 = std::chrono::steady_clock::now();
for (const auto [index, app]: utils::enumerate(apps->applications))
for (const auto [index, app]: utils::enumerate(*apps))
{
ImTextureID texture = [&]() -> ImTextureID {
if (app.image.empty())
@@ -457,8 +463,11 @@ void scenes::lobby::gui_connected()
}();
ImGui::PushStyleColor(ImGuiCol_Button, 0);
if (icon(app.name + "##" + app.id, texture, {256, 256}, 0, {icon_width, 0}))
if (icon(app.name + "##" + app.id, texture, {256, 256}, ImGuiButtonFlags_PressedOnClickRelease, {icon_width, 0}))
{
timestamp_start_application = predicted_display_time;
next_scene->start_application(app.id);
}
imgui_ctx->vibrate_on_hover();
ImGui::PopStyleColor(); // ImGuiCol_Button
@@ -470,7 +479,7 @@ void scenes::lobby::gui_connected()
std::vector<std::pair<std::string, ImTextureID>> to_be_removed;
for (const auto & [app_id, app_icon]: app_icons)
{
if (not std::ranges::contains(apps->applications, app_id, &to_headset::application_list::application::id))
if (not std::ranges::contains(*apps, app_id, &scenes::stream::app::id))
to_be_removed.emplace_back(app_id, app_icon);
}
for (const auto & [app_id, app_icon]: to_be_removed)
@@ -481,12 +490,26 @@ void scenes::lobby::gui_connected()
ScrollWhenDragging();
ImGui::EndChild();
ImGui::EndDisabled();
if (app_starting)
{
ImGui::SetCursorPos(ImGui::GetWindowSize() / 2 - ImVec2{200, 200} - ImGui::GetStyle().FramePadding);
ImSpinner::SpinnerAng("App starting spinner",
200, // Radius
40, // Thickness
ImColor{1.f, 1.f, 1.f, 1.f}, // Colour
ImColor{1.f, 1.f, 1.f, 0.f}, // Background
6, // Velocity
0.75f * 2 * M_PI // Angle
);
}
ImGui::PopStyleVar(); // ImGuiStyleVar_WindowPadding
}
ImGui::PopStyleVar();
ImGui::SetCursorPosX(ImGui::GetWindowSize().x - disconnect_size.x - 50);
ImGui::SetCursorPosY(ImGui::GetWindowSize().y - disconnect_size.y - 50);
ImGui::SetCursorPos(ImGui::GetWindowSize() - disconnect_size - ImVec2{50, 50});
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.2f, 0.2f, 0.40f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.8f, 0.2f, 0.2f, 1.00f));
@@ -867,7 +890,7 @@ void scenes::lobby::gui_settings()
ImGui::EndDisabled();
}
{
ImGui::BeginDisabled(not application::get_hand_tracking_supported());
ImGui::BeginDisabled(not system.hand_tracking_supported());
bool enabled = config.check_feature(feature::hand_tracking);
if (ImGui::Checkbox(_S("Enable hand tracking"), &enabled))
{
@@ -891,7 +914,7 @@ void scenes::lobby::gui_settings()
ImGui::EndDisabled();
}
{
ImGui::BeginDisabled(not application::get_face_tracking_supported());
ImGui::BeginDisabled(std::holds_alternative<std::monostate>(face_tracker));
bool enabled = config.check_feature(feature::face_tracking);
if (ImGui::Checkbox(_S("Enable face tracking"), &enabled))
{
@@ -903,8 +926,9 @@ void scenes::lobby::gui_settings()
imgui_ctx->vibrate_on_hover();
}
auto body_tracker = system.body_tracker_supported();
{
ImGui::BeginDisabled(not application::get_body_tracking_supported());
ImGui::BeginDisabled(body_tracker == xr::body_tracker_type::none);
bool enabled = config.check_feature(feature::body_tracking);
if (ImGui::Checkbox(_S("Enable body tracking"), &enabled))
{
@@ -919,19 +943,14 @@ void scenes::lobby::gui_settings()
}
else
{
std::visit(utils::overloaded{
[](auto &) {},
[this](xr::fb_body_tracker &) {
imgui_ctx->tooltip(_("Requires 'Hand and body tracking' to be enabled in the Quest movement tracking settings,\notherwise body data will be guessed from controller and headset positions"));
},
},
application::get_body_tracker());
if (body_tracker == xr::body_tracker_type::fb)
imgui_ctx->tooltip(_("Requires 'Hand and body tracking' to be enabled in the Quest movement tracking settings,\notherwise body data will be guessed from controller and headset positions"));
}
}
imgui_ctx->vibrate_on_hover();
}
if (std::holds_alternative<xr::fb_body_tracker>(application::get_body_tracker()))
if (body_tracker == xr::body_tracker_type::fb)
{
ImGui::BeginDisabled(not config.check_feature(feature::body_tracking));
ImGui::Indent();
@@ -957,7 +976,7 @@ void scenes::lobby::gui_settings()
ImGui::EndDisabled();
}
ImGui::BeginDisabled(passthrough_supported == xr::system::passthrough_type::no_passthrough);
ImGui::BeginDisabled(system.passthrough_supported() == xr::passthrough_type::none);
if (ImGui::Checkbox(_S("Enable video passthrough in lobby"), &config.passthrough_enabled))
{
setup_passthrough();
@@ -968,6 +987,13 @@ void scenes::lobby::gui_settings()
imgui_ctx->tooltip(_("This feature is not supported by your headset"));
ImGui::EndDisabled();
{
ImGui::Checkbox(_S("Enable in-stream window"), &config.enable_stream_gui);
imgui_ctx->vibrate_on_hover();
if (ImGui::IsItemHovered())
imgui_ctx->tooltip(_("Enables the configuration window to be shown while the game is streaming.\nIf enabled, the window is activated by pressing both thumbsticks."));
}
ImGui::PopStyleVar(); // ImGuiStyleVar_ItemSpacing
}
@@ -1282,12 +1308,12 @@ void scenes::lobby::gui_first_run()
item{
.f = feature::face_tracking,
.text = _S("Enable face tracking?"),
.supported = application::get_face_tracking_supported(),
.supported = not std::holds_alternative<std::monostate>(face_tracker),
},
item{
.f = feature::body_tracking,
.text = _S("Enable body tracking?"),
.supported = application::get_body_tracking_supported(),
.supported = system.body_tracker_supported() != xr::body_tracker_type::none,
},
};
@@ -1617,11 +1643,22 @@ static auto face_weights()
return res;
}
static const char * get_face_icon(XrTime predicted_display_time)
static const char * get_face_icon(XrTime predicted_display_time, xr::face_tracker & face_tracker)
{
static const auto w = face_weights();
wivrn::from_headset::tracking::fb_face2 expression;
std::get<xr::fb_face_tracker2>(application::get_face_tracker()).get_weights(predicted_display_time, expression);
const char * result = nullptr;
std::visit(utils::overloaded{
[](std::monostate &) {},
[&](xr::htc_face_tracker &) { result = ICON_FA_FACE_SMILE_WINK; },
[&](auto & ft) {
ft.get_weights(predicted_display_time, expression);
},
},
face_tracker);
if (result)
return result;
if (not expression.is_valid)
return ICON_FA_FACE_MEH;
@@ -1664,7 +1701,7 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
.icon_disabled = ICON_FA_MICROPHONE_SLASH,
});
if (application::get_hand_tracking_supported())
if (system.hand_tracking_supported())
{
items.push_back({
.f = feature::hand_tracking,
@@ -1685,21 +1722,18 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
});
}
if (application::get_face_tracking_supported())
if (not std::holds_alternative<std::monostate>(face_tracker))
{
const char * icon_enabled = std::holds_alternative<xr::fb_face_tracker2>(application::get_face_tracker())
? get_face_icon(predicted_display_time)
: ICON_FA_FACE_KISS_WINK_HEART;
items.push_back({
.f = feature::face_tracking,
.tooltip_enabled = _("Face tracking is enabled"),
.tooltip_disabled = _("Face tracking is disabled"),
.icon_enabled = icon_enabled,
.icon_enabled = get_face_icon(predicted_display_time, face_tracker),
.icon_disabled = ICON_FA_FACE_MEH_BLANK,
});
}
if (application::get_body_tracking_supported())
if (system.body_tracker_supported() != xr::body_tracker_type::none)
{
items.push_back({
.f = feature::body_tracking,
@@ -1773,7 +1807,7 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time)
if (*status.charge > 0.995)
icon_nr = 5;
else
icon_nr = application::now() / 500'000'000 % 5;
icon_nr = instance.now() / 500'000'000 % 5;
}
else
icon_nr = std::round((*status.charge) * 4);
@@ -1890,7 +1924,7 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTi
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {10, 10});
ImGui::Begin("WiVRn", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
gui_connected();
gui_connected(predicted_display_time);
ImGui::End();
ImGui::PopStyleVar(3); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_FrameRounding, ImGuiStyleVar_FramePadding
}
@@ -2002,7 +2036,7 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTi
if (not is_gui_visible(*imgui_ctx, predicted_display_time))
{
if (application::get_hand_tracking_supported())
if (system.hand_tracking_supported())
display_recentering_tip(*imgui_ctx, _("Press the grip button or put your palm up\nto move the main window"));
else
display_recentering_tip(*imgui_ctx, _("Press the grip button to move the main window"));
+77 -101
View File
@@ -17,11 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "constants.h"
#include "utils/overloaded.h"
#include "xr/fb_body_tracker.h"
#include "xr/body_tracker.h"
#include "xr/face_tracker.h"
#include "xr/fb_face_tracker2.h"
#include "xr/htc_body_tracker.h"
#include "xr/pico_body_tracker.h"
#include "xr/space.h"
#include <glm/gtc/matrix_access.hpp>
#include <glm/gtc/quaternion.hpp>
@@ -42,6 +40,7 @@
#include "utils/ranges.h"
#include "vk/pipeline.h"
#include "vk/shader.h"
#include "vk/specialization_constants.h"
#include "wivrn_packets.h"
#include <algorithm>
#include <mutex>
@@ -189,7 +188,11 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
self->network_session = std::move(network_session);
self->network_session->send_control([&]() {
from_headset::headset_info_packet info{};
from_headset::headset_info_packet info{
.language = application::get_messages_info().language,
.country = application::get_messages_info().country,
.variant = application::get_messages_info().variant,
};
auto view = self->system.view_configuration_views(self->viewconfig)[0];
view = override_view(view, guess_model());
@@ -251,39 +254,42 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
if (config.check_feature(feature::face_tracking))
{
info.face_tracking = std::visit(utils::overloaded{
[](std::monostate &) {
return from_headset::face_type::none;
},
[](xr::fb_face_tracker2 &) {
return from_headset::face_type::fb2;
},
[](xr::htc_face_tracker &) {
return from_headset::face_type::htc;
},
[](xr::pico_face_tracker &) {
return from_headset::face_type::fb2;
},
},
application::get_face_tracker());
switch (self->system.face_tracker_supported())
{
case xr::face_tracker_type::none:
info.face_tracking = from_headset::face_type::none;
break;
case xr::face_tracker_type::fb:
case xr::face_tracker_type::pico:
info.face_tracking = from_headset::face_type::fb2;
break;
case xr::face_tracker_type::htc:
info.face_tracking = from_headset::face_type::htc;
break;
}
}
info.num_generic_trackers = 0;
if (config.check_feature(feature::body_tracking))
{
info.num_generic_trackers = std::visit(utils::overloaded{
[&](std::monostate &) {
return size_t(0);
},
[&](auto & b) {
return b.count();
},
},
application::get_body_tracker());
switch (self->system.body_tracker_supported())
{
case xr::body_tracker_type::none:
break;
case xr::body_tracker_type::fb:
info.num_generic_trackers = xr::fb_body_tracker::get_whitelisted_joints(config.fb_lower_body, config.fb_hip).size();
break;
case xr::body_tracker_type::htc:
info.num_generic_trackers = application::get_generic_trackers().size();
break;
case xr::body_tracker_type::pico:
info.num_generic_trackers = xr::pico_body_tracker::joint_whitelist.size();
break;
}
}
info.palm_pose = application::space(xr::spaces::palm_left) or application::space(xr::spaces::palm_right);
info.passthrough = self->system.passthrough_supported() != xr::system::passthrough_type::no_passthrough;
info.passthrough = self->system.passthrough_supported() != xr::passthrough_type::none;
info.system_name = std::string(self->system.properties().systemName);
audio::get_audio_description(info);
@@ -311,15 +317,6 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
}
}
{
const auto & info = application::get_messages_info();
self->network_session->send_control(from_headset::get_application_list{
.language = info.language,
.country = info.country,
.variant = info.variant,
});
}
{
const auto & config = application::get_config();
self->override_foveation_enable = config.override_foveation_enable;
@@ -363,7 +360,7 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
{
self->haptics_actions.emplace(id, haptics_action{
.action = action.first,
.path = application::string_to_path(path),
.path = self->instance.string_to_path(path),
});
}
}
@@ -394,7 +391,7 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
void scenes::stream::on_focused()
{
gui_status_last_change = application::now();
gui_status_last_change = instance.now();
auto views = system.view_configuration_views(viewconfig);
// stream_view = override_view(views[0], guess_model());
@@ -402,7 +399,7 @@ void scenes::stream::on_focused()
height = views[0].recommendedImageRectHeight;
renderer.emplace(device, physical_device, queue, commandpool);
loader.emplace(device, physical_device, queue, application::queue_family_index(), renderer->get_default_material());
loader.emplace(device, physical_device, queue, queue_family_index, renderer->get_default_material());
std::string profile = controller_name();
input.emplace(
@@ -470,8 +467,11 @@ void scenes::stream::on_focused()
swapchain_imgui,
std::vector{vp});
plots_toggle_1 = get_action("plots_toggle_1").first;
plots_toggle_2 = get_action("plots_toggle_2").first;
if (application::get_config().enable_stream_gui)
{
plots_toggle_1 = get_action("plots_toggle_1").first;
plots_toggle_2 = get_action("plots_toggle_2").first;
}
recenter_left = get_action("recenter_left").first;
recenter_right = get_action("recenter_right").first;
foveation_pitch = get_action("foveation_pitch").first;
@@ -494,6 +494,8 @@ void scenes::stream::on_unfocused()
loader.reset();
renderer.reset();
clear_swapchains();
left_hand.reset();
right_hand.reset();
imgui_ctx.reset();
swapchain_imgui = xr::swapchain();
@@ -524,16 +526,16 @@ void scenes::stream::push_blit_handle(shard_accumulator * decoder, std::shared_p
{
if (decoder != decoders[stream].decoder.get())
return;
handle->feedback.received_from_decoder = application::now();
handle->feedback.received_from_decoder = instance.now();
std::swap(handle, decoders[stream].latest_frames[handle->feedback.frame_index % decoders[stream].latest_frames.size()]);
}
if (state_ != state::streaming and std::ranges::all_of(decoders, [](accumulator_images & i) {
return i.alpha() or not i.frames().empty();
return i.alpha() or not i.empty();
}))
{
state_ = state::streaming;
spdlog::info("Stream scene ready at t={}", application::now());
spdlog::info("Stream scene ready at t={}", instance.now());
}
}
@@ -548,15 +550,14 @@ bool scenes::stream::accumulator_images::alpha() const
return decoder->desc().channels == wivrn::to_headset::video_stream_description::channels_t::alpha;
}
std::vector<uint64_t> scenes::stream::accumulator_images::frames() const
bool scenes::stream::accumulator_images::empty() const
{
std::vector<uint64_t> result;
for (const auto & frame: latest_frames)
{
if (frame)
result.push_back(frame->feedback.frame_index);
return false;
}
return result;
return true;
}
std::vector<std::shared_ptr<shard_accumulator::blit_handle>> scenes::stream::common_frame(XrTime display_time)
@@ -766,7 +767,15 @@ void scenes::stream::render(const XrFrameState & frame_state)
}
if (state_ == state::stalled)
{
network_session->send_control(from_headset::get_application_list{
.language = application::get_messages_info().language,
.country = application::get_messages_info().country,
.variant = application::get_messages_info().variant,
});
application::pop_scene();
}
assert(swapchain);
for (auto & i: decoders)
@@ -799,51 +808,19 @@ void scenes::stream::render(const XrFrameState & frame_state)
const auto & description = i.decoder->desc();
vk::Extent2D image_size = i.decoder->image_size();
std::array useful_size{
float(description.width) / image_size.width,
float(description.height) / image_size.height,
};
spdlog::info("useful size: {}x{} with buffer {}x{}",
description.width,
description.height,
image_size.width,
image_size.height);
std::array specialization_constants_desc{
vk::SpecializationMapEntry{
.constantID = 0,
.offset = 0,
.size = sizeof(float),
},
vk::SpecializationMapEntry{
.constantID = 1,
.offset = sizeof(float),
.size = sizeof(float),
}};
auto vert_constants = make_specialization_constants(
float(description.width) / image_size.width,
float(description.height) / image_size.height);
vk::SpecializationInfo vert_specialization_info;
vert_specialization_info.setMapEntries(specialization_constants_desc);
vert_specialization_info.setData<float>(useful_size);
std::array<VkBool32, 2> constants = {
need_srgb_conversion(guess_model()),
i.alpha(),
};
std::array frag_specialization_constant_desc = {
vk::SpecializationMapEntry{
.constantID = 0,
.offset = 0,
.size = sizeof(constants[0]),
},
vk::SpecializationMapEntry{
.constantID = 1,
.offset = sizeof(constants[0]),
.size = sizeof(constants[1]),
},
};
vk::SpecializationInfo frag_specialization_info;
frag_specialization_info.setMapEntries(frag_specialization_constant_desc);
frag_specialization_info.setData<VkBool32>(constants);
auto frag_constants = make_specialization_constants(
VkBool32(need_srgb_conversion(guess_model())),
VkBool32(i.alpha()));
// Create graphics pipeline
vk::raii::ShaderModule vertex_shader = load_shader(device, "stream.vert");
@@ -861,13 +838,13 @@ void scenes::stream::render(const XrFrameState & frame_state)
.stage = vk::ShaderStageFlagBits::eVertex,
.module = *vertex_shader,
.pName = "main",
.pSpecializationInfo = &vert_specialization_info,
.pSpecializationInfo = vert_constants,
},
{
.stage = vk::ShaderStageFlagBits::eFragment,
.module = *fragment_shader,
.pName = "main",
.pSpecializationInfo = &frag_specialization_info,
.pSpecializationInfo = frag_constants,
}},
.VertexBindingDescriptions = {},
.VertexAttributeDescriptions = {},
@@ -953,7 +930,7 @@ void scenes::stream::render(const XrFrameState & frame_state)
if (not blit_handle or not *i.blit_pipeline)
continue;
blit_handle->feedback.blitted = application::now();
blit_handle->feedback.blitted = instance.now();
if (blit_handle->feedback.blitted - blit_handle->feedback.received_from_decoder > 1'000'000'000)
state_ = stream::state::stalled;
++blit_handle->feedback.times_displayed;
@@ -1002,6 +979,10 @@ void scenes::stream::render(const XrFrameState & frame_state)
uint16_t x_offset = 0;
for (auto & out: decoder_output)
{
vk::Extent2D decoder_out_size{
.width = decoder_out_image.info().extent.width,
.height = decoder_out_image.info().extent.height,
};
command_buffer.beginRenderPass(
{
.renderPass = *blit_render_pass,
@@ -1310,10 +1291,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
}
// Create outputs for the decoders
decoder_out_size = vk::Extent2D{video_width, video_height};
{
decoder_out_format = vk::Format::eA8B8G8R8SrgbPack32;
vk::ImageCreateInfo image_info{
.flags = vk::ImageCreateFlags{},
.imageType = vk::ImageType::e2D,
@@ -1337,7 +1315,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
vk::ImageViewCreateInfo image_view_info{
.image = vk::Image{decoder_out_image},
.viewType = vk::ImageViewType::e2D,
.format = vk::Format::eA8B8G8R8SrgbPack32,
.format = image_info.format,
.components = {},
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
@@ -1361,8 +1339,8 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
.renderPass = *blit_render_pass,
.attachmentCount = 1,
.pAttachments = &*output.image_view,
.width = decoder_out_size.width,
.height = decoder_out_size.height,
.width = image_info.extent.width,
.height = image_info.extent.height,
.layers = 1,
});
}
@@ -1387,7 +1365,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
spdlog::info("Creating decoder size {}x{} offset {},{}", item.width, item.height, item.offset_x, item.offset_y);
decoders.push_back(accumulator_images{
.decoder = std::make_unique<shard_accumulator>(device, physical_device, item, description.fps, shared_from_this(), stream_index),
.decoder = std::make_unique<shard_accumulator>(device, physical_device, instance, item, description.fps, shared_from_this(), stream_index),
});
}
}
@@ -1429,8 +1407,6 @@ void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint
device,
physical_device,
decoder_out_image,
vk::Extent2D{.width = video_width, .height = video_height},
2,
swapchain_images,
extent,
swapchain.format());
+15 -7
View File
@@ -48,6 +48,13 @@ public:
};
static const size_t image_buffer_size = 3;
struct app
{
std::string id;
std::string name;
std::vector<std::byte> image;
};
private:
static const size_t view_count = 2;
@@ -60,12 +67,12 @@ private:
vk::DescriptorSet descriptor_set = nullptr;
vk::raii::PipelineLayout blit_pipeline_layout = nullptr;
vk::raii::Pipeline blit_pipeline = nullptr;
// latest frames from oldest to most recent
// latest frames, rolling buffer
std::array<std::shared_ptr<wivrn::shard_accumulator::blit_handle>, image_buffer_size> latest_frames;
std::shared_ptr<wivrn::shard_accumulator::blit_handle> frame(uint64_t id) const;
bool alpha() const;
std::vector<uint64_t> frames() const;
bool empty() const;
};
wifi_lock::wifi wifi;
@@ -77,9 +84,9 @@ private:
std::unique_ptr<wivrn_session> network_session;
std::atomic<bool> exiting = false;
std::thread network_thread;
std::mutex tracking_control_mutex;
to_headset::tracking_control tracking_control{};
thread_safe<to_headset::tracking_control> tracking_control{};
std::array<std::atomic<interaction_profile>, 2> interaction_profiles; // left and right hand
std::atomic<bool> interaction_profile_changed = false;
std::atomic<bool> recenter_requested = false;
std::atomic<XrDuration> display_time_phase = 0;
std::atomic<XrDuration> display_time_period = 0;
@@ -93,8 +100,6 @@ private:
vk::raii::DescriptorPool blit_descriptor_pool = nullptr;
vk::raii::RenderPass blit_render_pass = nullptr;
vk::Extent2D decoder_out_size;
vk::Format decoder_out_format;
image_allocation decoder_out_image;
struct renderpass_output
@@ -125,6 +130,8 @@ private:
std::optional<audio> audio_handle;
std::optional<xr::hand_tracker> left_hand;
std::optional<xr::hand_tracker> right_hand;
std::optional<input_profile> input;
static inline const uint32_t layer_controllers = 1 << 0;
static inline const uint32_t layer_rays = 1 << 1;
@@ -176,7 +183,7 @@ private:
std::vector<std::shared_ptr<wivrn::shard_accumulator::blit_handle>> current_blit_handles;
// Last application list received from server
thread_safe<to_headset::application_list> applications;
thread_safe<std::vector<app>> applications;
stream();
@@ -202,6 +209,7 @@ public:
void operator()(to_headset::video_stream_description &&);
void operator()(to_headset::refresh_rate_change &&);
void operator()(to_headset::application_list &&);
void operator()(to_headset::application_icon &&);
void operator()(audio_data &&);
void push_blit_handle(wivrn::shard_accumulator * decoder, std::shared_ptr<wivrn::shard_accumulator::blit_handle> handle);
+32 -6
View File
@@ -389,8 +389,14 @@ void scenes::stream::gui_performance_metrics()
ImPlot::PopStyleColor(5);
{
std::lock_guard lock(tracking_control_mutex);
ImGui::Text("%s", fmt::format(_F("Estimated motion to photons latency: {}ms"), std::chrono::duration_cast<std::chrono::milliseconds>(tracking_control.max_offset).count()).c_str());
ImGui::Text(
"%s",
fmt::format(
_F("Estimated motion to photons latency: {}ms"),
std::chrono::duration_cast<std::chrono::milliseconds>(
tracking_control.lock()->max_offset)
.count())
.c_str());
if (is_gui_interactable())
ImGui::Text("%s", _S("Press the grip button to move the window"));
@@ -417,7 +423,12 @@ void scenes::stream::gui_compact_view()
f(_S("Upload"), 8 * compact_bandwidth_tx * 1e-6, "Mbit/s");
f(_S("CPU time"), compact_cpu_time * 1000, "ms");
f(_S("GPU time"), compact_gpu_time * 1000, "ms");
f(_S("Motion to photon latency"), std::chrono::duration_cast<std::chrono::microseconds>(tracking_control.max_offset).count() * 1e-3f, "ms");
f(_S("Motion to photon latency"),
std::chrono::duration_cast<std::chrono::microseconds>(
tracking_control.lock()->max_offset)
.count() *
1e-3f,
"ms");
ImGui::EndTable();
}
}
@@ -615,6 +626,8 @@ static glm::vec4 compute_ray_limits(const XrPosef & pose, float margin = 0)
void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period)
{
if (not(plots_toggle_1 and plots_toggle_2))
return;
bool interactable = true;
XrSpace world_space = application::space(xr::spaces::world);
auto views = session.locate_views(viewconfig, predicted_display_time, world_space).second;
@@ -632,6 +645,19 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte
break;
}
imgui_ctx->set_controllers_enabled(interactable);
if (interactable)
{
if (system.hand_tracking_supported())
{
left_hand = session.create_hand_tracker(XR_HAND_LEFT_EXT);
right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT);
}
}
else
{
left_hand.reset();
right_hand.reset();
}
if (gui_status != last_gui_status)
{
@@ -863,10 +889,10 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte
bool hide_left_controller = false;
bool hide_right_controller = false;
if (application::get_hand_tracking_supported())
if (left_hand and right_hand)
{
auto left = application::get_left_hand().locate(world_space, predicted_display_time);
auto right = application::get_right_hand().locate(world_space, predicted_display_time);
auto left = left_hand->locate(world_space, predicted_display_time);
auto right = right_hand->locate(world_space, predicted_display_time);
if (left and xr::hand_tracker::check_flags(*left, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
hide_left_controller = true;
+19 -1
View File
@@ -111,8 +111,26 @@ void scenes::stream::operator()(to_headset::application_list && apps)
std::ranges::sort(apps.applications, [](auto & l, auto & r) {
return una::casesens::collate_utf8(l.name, r.name) < 0;
});
auto locked = applications.lock();
*locked = std::move(apps);
locked->clear();
locked->reserve(apps.applications.size());
for (const auto & i: apps.applications)
{
locked->push_back(app{
.id = std::move(i.id),
.name = std::move(i.name),
});
}
}
void scenes::stream::operator()(to_headset::application_icon && icon)
{
auto locked = applications.lock();
if (auto it = std::ranges::find(*locked, icon.id, &app::id); it != locked->end())
it->image = std::move(icon.image);
}
void scenes::stream::start_application(std::string appid)
+97 -57
View File
@@ -24,6 +24,7 @@
#include "vk/allocation.h"
#include "vk/pipeline.h"
#include "vk/shader.h"
#include "vk/specialization_constants.h"
#include <array>
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
@@ -37,13 +38,7 @@ struct stream_reprojection::vertex
alignas(8) glm::vec2 position;
// input texture coordinates
alignas(8) glm::vec2 uv;
};
struct SgsrSpecializationConstants
{
VkBool32 use_edge_direction;
float edge_threshold;
float edge_sharpness;
int32_t shading_rate;
};
void stream_reprojection::ensure_vertices(size_t num_vertices)
@@ -74,16 +69,16 @@ stream_reprojection::vertex * stream_reprojection::get_vertices(size_t view)
stream_reprojection::stream_reprojection(
vk::raii::Device & device,
vk::raii::PhysicalDevice & physical_device,
vk::Image input_image_,
vk::Extent2D input_extent,
uint32_t view_count,
image_allocation & input_image,
std::vector<vk::Image> output_images_,
vk::Extent2D output_extent,
vk::Format format) :
view_count(view_count),
view_count(input_image.info().arrayLayers),
device(device),
input_image(input_image_),
input_extent(input_extent),
input_extent{
.width = input_image.info().extent.width,
.height = input_image.info().extent.height,
},
output_images(std::move(output_images_)),
output_extent(output_extent)
{
@@ -144,7 +139,7 @@ stream_reprojection::stream_reprojection(
vk::ImageViewCreateInfo iv_info{
.image = input_image,
.viewType = vk::ImageViewType::e2D,
.format = vk::Format::eA8B8G8R8SrgbPack32,
.format = input_image.info().format,
.components = {
.r = vk::ComponentSwizzle::eIdentity,
.g = vk::ComponentSwizzle::eIdentity,
@@ -219,50 +214,30 @@ stream_reprojection::stream_reprojection(
renderpass = vk::raii::RenderPass(device, renderpass_info.get());
// Vertex shader
vk::raii::ShaderModule vertex_shader = load_shader(device, "reprojection.vert");
// Fragment shader
std::string fragment_shader_name = "reprojection.frag";
vk::SpecializationInfo fragment_specialization_info;
SgsrSpecializationConstants fragment_specialization_constants;
std::vector<vk::SpecializationMapEntry> fragment_specialization_constants_desc;
const configuration::sgsr_settings sgsr = application::get_config().sgsr;
if (sgsr.enabled)
const auto & vk_device_extensions = application::get_vk_device_extensions();
bool fragment_shading_rate = false;
if (utils::contains(vk_device_extensions, VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME) and
utils::contains(vk_device_extensions, VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME))
{
fragment_shader_name = "reprojection_sgsr.frag";
fragment_specialization_constants = {
.use_edge_direction = sgsr.use_edge_direction,
.edge_threshold = float(sgsr.edge_threshold / 255.0),
.edge_sharpness = sgsr.edge_sharpness,
};
fragment_specialization_constants_desc = {
vk::SpecializationMapEntry{
.constantID = 0,
.offset = offsetof(SgsrSpecializationConstants, use_edge_direction),
.size = sizeof(fragment_specialization_constants.use_edge_direction),
},
vk::SpecializationMapEntry{
.constantID = 1,
.offset = offsetof(SgsrSpecializationConstants, edge_threshold),
.size = sizeof(fragment_specialization_constants.edge_threshold),
},
vk::SpecializationMapEntry{
.constantID = 2,
.offset = offsetof(SgsrSpecializationConstants, edge_sharpness),
.size = sizeof(fragment_specialization_constants.edge_sharpness),
},
};
fragment_specialization_info.setMapEntries(fragment_specialization_constants_desc);
fragment_specialization_info.setDataSize(sizeof(fragment_specialization_constants));
fragment_specialization_info.setPData(&fragment_specialization_constants);
const auto & [prop, rate_prop] = physical_device.getProperties2<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceFragmentShadingRatePropertiesKHR>();
const auto & [feat, fragment_feat] = physical_device.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceFragmentShadingRateFeaturesKHR>();
fragment_shading_rate = rate_prop.fragmentShadingRateNonTrivialCombinerOps and
fragment_feat.primitiveFragmentShadingRate and
fragment_feat.attachmentFragmentShadingRate;
}
vk::raii::ShaderModule fragment_shader = load_shader(device, fragment_shader_name);
// Vertex shader
vk::raii::ShaderModule vertex_shader = load_shader(device, fragment_shading_rate ? "reprojection_vsr.vert" : "reprojection.vert");
// Fragment shader
const configuration::sgsr_settings & sgsr = application::get_config().sgsr;
auto sgsr_specialization = make_specialization_constants(
VkBool32(sgsr.use_edge_direction),
float(sgsr.edge_threshold / 255.f),
float(sgsr.edge_sharpness));
vk::raii::ShaderModule fragment_shader = load_shader(device, sgsr.enabled ? "reprojection_sgsr.frag" : "reprojection.frag");
// Create graphics pipeline
vk::PipelineLayoutCreateInfo pipeline_layout_info;
@@ -270,7 +245,7 @@ stream_reprojection::stream_reprojection(
layout = vk::raii::PipelineLayout(device, pipeline_layout_info);
vk::pipeline_builder pipeline_info{
vk::pipeline_builder pipeline_info_builder{
.flags = {},
.Stages = {
{
@@ -282,7 +257,7 @@ stream_reprojection::stream_reprojection(
.stage = vk::ShaderStageFlagBits::eFragment,
.module = *fragment_shader,
.pName = "main",
.pSpecializationInfo = &fragment_specialization_info,
.pSpecializationInfo = sgsr.enabled ? sgsr_specialization.info() : nullptr,
},
},
.VertexBindingDescriptions = {
@@ -305,6 +280,12 @@ stream_reprojection::stream_reprojection(
.format = vk::Format::eR32G32Sfloat,
.offset = offsetof(vertex, uv),
},
{
.location = 2,
.binding = 0,
.format = vk::Format::eR32Sint,
.offset = offsetof(vertex, shading_rate),
},
},
.InputAssemblyState = {{
.topology = vk::PrimitiveTopology::eTriangleStrip,
@@ -337,6 +318,48 @@ stream_reprojection::stream_reprojection(
.subpass = 0,
};
vk::GraphicsPipelineCreateInfo pipeline_info(pipeline_info_builder);
// Variable fragment shading
std::array combiner{
vk::FragmentShadingRateCombinerOpKHR::eMax,
vk::FragmentShadingRateCombinerOpKHR::eMax,
};
vk::PipelineFragmentShadingRateStateCreateInfoKHR shading{
.fragmentSize = vk::Extent2D{1, 1},
.combinerOps = combiner,
};
if (fragment_shading_rate)
{
pipeline_info.pNext = &shading;
spdlog::info("Available fragment shading rates:");
for (const auto rate: physical_device.getFragmentShadingRatesKHR())
{
if (rate.sampleCounts & vk::SampleCountFlagBits::e1)
{
spdlog::info("\tfragment size: {}x{}", rate.fragmentSize.width, rate.fragmentSize.height);
int flags = 0;
if (rate.fragmentSize.width == 4)
flags |= 8;
else if (rate.fragmentSize.width == 2)
flags |= 4;
if (rate.fragmentSize.height == 4)
flags |= 2;
else if (rate.fragmentSize.height == 2)
flags |= 1;
for (int y = std::bit_width(rate.fragmentSize.height) - 1; y < 3; ++y)
{
for (int x = std::bit_width(rate.fragmentSize.width) - 1; x < 3; ++x)
{
if (fragment_sizes[x][y] == 0)
fragment_sizes[x][y] = flags;
}
}
}
}
}
pipeline = vk::raii::Pipeline(device, application::get_pipeline_cache(), pipeline_info);
// Create image views and framebuffers
@@ -390,6 +413,21 @@ static size_t required_vertices(const wivrn::to_headset::foveation_parameter & p
return (2 * (p.x.size() + 1) + 1) * p.y.size();
}
int32_t stream_reprojection::shading_rate(int pixels_x, int pixels_y)
{
int x = 0;
if (pixels_x >= 2)
x = 1;
if (pixels_x >= 4)
x = 2;
int y = 0;
if (pixels_y >= 2)
y = 1;
if (pixels_y >= 4)
y = 2;
return fragment_sizes[x][y];
}
void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer,
const std::array<wivrn::to_headset::foveation_parameter, 2> & foveation,
int destination)
@@ -425,10 +463,12 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer,
*vertices++ = {
.position = out * out_pixel_size - glm::vec2(1),
.uv = in * in_pixel_size,
.shading_rate = shading_rate(ratio_x, ratio_y),
};
*vertices++ = {
.position = (out + glm::vec2(0, n_out_y * ratio_y)) * out_pixel_size - glm::vec2(1),
.uv = (in + glm::vec2(0, n_out_y)) * in_pixel_size,
.shading_rate = shading_rate(ratio_x, ratio_y),
};
in.x += n_out_x;
out.x += n_out_x * ratio_x;
+10 -4
View File
@@ -41,9 +41,15 @@ class stream_reprojection
vk::raii::RenderPass renderpass = nullptr;
vk::raii::Pipeline pipeline = nullptr;
// Allowed sizes for variable shading rate
// indices are for x, y
// 0 is 1 pixel
// 1 is 2 or 3 pixels
// 2 is 4 pixels or more
int32_t fragment_sizes[3][3] = {};
// Source image
vk::raii::Sampler sampler = nullptr;
vk::Image input_image;
std::vector<vk::raii::ImageView> input_image_views;
std::vector<vk::DescriptorSet> descriptor_sets;
vk::Extent2D input_extent;
@@ -57,13 +63,13 @@ class stream_reprojection
void ensure_vertices(size_t num_vertices);
vertex * get_vertices(size_t view);
int32_t shading_rate(int pixels_x, int pixels_y);
public:
stream_reprojection(
vk::raii::Device & device,
vk::raii::PhysicalDevice & physical_device,
vk::Image input_image,
vk::Extent2D input_extent,
uint32_t view_count,
image_allocation & input_image,
std::vector<vk::Image> output_images,
vk::Extent2D output_extent,
vk::Format format);
+81 -63
View File
@@ -21,7 +21,10 @@
#include "stream.h"
#include "utils/overloaded.h"
#include "wivrn_packets.h"
#include "xr/body_tracker.h"
#include "xr/face_tracker.h"
#include "xr/fb_body_tracker.h"
#include "xr/to_string.h"
#include <magic_enum.hpp>
#include <ranges>
#include <spdlog/spdlog.h>
@@ -377,7 +380,6 @@ void scenes::stream::tracking()
std::vector<from_headset::tracking> tracking;
std::vector<from_headset::tracking> tracking_pool; // pre-allocated objects
std::vector<from_headset::hand_tracking> hands;
std::array<bool, 2> should_skip_simple_controllers{};
std::vector<from_headset::body_tracking> body;
std::vector<XrView> views;
@@ -385,31 +387,14 @@ void scenes::stream::tracking()
std::vector<serialization_packet> packets;
const bool hand_tracking = config.check_feature(feature::hand_tracking);
std::optional<xr::hand_tracker> left_hand;
std::optional<xr::hand_tracker> right_hand;
const bool face_tracking = config.check_feature(feature::face_tracking);
auto & face_tracker = application::get_face_tracker();
if (face_tracking)
{
// We can't start the face tracking on application initialisation like we do for
// other face trackers due to a Pico runtime bug where face tracking freezes when
// the headset is taken off or the application is quit.
if (auto * face_pico = std::get_if<xr::pico_face_tracker>(&face_tracker))
face_pico->start();
}
xr::face_tracker face_tracker;
const bool body_tracking = config.check_feature(feature::body_tracking);
auto & body_tracker = application::get_body_tracker();
if (body_tracking)
{
if (auto * body_fb = std::get_if<xr::fb_body_tracker>(&body_tracker))
{
// TODO maybe handle reconnection better, if the settings are changed since
// last connection to running server, the tracker count will mismatch and
// stuff might break
body_fb->stop();
body_fb->start(config.fb_lower_body, config.fb_hip);
}
}
xr::body_tracker body_tracker;
locate_spaces_functor locate_spaces{instance, world_space};
@@ -433,11 +418,50 @@ void scenes::stream::tracking()
timer t(instance);
int samples = 0;
to_headset::tracking_control control;
auto control = *tracking_control.lock();
bool interaction_profile_changed = this->interaction_profile_changed.exchange(false);
if (control.enabled[size_t(tid::left_hand)])
{
std::lock_guard lock(tracking_control_mutex);
control = tracking_control;
if (not left_hand and system.hand_tracking_supported())
left_hand = session.create_hand_tracker(XR_HAND_LEFT_EXT);
}
else
left_hand.reset();
if (control.enabled[size_t(tid::right_hand)])
{
if (not right_hand and system.hand_tracking_supported())
right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT);
}
else
right_hand.reset();
if (face_tracking and control.enabled[size_t(tid::face)])
{
if (std::holds_alternative<std::monostate>(face_tracker))
face_tracker = xr::make_face_tracker(instance, system, session);
}
else
face_tracker.emplace<std::monostate>();
if (body_tracking and control.enabled[size_t(tid::generic_tracker)])
{
if (std::holds_alternative<std::monostate>(body_tracker))
body_tracker = xr::make_body_tracker(
instance,
system,
session,
application::get_generic_trackers(),
config.fb_lower_body,
config.fb_hip);
}
else
body_tracker.emplace<std::monostate>();
if (interaction_profile_changed)
if (auto htc = std::get_if<xr::htc_body_tracker>(&body_tracker))
htc->update_active();
XrDuration prediction = std::clamp<XrDuration>(control.max_offset.count(), 0, 80'000'000);
auto period = std::max<XrDuration>(display_time_period.load(), 1'000'000);
@@ -469,9 +493,9 @@ void scenes::stream::tracking()
(Δt == 0 or Δt >= prediction - 2 * period))
{
last_hand_sample = t0;
if (control.enabled[size_t(tid::left_hand)])
if (left_hand)
{
auto joints = locate_hands(application::get_left_hand(), world_space, t0 + Δt);
auto joints = locate_hands(*left_hand, world_space, t0 + Δt);
hands.emplace_back(
t0,
t0 + Δt,
@@ -479,9 +503,9 @@ void scenes::stream::tracking()
joints);
}
if (control.enabled[size_t(tid::right_hand)])
if (right_hand)
{
auto joints = locate_hands(application::get_right_hand(), world_space, t0 + Δt);
auto joints = locate_hands(*right_hand, world_space, t0 + Δt);
hands.emplace_back(
t0,
t0 + Δt,
@@ -498,37 +522,33 @@ void scenes::stream::tracking()
}
locate_spaces.resolve(session, t0 + Δt, packet.device_poses);
if (body_tracking)
std::visit(utils::overloaded{
[](std::monostate &) {},
[&](auto & b) {
if (t0 >= last_body_sample + period and
(Δt == 0 or Δt >= prediction - 2 * period))
std::visit(utils::overloaded{
[](std::monostate &) {},
[&](auto & b) {
if (t0 >= last_body_sample + period and
(Δt == 0 or Δt >= prediction - 2 * period))
{
last_body_sample = t0;
if (control.enabled[size_t(tid::generic_tracker)])
{
last_body_sample = t0;
if (control.enabled[size_t(tid::generic_tracker)])
{
body.push_back(from_headset::body_tracking{
.production_timestamp = t0,
.timestamp = t0 + Δt,
.poses = b.locate_spaces(t0 + Δt, world_space),
});
}
body.push_back(from_headset::body_tracking{
.production_timestamp = t0,
.timestamp = t0 + Δt,
.poses = b.locate_spaces(t0 + Δt, world_space),
});
}
},
},
body_tracker);
}
},
},
body_tracker);
if (face_tracking && control.enabled[size_t(tid::face)])
{
std::visit(utils::overloaded{
[](std::monostate &) {},
[&](auto & ft) {
ft.get_weights(t0 + Δt, packet.face.emplace<typename std::remove_reference_t<decltype(ft)>::packet_type>());
},
},
face_tracker);
}
std::visit(utils::overloaded{
[](std::monostate &) {},
[&](auto & ft) {
ft.get_weights(t0 + Δt, packet.face.emplace<typename std::remove_reference_t<decltype(ft)>::packet_type>());
},
},
face_tracker);
}
catch (const std::system_error & e)
{
@@ -629,20 +649,17 @@ void scenes::stream::tracking()
exit();
}
}
if (auto * face_pico = std::get_if<xr::pico_face_tracker>(&face_tracker))
face_pico->stop();
}
void scenes::stream::operator()(to_headset::tracking_control && packet)
{
std::lock_guard lock(tracking_control_mutex);
auto locked = tracking_control.lock();
auto m = size_t(to_headset::tracking_control::id::microphone);
if (audio_handle)
audio_handle->set_mic_state(packet.enabled[m]);
tracking_control = packet;
tracking_control.min_offset = std::min(tracking_control.min_offset, tracking_control.max_offset);
*locked = packet;
locked->min_offset = std::min(locked->min_offset, locked->max_offset);
}
static device_id derived_from(device_id target)
@@ -663,6 +680,7 @@ static device_id derived_from(device_id target)
void scenes::stream::on_interaction_profile_changed(const XrEventDataInteractionProfileChanged &)
{
interaction_profile_changed = true;
std::array path = {
"/user/hand/left",
"/user/hand/right",