Reallocate swapchains if a bigger one is needed

This commit is contained in:
Patrick Nicolas
2025-07-08 08:59:17 +02:00
parent 88f03acfbf
commit ecf6dfd215
4 changed files with 68 additions and 29 deletions
+40 -14
View File
@@ -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<int, view_count> 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)
+1 -1
View File
@@ -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;
+24 -13
View File
@@ -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<XrExtent2Di> stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer,
const std::array<wivrn::to_headset::foveation_parameter, 2> & foveation,
int destination)
void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer,
const std::array<wivrn::to_headset::foveation_parameter, 2> & 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<XrExtent2Di> out_extents(view_count, XrExtent2Di{});
for (size_t view = 0; view < view_count; ++view)
{
@@ -409,13 +408,6 @@ std::vector<XrExtent2Di> 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<XrExtent2Di> 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<XrExtent2Di> 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<uint16_t> & 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),
};
}
+3 -1
View File
@@ -70,8 +70,10 @@ public:
stream_reprojection(const stream_reprojection &) = delete;
std::vector<XrExtent2Di> reproject(
void reproject(
vk::raii::CommandBuffer & command_buffer,
const std::array<wivrn::to_headset::foveation_parameter, 2> & foveation,
int destination);
XrExtent2Di defoveated_size(const wivrn::to_headset::foveation_parameter &) const;
};