From ecf6dfd215c3d7d6ca3926f0852b1f28d876da9f Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 6 Jul 2025 21:03:57 +0200 Subject: [PATCH] Reallocate swapchains if a bigger one is needed --- client/scenes/stream.cpp | 54 ++++++++++++++++++++------- client/scenes/stream.h | 2 +- client/scenes/stream_reprojection.cpp | 37 +++++++++++------- client/scenes/stream_reprojection.h | 4 +- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index fc226175..352350f9 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -391,7 +391,10 @@ void scenes::stream::on_focused() } assert(video_stream_description); - setup_reprojection_swapchain(); + std::unique_lock lock(decoder_mutex); + setup_reprojection_swapchain( + video_stream_description->defoveated_width / view_count, + video_stream_description->defoveated_height); } void scenes::stream::on_unfocused() @@ -755,8 +758,6 @@ void scenes::stream::render(const XrFrameState & frame_state) session.begin_frame(); - int image_index = swapchain.acquire(); - swapchain.wait(); std::array image_indices; command_buffer.reset(); @@ -896,15 +897,43 @@ void scenes::stream::render(const XrFrameState & frame_state) command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 1); - // defoveate the image - auto extents = reprojector->reproject(command_buffer, foveation, image_index); - for (size_t i = 0; i < view_count; ++i) + XrExtent2Di extents[view_count]; { - extents[i] = { - .width = std::min(extents[i].width, swapchain.width()), - .height = std::min(extents[i].height, swapchain.height()), - }; + int32_t max_width = 0; + int32_t max_height = 0; + for (size_t i = 0; i < view_count; ++i) + { + extents[i] = reprojector->defoveated_size(foveation[i]); + max_width = std::max(max_width, extents[i].width); + max_height = std::max(max_height, extents[i].height); + } + // If the defoveated image is larger than the swapchain, try to reallocate one + if (swapchain.width() < max_width or swapchain.height() < max_height) + { + try + { + spdlog::info("Recreating swapchain, from {}x{} to {}x{}", + swapchain.width(), + swapchain.height(), + max_width, + max_height); + setup_reprojection_swapchain(max_width, max_height); + } + catch (std::exception & e) + { + spdlog::warn("failed to increase swapchain size"); + for (size_t i = 0; i < view_count; ++i) + { + extents[i].width = std::min(extents[i].width, swapchain.width()); + extents[i].height = std::min(extents[i].height, swapchain.height()); + } + } + } } + // defoveate the image + int image_index = swapchain.acquire(); + swapchain.wait(); + reprojector->reproject(command_buffer, foveation, image_index); command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 2); @@ -1191,16 +1220,13 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript } } -void scenes::stream::setup_reprojection_swapchain() +void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint32_t swapchain_height) { - std::unique_lock lock(decoder_mutex); device.waitIdle(); session.set_refresh_rate(video_stream_description->fps); const uint32_t video_width = video_stream_description->width / view_count; const uint32_t video_height = video_stream_description->height; - uint32_t swapchain_width = video_stream_description->defoveated_width / view_count; - uint32_t swapchain_height = video_stream_description->defoveated_height; const configuration::sgsr_settings sgsr = application::get_config().sgsr; if (sgsr.enabled) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index d841dbe5..40e94714 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -180,7 +180,7 @@ private: void on_interaction_profile_changed(const XrEventDataInteractionProfileChanged &); void setup(const to_headset::video_stream_description &); - void setup_reprojection_swapchain(); + void setup_reprojection_swapchain(uint32_t width, uint32_t height); void exit(); vk::raii::QueryPool query_pool = nullptr; diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index eae19ffb..deddb0b2 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -390,15 +390,14 @@ static size_t required_vertices(const wivrn::to_headset::foveation_parameter & p return (2 * (p.x.size() + 1) + 1) * p.y.size(); } -std::vector stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, - const std::array & foveation, - int destination) +void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, + const std::array & foveation, + int destination) { if (destination < 0 || destination >= (int)output_images.size()) throw std::runtime_error("Invalid destination image index"); ensure_vertices(std::max(required_vertices(foveation[0]), required_vertices(foveation[1]))); - std::vector out_extents(view_count, XrExtent2Di{}); for (size_t view = 0; view < view_count; ++view) { @@ -409,13 +408,6 @@ std::vector stream_reprojection::reproject(vk::raii::CommandBuffer const int n_ratio_y = (py.size() - 1) / 2; const int n_ratio_x = (px.size() - 1) / 2; - for (auto [ix, n_out_x]: utils::enumerate_range(px)) - { - // number of output pixels per source pixels - const int ratio_x = std::abs(n_ratio_x - int(ix)) + 1; - out_extents[view].width += ratio_x * n_out_x; - } - glm::vec2 in(0), out(0); // pixel coordinates glm::vec2 in_pixel_size(1. / input_extent.width, 1. / input_extent.height); @@ -425,7 +417,6 @@ std::vector stream_reprojection::reproject(vk::raii::CommandBuffer { // number of output pixels per source pixels const int ratio_y = std::abs(n_ratio_y - int(iy)) + 1; - out_extents[view].height += ratio_y * n_out_y; in.x = 0; out.x = 0; for (auto [ix, n_out_x]: utils::enumerate_range(px)) @@ -496,5 +487,25 @@ std::vector stream_reprojection::reproject(vk::raii::CommandBuffer command_buffer.draw(required_vertices(foveation[view]), 1, 0, 0); command_buffer.endRenderPass(); } - return out_extents; +} + +static uint16_t count_pixels(const std::vector & param) +{ + uint16_t res = 0; + const int n_ratio = (param.size() - 1) / 2; + for (auto [i, n_out]: utils::enumerate_range(param)) + { + // number of output pixels per source pixels + const int ratio = std::abs(n_ratio - int(i)) + 1; + res += ratio * n_out; + } + return res; +} + +XrExtent2Di stream_reprojection::defoveated_size(const wivrn::to_headset::foveation_parameter & view) const +{ + return { + count_pixels(view.x), + count_pixels(view.y), + }; } diff --git a/client/scenes/stream_reprojection.h b/client/scenes/stream_reprojection.h index 72ef1ad8..6854d4d6 100644 --- a/client/scenes/stream_reprojection.h +++ b/client/scenes/stream_reprojection.h @@ -70,8 +70,10 @@ public: stream_reprojection(const stream_reprojection &) = delete; - std::vector reproject( + void reproject( vk::raii::CommandBuffer & command_buffer, const std::array & foveation, int destination); + + XrExtent2Di defoveated_size(const wivrn::to_headset::foveation_parameter &) const; };