From fd6b9f539a091abf48c76649a571a2bb40a6de88 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 5 Apr 2024 15:21:12 +0200 Subject: [PATCH] Fast-path 0-amplitude haptics --- client/scenes/stream.cpp | 8 ++++---- client/scenes/stream.h | 8 +++++++- client/scenes/stream_actions.cpp | 18 ++++++++++-------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 82000b8f..de4a4d5a 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -132,11 +132,11 @@ std::shared_ptr scenes::stream::create(std::unique_ptrfence = self->device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled}); // Look up the XrActions for haptics - self->haptics_actions[0].first = application::get_action("/user/hand/left/output/haptic").first; - self->haptics_actions[0].second = application::string_to_path("/user/hand/left"); + self->haptics_actions[0].action = application::get_action("/user/hand/left/output/haptic").first; + self->haptics_actions[0].path = application::string_to_path("/user/hand/left"); - self->haptics_actions[1].first = application::get_action("/user/hand/right/output/haptic").first; - self->haptics_actions[1].second = application::string_to_path("/user/hand/right"); + self->haptics_actions[1].action = application::get_action("/user/hand/right/output/haptic").first; + self->haptics_actions[1].path = application::string_to_path("/user/hand/right"); // Look up the XrActions for input for (const auto & [action, action_type, name]: application::inputs()) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 39328f95..acd9ddbf 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -94,7 +94,13 @@ private: vk::raii::Fence fence = nullptr; vk::raii::CommandBuffer command_buffer = nullptr; - std::array, 2> haptics_actions; + struct haptics_action + { + XrAction action; + XrPath path; + std::atomic amplitude; + }; + std::array haptics_actions; std::vector> input_actions; state state_ = state::initializing; diff --git a/client/scenes/stream_actions.cpp b/client/scenes/stream_actions.cpp index 2889c0d3..512c4128 100644 --- a/client/scenes/stream_actions.cpp +++ b/client/scenes/stream_actions.cpp @@ -70,19 +70,21 @@ void scenes::stream::read_actions() void scenes::stream::operator()(to_headset::haptics && haptics) { - XrAction action; - + size_t i; if (haptics.id == device_id::LEFT_CONTROLLER_HAPTIC) - { - action = haptics_actions[0].first; - } + i = 0; else if (haptics.id == device_id::RIGHT_CONTROLLER_HAPTIC) - { - action = haptics_actions[1].first; - } + i = 1; else return; + XrAction action = haptics_actions[i].action; + float old = haptics_actions[i].amplitude.exchange(haptics.amplitude); + // Some runtimes may be slow to process actions + // Skip it if not necessary + if (old == 0 and haptics.amplitude == 0) + return; + if (haptics.amplitude > 0) application::haptic_start(action, XR_NULL_PATH, haptics.duration.count(), haptics.frequency, std::min(1.0f, haptics.amplitude)); else