From 31c347475e4c84f1c7e6ab14b1fe045bf252ddf9 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 15 Aug 2025 23:16:53 +0200 Subject: [PATCH 1/8] Cleanup some redundant variables --- client/scenes/stream.cpp | 15 +++++++-------- client/scenes/stream.h | 2 -- client/scenes/stream_reprojection.cpp | 14 +++++++------- client/scenes/stream_reprojection.h | 5 +---- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 7dd945a3..9db56dbb 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -1008,6 +1008,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, @@ -1316,10 +1320,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, @@ -1343,7 +1344,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, @@ -1367,8 +1368,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, }); } @@ -1435,8 +1436,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()); diff --git a/client/scenes/stream.h b/client/scenes/stream.h index aa28435a..3c227e90 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -101,8 +101,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 diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index deddb0b2..91443138 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -74,16 +74,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 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 +144,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, diff --git a/client/scenes/stream_reprojection.h b/client/scenes/stream_reprojection.h index 6854d4d6..49143149 100644 --- a/client/scenes/stream_reprojection.h +++ b/client/scenes/stream_reprojection.h @@ -43,7 +43,6 @@ class stream_reprojection // Source image vk::raii::Sampler sampler = nullptr; - vk::Image input_image; std::vector input_image_views; std::vector descriptor_sets; vk::Extent2D input_extent; @@ -61,9 +60,7 @@ 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 output_images, vk::Extent2D output_extent, vk::Format format); From 757e0fd97b9a2091fe15334cd9119939ea2d6118 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 15 Aug 2025 23:27:44 +0200 Subject: [PATCH 2/8] merge tracking control and its mutex --- client/scenes/stream.h | 3 +-- client/scenes/stream_gui.cpp | 17 ++++++++++++++--- client/scenes/stream_tracking.cpp | 12 ++++-------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 3c227e90..ac1bf2de 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -85,8 +85,7 @@ private: std::unique_ptr network_session; std::atomic exiting = false; std::thread network_thread; - std::mutex tracking_control_mutex; - to_headset::tracking_control tracking_control{}; + thread_safe tracking_control{}; std::array, 2> interaction_profiles; // left and right hand std::atomic recenter_requested = false; std::atomic display_time_phase = 0; diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 1164543f..e02b8719 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -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(tracking_control.max_offset).count()).c_str()); + ImGui::Text( + "%s", + fmt::format( + _F("Estimated motion to photons latency: {}ms"), + std::chrono::duration_cast( + 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(tracking_control.max_offset).count() * 1e-3f, "ms"); + f(_S("Motion to photon latency"), + std::chrono::duration_cast( + tracking_control.lock()->max_offset) + .count() * + 1e-3f, + "ms"); ImGui::EndTable(); } } diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index afc3c958..9ce2322b 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -433,11 +433,7 @@ void scenes::stream::tracking() timer t(instance); int samples = 0; - to_headset::tracking_control control; - { - std::lock_guard lock(tracking_control_mutex); - control = tracking_control; - } + auto control = *tracking_control.lock(); XrDuration prediction = std::clamp(control.max_offset.count(), 0, 80'000'000); auto period = std::max(display_time_period.load(), 1'000'000); @@ -636,13 +632,13 @@ void scenes::stream::tracking() 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) From 5784cbd4fa0b7970f70218dd5492ec37b9e84d87 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 15 Aug 2025 23:28:35 +0200 Subject: [PATCH 3/8] remove unused include --- client/scenes/stream.h | 1 - 1 file changed, 1 deletion(-) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index ac1bf2de..1b61d4ed 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -21,7 +21,6 @@ #include "audio/audio.h" #include "decoder/shard_accumulator.h" -#include "imgui.h" #include "render/imgui_impl.h" #include "scene.h" #include "scenes/input_profile.h" From b1d6d50a4a30a7897fe06129e650cc5f252e8ecc Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Fri, 15 Aug 2025 23:40:40 +0200 Subject: [PATCH 4/8] Replace unused frames() method with empty() --- client/scenes/stream.cpp | 9 ++++----- client/scenes/stream.h | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 9db56dbb..dccc3291 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -527,7 +527,7 @@ void scenes::stream::push_blit_handle(shard_accumulator * decoder, std::shared_p } 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; @@ -546,15 +546,14 @@ bool scenes::stream::accumulator_images::alpha() const return decoder->desc().channels == wivrn::to_headset::video_stream_description::channels_t::alpha; } -std::vector scenes::stream::accumulator_images::frames() const +bool scenes::stream::accumulator_images::empty() const { - std::vector result; for (const auto & frame: latest_frames) { if (frame) - result.push_back(frame->feedback.frame_index); + return false; } - return result; + return true; } std::vector> scenes::stream::common_frame(XrTime display_time) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 1b61d4ed..391d2a9b 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -67,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, image_buffer_size> latest_frames; std::shared_ptr frame(uint64_t id) const; bool alpha() const; - std::vector frames() const; + bool empty() const; }; wifi_lock::wifi wifi; From dc04f7edccce4cce1dba5d0385154b3277c45172 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 16 Aug 2025 10:28:45 +0200 Subject: [PATCH 5/8] Add utility to write specialization constants --- client/render/scene_renderer.cpp | 40 +++---------- client/scenes/stream.cpp | 49 +++------------- client/scenes/stream_reprojection.cpp | 54 +++-------------- common/vk/specialization_constants.h | 84 +++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 117 deletions(-) create mode 100644 common/vk/specialization_constants.h diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index d944c559..da28ec8f 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -28,6 +28,7 @@ #include "vk/allocation.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "vk/specialization_constants.h" #include #include #include @@ -535,34 +536,11 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) auto vertex_shader = load_shader(device, info.shader_name + ".vert"); auto fragment_shader = load_shader(device, info.shader_name + ".frag"); - std::array specialization_constants_desc{ - vk::SpecializationMapEntry{ - .constantID = 0, - .offset = offsetof(pipeline_info, nb_texcoords), - .size = sizeof(int32_t), - }, - vk::SpecializationMapEntry{ - .constantID = 1, - .offset = offsetof(pipeline_info, dithering), - .size = sizeof(VkBool32), - }, - vk::SpecializationMapEntry{ - .constantID = 2, - .offset = offsetof(pipeline_info, alpha_cutout), - .size = sizeof(VkBool32), - }, - vk::SpecializationMapEntry{ - .constantID = 3, - .offset = offsetof(pipeline_info, skinning), - .size = sizeof(VkBool32), - }}; - - vk::SpecializationInfo specialization{ - .mapEntryCount = specialization_constants_desc.size(), - .pMapEntries = specialization_constants_desc.data(), - .dataSize = sizeof(pipeline_info), - .pData = &info, - }; + auto specialization = make_specialization_constants( + int32_t(info.nb_texcoords), + VkBool32(info.dithering), + VkBool32(info.alpha_cutout), + VkBool32(info.skinning)); return vk::raii::Pipeline{ device, @@ -573,14 +551,14 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) .stage = vk::ShaderStageFlagBits::eVertex, .module = *vertex_shader, .pName = "main", - .pSpecializationInfo = &specialization, + .pSpecializationInfo = specialization, }, vk::PipelineShaderStageCreateInfo{ .stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main", - .pSpecializationInfo = &specialization, + .pSpecializationInfo = specialization, }, }, @@ -797,7 +775,7 @@ void scene_renderer::update_material_descriptor_set(renderer::material & materia device.updateDescriptorSets(write_ds, {}); } -static void print_scene_hierarchy(const entt::registry & scene, entt::entity root = entt::null, int level = 0) +[[maybe_unused]] static void print_scene_hierarchy(const entt::registry & scene, entt::entity root = entt::null, int level = 0) { if (level == 0) spdlog::info("Node hierarchy:"); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index dccc3291..50dca0a8 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -42,6 +42,7 @@ #include "utils/ranges.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "vk/specialization_constants.h" #include "wivrn_packets.h" #include #include @@ -804,51 +805,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(useful_size); - - std::array 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(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"); @@ -866,13 +835,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 = {}, diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index 91443138..6528ea13 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -24,6 +24,7 @@ #include "vk/allocation.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "vk/specialization_constants.h" #include #include #include @@ -39,13 +40,6 @@ struct stream_reprojection::vertex alignas(8) glm::vec2 uv; }; -struct SgsrSpecializationConstants -{ - VkBool32 use_edge_direction; - float edge_threshold; - float edge_sharpness; -}; - void stream_reprojection::ensure_vertices(size_t num_vertices) { vk::BufferCreateInfo create_info{ @@ -223,46 +217,14 @@ stream_reprojection::stream_reprojection( 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 fragment_specialization_constants_desc; - const configuration::sgsr_settings sgsr = application::get_config().sgsr; - if (sgsr.enabled) - { - fragment_shader_name = "reprojection_sgsr.frag"; + 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)); - 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); - } - - vk::raii::ShaderModule fragment_shader = load_shader(device, fragment_shader_name); + vk::raii::ShaderModule fragment_shader = load_shader(device, sgsr.enabled ? "reprojection_sgsr.frag" : "reprojection.frag"); // Create graphics pipeline vk::PipelineLayoutCreateInfo pipeline_layout_info; @@ -282,7 +244,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 = { diff --git a/common/vk/specialization_constants.h b/common/vk/specialization_constants.h new file mode 100644 index 00000000..a2d513ab --- /dev/null +++ b/common/vk/specialization_constants.h @@ -0,0 +1,84 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include +#include + +template +class specialization_constants +{ + std::tuple values; + std::array entries; + vk::SpecializationInfo info_; + + specialization_constants(specialization_constants &&) = delete; + specialization_constants(const specialization_constants &) = delete; + specialization_constants & operator=(const specialization_constants &) = delete; + specialization_constants & operator=(specialization_constants &&) = delete; + + template + void fill_entries() + { + entries[i] = vk::SpecializationMapEntry{ + .constantID = i, + .offset = uint32_t(uintptr_t(std::addressof(std::get(values))) - uintptr_t(std::addressof(values))), + .size = sizeof(std::get(values)), + }; + if constexpr (i > 0) + fill_entries(); + } + +public: + specialization_constants(Args &&... args) : + values(std::forward(args)...), + info_{ + .mapEntryCount = uint32_t(entries.size()), + .pMapEntries = entries.data(), + .dataSize = sizeof(values), + .pData = &values, + } + { + fill_entries(); + } + + operator vk::SpecializationInfo *() + { + return &info_; + } + + vk::SpecializationInfo * info() + { + return &info_; + } + + template + auto & data() + { + return std::get(values); + } +}; + +template +specialization_constants make_specialization_constants(Args &&... args) +{ + return specialization_constants(std::forward(args)...); +} From c15de9bf27113965561488ced8bff14fdcfe05ae Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 16 Aug 2025 10:54:30 +0200 Subject: [PATCH 6/8] Automatically name image/buffer allocations --- client/application.cpp | 14 +++-- client/render/text_rasterizer.cpp | 68 ++++++++++++------------- common/vk/allocation.h | 6 +++ common/vk/vk_allocator.cpp | 3 +- common/vk/vk_allocator.h | 3 +- server/driver/wivrn_comp_target.cpp | 7 ++- server/driver/wivrn_foveation.cpp | 1 - server/encoder/video_encoder_vulkan.cpp | 16 +++--- server/encoder/video_encoder_x264.cpp | 8 +-- server/utils/wivrn_vk_bundle.cpp | 11 ++-- 10 files changed, 75 insertions(+), 62 deletions(-) diff --git a/client/application.cpp b/client/application.cpp index b0433274..384dee76 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -902,10 +902,16 @@ void application::initialize_vulkan() pipeline_cache = vk::raii::PipelineCache(vk_device, pipeline_cache_info); allocator.emplace(VmaAllocatorCreateInfo{ - .physicalDevice = *vk_physical_device, - .device = *vk_device, - .instance = *vk_instance, - }); + .physicalDevice = *vk_physical_device, + .device = *vk_device, + .instance = *vk_instance, + }, +#ifndef NDEBUG + debug_utils_found +#else + false +#endif + ); } void application::log_views() diff --git a/client/render/text_rasterizer.cpp b/client/render/text_rasterizer.cpp index d206aa15..ac0f7a83 100644 --- a/client/render/text_rasterizer.cpp +++ b/client/render/text_rasterizer.cpp @@ -139,46 +139,42 @@ text_rasterizer::text_rasterizer(vk::raii::Device & device, vk::raii::PhysicalDe image_allocation text_rasterizer::create_image(vk::Extent2D size) { - image_allocation alloc{device, - vk::ImageCreateInfo{ - .imageType = vk::ImageType::e2D, - .format = text::format, - .extent = { - .width = size.width, - .height = size.height, - .depth = 1, - }, - .mipLevels = 1, - .arrayLayers = 1, - .samples = vk::SampleCountFlagBits::e1, - .tiling = vk::ImageTiling::eOptimal, - .usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst, - .sharingMode = vk::SharingMode::eExclusive, - .initialLayout = vk::ImageLayout::eUndefined, - }, - VmaAllocationCreateInfo{ - .flags = 0, - .usage = VMA_MEMORY_USAGE_AUTO, - }}; - - application::set_debug_reports_name(alloc, "text_rasterizer image"); - return alloc; + return {device, + vk::ImageCreateInfo{ + .imageType = vk::ImageType::e2D, + .format = text::format, + .extent = { + .width = size.width, + .height = size.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = vk::SampleCountFlagBits::e1, + .tiling = vk::ImageTiling::eOptimal, + .usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst, + .sharingMode = vk::SharingMode::eExclusive, + .initialLayout = vk::ImageLayout::eUndefined, + }, + VmaAllocationCreateInfo{ + .flags = 0, + .usage = VMA_MEMORY_USAGE_AUTO, + }, + "text_rasterizer image"}; } buffer_allocation text_rasterizer::create_buffer(size_t size) { - buffer_allocation alloc{device, - vk::BufferCreateInfo{ - .size = size, - .usage = vk::BufferUsageFlagBits::eTransferSrc, - }, - VmaAllocationCreateInfo{ - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, - .usage = VMA_MEMORY_USAGE_AUTO, - }}; - application::set_debug_reports_name(alloc, "text_rasterizer buffer"); - - return alloc; + return {device, + vk::BufferCreateInfo{ + .size = size, + .usage = vk::BufferUsageFlagBits::eTransferSrc, + }, + VmaAllocationCreateInfo{ + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }, + "text_rasterizer buffer"}; } text_rasterizer::~text_rasterizer() diff --git a/common/vk/allocation.h b/common/vk/allocation.h index e47242be..91c36d20 100644 --- a/common/vk/allocation.h +++ b/common/vk/allocation.h @@ -131,6 +131,12 @@ public: std::tie(resource, allocation) = traits::create(device, create_info, alloc_info); vmaSetAllocationName(vk_allocator::instance(), allocation, name.c_str()); + if (vk_allocator::instance().has_debug_utils) + device.setDebugUtilsObjectNameEXT(vk::DebugUtilsObjectNameInfoEXT{ + .objectType = T::objectType, + .objectHandle = uint64_t(CType(*resource)), + .pObjectName = name.c_str(), + }); } basic_allocation(const basic_allocation &) = delete; diff --git a/common/vk/vk_allocator.cpp b/common/vk/vk_allocator.cpp index 9ea62e63..27371fcd 100644 --- a/common/vk/vk_allocator.cpp +++ b/common/vk/vk_allocator.cpp @@ -20,7 +20,8 @@ #include "vk_allocator.h" #include "vk/check.h" -vk_allocator::vk_allocator(const VmaAllocatorCreateInfo & info) +vk_allocator::vk_allocator(const VmaAllocatorCreateInfo & info, bool has_debug_utils) : + has_debug_utils(has_debug_utils) { CHECK_VK(vmaCreateAllocator(&info, &handle)); } diff --git a/common/vk/vk_allocator.h b/common/vk/vk_allocator.h index 182ca114..ecb156e3 100644 --- a/common/vk/vk_allocator.h +++ b/common/vk/vk_allocator.h @@ -27,7 +27,8 @@ class vk_allocator : public singleton VmaAllocator handle = nullptr; public: - vk_allocator(const VmaAllocatorCreateInfo &); + const bool has_debug_utils; + vk_allocator(const VmaAllocatorCreateInfo &, bool has_debug_utils); ~vk_allocator(); operator VmaAllocator() diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 2c0aa5ed..b529e359 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -232,9 +232,12 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags for (uint32_t i = 0; i < cn->image_count; i++) { auto & image = cn->psc.images[i].image; - image = image_allocation(device, image_info.get(), {.usage = VMA_MEMORY_USAGE_AUTO}); + image = image_allocation( + device, + image_info.get(), + {.usage = VMA_MEMORY_USAGE_AUTO}, + std::format("comp target image {}", i)); cn->images[i].handle = image; - cn->wivrn_bundle->name(vk::Image(image), "comp target image"); } for (uint32_t i = 0; i < cn->image_count; i++) diff --git a/server/driver/wivrn_foveation.cpp b/server/driver/wivrn_foveation.cpp index 500605d2..747ef6e5 100644 --- a/server/driver/wivrn_foveation.cpp +++ b/server/driver/wivrn_foveation.cpp @@ -335,7 +335,6 @@ wivrn_foveation::wivrn_foveation(wivrn_vk_bundle & bundle, const xrt_hmd_parts & .usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST, }, "foveation staging buffer"); - bundle.name(vk::Buffer(host_buffer), "foveation staging buffer"); } bundle.name(command_pool, "foveation command pool"); bundle.name(cmd, "foveation command buffer"); diff --git a/server/encoder/video_encoder_vulkan.cpp b/server/encoder/video_encoder_vulkan.cpp index c18e21a9..9da1bc06 100644 --- a/server/encoder/video_encoder_vulkan.cpp +++ b/server/encoder/video_encoder_vulkan.cpp @@ -176,8 +176,8 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca img_create_info, { .usage = VMA_MEMORY_USAGE_AUTO, - }); - vk.name(vk::Image(dpb_image), "vulkan encoder DPB image"); + }, + "vulkan encoder DPB image"); } // Output buffer @@ -195,8 +195,8 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca { .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT, .usage = VMA_MEMORY_USAGE_AUTO, - }); - vk.name(vk::Buffer(item.output_buffer), "vulkan encode output buffer"); + }, + "vulkan encode output buffer"); if (not(item.output_buffer.properties() & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) { @@ -208,8 +208,8 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca { .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT, .usage = VMA_MEMORY_USAGE_AUTO, - }); - vk.name(vk::Buffer(item.host_buffer), "vulkan encode host buffer"); + }, + "vulkan encode host buffer"); } } @@ -292,8 +292,8 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca }, { .usage = VMA_MEMORY_USAGE_AUTO, - }); - vk.name(vk::Image(slot_data[i].tmp_image), "vulkan encoder temporary image"); + }, + "vulkan encoder temporary image"); image_view_template.image = vk::Image(slot_data[i].tmp_image); slot_data[i].view = vk.device.createImageView(image_view_template); vk.name(slot_data[i].view, "vulkan encoder temporary image view"); diff --git a/server/encoder/video_encoder_x264.cpp b/server/encoder/video_encoder_x264.cpp index fcb28162..048282df 100644 --- a/server/encoder/video_encoder_x264.cpp +++ b/server/encoder/video_encoder_x264.cpp @@ -168,7 +168,8 @@ video_encoder_x264::video_encoder_x264( { .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT, .usage = VMA_MEMORY_USAGE_AUTO, - }); + }, + "x264 luma buffer"); i.chroma = buffer_allocation( vk.device, { .size = vk::DeviceSize(settings.video_width * settings.video_height / 2), @@ -177,9 +178,8 @@ video_encoder_x264::video_encoder_x264( { .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT, .usage = VMA_MEMORY_USAGE_AUTO, - }); - vk.name(vk::Buffer(i.luma), "x264 luma buffer"); - vk.name(vk::Buffer(i.chroma), "x264 chroma buffer"); + }, + "x264 chroma buffer"); auto & pic = i.pic; x264_picture_init(&pic); diff --git a/server/utils/wivrn_vk_bundle.cpp b/server/utils/wivrn_vk_bundle.cpp index 5a01c2a9..5d9b688a 100644 --- a/server/utils/wivrn_vk_bundle.cpp +++ b/server/utils/wivrn_vk_bundle.cpp @@ -83,11 +83,12 @@ wivrn::wivrn_vk_bundle::wivrn_vk_bundle(vk_bundle & vk, std::span physical_device(instance, vk.physical_device), device(physical_device, vk.device), allocator({ - .physicalDevice = vk.physical_device, - .device = vk.device, - .instance = vk.instance, - .vulkanApiVersion = VK_MAKE_VERSION(1, 3, 0), // FIXME: sync with wivrn_session.cpp - }), + .physicalDevice = vk.physical_device, + .device = vk.device, + .instance = vk.instance, + .vulkanApiVersion = VK_MAKE_VERSION(1, 3, 0), // FIXME: sync with wivrn_session.cpp + }, + vk.has_EXT_debug_utils), queue(device, vk.queue_family_index, vk.queue_index), queue_family_index(vk.queue_family_index), #ifdef VK_KHR_video_encode_queue From b9052c6ebb20466b0d01afdf5fe294bfa281cdd0 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 16 Aug 2025 11:52:13 +0200 Subject: [PATCH 7/8] controller/hand_interaction: fix ODR, race condition --- server/driver/wivrn_controller.cpp | 149 ++++++++++++----------- server/driver/wivrn_controller.h | 7 ++ server/driver/wivrn_hand_interaction.cpp | 98 ++++----------- 3 files changed, 103 insertions(+), 151 deletions(-) diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index f5f6633f..796eb14c 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -37,6 +37,8 @@ namespace wivrn { +namespace +{ enum wivrn_controller_input_index { WIVRN_CONTROLLER_INPUT_INVALID = -1, @@ -98,7 +100,7 @@ struct input_data xrt_device_type device; }; -static input_data map_input(device_id id) +input_data map_input(device_id id) { switch (id) { @@ -247,18 +249,18 @@ static input_data map_input(device_id id) return {WIVRN_CONTROLLER_INPUT_INVALID, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_UNKNOWN}; } -static xrt_binding_input_pair simple_input_binding[] = { +xrt_binding_input_pair simple_input_binding[] = { {XRT_INPUT_SIMPLE_SELECT_CLICK, XRT_INPUT_TOUCH_TRIGGER_VALUE}, {XRT_INPUT_SIMPLE_MENU_CLICK, XRT_INPUT_TOUCH_MENU_CLICK}, {XRT_INPUT_SIMPLE_GRIP_POSE, XRT_INPUT_TOUCH_GRIP_POSE}, {XRT_INPUT_SIMPLE_AIM_POSE, XRT_INPUT_TOUCH_AIM_POSE}, }; -static xrt_binding_output_pair simple_output_binding[] = { +xrt_binding_output_pair simple_output_binding[] = { {XRT_OUTPUT_NAME_SIMPLE_VIBRATION, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_input_pair index_input_binding[] = { +xrt_binding_input_pair index_input_binding[] = { {XRT_INPUT_INDEX_SYSTEM_CLICK, XRT_INPUT_TOUCH_SYSTEM_CLICK}, {XRT_INPUT_INDEX_SYSTEM_TOUCH, XRT_INPUT_INDEX_SYSTEM_TOUCH}, {XRT_INPUT_INDEX_A_CLICK, XRT_INPUT_TOUCH_A_CLICK}, @@ -280,11 +282,11 @@ static xrt_binding_input_pair index_input_binding[] = { {XRT_INPUT_INDEX_AIM_POSE, XRT_INPUT_TOUCH_AIM_POSE}, }; -static xrt_binding_output_pair index_output_binding[] = { +xrt_binding_output_pair index_output_binding[] = { {XRT_OUTPUT_NAME_INDEX_HAPTIC, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_input_pair focus3_input_binding[] = { +xrt_binding_input_pair focus3_input_binding[] = { {XRT_INPUT_VIVE_FOCUS3_X_CLICK, XRT_INPUT_TOUCH_X_CLICK}, {XRT_INPUT_VIVE_FOCUS3_Y_CLICK, XRT_INPUT_TOUCH_Y_CLICK}, {XRT_INPUT_VIVE_FOCUS3_MENU_CLICK, XRT_INPUT_TOUCH_MENU_CLICK}, @@ -305,11 +307,11 @@ static xrt_binding_input_pair focus3_input_binding[] = { {XRT_INPUT_VIVE_FOCUS3_AIM_POSE, XRT_INPUT_TOUCH_AIM_POSE}, }; -static xrt_binding_output_pair focus3_output_binding[] = { +xrt_binding_output_pair focus3_output_binding[] = { {XRT_OUTPUT_NAME_VIVE_FOCUS3_HAPTIC, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_input_pair touch_pro_input_binding[] = { +xrt_binding_input_pair touch_pro_input_binding[] = { {XRT_INPUT_TOUCH_PRO_X_CLICK, XRT_INPUT_TOUCH_X_CLICK}, {XRT_INPUT_TOUCH_PRO_X_TOUCH, XRT_INPUT_TOUCH_X_TOUCH}, {XRT_INPUT_TOUCH_PRO_Y_CLICK, XRT_INPUT_TOUCH_Y_CLICK}, @@ -337,13 +339,13 @@ static xrt_binding_input_pair touch_pro_input_binding[] = { {XRT_INPUT_TOUCH_PRO_STYLUS_FORCE, XRT_INPUT_TOUCH_PRO_STYLUS_FORCE}, }; -static xrt_binding_output_pair touch_pro_output_binding[] = { +xrt_binding_output_pair touch_pro_output_binding[] = { {XRT_OUTPUT_NAME_TOUCH_PRO_HAPTIC, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, {XRT_OUTPUT_NAME_TOUCH_PRO_HAPTIC_THUMB, XRT_OUTPUT_NAME_TOUCH_PRO_HAPTIC_THUMB}, {XRT_OUTPUT_NAME_TOUCH_PRO_HAPTIC_TRIGGER, XRT_OUTPUT_NAME_TOUCH_PRO_HAPTIC_TRIGGER}, }; -static xrt_binding_input_pair touch_plus_input_binding[] = { +xrt_binding_input_pair touch_plus_input_binding[] = { {XRT_INPUT_TOUCH_PLUS_X_CLICK, XRT_INPUT_TOUCH_X_CLICK}, {XRT_INPUT_TOUCH_PLUS_X_TOUCH, XRT_INPUT_TOUCH_X_TOUCH}, {XRT_INPUT_TOUCH_PLUS_Y_CLICK, XRT_INPUT_TOUCH_Y_CLICK}, @@ -370,11 +372,11 @@ static xrt_binding_input_pair touch_plus_input_binding[] = { {XRT_INPUT_TOUCH_PLUS_TRIGGER_SLIDE, XRT_INPUT_TOUCH_PRO_TRIGGER_SLIDE}, }; -static xrt_binding_output_pair touch_plus_output_binding[] = { +xrt_binding_output_pair touch_plus_output_binding[] = { {XRT_OUTPUT_NAME_TOUCH_PLUS_HAPTIC, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_input_pair pico_neo3_input_binding[] = { +xrt_binding_input_pair pico_neo3_input_binding[] = { {XRT_INPUT_PICO_NEO3_X_CLICK, XRT_INPUT_TOUCH_X_CLICK}, {XRT_INPUT_PICO_NEO3_X_TOUCH, XRT_INPUT_TOUCH_X_TOUCH}, {XRT_INPUT_PICO_NEO3_Y_CLICK, XRT_INPUT_TOUCH_Y_CLICK}, @@ -397,11 +399,11 @@ static xrt_binding_input_pair pico_neo3_input_binding[] = { {XRT_INPUT_PICO_NEO3_B_TOUCH, XRT_INPUT_TOUCH_B_TOUCH}, }; -static xrt_binding_output_pair pico_neo3_output_binding[] = { +xrt_binding_output_pair pico_neo3_output_binding[] = { {XRT_OUTPUT_NAME_PICO_NEO3_HAPTIC, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_input_pair pico4_input_binding[] = { +xrt_binding_input_pair pico4_input_binding[] = { {XRT_INPUT_PICO4_X_CLICK, XRT_INPUT_TOUCH_X_CLICK}, {XRT_INPUT_PICO4_X_TOUCH, XRT_INPUT_TOUCH_X_TOUCH}, {XRT_INPUT_PICO4_Y_CLICK, XRT_INPUT_TOUCH_Y_CLICK}, @@ -424,11 +426,11 @@ static xrt_binding_input_pair pico4_input_binding[] = { {XRT_INPUT_PICO4_MENU_CLICK, XRT_INPUT_TOUCH_MENU_CLICK}, }; -static xrt_binding_output_pair pico4_output_binding[] = { +xrt_binding_output_pair pico4_output_binding[] = { {XRT_OUTPUT_NAME_PICO4_HAPTIC, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_profile wivrn_binding_profiles[] = { +xrt_binding_profile wivrn_binding_profiles[] = { { .name = XRT_DEVICE_SIMPLE_CONTROLLER, .inputs = simple_input_binding, @@ -480,13 +482,10 @@ static xrt_binding_profile wivrn_binding_profiles[] = { }, }; -namespace -{ struct xrt_space_relation_csv_header {}; -} // namespace -static std::ostream & operator<<(std::ostream & out, const xrt_space_relation_csv_header &) +std::ostream & operator<<(std::ostream & out, const xrt_space_relation_csv_header &) { for (auto [value, name]: magic_enum::enum_entries()) { @@ -503,44 +502,7 @@ static std::ostream & operator<<(std::ostream & out, const xrt_space_relation_cs out << "avx,avy,avz"; return out; } - -static std::ostream & operator<<(std::ostream & out, const xrt_space_relation & rel) -{ - const auto & pos = rel.pose.position; - const auto & o = rel.pose.orientation; - const auto & v = rel.linear_velocity; - const auto & av = rel.angular_velocity; - for (const auto & [value, name]: magic_enum::enum_entries()) - { - if (value and value != XRT_SPACE_RELATION_BITMASK_ALL) - out << bool(rel.relation_flags & value) << ','; - } - out << pos.x << ',' << pos.y << ',' << pos.z << ','; - out << o.w << ',' << o.x << ',' << o.y << ',' << o.z << ','; - out << v.x << ',' << v.y << ',' << v.z << ','; - out << av.x << ',' << av.y << ',' << av.z; - return out; -} - -static std::mutex tracking_dump_mutex; -static std::ofstream & tracking_dump() -{ - static auto res = [] { - std::ofstream res; - if (auto wivrn_dump = std::getenv("WIVRN_DUMP_TRACKING")) - { - res.open(wivrn_dump); - res << "device_id," - "now_ns," - "timestamp_ns," - "extrapolation_ns," - "receive/get," - << xrt_space_relation_csv_header{} << std::endl; - } - return res; - }(); - return res; -} +} // namespace wivrn_controller::wivrn_controller(int hand_id, xrt_device * hmd, @@ -760,9 +722,8 @@ xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_ return XRT_ERROR_INPUT_UNSUPPORTED; } cnx->add_predict_offset(extrapolation_time); - if (auto & out = tracking_dump()) + if (auto out = tracking_dump()) { - std::lock_guard lock{tracking_dump_mutex}; auto device = [&] { switch (name) { @@ -773,12 +734,12 @@ xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_ assert(false); __builtin_unreachable(); } }(); - out << magic_enum::enum_name(device) << ',' - << os_monotonic_get_ns() << ',' - << at_timestamp_ns << ',' - << extrapolation_time.count() << ',' - << "g," - << *res << std::endl; + *out->lock() << magic_enum::enum_name(device) << ',' + << os_monotonic_get_ns() << ',' + << at_timestamp_ns << ',' + << extrapolation_time.count() << ',' + << "g," + << *res << std::endl; ; } return XRT_SUCCESS; @@ -832,19 +793,19 @@ void wivrn_controller::update_tracking(const from_headset::tracking & tracking, cnx->set_enabled(grip.device, false); if (not palm.update_tracking(tracking, offset)) cnx->set_enabled(palm.device, false); - if (auto & out = tracking_dump(); out and offset) + if (auto out = tracking_dump(); out and offset) { - std::lock_guard lock{tracking_dump_mutex}; + auto locked = out->lock(); auto now = os_monotonic_get_ns(); for (const auto & pose: tracking.device_poses) { if (pose.device == aim.device or pose.device == grip.device or pose.device == palm.device) - out << magic_enum::enum_name(pose.device) << ',' - << now << ',' - << offset.from_headset(tracking.timestamp) << ',' - << tracking.timestamp - tracking.production_timestamp << ',' - << "r," - << pose_list::convert_pose(pose) << std::endl; + *locked << magic_enum::enum_name(pose.device) << ',' + << now << ',' + << offset.from_headset(tracking.timestamp) << ',' + << tracking.timestamp - tracking.production_timestamp << ',' + << "r," + << pose_list::convert_pose(pose) << std::endl; } } } @@ -895,4 +856,44 @@ void wivrn_controller::reset_history() aim.reset(); palm.reset(); } + +thread_safe * wivrn_controller::tracking_dump() +{ + static std::unique_ptr> res = [] { + if (auto wivrn_dump = std::getenv("WIVRN_DUMP_TRACKING")) + { + U_LOG_I("Controller tracking dump enabled: %s", wivrn_dump); + std::ofstream res; + res.open(wivrn_dump); + res << "device_id," + "now_ns," + "timestamp_ns," + "extrapolation_ns," + "receive/get," + << xrt_space_relation_csv_header{} << std::endl; + + return std::make_unique>(std::move(res)); + } + return std::unique_ptr>(); + }(); + return res.get(); +} } // namespace wivrn + +std::ostream & operator<<(std::ostream & out, const xrt_space_relation & rel) +{ + const auto & pos = rel.pose.position; + const auto & o = rel.pose.orientation; + const auto & v = rel.linear_velocity; + const auto & av = rel.angular_velocity; + for (const auto & [value, name]: magic_enum::enum_entries()) + { + if (value and value != XRT_SPACE_RELATION_BITMASK_ALL) + out << bool(rel.relation_flags & value) << ','; + } + out << pos.x << ',' << pos.y << ',' << pos.z << ','; + out << o.w << ',' << o.x << ',' << o.y << ',' << o.z << ','; + out << v.x << ',' << v.y << ',' << v.z << ','; + out << av.x << ',' << av.y << ',' << av.z; + return out; +} diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index 4a737e0b..c560e820 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -23,7 +23,9 @@ #include "hand_joints_list.h" #include "pose_list.h" +#include "utils/thread_safe.h" +#include #include #include @@ -64,5 +66,10 @@ public: void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &); void reset_history(); + + static thread_safe * tracking_dump(); }; + } // namespace wivrn + +std::ostream & operator<<(std::ostream & out, const xrt_space_relation & rel); diff --git a/server/driver/wivrn_hand_interaction.cpp b/server/driver/wivrn_hand_interaction.cpp index ff68e484..698d9ab4 100644 --- a/server/driver/wivrn_hand_interaction.cpp +++ b/server/driver/wivrn_hand_interaction.cpp @@ -31,13 +31,13 @@ #include #include -#include - #include "xrt/xrt_defines.h" namespace wivrn { +namespace +{ enum wivrn_hand_interaction_input_index { WIVRN_HAND_INTERACTION_INPUT_INVALID = -1, @@ -73,7 +73,7 @@ struct input_data xrt_device_type device; }; -static input_data map_input(device_id id) +input_data map_input(device_id id) { switch (id) { @@ -134,7 +134,7 @@ static input_data map_input(device_id id) return {WIVRN_HAND_INTERACTION_INPUT_INVALID, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_UNKNOWN}; } -static xrt_binding_input_pair hand_interaction_input_binding[] = { +xrt_binding_input_pair hand_interaction_input_binding[] = { {XRT_INPUT_HAND_GRIP_POSE, XRT_INPUT_HAND_GRIP_POSE}, {XRT_INPUT_HAND_AIM_POSE, XRT_INPUT_HAND_AIM_POSE}, {XRT_INPUT_HAND_PINCH_POSE, XRT_INPUT_HAND_PINCH_POSE}, @@ -147,13 +147,13 @@ static xrt_binding_input_pair hand_interaction_input_binding[] = { {XRT_INPUT_HAND_GRASP_READY, XRT_INPUT_HAND_GRASP_READY}, }; -static xrt_binding_input_pair simple_input_binding[] = { +xrt_binding_input_pair simple_input_binding[] = { {XRT_INPUT_SIMPLE_SELECT_CLICK, XRT_INPUT_HAND_PINCH_VALUE}, {XRT_INPUT_SIMPLE_GRIP_POSE, XRT_INPUT_HAND_GRIP_POSE}, {XRT_INPUT_SIMPLE_AIM_POSE, XRT_INPUT_HAND_AIM_POSE}, }; -static xrt_binding_profile wivrn_binding_profiles[] = { +xrt_binding_profile wivrn_binding_profiles[] = { { .name = XRT_DEVICE_EXT_HAND_INTERACTION, .inputs = hand_interaction_input_binding, @@ -169,63 +169,8 @@ static xrt_binding_profile wivrn_binding_profiles[] = { .output_count = 0, }, }; - -namespace -{ -struct xrt_space_relation_csv_header -{}; } // namespace -static std::ostream & operator<<(std::ostream & out, const xrt_space_relation_csv_header &) -{ - for (auto [value, name]: magic_enum::enum_entries()) - { - if (value and value != XRT_SPACE_RELATION_BITMASK_ALL) - { - name = name.substr(strlen("XRT_SPACE_RELATION_")); - name = name.substr(0, name.size() - strlen("_BIT")); - out << name << ","; - } - } - out << "x,y,z,"; - out << "qw,qx,qy,qz"; - return out; -} - -static std::ostream & operator<<(std::ostream & out, const xrt_space_relation & rel) -{ - const auto & pos = rel.pose.position; - const auto & o = rel.pose.orientation; - for (const auto & [value, name]: magic_enum::enum_entries()) - { - if (value and value != XRT_SPACE_RELATION_BITMASK_ALL) - out << bool(rel.relation_flags & value) << ','; - } - out << pos.x << ',' << pos.y << ',' << pos.z << ','; - out << o.w << ',' << o.x << ',' << o.y << ',' << o.z; - return out; -} - -static std::mutex tracking_dump_mutex; -static std::ofstream & tracking_dump() -{ - static auto res = [] { - std::ofstream res; - if (auto wivrn_dump = std::getenv("WIVRN_DUMP_TRACKING")) - { - res.open(wivrn_dump); - res << "device_id," - "now_ns," - "timestamp_ns," - "extrapolation_ns," - "receive/get," - << xrt_space_relation_csv_header{} << std::endl; - } - return res; - }(); - return res; -} - wivrn_hand_interaction::wivrn_hand_interaction(int hand_id, xrt_device * hmd, wivrn::wivrn_session * cnx) : @@ -410,9 +355,8 @@ xrt_result_t wivrn_hand_interaction::get_tracked_pose(xrt_input_name name, int64 return XRT_ERROR_INPUT_UNSUPPORTED; } cnx->add_predict_offset(extrapolation_time); - if (auto & out = tracking_dump()) + if (auto out = wivrn_controller::tracking_dump()) { - std::lock_guard lock{tracking_dump_mutex}; auto device = [&] { switch (name) { @@ -425,12 +369,12 @@ xrt_result_t wivrn_hand_interaction::get_tracked_pose(xrt_input_name name, int64 assert(false); __builtin_unreachable(); } }(); - out << magic_enum::enum_name(device) << ',' - << os_monotonic_get_ns() << ',' - << at_timestamp_ns << ',' - << extrapolation_time.count() << ',' - << "g," - << *res << std::endl; + *out->lock() << magic_enum::enum_name(device) << ',' + << os_monotonic_get_ns() << ',' + << at_timestamp_ns << ',' + << extrapolation_time.count() << ',' + << "g," + << *res << std::endl; ; } return XRT_SUCCESS; @@ -468,19 +412,19 @@ void wivrn_hand_interaction::update_tracking(const from_headset::tracking & trac cnx->set_enabled(pinch_ext.device, false); if (not poke_ext.update_tracking(tracking, offset)) cnx->set_enabled(poke_ext.device, false); - if (auto & out = tracking_dump(); out and offset) + if (auto out = wivrn_controller::tracking_dump(); out and offset) { - std::lock_guard lock{tracking_dump_mutex}; auto now = os_monotonic_get_ns(); + auto locked = out->lock(); for (const auto & pose: tracking.device_poses) { if (pose.device == aim.device or pose.device == grip.device or pose.device == palm.device) - out << magic_enum::enum_name(pose.device) << ',' - << now << ',' - << offset.from_headset(tracking.timestamp) << ',' - << tracking.timestamp - tracking.production_timestamp << ',' - << "r," - << pose_list::convert_pose(pose) << std::endl; + *locked << magic_enum::enum_name(pose.device) << ',' + << now << ',' + << offset.from_headset(tracking.timestamp) << ',' + << tracking.timestamp - tracking.production_timestamp << ',' + << "r," + << pose_list::convert_pose(pose) << std::endl; } } } From bce3e498e8e5fee7f42c3b44ecdd7cecab75c0af Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 16 Aug 2025 12:08:58 +0200 Subject: [PATCH 8/8] Fix destruction of textures still in use --- client/render/imgui_impl.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 53d59862..1f762af9 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -1099,6 +1099,13 @@ ImTextureID imgui_context::load_texture(const std::string & filename) void imgui_context::free_texture(ImTextureID texture) { + // Make sure we are done using the texture + std::vector fences; + for (auto & f: command_buffers) + fences.push_back(*f.fence); + if (auto result = device.waitForFences(fences, true, 1'000'000'000); result != vk::Result::eSuccess) + spdlog::error("vkWaitForfences: {}", vk::to_string(result)); + textures.erase(texture); }